home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / cls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-21  |  3.0 KB  |  166 lines

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