summaryrefslogtreecommitdiff
path: root/purge.c
blob: ede265622a49002715f4dfcd01addc8cba0ef96e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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;
}