home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / cvs-berliner / part01 / src / find_names.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.2 KB  |  136 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 !lint
  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/param.h>
  24. #include <sys/types.h>
  25. #include <dirent.h>
  26. #include "cvs.h"
  27.  
  28. Find_Names(pargc, argv, which)
  29.     int *pargc;
  30.     char *argv[];
  31.     enum ftype which;
  32. {
  33.     char dir[MAXPATHLEN], line[MAXLINELEN];
  34.     FILE *fpin;
  35.     char *cp;
  36.  
  37.     *pargc = 0;
  38.     argv[0] = NULL;            /* Assume none */
  39.     if (which == MOD) {
  40.     fpin = open_file(CVSADM_MOD, "r");
  41.     /*
  42.      * Parse the Mod file, and calling addname() for each line.
  43.      */
  44.     while (fgets(line, sizeof(line), fpin) != NULL) {
  45.         if ((cp = rindex(line, '\n')) != NULL)
  46.         *cp = '\0';
  47.         *cp = '\0';
  48.         addname(pargc, argv, line);
  49.     }
  50.     (void) fclose(fpin);
  51.     } else {
  52.     fpin = open_file(CVSADM_ENT, "r");
  53.     /*
  54.      * Only scan for ,v files if Entries.Static does not exist
  55.      */
  56.     if (!isfile(CVSADM_ENTSTAT)) {
  57.         if (find_rcs(Repository, pargc, argv) != 0)
  58.         error(1, "cannot open directory %s", Repository);
  59.         if (which == ALLPLUSATTIC) {
  60.         (void) sprintf(dir, "%s/%s", Repository, CVSATTIC);
  61.         (void) find_rcs(dir, pargc, argv);
  62.         }
  63.     }
  64.     /*
  65.      * Parse the Entries file, and calling addname() for each one.
  66.      */
  67.     while (fgets(line, sizeof(line), fpin) != NULL) {
  68.         if ((cp = rindex(line, '|')) == NULL)
  69.         continue;
  70.         *cp = '\0';
  71.         if ((cp = rindex(line, ' ')) == NULL)
  72.         continue;
  73.         cp++;
  74.         addname(pargc, argv, cp);
  75.     }
  76.     (void) fclose(fpin);
  77.     }
  78.     /*
  79.      * And finally, sort the names so that they look reasonable
  80.      * as they are processed (there *is* order in the world)
  81.      */
  82.     qsort((char *)&argv[0], *pargc, sizeof(argv[0]), ppstrcmp);
  83. }
  84.  
  85. /*
  86.  * Finds all the ,v files in the argument directory, and adds them to the
  87.  * argv list.  Returns 0 for success and non-zero if the argument
  88.  * directory cannot be opened.
  89.  */
  90. static
  91. find_rcs(dir, pargc, argv)
  92.     char *dir;
  93.     int *pargc;
  94.     char *argv[];
  95. {
  96.     char *cp, line[50];
  97.     struct dirent *dp;
  98.     DIR *dirp;
  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.         /* strip the ,v */
  108.         *rindex(dp->d_name, ',') = '\0';
  109.         addname(pargc, argv, dp->d_name);
  110.     }
  111.     }
  112.     (void) closedir(dirp);
  113.     return (0);
  114. }
  115.  
  116. /*
  117.  * addname() adds a name to the argv array, only if it is not in
  118.  * the array already
  119.  */
  120. static
  121. addname(pargc, argv, name)
  122.     int *pargc;
  123.     char *argv[];
  124.     char *name;
  125. {
  126.     register int i;
  127.  
  128.     for (i = 0; i < *pargc; i++) {
  129.     if (strcmp(argv[i], name) == 0)
  130.         return;
  131.     }
  132.     (*pargc)++;
  133.     argv[i] = xmalloc(strlen(name) + 1);
  134.     (void) strcpy(argv[i], name);
  135. }
  136.