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

  1. #include <string.h>
  2. #include <sys/param.h>
  3. #include <sys/stat.h>
  4.  
  5. extern char * getenv();
  6. extern void cannoname();
  7. extern char * unsymlink();
  8. extern char * getwd();
  9.  
  10. /* kshgetwd is a routine that acts just like getwd, but is optimized
  11.  * for ksh users, taking advantage of the fact that ksh maintains
  12.  * an environment variable named PWD holding path name of the
  13.  * current working directory.
  14.  *
  15.  * The primary motivation for this is not really that it is algorithmically
  16.  * simpler, but that it is much less likely to bother NFS if we can just
  17.  * guess the name of the current working directory using the hint that
  18.  * ksh maintains. Anything that avoids NFS gettar failed messages is
  19.  * worth doing.
  20.  */
  21. char *
  22. kshgetwd(pathname)
  23.    char *   pathname;
  24. {
  25.    struct stat kshstat, dotstat ;
  26.    char        kshname[MAXPATHLEN] ;
  27.    char *      kshp ;
  28.  
  29.    kshp = getenv("PWD") ;
  30.    if (kshp) {
  31.       /* OK, there was a PWD environment variable */
  32.       strcpy(kshname, kshp) ;
  33.       if (unsymlink(kshname)) {
  34.          /* And we could resolve the symbolic links through it */
  35.          if (kshname[0] == '/') {
  36.             /* And the name we have is an absolute path name */
  37.             if (stat(kshname, &kshstat) == 0) {
  38.                /* And we can stat the name */
  39.                if (stat(".", &dotstat) == 0) {
  40.                   /* And we can stat "." */
  41.                   if ((kshstat.st_dev == dotstat.st_dev) &&
  42.                       (kshstat.st_ino == dotstat.st_ino)) {
  43.                      /* By golly, that name is the same file as "." ! */
  44.                      return(strcpy(pathname, kshname)) ;
  45.                   }
  46.                }
  47.             }
  48.          }
  49.       }
  50.    }
  51.    /* Oh well, something did not work out right, do it the hard way */
  52.    return(getwd(pathname)) ;
  53. }
  54.