summaryrefslogtreecommitdiff
path: root/purge.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2019-03-27 15:20:39 +0000
committerIan C <ianc@noddybox.co.uk>2019-03-27 15:20:39 +0000
commit75c2ad3b87cd5c7a4b4ec591e779082f3e6c6101 (patch)
tree6f9d406a31e4e401498968a1c33430aa9056bcc9 /purge.c
parent86876952fcdad96e0f24602b7b09089a302d612e (diff)
Added faster C based version of purge script.
Diffstat (limited to 'purge.c')
-rw-r--r--purge.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/purge.c b/purge.c
new file mode 100644
index 0000000..ede2656
--- /dev/null
+++ b/purge.c
@@ -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;
+}