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 / amiga / batcher.c next >
C/C++ Source or Header  |  1990-12-30  |  4KB  |  175 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include "config.h"
  5. #include "alloc.h"
  6.  
  7. #define PREFIX            "batch"
  8. #define BATCHSIZE        49152
  9. #define BATCHNAMELEN    128
  10.  
  11. #define SYSNAMESIZ        12
  12. #define NAMESIZ            32
  13.  
  14. batchit(struct _parms *p, char *fname, char *pfx);
  15.  
  16. extern char *optarg;
  17. char *progname;
  18. struct _parms {
  19.     long bsize, nque;
  20.     char *prgb, *prgc, *prgx;
  21. } defparms, sysparms;
  22.  
  23. void usage(int ier, char *fmt, ...)
  24. {
  25.     if (fmt && *fmt) {
  26.         va_list args;
  27.  
  28.         va_start(args, fmt);
  29.         vprintf(fmt, args);
  30.         va_end(args);
  31.     }
  32.     printf("Usage:  %s [-S system] [-s size] [-p prefix] -f batch\n", progname);
  33.     exit( ier );
  34.     /* NOTREACHED */
  35. }
  36.  
  37. void saveparms(struct _parms *p, long sz, long nq, char *pb, char *pc, char *px)
  38. {
  39.     p->bsize = sz;
  40.     p->nque  = nq;
  41.     p->prgb  = strsave(pb);
  42.     p->prgc  = strsave(pc);
  43.     p->prgx  = strsave(px);
  44. }
  45.  
  46. struct _parms *readparms(char *sys)
  47. {
  48.     FILE *bp;
  49.     char buff[BUFSIZ];
  50.     char name[SYSNAMESIZ], prgb[NAMESIZ], prgc[NAMESIZ], prgx[NAMESIZ];
  51.     long bsize, nque;
  52.  
  53.     if (bp = fopen(ctlfile("batch/batchparms"), "r")) {
  54.         while (fgets(buff, sizeof(buff), bp)) {
  55.             if (*buff == '#' || *buff == '\n')
  56.                 continue;
  57.             if (sscanf(buff, "%s %ld %ld %s %s %s",
  58.                 name, &bsize, &nque, prgb, prgc, prgx) != 6)
  59.                     printf("Invalid line in batchparms ignored\n");
  60.             if (bsize <= 0 || nque < 0)
  61.                 continue;
  62.             if (!strcmp("default", name))
  63.                 saveparms(&defparms, bsize, nque, prgb, prgc, prgx);
  64.             else if (!strcmp(sys, name)) {
  65.                 saveparms(&sysparms, bsize, nque, prgb, prgc, prgx);
  66.                 break;
  67.             }
  68.         }
  69.         fclose(bp);
  70.     }
  71.     return( sysparms.bsize ? &sysparms : defparms.bsize ? &defparms : 0 );
  72. }
  73.  
  74. main(int argc, char **argv)
  75. {
  76.     char *prefix = PREFIX, *system = NULL, *file = NULL;
  77.     struct _parms *parms;
  78.     size_t batchsize = 0;
  79.     int ch;
  80.  
  81.     if ((progname=strrchr(*argv, ':')) || (progname=strrchr(*argv, '/')))
  82.         progname++;
  83.     else
  84.         progname = *argv;
  85.  
  86.     while ((ch = getopt(argc, argv, "S:f:p:s:")) != EOF)
  87.         switch (ch) {
  88.         case 'S' :        /* System name being batched */
  89.             if (system)
  90.                 usage(10, "system name specified twice\n");
  91.             system = optarg;
  92.             break;
  93.         case 'f' :        /* File containing the batch list */
  94.             if (file)
  95.                 usage(10, "batch file specified twice\n");
  96.             file = optarg;
  97.             break;
  98.         case 'p' :        /* Prefix for output batches */
  99.             if (prefix)
  100.                 usage(10, "prefix specified twice\n");
  101.             if (strlen(optarg) > BATCHNAMELEN-4)
  102.                 usage(10, "Prefix too long; max %d\n", BATCHNAMELEN-4);
  103.             else
  104.                 prefix = optarg;
  105.             break;
  106.         case 's' :        /* Size constraint of batches */
  107.             if (batchsize > 0)
  108.                 usage(10, "batchsize specified twice\n");
  109.             ch = atoi(optarg);
  110.             if (ch < 10240)
  111.                 usage(10, "Invalid batch size %d; min 10K\n", ch);
  112.             else
  113.                 batchsize = ch;
  114.             break;
  115.         default :
  116.             usage(10, NULL);
  117.         }
  118.     if (parms = readparms(system))
  119.         exit( 10 );
  120.     if (batchsize > 0)
  121.         parms->bsize = batchsize;
  122.     if (!batchit(parms, file, prefix))
  123.         usage(10, "system %s: can't batch from %s!\n", system, file);
  124.     return( 0 );
  125. }
  126.  
  127. batchit(register struct _parms *p, char *fname, char *pfx)
  128. {
  129.     static char buf[32 * BUFSIZ];            /* 32K buffer */
  130.  
  131.     char name[BATCHNAMELEN];
  132.     char batchname[BATCHNAMELEN];
  133.     int fdb = 0, batchcount = 0;
  134.     int current;
  135.     FILE *fpr;
  136.  
  137.     register int art;
  138.     register size_t len;
  139.     register size_t ttlsofar = 0;
  140.  
  141.     if (fpr = fopen(fname, "r")) {
  142.         while (fscanf(fpr, "%s %ld\n", name, ¤t) == 2) {
  143.             if (current+ttlsofar > p->bsize || (!ttlsofar && current > p->bsize)) {
  144.                 if (fdb)
  145.                     close(fdb);
  146.                 ttlsofar = 0;
  147.                 fdb = 0;
  148.             }
  149.             if (!fdb) {
  150.                 sprintf(batchname, "%s_%04d", pfx, ++batchcount);
  151.                 fdb = open(batchname, O_WRONLY | O_CREAT | O_TRUNC);
  152.                 if (fdb == -1) {
  153.                     perror(batchname);
  154.                     exit( 10 );
  155.                 }
  156.             }
  157.             if ((art = open(artfile(name), O_RDONLY)) != -1) {
  158.                 sprintf(buf, "#! rnews %ld\n", current);
  159.                 ttlsofar += write(fdb, buf, strlen(buf));
  160.                 while ((len = read(art, buf, sizeof(buf))) > 0)
  161.                     if (write(fdb, buf, len) != len) {
  162.                         perror(batchname);
  163.                         exit( 10 );
  164.                     }
  165.                 close(art);
  166.                 ttlsofar += current;
  167.             } else
  168.                 perror(artfile(name));
  169.         }
  170.         fclose(fpr);
  171.     } else
  172.         perror(fname);
  173.     return( 0 );
  174. }
  175.