#include #include #include #include #include #include #include #include #include #include #include char *match = "0123456789"; char *charp; void renamer(char*, char*, char*); int selection(const struct dirent *); extern char **environ; char **envpoint; int main(argc, argv, envp) int argc; char *argv[]; char *envp[]; { int dv; struct dirent **namelist; struct stat *buf = malloc(sizeof(struct stat)); struct tm *timebuf; char newfilenamebuf[100]; char *newfilename = newfilenamebuf; char *extension; char *directory; char *pathname; // = malloc(sizeof(struct tm)); int n; int i; char *filename; envpoint = envp; directory=argv[1]; dv = strlen(directory); // change this to support longer filenames pathname=malloc(512 + dv); strcpy(pathname, directory); n = scandir(directory, &namelist, selection, alphasort); if (n < 0) { perror("scandir"); return EXIT_FAILURE; } for (i = 0; i < n; i++) { filename = namelist[i]->d_name; strcpy(pathname + dv, filename); stat(pathname, buf); // printf("--> %s mode %d\n", filename, buf->st_mode); // if (S_ISREG(buf->st_mode)) { extension = rindex(filename, '.'); timebuf = gmtime(&buf->st_mtime); if (extension != NULL) { sprintf(newfilename, "%s%02u-%02u-%02u-%02u-%02u-%02u%s", directory, 1900 + timebuf->tm_year, \ 1 + timebuf->tm_mon, timebuf->tm_mday, timebuf->tm_hour, \ timebuf->tm_min, timebuf->tm_sec, extension); if (strcmp(extension, ".mp3") == 0) { renamer(pathname, newfilename, filename); } } // } free(namelist[i]); } free(namelist); return EXIT_SUCCESS; } // choose only files that don't start with numbers int selection(const struct dirent *file) { if (index(match, (file->d_name)[0]) == 0) { return 1; } return 0; } // do the actual renaming void renamer(char *old, char *new, char *tag) { int f; int pid; printf("Change %s to %s tag as %s\r\n", old, new, tag); rename(old, new); pid = fork(); if (pid == 0) { char *argv[5]; argv[0]="id3v2"; argv[1]="--song"; argv[2]=tag; argv[3]=new; argv[4]=0; f = execve("/usr/bin/id3v2", argv, envpoint); perror("id3v2"); exit(127); } waitpid(pid, NULL, 0); }