home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / nov / expovguts.c < prev    next >
C/C++ Source or Header  |  1995-04-27  |  4KB  |  172 lines

  1. /*
  2.  * expovguts - do the inner-loop work of expov
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include "fgetfln.h"
  12. #include "news.h"
  13.  
  14. int debug = 0;
  15. char *progname;
  16.  
  17. char *inname;
  18. char *tmpname;
  19.  
  20. extern void error();
  21. #define    mkprogname(a)    (((a) == NULL) ? "?noname?" : (a))
  22.  
  23. void mapfill();
  24. void filter();
  25.  
  26. /*
  27.  - main - parse arguments and handle options
  28.  */
  29. main(argc, argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.     int c;
  34.     int errflg = 0;
  35.     long start, stop;
  36.     char *amap;
  37.     FILE *in;
  38.     FILE *out;
  39.     extern int optind;
  40.     extern char *optarg;
  41.     extern FILE *efopen();
  42.  
  43.     progname = mkprogname(argv[0]);
  44.  
  45.     while ((c = getopt(argc, argv, "X")) != EOF)
  46.         switch (c) {
  47.         case 'X':    /* Debugging. */
  48.             debug++;
  49.             break;
  50.         case '?':
  51.         default:
  52.             errflg++;
  53.             break;
  54.         }
  55.     if (errflg || optind != argc-3) {
  56.         fprintf(stderr, "usage: %s ", progname);
  57.         fprintf(stderr, "max min overviewfile\n");
  58.         exit(2);
  59.     }
  60.  
  61.     start = atol(argv[optind+1]);
  62.     stop = atol(argv[optind]);        /* tentatively */
  63.     inname = argv[optind+2];
  64.     if (start == 0) {
  65.         fprintf(stderr, "%s: in expiring `%s',\n", progname, inname);
  66.         fprintf(stderr, "\tfound problem in active file:  min == 0\n",
  67.                             argv[optind+1]);
  68.         exit(2);
  69.     }
  70.     if (start > stop+1) {
  71.         fprintf(stderr, "%s: in expiring `%s',\n", progname, inname);
  72.         fprintf(stderr, "\tfound problem in active file:  min (%s) > max (%s) + 1\n",
  73.                     argv[optind+1], argv[optind]);
  74.         exit(2);
  75.     }
  76.     stop += 100;                /* a bit of headroom */
  77.     if (debug)
  78.         printf("start %ld, stop %ld\n", start, stop);
  79.  
  80.     tmpname = str3save(inname, ".tmp", "");
  81.  
  82.     amap = malloc((size_t)(stop - start));
  83.     if (amap == NULL)
  84.         error("cannot allocate map", "");
  85.     memset(amap, (size_t)(stop - start), 0);
  86.  
  87.     mapfill(amap, start, stop, stdin);
  88.  
  89.     in = efopen(inname, "r");
  90.     out = efopen(tmpname, "w");
  91.     filter(in, out, amap, start, stop);
  92.     if (ferror(in) || fclose(in) != 0)
  93.         error("I/O error in reading %s", inname);
  94.     if (ferror(out) || fclose(out) != 0)
  95.         error("I/O error in writing %s", tmpname);
  96.  
  97.     if (rename(tmpname, inname) != 0)
  98.         error("cannot rename %s", tmpname);
  99.  
  100.     exit(0);
  101. }
  102.  
  103. /*
  104.  - mapfill - fill in the map with the input filenames
  105.  */
  106. void
  107. mapfill(map, start, stop, f)
  108. char *map;
  109. long start;
  110. long stop;
  111. FILE *f;
  112. {
  113.     register char *line;
  114.     register char *p;
  115.     register long fno;
  116.     register int nbad = 0;
  117.  
  118.     while ((line = fgetline(f, (size_t *)NULL)) != NULL) {
  119.         fno = 0;
  120.         for (p = line; isdigit(*p); p++)
  121.             fno = fno*10 + (*p) - '0';
  122.         if (debug)
  123.             printf("name `%s', number %ld\n", line, fno);
  124.         if (*p == '\0') {    /* it was all numeric */
  125.             if (fno >= start && fno < stop) {
  126.                 map[fno - start] = 1;
  127.                 if (debug)
  128.                     printf("setting %ld\n", fno-start);
  129.             } else if (fno < start)
  130.                 nbad++;
  131.             /* fno >= stop presumably will be filed later */
  132.         }
  133.     }
  134.  
  135.     if (nbad > 0) {
  136.         fprintf(stderr, "%s: (warning) in expiring `%s',\n", progname,
  137.                                 inname);
  138.         fprintf(stderr, "\tfound %d files with numbers < min (%ld),\n",
  139.                                 nbad, start);
  140.         fprintf(stderr, "\tindicating problems with upact\n");
  141.     }
  142. }
  143.  
  144. /*
  145.  - filter - pass overview file through, checking against map
  146.  */
  147. void
  148. filter(in, out, map, start, stop)
  149. FILE *in;
  150. FILE *out;
  151. char *map;
  152. long start;
  153. long stop;
  154. {
  155.     char *line;
  156.     register char *p;
  157.     register long no;
  158.  
  159.     while ((line = fgetline(in, (size_t *)NULL)) != NULL) {
  160.         p = strchr(line, '\t');
  161.         if (p != NULL) {    /* quietly drop malformed lines */
  162.             *p = '\0';
  163.             no = atol(line);
  164.             *p = '\t';
  165.             if (no >= start && (no >= stop || map[no-start] != 0)) {
  166.                 fputs(line, out);
  167.                 putc('\n', out);
  168.             }
  169.         }
  170.     }
  171. }
  172.