]> granicus.if.org Git - apache/blob - test/cls.c
e5910d885aae795182e31477f28d2577dfb4b19a
[apache] / test / cls.c
1 #include <ctype.h>
2 #include <dirent.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <time.h>
6
7 /*
8  * Compare a string to a mask
9  * Mask characters:
10  *   @ - uppercase letter
11  *   # - lowercase letter
12  *   & - hex digit
13  *   # - digit
14  *   * - swallow remaining characters 
15  *  <x> - exact match for any other character
16  */
17 static int checkmask(const char *data, const char *mask)
18 {
19     int i, ch, d;
20
21     for (i = 0; mask[i] != '\0' && mask[i] != '*'; i++) {
22         ch = mask[i];
23         d = data[i];
24         if (ch == '@') {
25             if (!apr_isupper(d))
26                 return 0;
27         }
28         else if (ch == '$') {
29             if (!apr_islower(d))
30                 return 0;
31         }
32         else if (ch == '#') {
33             if (!apr_isdigit(d))
34                 return 0;
35         }
36         else if (ch == '&') {
37             if (!apr_isxdigit(d))
38                 return 0;
39         }
40         else if (ch != d)
41             return 0;
42     }
43
44     if (mask[i] == '*')
45         return 1;
46     else
47         return (data[i] == '\0');
48 }
49
50 /*
51  * Converts 8 hex digits to a time integer
52  */
53 static int hex2sec(const char *x)
54 {
55     int i, ch;
56     unsigned int j;
57
58     for (i = 0, j = 0; i < 8; i++) {
59         ch = x[i];
60         j <<= 4;
61         if (apr_isdigit(ch))
62             j |= ch - '0';
63         else if (apr_isupper(ch))
64             j |= ch - ('A' - 10);
65         else
66             j |= ch - ('a' - 10);
67     }
68     if (j == 0xffffffff)
69         return -1;              /* so that it works with 8-byte ints */
70     else
71         return j;
72 }
73
74 int main(int argc, char **argv)
75 {
76     int i, ver;
77     DIR *d;
78     struct dirent *e;
79     const char *s;
80     FILE *fp;
81     char path[FILENAME_MAX + 1];
82     char line[1035];
83     time_t date, lmod, expire;
84     unsigned int len;
85     struct tm ts;
86     char sdate[30], slmod[30], sexpire[30];
87     const char time_format[] = "%e %b %Y %R";
88
89     if (argc != 2) {
90         printf("Usage: cls directory\n");
91         exit(0);
92     }
93
94     d = opendir(argv[1]);
95     if (d == NULL) {
96         perror("opendir");
97         exit(1);
98     }
99
100     for (;;) {
101         e = readdir(d);
102         if (e == NULL)
103             break;
104         s = e->d_name;
105         if (s[0] == '.' || s[0] == '#')
106             continue;
107         sprintf(path, "%s/%s", argv[1], s);
108         fp = fopen(path, "r");
109         if (fp == NULL) {
110             perror("fopen");
111             continue;
112         }
113         if (fgets(line, 1034, fp) == NULL) {
114             perror("fgets");
115             fclose(fp);
116             continue;
117         }
118         if (!checkmask(line, "&&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&&\n")) {
119             fprintf(stderr, "Bad cache file\n");
120             fclose(fp);
121             continue;
122         }
123         date = hex2sec(line);
124         lmod = hex2sec(line + 9);
125         expire = hex2sec(line + 18);
126         ver = hex2sec(line + 27);
127         len = hex2sec(line + 35);
128         if (fgets(line, 1034, fp) == NULL) {
129             perror("fgets");
130             fclose(fp);
131             continue;
132         }
133         fclose(fp);
134         i = strlen(line);
135         if (strncmp(line, "X-URL: ", 7) != 0 || line[i - 1] != '\n') {
136             fprintf(stderr, "Bad cache file\n");
137             continue;
138         }
139         line[i - 1] = '\0';
140         if (date != -1) {
141             ts = *gmtime(&date);
142             strftime(sdate, 30, time_format, &ts);
143         }
144         else
145             strcpy(sdate, "-");
146
147         if (lmod != -1) {
148             ts = *gmtime(&lmod);
149             strftime(slmod, 30, time_format, &ts);
150         }
151         else
152             strcpy(slmod, "-");
153
154         if (expire != -1) {
155             ts = *gmtime(&expire);
156             strftime(sexpire, 30, time_format, &ts);
157         }
158         else
159             strcpy(sexpire, "-");
160
161         printf("%s: %d; %s  %s  %s\n", line + 7, ver, sdate, slmod, sexpire);
162     }
163
164     closedir(d);
165     return 0;
166 }