home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume24 / mkid2 / part01 / opensrc.c < prev    next >
C/C++ Source or Header  |  1991-10-09  |  3KB  |  131 lines

  1. /* Copyright (c) 1986, Greg McGary */
  2. static char sccsid[] = "@(#)opensrc.c    1.1 86/10/09";
  3.  
  4. #include    <stdio.h>
  5. #include    <string.h>
  6. #include    <sys/types.h>
  7. #include    <sys/stat.h>
  8.  
  9. char *findSrcFILE();
  10. char *getSCCS();
  11. char *coRCS();
  12.  
  13. FILE *
  14. openSrcFILE(path, sccsDir, rcsDir, filter)
  15.     char        *path;
  16.     char        *sccsDir;
  17.     char        *rcsDir;
  18.     char        *filter;
  19. {
  20.     char        *command = NULL;
  21.     char        *what = NULL;
  22.     char        *get = "get SCCS file";
  23.     char        *checkout = "checkout RCS file";
  24.     char        *dirName;
  25.     char        *baseName;
  26.     struct stat    statb;
  27.     char        popcom[1024];
  28.     FILE        *srcFILE;
  29.  
  30.     if (stat(path, &statb) != 0) {
  31.         if ((baseName = strrchr(path, '/')) == NULL) {
  32.             dirName = ".";
  33.             baseName = path;
  34.         } else {
  35.             dirName = path;
  36.             *baseName++ = '\0';
  37.         }
  38.  
  39.         if (rcsDir && (command = coRCS(dirName, baseName, rcsDir)))
  40.             what = checkout;
  41.         else if (sccsDir && (command = getSCCS(dirName, baseName, sccsDir)))
  42.             what = get;
  43.         else if ((command = coRCS(dirName, baseName, "RCS"))
  44.              ||  (command = coRCS(dirName, baseName, ".")))
  45.             what = checkout;
  46.         else if ((command = getSCCS(dirName, baseName, "SCCS"))
  47.              ||  (command = getSCCS(dirName, baseName, "sccs"))
  48.              ||  (command = getSCCS(dirName, baseName, ".")))
  49.             what = get;
  50.  
  51.         if (dirName == path)
  52.             *--baseName = '/';
  53.  
  54.         if (!command) {
  55.             filerr("open", path);
  56.             return NULL;
  57.         }
  58.  
  59.         system(command);
  60.         fprintf(stderr, "%s\n", command);
  61.     }
  62.     if (stat(path, &statb) != 0) {
  63.         filerr("open", path);
  64.         return NULL;
  65.     }
  66.     if (filter != NULL) {
  67.         sprintf(popcom,filter,path);
  68.         srcFILE = popen(popcom, "r");
  69.     } else {
  70.         srcFILE = fopen(path, "r");
  71.     }
  72.     if (srcFILE == NULL) {
  73.         filerr("open", path);
  74.     }
  75.     return srcFILE;
  76. }
  77.  
  78. void
  79. closeSrcFILE(fp, filter)
  80.     FILE        *fp;
  81.     char        *filter;
  82. {
  83.     if (filter != NULL) {
  84.         pclose(fp);
  85.     } else {
  86.         fclose(fp);
  87.     }
  88. }
  89.  
  90. char *
  91. getSCCS(dir, base, sccsDir)
  92.     char        *dir;
  93.     char        *base;
  94.     char        *sccsDir;
  95. {
  96.     static char    cmdBuf[BUFSIZ];
  97.     char        fileBuf[BUFSIZ];
  98.     struct stat    statBuf;
  99.  
  100.     if (!*sccsDir)
  101.         sccsDir = ".";
  102.  
  103.     sprintf(fileBuf, "%s/%s/s.%s", dir, sccsDir, base);
  104.     if (stat(fileBuf, &statBuf) < 0)
  105.         return NULL;
  106.     sprintf(cmdBuf, "cd %s; get -s %s/s.%s", dir, sccsDir, base);
  107.  
  108.     return cmdBuf;
  109. }
  110.  
  111. char *
  112. coRCS(dir, base, rcsDir)
  113.     char        *dir;
  114.     char        *base;
  115.     char        *rcsDir;
  116. {
  117.     static char    cmdBuf[BUFSIZ];
  118.     char        fileBuf[BUFSIZ];
  119.     struct stat    statBuf;
  120.  
  121.     if (!*rcsDir)
  122.         rcsDir = ".";
  123.  
  124.     sprintf(fileBuf, "%s/%s/%s,v", dir, rcsDir, base);
  125.     if (stat(fileBuf, &statBuf) < 0)
  126.         return NULL;
  127.     sprintf(cmdBuf, "cd %s; co -q %s/%s,v", dir, rcsDir, base);
  128.  
  129.     return cmdBuf;
  130. }
  131.