home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / maint / histinfo.c < prev    next >
C/C++ Source or Header  |  1995-01-02  |  3KB  |  155 lines

  1. /*
  2.  * histinfo - print history file lines for articles named on stdin
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>        /* for modified time (date received) */
  8. #include <string.h>
  9. #include <errno.h>
  10. #include "fixerrno.h"
  11. #include "config.h"
  12. #include "fgetfln.h"
  13. #include "alloc.h"
  14. #include "case.h"
  15.  
  16. #define STRLEN(s) (sizeof(s) - 1)    /* s must be a char array */
  17.  
  18. char *progname;
  19. int debug;
  20.  
  21.  
  22. char *spdir;
  23. int spdirlen;
  24.  
  25. /*
  26.  * main - parse arguments and handle options
  27.  */
  28. main(argc, argv)
  29. int argc;
  30. char *argv[];
  31. {
  32.     int c;
  33.     int errflg = 0;
  34.     FILE *in;
  35.     char *inname;
  36.     register int i;
  37.     extern int optind;
  38.     extern char *optarg;
  39.  
  40.     progname = argv[0];
  41.     while ((c = getopt(argc, argv, "d")) != EOF)
  42.         switch (c) {
  43.         case 'd':
  44.             ++debug;
  45.             break;
  46.         default:
  47.             errflg++;
  48.             break;
  49.         }
  50.     if (optind < argc || errflg) {
  51.         (void) fprintf(stderr, "usage: %s [-d]\n", progname);
  52.         exit(2);
  53.     }
  54.  
  55.     spdir = artfile((char *)NULL);
  56.     spdirlen = strlen(spdir);
  57.     
  58.     while ((inname = fgetline(stdin, (size_t *)NULL)) != NULL)
  59.         if (strchr(inname, '.') == NULL) {    /* skip dot names */
  60.             errno = 0;
  61.             in = fopen(inname, "r");
  62.             if (in == NULL && errno != ENOENT)
  63.                 error("cannot open file `%s'", inname);
  64.             if (in != NULL) {
  65.                 process(in, inname);
  66.                 (void) fclose(in);
  67.             }
  68.         }
  69.     exit(0);
  70. }
  71.  
  72. /*
  73.  * process - process input file
  74.  */
  75. process(in, inname)
  76. FILE *in;
  77. char *inname;
  78. {
  79.     char *name;
  80.     char *line;
  81.     char *msgid;
  82.     char *expiry;
  83.     time_t datercv;
  84.     struct stat statb;
  85.     static char msgidnm[] =  "Message-ID:";
  86.     static char expnm[] =    "Expires:";
  87.     register char *p;
  88.     register char *lastp;
  89.     register int i;
  90.  
  91.     expiry = strsave("-");
  92.     msgid = NULL;
  93.  
  94.     /* read until EOF or blank line (end of headers) */
  95.     while ((line = fgetline(in, (size_t *)NULL)) != NULL && line[0] != '\0')
  96.         if (CISTREQN(line, msgidnm, STRLEN(msgidnm))) {
  97.             if (msgid != NULL)
  98.                 free(msgid);
  99.             p = line + STRLEN(msgidnm);
  100.             msgid = strsave(p + strspn(p, " \t"));
  101.         } else if (CISTREQN(line, expnm, STRLEN(expnm))) {
  102.             free(expiry);
  103.             p = line + STRLEN(expnm);
  104.             expiry = strsave(p + strspn(p, " \t"));
  105.         }
  106.  
  107.     /* generate the "filename" */
  108.     name = inname;
  109.     lastp = NULL;
  110.     for (p = name; *p != '\0'; p++)
  111.         if (*p == '/') {
  112.             *p = '.';
  113.             lastp = p;
  114.         }
  115.     if (lastp != NULL)
  116.         *lastp = '/';
  117.  
  118.     /* generate the date received */
  119.     (void) fstat(fileno(in), &statb);
  120.     datercv = statb.st_mtime;
  121.  
  122.     /* deal with empty and trash articles */
  123.     if (msgid == NULL || strchr(msgid, '\t') != NULL || msgid[0] != '<' ||
  124.                     msgid[strlen(msgid)-1] != '>' ||
  125.                     strchr(expiry, '\t') != NULL ||
  126.                     strchr(expiry, '~') != NULL) {
  127.         if (msgid != NULL)
  128.             free(msgid);
  129.         msgid = str3save("<", name, "@trash>");
  130.         datercv = 0;
  131.         free(expiry);
  132.         expiry = strsave("0");
  133.     }
  134.  
  135.     /* whomp out the history line */
  136.     (void) fputs(msgid, stdout);
  137.     printf("\t%ld~", (long)datercv);
  138.     (void) fputs(expiry, stdout);
  139.     (void) putchar('\t');
  140.     (void) fputs(name, stdout);
  141.     (void) putchar('\n');
  142.     (void) fflush(stdout);
  143.     free(msgid);
  144.     free(expiry);
  145. }
  146.  
  147. /*
  148.  * unprivileged - no-op to keep pathname stuff happy
  149.  */
  150. void
  151. unprivileged(reason)
  152. char *reason;
  153. {
  154. }
  155.