#include #include #include typedef struct { int max; int min; char file[4096]; } FileList; static int flist_count; static FileList *flist; static FileList* FindFile(const char *fn) { FileList* fl = NULL; int f; for(f = 0; f < flist_count && !fl; f++) { if (strcmp(fn, flist[f].file) == 0) { fl = flist + f; } } if (!fl) { flist_count++; flist = realloc(flist, sizeof *flist * flist_count); strcpy(flist[flist_count-1].file, fn); flist[flist_count-1].max = 0; flist[flist_count-1].min = 999999; fl = flist + flist_count-1; } return fl; } int main(int argc, char *argv[]) { int f; if(argc == 1) { printf("%s: usage %s file_list\n",argv[0],argv[0]); return EXIT_FAILURE; } for(f = 1; f < argc; f++) { char *tok[2] = {0}; if ((tok[0] = strtok(argv[f], ";"))) { tok[1] = strtok(NULL, ";"); } if (tok[0] && tok[1]) { FileList *fl; int vers; vers = atoi(tok[1]); fl = FindFile(tok[0]); if (vers > fl->max) { fl->max = vers; } if (vers < fl->min) { fl->min = vers; } } } for(f = 0; f < flist_count; f++) { char s[4096]; int n; for(n = flist[f].min; n < flist[f].max; n++) { snprintf(s, sizeof s, "%s;%d", flist[f].file, n); remove(s); } snprintf(s, sizeof s, "%s;%d", flist[f].file, flist[f].max); rename(s, flist[f].file); } return EXIT_SUCCESS; }