diff options
author | Ian C <ianc@noddybox.co.uk> | 2019-03-27 15:20:39 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2019-03-27 15:20:39 +0000 |
commit | 75c2ad3b87cd5c7a4b4ec591e779082f3e6c6101 (patch) | |
tree | 6f9d406a31e4e401498968a1c33430aa9056bcc9 | |
parent | 86876952fcdad96e0f24602b7b09089a302d612e (diff) |
Added faster C based version of purge script.
-rw-r--r-- | purge.c | 102 |
1 files changed, 102 insertions, 0 deletions
@@ -0,0 +1,102 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +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; +} |