1: /*
   2:  * getwd() returns the pathname of the current working directory. On error
   3:  * an error message is copied to pathname and null pointer is returned.
   4:  *
   5:  * A version of this function was copied to the standard C library. It is
   6:  * only included here for compatibility with non-BSD UNIX systems.
   7:  */
   8: #include <sys/param.h>
   9: #include <sys/stat.h>
  10: #include <sys/dir.h>
  11: #include "null.h"
  12: #include "path.h"
  13: 
  14: #define GETWDERR(s) strcpy(pathname, (s));
  15: 
  16: char *strcpy();             /* string copy */
  17: 
  18: static int pathsize;            /* pathname length */
  19: 
  20: char *
  21: getwd(pathname)
  22:     char *pathname;
  23: {
  24:     char pathbuf[PATHSIZE];     /* temporary pathname buffer */
  25:     char *pnptr = &pathbuf[(sizeof pathbuf)-1]; /* pathname pointer */
  26:     char *prepend();        /* prepend dirname to pathname */
  27:     dev_t rdev;         /* root device number */
  28:     DIR *dirp;          /* directory stream */
  29:     ino_t rino;         /* root inode number */
  30:     struct direct *dir;     /* directory entry struct */
  31:     struct stat d ,dd;      /* file status struct */
  32: 
  33:     pathsize = 0;
  34:     *pnptr = '\0';
  35:     stat(ROOTDIR, &d);
  36:     rdev = d.st_dev;
  37:     rino = d.st_ino;
  38:     for (;;)
  39:         {
  40:         stat(CURDIR, &d);
  41:         if (d.st_ino == rino && d.st_dev == rdev)
  42:             break;      /* reached root directory */
  43:         if ((dirp = opendir(PARENTDIR)) == NULL)
  44:             {
  45:             GETWDERR("getwd: can't open ..");
  46:             goto fail;
  47:             }
  48:         if (chdir(PARENTDIR) < 0)
  49:             {
  50:             GETWDERR("getwd: can't chdir to ..");
  51:             goto fail;
  52:             }
  53:         fstat(dirp->dd_fd, &dd);
  54:         if(d.st_dev == dd.st_dev)
  55:             {
  56:             if(d.st_ino == dd.st_ino)
  57:                 {   /* reached root directory */
  58:                 closedir(dirp);
  59:                 break;
  60:                 }
  61:             do  {
  62:                 if ((dir = readdir(dirp)) == NULL)
  63:                     {
  64:                     closedir(dirp);
  65:                     GETWDERR("getwd: read error in ..");
  66:                     goto fail;
  67:                     }
  68:                 } while (dir->d_ino != d.st_ino);
  69:             }
  70:         else do {
  71:             if((dir = readdir(dirp)) == NULL)
  72:                 {
  73:                 closedir(dirp);
  74:                 GETWDERR("getwd: read error in ..");
  75:                 goto fail;
  76:                 }
  77:             stat(dir->d_name, &dd);
  78:             } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  79:         closedir(dirp);
  80:         pnptr = prepend(PATHSEP, prepend(dir->d_name, pnptr));
  81:         }
  82:     if (*pnptr == '\0')
  83:         {           /* current dir == root dir */
  84:         strcpy(pathname, ROOTDIR);
  85:         }
  86:     else    {
  87:         strcpy(pathname, pnptr);
  88:         if (chdir(pnptr) < 0)
  89:             {
  90:             GETWDERR("getwd: can't change back to .");
  91:             return(NULL);
  92:             }
  93:         }
  94:     return(pathname);
  95: 
  96: fail:   chdir(prepend(CURDIR, pnptr));
  97:     return(NULL);
  98: }
  99: 
 100: 
 101: 
 102: /*
 103:  * prepend() tacks a directory name onto the front of a pathname.
 104:  */
 105: static char *
 106: prepend(dirname, pathname)
 107:     register char *dirname;
 108:     register char *pathname;
 109: {
 110:     register int i;         /* directory name size counter */
 111: 
 112:     for (i = 0; *dirname != '\0'; i++, dirname++)
 113:         continue;
 114:     if ((pathsize += i) < PATHSIZE)
 115:         while (i-- > 0)
 116:             *--pathname = *--dirname;
 117:     return(pathname);
 118: }

Defined functions

getwd defined in line 20; used 2 times
prepend defined in line 105; used 4 times

Defined variables

pathsize defined in line 18; used 2 times

Defined macros

GETWDERR defined in line 14; used 5 times
Last modified: 1985-07-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 571
Valid CSS Valid XHTML 1.0 Strict