+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
* # - lowercase letter
* & - hex digit
* # - digit
- * * - swallow remaining characters
+ * * - swallow remaining characters
* <x> - exact match for any other character
*/
static int checkmask(const char *data, const char *mask)
int i, ch, d;
for (i = 0; mask[i] != '\0' && mask[i] != '*'; i++) {
- ch = mask[i];
- d = data[i];
- if (ch == '@') {
- if (!isupper(d))
- return 0;
- }
- else if (ch == '$') {
- if (!islower(d))
- return 0;
- }
- else if (ch == '#') {
- if (!isdigit(d))
- return 0;
- }
- else if (ch == '&') {
- if (!isxdigit(d))
- return 0;
- }
- else if (ch != d)
- return 0;
+ ch = mask[i];
+ d = data[i];
+ if (ch == '@') {
+ if (!isupper(d))
+ return 0;
+ }
+ else if (ch == '$') {
+ if (!islower(d))
+ return 0;
+ }
+ else if (ch == '#') {
+ if (!isdigit(d))
+ return 0;
+ }
+ else if (ch == '&') {
+ if (!isxdigit(d))
+ return 0;
+ }
+ else if (ch != d)
+ return 0;
}
if (mask[i] == '*')
- return 1;
+ return 1;
else
- return (data[i] == '\0');
+ return (data[i] == '\0');
}
/*
unsigned int j;
for (i = 0, j = 0; i < 8; i++) {
- ch = x[i];
- j <<= 4;
- if (isdigit(ch))
- j |= ch - '0';
- else if (isupper(ch))
- j |= ch - ('A' - 10);
- else
- j |= ch - ('a' - 10);
+ ch = x[i];
+ j <<= 4;
+ if (isdigit(ch))
+ j |= ch - '0';
+ else if (isupper(ch))
+ j |= ch - ('A' - 10);
+ else
+ j |= ch - ('a' - 10);
}
if (j == 0xffffffff)
- return -1; /* so that it works with 8-byte ints */
+ return -1; /* so that it works with 8-byte ints */
else
- return j;
+ return j;
}
int main(int argc, char **argv)
const char time_format[] = "%e %b %Y %R";
if (argc != 2) {
- printf("Usage: cls directory\n");
- exit(0);
+ printf("Usage: cls directory\n");
+ exit(0);
}
d = opendir(argv[1]);
if (d == NULL) {
- perror("opendir");
- exit(1);
+ perror("opendir");
+ exit(1);
}
for (;;) {
- e = readdir(d);
- if (e == NULL)
- break;
- s = e->d_name;
- if (s[0] == '.' || s[0] == '#')
- continue;
- sprintf(path, "%s/%s", argv[1], s);
- fp = fopen(path, "r");
- if (fp == NULL) {
- perror("fopen");
- continue;
- }
- if (fgets(line, 1034, fp) == NULL) {
- perror("fgets");
- fclose(fp);
- continue;
- }
- if (!checkmask(line, "&&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&&\n")) {
- fprintf(stderr, "Bad cache file\n");
- fclose(fp);
- continue;
- }
- date = hex2sec(line);
- lmod = hex2sec(line + 9);
- expire = hex2sec(line + 18);
- ver = hex2sec(line + 27);
- len = hex2sec(line + 35);
- if (fgets(line, 1034, fp) == NULL) {
- perror("fgets");
- fclose(fp);
- continue;
- }
- fclose(fp);
- i = strlen(line);
- if (strncmp(line, "X-URL: ", 7) != 0 || line[i - 1] != '\n') {
- fprintf(stderr, "Bad cache file\n");
- continue;
- }
- line[i - 1] = '\0';
- if (date != -1) {
- ts = *gmtime(&date);
- strftime(sdate, 30, time_format, &ts);
- }
- else
- strcpy(sdate, "-");
+ e = readdir(d);
+ if (e == NULL)
+ break;
+ s = e->d_name;
+ if (s[0] == '.' || s[0] == '#')
+ continue;
+ sprintf(path, "%s/%s", argv[1], s);
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ perror("fopen");
+ continue;
+ }
+ if (fgets(line, 1034, fp) == NULL) {
+ perror("fgets");
+ fclose(fp);
+ continue;
+ }
+ if (!checkmask(line, "&&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&&\n")) {
+ fprintf(stderr, "Bad cache file\n");
+ fclose(fp);
+ continue;
+ }
+ date = hex2sec(line);
+ lmod = hex2sec(line + 9);
+ expire = hex2sec(line + 18);
+ ver = hex2sec(line + 27);
+ len = hex2sec(line + 35);
+ if (fgets(line, 1034, fp) == NULL) {
+ perror("fgets");
+ fclose(fp);
+ continue;
+ }
+ fclose(fp);
+ i = strlen(line);
+ if (strncmp(line, "X-URL: ", 7) != 0 || line[i - 1] != '\n') {
+ fprintf(stderr, "Bad cache file\n");
+ continue;
+ }
+ line[i - 1] = '\0';
+ if (date != -1) {
+ ts = *gmtime(&date);
+ strftime(sdate, 30, time_format, &ts);
+ }
+ else
+ strcpy(sdate, "-");
- if (lmod != -1) {
- ts = *gmtime(&lmod);
- strftime(slmod, 30, time_format, &ts);
- }
- else
- strcpy(slmod, "-");
+ if (lmod != -1) {
+ ts = *gmtime(&lmod);
+ strftime(slmod, 30, time_format, &ts);
+ }
+ else
+ strcpy(slmod, "-");
- if (expire != -1) {
- ts = *gmtime(&expire);
- strftime(sexpire, 30, time_format, &ts);
- }
- else
- strcpy(sexpire, "-");
+ if (expire != -1) {
+ ts = *gmtime(&expire);
+ strftime(sexpire, 30, time_format, &ts);
+ }
+ else
+ strcpy(sexpire, "-");
- printf("%s: %d; %s %s %s\n", line + 7, ver, sdate, slmod, sexpire);
+ printf("%s: %d; %s %s %s\n", line + 7, ver, sdate, slmod, sexpire);
}
closedir(d);