home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / macps / ucbwhich.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  3.2 KB  |  126 lines

  1. /*
  2.  * Copyright (c) 1988, The Regents of the University of California.
  3.  * Edward Moy, Workstation Software Support Group, Workstation Support Serices,
  4.  * Information Systems and Technology.
  5.  *
  6.  * Permission is granted to any individual or institution to use, copy,
  7.  * or redistribute this software so long as it is not sold for profit,
  8.  * provided that this notice and the original copyright notices are
  9.  * retained.  The University of California makes no representations about the
  10.  * suitability of this software for any purpose.  It is provided "as is"
  11.  * without express or implied warranty.
  12.  */
  13.  
  14. #ifndef CONFIGDIR
  15. #ifndef lint
  16. static char *SCCSid = "@(#)ucbwhich.c    2.2 10/24/89";
  17. #endif lint
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include "ucbwhich.h"
  23.  
  24. #define    F_OK        0    /* does file exist */
  25. #define    X_OK        1    /* is it executable by caller */
  26. #define    W_OK        2    /* writable by caller */
  27. #define    R_OK        4    /* readable by caller */
  28.  
  29. #define    LIBLEN        4
  30. #ifdef SYSV
  31. #define    index        strchr
  32. #define    rindex        strrchr
  33. #endif SYSV
  34.  
  35. static char lib[] = "/lib";
  36.  
  37. char ucblib[UCBMAXPATHLEN];
  38. int ucbalternate = 0;
  39. char ucbpath[UCBMAXPATHLEN];
  40.  
  41. ucbwhich(str)
  42. char *str;
  43. {
  44.     register char *dir, *name, *cp, *tp;
  45.     register int len;
  46.     char dirbuf[UCBMAXPATHLEN], namebuf[UCBMAXPATHLEN];
  47.     struct stat sbuf;
  48.     char *index(), *rindex(), *getwd(), *getenv();
  49.  
  50.     strcpy(name = namebuf, str);
  51.     if(*name == '/')    /* absolute pathname */
  52.         *(rindex(dir = name, '/')) = 0 ; /* remove tail */
  53.     else {
  54.         if(cp = index(name, '/')) { /* relative pathname */
  55.             if((dir = getwd(dirbuf)) == NULL)
  56.                 return(0);
  57.              /* if any errors occurs assume standard version */
  58.             *cp++ = 0;
  59.             for( ; ; ) {
  60.                 if(*name != 0) { /* multiple slashes */
  61.                     if(strcmp(name, "..") == 0) {
  62.                         /* parent directory */
  63.                         if((tp = rindex(dir, '/')) ==
  64.                          NULL)
  65.                              return(0);
  66.                         if(tp == dir)
  67.                             tp++;
  68.                          /* root directory */
  69.                         *tp = 0;
  70.                          /* remove last component */
  71.                     } else if(strcmp(name, ".") != 0) {
  72.                         /* subdirectory */
  73.                         strcat(dir, "/");
  74.                         strcat(dir, name);
  75.                     }
  76.                 }
  77.                 name = cp;
  78.                 if((cp = index(name, '/')) == NULL) break;
  79.                 /* ignore last component */
  80.                 *cp++ = 0;
  81.             }
  82.         } else { /* look through $PATH variable */
  83.             if((tp = getenv("PATH")) == NULL)
  84.                 return(0);
  85.             for(name = namebuf ; ; ) {
  86.                 if(*tp == 0)
  87.                     return(0);
  88.                 else if(*tp == ':')
  89.                     tp++;
  90.                 if((cp = index(tp, ':')) == NULL)
  91.                     cp = tp + strlen(tp);
  92.                  /* positioned on null */
  93.                 for(dir = dirbuf ; tp < cp ; )
  94.                     *dir++ = *tp++;
  95.                 *dir = 0;
  96.                 strcpy(name, dir = dirbuf);
  97.                 strcat(name, "/");
  98.                 strcat(name, str);
  99.                 if(stat(name, &sbuf) < 0 || (sbuf.st_mode &
  100.                  S_IFMT) != S_IFREG)
  101.                     continue;
  102.                 if(access(name, X_OK) == 0) {
  103.                     if(strcmp(dir, ".") == 0 &&
  104.                      (dir = getwd(dirbuf)) == NULL)
  105.                         return(0);
  106.                     break;
  107.                 }
  108.             }
  109.         }
  110.     }
  111.     strcpy(ucbpath, dir);
  112.     strcpy(ucblib, dir);
  113.     if((len = strlen(dir)) < LIBLEN || strcmp(&dir[len - LIBLEN], lib)
  114.      != 0)
  115.         strcat(ucblib, lib);
  116.     else
  117.         ucbpath[len - LIBLEN] = 0;
  118.     ucbalternate = (strcmp(ucbpath, UCBSTANDARD) != 0);
  119. #ifdef EBUG
  120.     fprintf(stderr, "ucbwhich: alt=%d path=%s lib=%s\n", ucbalternate,
  121.      ucbpath, ucblib);
  122. #endif EBUG
  123.     return(ucbalternate);
  124. }
  125. #endif CONFIGDIR
  126.