home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / nov / ovsplit.c < prev    next >
C/C++ Source or Header  |  1994-08-28  |  2KB  |  90 lines

  1. /*
  2.  * ovsplit - distribute mkov output lines into overview files
  3.  * Kinda annoying that not everybody has a modern awk, which can do this...
  4.  * but this is probably faster anyway.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <fgetfln.h>
  13.  
  14. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  15.  
  16. /* imports */
  17. extern int optind;
  18. extern char *optarg;
  19. extern char *strsave(), *str3save();
  20. extern FILE *efopen();
  21.  
  22. /* exports */
  23. char *progname = "";
  24. int debug;
  25.  
  26. char *dirname = NULL;
  27. char *ovname = NULL;
  28. FILE *ov = NULL;
  29.  
  30. /*
  31.  * main - do it
  32.  */
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     int c, errflg = 0;
  38.     char *line;
  39.     register char *t;
  40.     register char *prefix;
  41.  
  42.     if (argc > 0)
  43.         progname = argv[0];
  44.     while ((c = getopt(argc, argv, "d")) != EOF)
  45.         switch (c) {
  46.         case 'd':
  47.             ++debug;
  48.             break;
  49.         default:
  50.             errflg++;
  51.             break;
  52.         }
  53.     if (errflg || optind != argc-1) {
  54.         (void) fprintf(stderr, "usage: %s [-d] $NEWSOV\n",
  55.             progname);
  56.         exit(2);
  57.     }
  58.     prefix = str3save(argv[optind], "/", "");
  59.  
  60.     while ((line = fgetline(stdin, (size_t *)NULL)) != NULL) {
  61.         t = strchr(line, '\t');
  62.         if (t == NULL)
  63.             error("bad input line %.40s...", line);
  64.         *t = '\0';
  65.         if (dirname == NULL || !STREQ(dirname, line)) {
  66.             if (dirname != NULL)
  67.                 free(dirname);
  68.             if (ov != NULL)
  69.                 if (nfclose(ov) == EOF)
  70.                     error("nfclose(%s) failed", ovname);
  71.             if (ovname != NULL)
  72.                 free(ovname);
  73.             dirname = strsave(line);
  74.             ovname = str3save(prefix, dirname, "/.overview");
  75.             ov = efopen(ovname, "a");
  76.         }
  77.         *t = '\t';
  78.         t++;
  79.         if (debug)
  80.             printf("%.40s... to %s\n", t, ovname);
  81.         fputs(t, ov);
  82.         putc('\n', ov);
  83.     }
  84.  
  85.     if (ov != NULL)
  86.         if (nfclose(ov) == EOF)
  87.             error("nfclose(%s) failed", ovname);
  88.     exit(0);
  89. }
  90.