home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 400-499 / ff473.lzh / CNewsSrc / cnews_src.lzh / misc / canonhdr.c < prev    next >
C/C++ Source or Header  |  1990-12-30  |  2KB  |  98 lines

  1. /*
  2.  * canonhdr - canonicalise capitalisation of header keywords
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #ifdef unix
  8. # include <sys/types.h>
  9. #else
  10. # include <time.h>
  11. #endif
  12. #include "news.h"
  13. #include "libc.h"
  14.  
  15. char *progname;
  16. int debug;
  17.  
  18. /*
  19.  * main - parse arguments and handle options
  20.  */
  21. main(argc, argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.     int c;
  26.     int errflg = 0;
  27.     extern int optind;
  28.     extern char *optarg;
  29.     extern FILE *efopen();
  30.  
  31.     progname = argv[0];
  32.     while ((c = getopt(argc, argv, "d")) != EOF)
  33.         switch (c) {
  34.         case 'd':
  35.             ++debug;
  36.             break;
  37.         default:
  38.             errflg++;
  39.             break;
  40.         }
  41.     if (errflg) {
  42.         (void) fprintf(stderr, "usage: %s [-d] [file]...\n", progname);
  43.         exit(2);
  44.     }
  45.  
  46.     if (optind >= argc)
  47.         process(stdin, "stdin");
  48.     else
  49.         for (; optind < argc; optind++)
  50.             if (STREQ(argv[optind], "-"))
  51.                 process(stdin, "-");
  52.             else {
  53.                 FILE *in = efopen(argv[optind], "r");
  54.  
  55.                 process(in, argv[optind]);
  56.                 (void) fclose(in);
  57.             }
  58.     exit(0);
  59. }
  60.  
  61. /*
  62.  * process - process input file
  63.  */
  64. process(in, inname)
  65. FILE *in;
  66. char *inname;
  67. {
  68.     register char *hdr;
  69.     int ishdr = YES;
  70.     long nolimit = -1;
  71.     static int washdr = YES;
  72.  
  73.     if (!washdr)
  74.         return;
  75.     while ((hdr = gethdr(in, &nolimit, &ishdr)) != NULL && ishdr) {
  76.         register char *cp;
  77.         static char canonmsgid[] = "Message-Id:";
  78.         static char magicmsgid[] = "Message-ID:";
  79.  
  80.         /* capitalise initial letter of each word, Message-ID: special */
  81.         for (cp = hdr; *cp != ':' && *cp != '\0'; cp++)
  82.             if (cp == hdr || cp[-1] == '-') {
  83.                 if (isascii(*cp) && islower(*cp))
  84.                     *cp = toupper(*cp);
  85.             } else
  86.                 if (isascii(*cp) && isupper(*cp))
  87.                     *cp = tolower(*cp);
  88.         if (STREQN(hdr, canonmsgid, STRLEN(canonmsgid)))
  89.             (void) strncpy(hdr, magicmsgid, STRLEN(magicmsgid));
  90.         (void) fputs(hdr, stdout);
  91.         /* must not free hdr; gethdr will do so automatically */
  92.     }
  93.     if (hdr != NULL)
  94.         free(hdr);
  95.     if (!ishdr)
  96.         washdr = NO;
  97. }
  98.