home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2831 / backwater.c next >
Encoding:
C/C++ Source or Header  |  1991-02-22  |  2.6 KB  |  135 lines

  1. /*
  2.  * backwater - print file names for articles that got stuck in a net backwater
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/timeb.h>
  8. #include <sys/stat.h>        /* for modified time (date received) */
  9. #include <string.h>
  10. #include "config.h"
  11. #include "fgetmfs.h"
  12. #include "alloc.h"
  13. #include "case.h"
  14.  
  15. #define STRLEN(s) (sizeof(s) - 1)    /* s must be a char array */
  16. #define    DAYSECS    (60*60*24L)
  17.  
  18. long    dateoffset = DAYSECS*10;
  19. char *progname;
  20. int debug;
  21. struct timeb ftnow;        /* ftime() result for getdate() */
  22.  
  23. FILE *efopen();
  24.  
  25. char *spdir;
  26. int spdirlen;
  27.  
  28. /*
  29.  * main - parse arguments and handle options
  30.  */
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     int c;
  36.     int errflg = 0;
  37.     FILE *in;
  38.     char *inname;
  39.     extern int optind;
  40.     extern char *optarg;
  41.  
  42.     progname = argv[0];
  43.     ftime(&ftnow);
  44.     while ((c = getopt(argc, argv, "d")) != EOF)
  45.         switch (c) {
  46.         case 'd':
  47.             ++debug;
  48.             break;
  49.         default:
  50.             errflg++;
  51.             break;
  52.         }
  53.     if (optind < argc) {
  54.         dateoffset=atoi(argv[optind++])*DAYSECS;
  55.         if (dateoffset<DAYSECS) ++errflg;
  56.     }
  57.     if (optind < argc || errflg) {
  58.         (void) fprintf(stderr, "usage: %s [-d] [days]\n", progname);
  59.         exit(2);
  60.     }
  61.  
  62.     spdir = artfile((char *)NULL);
  63.     spdirlen = strlen(spdir);
  64.     
  65.     while ((inname = fgetms(stdin)) != NULL) {
  66.         inname[strlen(inname)-1] = '\0';    /* kill newline */
  67.         if (strchr(inname, '.') == NULL) {    /* skip dot names */
  68.             in = efopen(inname, "r");
  69.             process(in, inname);
  70.             (void) fclose(in);
  71.         }
  72.         free(inname);
  73.     }
  74.     exit(0);
  75. }
  76.  
  77. /*
  78.  * process - process input file
  79.  */
  80. process(in, inname)
  81. FILE *in;
  82. char *inname;
  83. {
  84.     char *name;
  85.     char *line;
  86.     char *date;
  87.     time_t datercv,origdate;
  88.     struct stat statb;
  89.     static char datenm[] =    "Date: ";
  90.     register char *p;
  91.  
  92.     date = NULL;
  93.  
  94.     /* read until EOF or blank line (end of headers) */
  95.     while ((line = fgetms(in)) != NULL && strcmp(line, "\n") != 0) {
  96.         line[strlen(line)-1] = '\0';        /* trim newline */
  97.         if (CISTREQN(line, datenm, STRLEN(datenm))) {
  98.             if (date != NULL)
  99.                 free(date);
  100.             date = strsave(line+STRLEN(datenm));
  101.         }
  102.         free(line);
  103.     }
  104.     if (line != NULL)
  105.         free(line);
  106.  
  107.     if (date!=NULL) {
  108.         /* generate the date received */
  109.         (void) fstat(fileno(in), &statb);
  110.         datercv = statb.st_mtime;
  111.  
  112.         /* find out when it was posted */
  113.         origdate = getdate(date, &ftnow);
  114.  
  115.         if (origdate == -1)
  116.             fprintf(stderr,"%s: Invalid date in %s:`%s'\n",progname,inname,date);
  117.         else if (origdate+dateoffset<datercv) {
  118.             /* whomp out the file name */
  119.             (void) fputs(inname, stdout);
  120.             (void) putchar('\n');
  121.             (void) fflush(stdout);
  122.           }
  123.         free(date);
  124.     }
  125. }
  126.  
  127. /*
  128.  * unprivileged - no-op to keep pathname stuff happy
  129.  */
  130. void
  131. unprivileged(reason)
  132. char *reason;
  133. {
  134. }
  135.