home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume34 / unpackmaps / part01 / runpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  7.9 KB  |  325 lines

  1. /* Copyright 1992, Chris Lewis.  All Rights Reserved
  2.    Please see the README for the terms of the copyright.
  3.    1.4 92/07/12
  4.  */
  5.  
  6. #ifndef lint
  7. static char SCCSid[] = "@(#)runpath.c 1.4 92/07/12 16:43:27";
  8. #endif
  9. #define    UNPACKMAPS
  10. #include "unpack.h"
  11.  
  12. int stunlinked = 0;
  13. int stprocessed = 0;
  14.  
  15. dorunpath() {
  16.     extern char *zfgets();
  17.     extern FILE *zfopen();
  18.     struct stat stb;
  19.     register char *p;
  20.     register int recno;
  21.     FILE *list, *mapf, *path, *whfp;
  22.     char buf[BUFSIZ];
  23.     long pathtime = 0;
  24.     char **mapptr, **locptr;
  25.  
  26.     fatal(chdir(unpackdir), "Can't change to unpacking directory");
  27.  
  28.     (void) umask(022);
  29.  
  30.     (void) unlink(pathtmp);
  31.     (void) unlink(pathtmp2);
  32.     (void) unlink(wheretmp);
  33.  
  34.     /* awwww, do we really have to run it?
  35.        these tests are to confirm that we have to run pathalias
  36.        (if forcepath = 0)
  37.      */
  38.  
  39.     if (!forcepath) {
  40.     if (stat(wheredb, &stb))
  41.         forcepath = 1;        /* no where file, run it. */
  42.     else
  43.         pathtime = stb.st_mtime;
  44.     if (!whereonly) {
  45.         if (stat(pathfile, &stb))
  46.         forcepath = 1;        /* no path file, run it. */
  47.         else /* minimum time is trigger */
  48.         pathtime = stb.st_mtime < pathtime ? stb.st_mtime: pathtime;
  49.     }
  50.     }
  51.  
  52.     if (debug)
  53.     (void) fprintf(stderr, "in dorunpath: pathtime: %ld, force: %d\n",
  54.         pathtime, forcepath);
  55.  
  56.     getmaps(&mapfiles, 1);
  57.     for (mapptr = mapfiles.list; mapptr && *mapptr; mapptr++)
  58.     if (stat(*mapptr, &stb))
  59.         (void) fprintf(stderr, "Can't stat %s\n", *mapptr);
  60.     else {
  61.         if (pathtime < stb.st_mtime)    /* map file newer, run it */
  62.         forcepath = 1;
  63.         if (debug)
  64.         (void) fprintf(stderr, "check %s, t: %ld, force: %d\n", buf,
  65.             stb.st_mtime, forcepath);
  66.     }
  67.  
  68.     /* then, collect the map file mentioned in the command line */
  69.     recno = mapfiles.curptr - mapfiles.list;
  70.  
  71.     for (mapptr = localmaps.list; mapptr && *mapptr; mapptr++)
  72.     if (stat(*mapptr, &stb))
  73.         (void) fprintf(stderr, "Can't stat %s\n", *mapptr);
  74.     else {
  75.         if (pathtime < stb.st_mtime)    /* local map newer, run it */
  76.         forcepath = 1;
  77.         savestr(&mapfiles, *mapptr);
  78.     }
  79.  
  80.     /* pointers >= locptr are command line maps */
  81.     locptr = mapfiles.list + recno;
  82.  
  83.     if (debug) {
  84.     (void) fprintf(stderr, "finish: forcepath: %d\n", forcepath);
  85.     for (mapptr = mapfiles.list; mapptr && *mapptr; mapptr++)
  86.         (void) fprintf(stderr, "map %s%s\n", *mapptr,
  87.         mapptr >= locptr? "*": "");
  88.     }
  89.  
  90.  
  91.     if (!forcepath)
  92.     return;
  93.  
  94.     /* only doing it one step at a time.  If we make the popen
  95.        multistage (sed reorder + sort), then we lose the ability
  96.        to replace the sed with something faster, *and* the ability
  97.        to check for errors in each step.
  98.  
  99.        This should really be forked, with output to pathalias
  100.        on one side, and input from pathalias and output to
  101.        sort on the other.  Then we don't have to have *two*
  102.        path temporaries.  Next time...
  103.      */
  104.  
  105.     if (whereonly)
  106.     path = (FILE *) NULL;
  107.     else {
  108.     (void) sprintf(tempbuf, "%s > %s", pathalias, pathtmp);
  109.     fatal(!(path = popen(tempbuf, "w")), "Can't popen pathalias");
  110.     (void) fprintf(stderr, "Starting pathalias\n");
  111.     }
  112.  
  113.     fatal(!(whfp = fopen(wheretmp, "w")), "Can't open where.tmp");
  114.  
  115.     /* Here we GOOOOOOOO!!!! */
  116.  
  117.     (void) fflush(stderr);
  118.  
  119.     for (mapptr = mapfiles.list; mapptr && *mapptr; mapptr++) {
  120.     int writing = 1;    /* set to false if not pumping to pathalias */
  121.     int skipmap;
  122.     char *((*rdfcn)());
  123.     int compressed = 0;
  124.  
  125.     (void) strcpy(tempbuf, *mapptr);
  126.  
  127.     p = *mapptr + strlen(*mapptr) - 2;
  128.  
  129.     if (*p == '.' && *(p+1) == 'Z') {
  130.         compressed = 1;
  131.         tempbuf[p - *mapptr] = '\0';
  132.     }
  133.  
  134.     skipmap = (mapptr < locptr) && search(lmapfiles.list, tempbuf);
  135.     if (!path || skipmap)
  136.         writing = 0;
  137.     
  138.     if (unlinkskip && skipmap && **mapptr == 'u') {
  139.         if (verbose)
  140.         (void) fprintf(stderr, "Unlinking %s\n", tempbuf);
  141.         stunlinked++;
  142.         (void) unlink(*mapptr);
  143.         continue;
  144.     }
  145.  
  146.     if (debug)
  147.         (void) fprintf(stderr, "opening %s, .Z=: %d, writing: %d, skip: %d\n", *mapptr,
  148.         compressed, writing, skipmap);
  149.  
  150.     if (compressed) {
  151.         fatal(!(mapf = zfopen(*mapptr, "r")), "Can't zfopen map file");
  152.         rdfcn = zfgets;
  153.     } else {
  154.         fatal(!(mapf = fopen(*mapptr, "r")), "Can't fopen map file");
  155.         rdfcn = fgets;
  156.     }
  157.     stprocessed++;
  158.  
  159. #ifndef    OLDPATHALIAS
  160.     if (writing && path)
  161.         (void) fprintf(path, "file {%s}\n", *mapptr);
  162. #endif
  163.     recno = 0;
  164.  
  165.     while(rdfcn(buf, sizeof(buf), mapf)) {
  166.         recno++;
  167.  
  168.         /* optimization: delete comments from pathalias input. */
  169.         if (buf[0] == '#') {
  170.         if (writing)
  171.             (void) putc('\n', path);
  172.  
  173.         /* If a #N[ame] record, save information */
  174.         if (buf[1] == 'N') {
  175.  
  176.             /* #N[ame][ \t]+site[, ]+site[, ]+,site */
  177.             for (p = buf + 2; *p && !isspace(*p); p++)
  178.             continue;
  179.             while(*p && isspace(*p)) p++;
  180.             while(*p && *p != '\n') {
  181.  
  182.             while(*p && *p != ',' && !isspace(*p)) {
  183.                 (void) putc(*p, whfp);
  184.                 p++;    /* ya never know */
  185.             }
  186.  
  187.             (void) fprintf(whfp, "\t%s\t%d\n", tempbuf, recno);
  188.  
  189.             while(*p && (*p == ',' || isspace(*p)))
  190.                 p++;
  191.             }
  192.         }
  193.         } else {
  194.         p = strchr(buf, '#');
  195.         if (p) {
  196.             for (p--; p >= buf && (*p == ' ' || *p == '\t'); p--)
  197.             continue;
  198.             if (p < buf) {
  199.             *++p = ' ';    /* wierd but true - pathalias */
  200.             *++p = '#';    /* needs it sometimes */
  201.             }
  202.             *++p = '\n';
  203.             *++p = '\0';
  204.         }
  205.         if (writing)
  206.             (void) fputs(buf, path);
  207.         }
  208.     }
  209.  
  210.     if (compressed)
  211.         (void) zfclose(mapf);
  212.     else
  213.         (void) fclose(mapf);
  214.     if (skipmap && unlinkskip && **mapptr == 'u') {
  215.         (void) fprintf(stderr, "Unlinking %s\n", *mapptr);
  216.         (void) unlink(*mapptr);
  217.     }
  218. #ifndef    OLDPATHALIAS
  219.     if (writing && path)
  220.         (void) fprintf(path, "private {}\n");
  221. #endif
  222.     }
  223.  
  224.     if (stunlinked)
  225.     (void) fprintf(stderr, "%d map files discarded by discard list\n",
  226.         stunlinked);
  227.     (void) fprintf(stderr, "%d map files processed\n", stprocessed);
  228.  
  229.     (void) zfiofree();
  230.  
  231.     (void) fclose(whfp);
  232.  
  233.     (void) fflush(stderr);
  234.     if (path && pclose(path)) {
  235.     (void) unlink(pathtmp);
  236.     fatal(1, "pathalias failed");
  237.     }
  238.  
  239.     if (!whereonly)
  240.     (void) fprintf(stderr, "Pathalias completed: okay\n");
  241.  
  242.     sortwdb();
  243.  
  244.     (void) fprintf(stderr, "%s completed: okay\n", wheredb);
  245.     if (whereonly)
  246.     return;
  247.  
  248.     (void) fprintf(stderr, "Sorting pathalias output\n");
  249.     fatal(!(whfp = fopen(pathtmp, "r")), "reopen path temp");
  250.     (void) sprintf(tempbuf, "sort -u > %s", pathtmp2);
  251.  
  252.     fatal(!(path = popen(tempbuf, "w")), "popen sort");
  253.     (void) unlink(pathtmp);        /* we don't have to do cleanup later */
  254.  
  255.     /* swap fields around */
  256.     while(fgets(buf, sizeof(buf), whfp)) {
  257.     char *site, *route, *t;
  258.  
  259.     site = strchr(buf, '\t');
  260.     *site++ = '\0';
  261.     if (route = strchr(site, '\t'))
  262.         *route++ = '\0';
  263.     else
  264.         {
  265.             route = site;
  266.             site = buf;
  267.         }
  268.     t = strchr(route, '\n');
  269.     *t = '\0';
  270.  
  271. #ifdef    NODOTDOMAIN
  272.     if (*site == '.')
  273.         continue;
  274. #endif
  275.  
  276.     /* lcasep's job */
  277.     for (t = site; *t; t++)
  278.         if (isupper(*t))
  279.         *t = tolower(*t);
  280.  
  281.     if (internet)
  282.         dointernet(site, &route);
  283.  
  284.     (void) fputs(site, path);
  285.     (void) putc('\t', path);
  286.     (void) fputs(route, path);
  287.     if (site != buf)
  288.     {
  289.         (void) putc('\t', path);
  290.         (void) fputs(buf, path);
  291.     }
  292.     (void) putc('\n', path);
  293.     }
  294.  
  295.     (void) fclose(whfp);
  296.     (void) unlink(pathtmp);
  297.     if (pclose(path)) {
  298.     (void) unlink(pathtmp2);
  299.     fatal(1, "sort failed");
  300.     }
  301.     (void) fprintf(stderr, "Sorting pathalias output completed: okay\n");
  302.  
  303.     fatal(!(whfp = fopen(pathtmp2, "r")), "Open of pathtmp2 for copy failed");
  304.     fatal(!(path = fopen(pathfile, "w")), "Open of path output file failed");
  305.     while((recno = fread(tempbuf, sizeof(char), BUFSIZ, whfp)) > 0)
  306.     fatal(recno != fwrite(tempbuf, sizeof(char), recno, path),
  307.         "write failed");
  308.     (void) fclose(whfp);
  309.     (void) fclose(path);
  310.     (void) fprintf(stderr, "Creation of %s completed: okay\n", pathfile);
  311.  
  312. }
  313.  
  314. search(list, item)
  315. register char **list, *item; {
  316.     if (debug)
  317.     (void) fprintf(stderr, "find %s\n", item);
  318.     if (!list)
  319.     return(0);
  320.     while(*list)
  321.     if (strcmp(*list++, item) == 0)
  322.         return(1);
  323.     return(0);
  324. }
  325.