#include #include #include #include #include #include #include void to_lowercase_filename(const char *path) { // Separate directory and filename char path_copy1[1024]; char path_copy2[1024]; strncpy(path_copy1, path, sizeof(path_copy1) - 1); strncpy(path_copy2, path, sizeof(path_copy2) - 1); char *dir = dirname(path_copy1); char *base = basename(path_copy2); // Convert only the filename part to lowercase char new_base[256]; size_t i; for (i = 0; base[i] != '\0' && i < sizeof(new_base) - 1; i++) { new_base[i] = tolower((unsigned char)base[i]); } new_base[i] = '\0'; // Construct new path char new_path[1024]; if (strcmp(dir, ".") == 0) { snprintf(new_path, sizeof(new_path), "%s", new_base); } else { snprintf(new_path, sizeof(new_path), "%s/%s", dir, new_base); } // Skip if unchanged if (strcmp(path, new_path) == 0) { return; } // Rename file if (rename(path, new_path) == 0) { printf("Renamed: '%s' -> '%s'\n", path, new_path); } else { perror("Error renaming file"); } } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s [pattern2 ...]\n", argv[0]); fprintf(stderr, "Example: %s \"*.TXT\" \"IMAGE_*.JPG\"\n", argv[0]); return 1; } glob_t glob_result; int flags = GLOB_NOCHECK | GLOB_TILDE; for (int i = 1; i < argc; i++) { // Expand wildcards if (glob(argv[i], (i == 1 ? flags : flags | GLOB_APPEND), NULL, &glob_result) != 0) { fprintf(stderr, "Error matching pattern: %s\n", argv[i]); continue; } } for (size_t i = 0; i < glob_result.gl_pathc; i++) { // Process matching regular files only if (access(glob_result.gl_pathv[i], F_OK) == 0) { to_lowercase_filename(glob_result.gl_pathv[i]); } } globfree(&glob_result); return 0; }