home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / find_nam.c < prev    next >
C/C++ Source or Header  |  1992-02-23  |  4KB  |  147 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Id: find_names.c,v 1.11 89/11/19 23:20:02 berliner Exp $";
  3. #endif 
  4.  
  5. /*
  6.  *    Copyright (c) 1989, Brian Berliner
  7.  *
  8.  *    You may distribute under the terms of the GNU General Public License
  9.  *    as specified in the README file that comes with the CVS 1.0 kit.
  10.  *
  11.  * Find Names
  12.  *
  13.  *    Writes all the pertinent file names, both from the administration
  14.  *    and from the repository to the argc/argv arguments.
  15.  *
  16.  *    The names should be freed by callin free_names() when they are no
  17.  *    longer needed.
  18.  *
  19.  *    Note that Find_Names() honors the administration Entries.Static file
  20.  *    to indicate that the Repository should not be searched for new files.
  21.  */
  22.  
  23. #include <sys/types.h>
  24. #include "dir.h"
  25. #include "cvs.h"
  26.  
  27. Find_Names(pargc, argv, which)
  28.     int *pargc;
  29.     char *argv[];
  30.     enum ftype which;
  31. {
  32.     char dir[MAXPATHLEN], line[MAXLINELEN];
  33.     FILE *fpin;
  34.     char *cp;
  35.  
  36.     *pargc = 0;
  37.     argv[0] = NULL;            /* Assume none */
  38.     if (which == MOD) {
  39.     fpin = open_file(CVSADM_MOD, "r");
  40.     /*
  41.      * Parse the Mod file, and calling addname() for each line.
  42.      */
  43.     while (fgets(line, sizeof(line), fpin) != NULL) {
  44.         if ((cp = rindex(line, '\n')) != NULL)
  45.         *cp = '\0';
  46.         *cp = '\0';
  47.         addname(pargc, argv, line);
  48.     }
  49.     (void) fclose(fpin);
  50.     } else {
  51.     fpin = open_file(CVSADM_ENT, "r");
  52.     /*
  53.      * Only scan for ,v files if Entries.Static does not exist
  54.      */
  55.     if (!isfile(CVSADM_ENTSTAT)) {
  56.         if (find_rcs(Repository, pargc, argv) != 0)
  57.         error(1, "cannot open directory %s", Repository);
  58.         if (which == ALLPLUSATTIC) {
  59.         (void) sprintf(dir, "%s%c%s", Repository, DIRSEP, CVSATTIC);
  60.         (void) find_rcs(dir, pargc, argv);
  61.         }
  62.     }
  63.     /*
  64.      * Parse the Entries file, and calling addname() for each one.
  65.      */
  66.     while (fgets(line, sizeof(line), fpin) != NULL) {
  67.         if ((cp = rindex(line, '|')) == NULL)
  68.         continue;
  69.         *cp = '\0';
  70.         if ((cp = rindex(line, ' ')) == NULL)
  71.         continue;
  72.         cp++;
  73.         addname(pargc, argv, cp);
  74.     }
  75.     (void) fclose(fpin);
  76.     }
  77.     /*
  78.      * And finally, sort the names so that they look reasonable
  79.      * as they are processed (there *is* order in the world)
  80.      */
  81.     qsort((char *)&argv[0], *pargc, sizeof(argv[0]), ppstrcmp);
  82. }
  83.  
  84. /*
  85.  * Finds all the ,v files in the argument directory, and adds them to the
  86.  * argv list.  Returns 0 for success and non-zero if the argument
  87.  * directory cannot be opened.
  88.  */
  89. static
  90. find_rcs(dir, pargc, argv)
  91.     char *dir;
  92.     int *pargc;
  93.     char *argv[];
  94. {
  95.     char *cp, line[256];
  96.     struct dirent *dp;
  97.     DIR *dirp;
  98.     FILE *fp;
  99.  
  100.     if ((dirp = opendir(dir)) == NULL)
  101.     return (1);
  102.     (void) sprintf(line, ".*%s$", RCSEXT);
  103.     if ((cp = re_comp(line)) != NULL)
  104.     error(0, "%s", cp);
  105.     while ((dp = readdir(dirp)) != NULL) {
  106.     if (re_exec(dp->d_name)) {
  107.             /* check if really a RCS file */
  108.             (void) sprintf(line, "%s%c%s", dir, DIRSEP, dp->d_name);
  109.             if ((fp = fopen(line, "r")) == NULL)
  110.                 continue;
  111.             cp = fgets(line, strlen(RCSHEAD) + 1, fp);
  112.             fclose(fp);
  113.             if (cp == NULL)
  114.                 continue;
  115.             if (strncmp(line, RCSHEAD, strlen(RCSHEAD)))
  116.                 continue;
  117.         /* strip the ,v */
  118.             if ((cp = rindex(dp->d_name, ',')) != NULL)
  119.             *cp = '\0';
  120.         addname(pargc, argv, dp->d_name);
  121.     }
  122.     }
  123.     (void) closedir(dirp);
  124.     return (0);
  125. }
  126.  
  127. /*
  128.  * addname() adds a name to the argv array, only if it is not in
  129.  * the array already
  130.  */
  131. static
  132. addname(pargc, argv, name)
  133.     int *pargc;
  134.     char *argv[];
  135.     char *name;
  136. {
  137.     register int i;
  138.  
  139.     for (i = 0; i < *pargc; i++) {
  140.     if (strcmp(argv[i], name) == 0)
  141.         return 0;
  142.     }
  143.     (*pargc)++;
  144.     argv[i] = xmalloc(strlen(name) + 1);
  145.     (void) strcpy(argv[i], name);
  146. }
  147.