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 / batch / batcher.c next >
C/C++ Source or Header  |  1990-12-22  |  3KB  |  120 lines

  1. /*
  2.  * batcher - send a bunch of news articles as an unbatch script
  3.  *
  4.  * Usage: batcher listfile
  5.  *
  6.  *    where listfile is a file containing a list, one per line, of
  7.  *    names of files containing articles.  Only the first
  8.  *    field of each line is looked at, so there can be more if needed
  9.  *    for other things.  Non-absolute pathnames are understood to lie
  10.  *    under the current directory; chdiring to the right place is the
  11.  *    parent's problem.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <signal.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include "fgetmfs.h"
  20.  
  21. #ifndef READSIZE
  22. #define READSIZE 8192    /* allows for even 4.2 worst case file systems */
  23. #endif
  24. char buffer[READSIZE];
  25.  
  26. char *progname;
  27.  
  28. int debug = 0;            /* Debugging? */
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     int c;
  35.     int errflg = 0;
  36.     extern int optind;
  37.     extern char *optarg;
  38.     register FILE *list;
  39.     char *article;
  40.     int ret;
  41.  
  42.     progname = argv[0];
  43.     while ((c = getopt(argc, argv, "x")) != EOF)
  44.         switch (c) {
  45.         case 'x':    /* Debugging. */
  46.             debug++;
  47.             break;
  48.         case '?':
  49.         default:
  50.             errflg++;
  51.             break;
  52.         }
  53.     if (errflg || optind != argc-1) {
  54.         (void) fprintf(stderr,
  55.             "Usage: batcher listfile\n");
  56.         exit(2);
  57.     }
  58.  
  59.     list = fopen(argv[optind], "r");
  60.     if (list == NULL)
  61.         error("unable to open `%s'", argv[optind]);
  62.  
  63.     while ((article = fgetms(list)) != NULL) {
  64.         process(article);
  65.         free(article);
  66.     }
  67.     if (!feof(list))
  68.         error("fgetmfs failure", "");
  69.  
  70.     exit(0);
  71. }
  72.  
  73. /*
  74.  - process - process an article
  75.  */
  76. process(article)
  77. register char *article;
  78. {
  79.     register int artfile;
  80.     register int count;
  81.     struct stat sbuf;
  82.     register char *endp;
  83.  
  84.     endp = strchr(article, '\t');
  85.     if (endp == NULL)
  86.         endp = strchr(article, ' ');
  87.     if (endp == NULL)
  88.         endp = strchr(article, '\n');
  89.     if (endp != NULL)
  90.         *endp = '\0';
  91.  
  92.     artfile = open(article, 0);
  93.     if (artfile < 0) {
  94.         /*
  95.          * Can't read the article.  This isn't necessarily a
  96.          * disaster, since things like cancellations will do
  97.          * this.  Mumble and carry on.
  98.          */
  99.         if (debug)
  100.             warning("can't find `%s'", article);
  101.         return;
  102.     }
  103.  
  104.     if (fstat(artfile, &sbuf) < 0)
  105.         error("internal disaster, can't fstat", "");
  106.     if ((sbuf.st_mode&S_IFMT) != S_IFREG) {
  107.         (void) close(artfile);
  108.         return;        /* Don't try to batch directories etc. */
  109.     }
  110.  
  111.     printf("#! rnews %ld\n", sbuf.st_size);
  112.     while ((count = read(artfile, buffer, sizeof buffer)) > 0)
  113.         if (fwrite(buffer, sizeof(char), count, stdout) != count)
  114.             error("write failure in `%s'", article);
  115.     if (count < 0)
  116.         error("read failure in `%s'", article);
  117.  
  118.     (void) close(artfile);
  119. }
  120.