./src/rgsub.c in aliens-1.0.82 vs ./src/rgsub.c in aliens-1.0.87
- old
+ new
@@ -272,60 +272,59 @@
printf("%s: %s: %ld replacements for %s => %s\n", prog_name, filename,
num_matches, one, two);
}
}
-static void process_directory(const char *dirPath, const char *one,
+static void process_directory(const char *directory_path, const char *one,
const char *two, bool check_file) {
- if (strstr(dirPath, "/.")) {
+ if (strstr(directory_path, "/.")) {
// printf("Skipping hidden file '%s'\n", filename);
return;
}
- DIR *dir = opendir(dirPath);
+ DIR *dir = opendir(directory_path);
if (dir == NULL) {
if (check_file) {
- process_file(dirPath, one, two);
+ process_file(directory_path, one, two);
return;
}
- printf("Failed to open directory: '%s'\n", dirPath);
+ printf("Failed to open directory: '%s'\n", directory_path);
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if ((!strcmp(entry->d_name, ".")) || (!strcmp(entry->d_name, "..")))
continue;
char path[PATH_MAX];
- snprintf(path, sizeof(path), "%s/%s", dirPath, entry->d_name);
+ snprintf(path, sizeof(path), "%s/%s", directory_path, entry->d_name);
- if (rename_mode) {
- bool use_orig;
- size_t num_matches;
- char *new_text;
-
- string_replace(path, one, two, strlen(path), strlen(one), strlen(two),
- &use_orig, &num_matches, &new_text);
-
- if (use_orig)
- continue;
- printf("%s: Renamed %s => %s\n", prog_name, path, new_text);
-
- rename(path, new_text);
- free(new_text);
- continue;
- }
-
struct stat statbuf;
if (stat(path, &statbuf) == -1) {
printf("Failed to get file information: %s\n", path);
continue;
}
-
+
if (S_ISDIR(statbuf.st_mode)) {
process_directory(path, one, two, false);
} else if (S_ISREG(statbuf.st_mode)) {
- process_file(path, one, two);
+ if (unlikely(rename_mode)) {
+ bool use_orig;
+ size_t num_matches;
+ char *new_text;
+
+ string_replace(path, one, two, strlen(path), len1, len2, &use_orig,
+ &num_matches, &new_text);
+ if (use_orig)
+ continue;
+
+ printf("%s: Renamed %s => %s\n", prog_name, path, new_text);
+
+ rename(path, new_text);
+ free(new_text);
+ } else {
+ process_file(path, one, two);
+ }
}
}
closedir(dir);
}
\ No newline at end of file