1: /*******************************************************************
   2:  *                     curdir: get current directory
   3:  *******************************************************************
   4:  * returns full pathname of working (current) directory.
   5:  * This is an adaptation of pwd, and works for grafted directories.
   6:  * Unlike pwd, returns to current directory after it is finished.
   7:  * Uses stdio buffering for directory reads.
   8:  */
   9: 
  10:  static char rcsid[]=
  11:  "$Header: /arthur/src/local/bin/rcs/src/RCS/curdir.c,v 3.2 87/10/18 10:21:49 narten Exp $";
  12: 
  13: /*******************************************************************
  14:  *      $Log:	curdir.c,v $
  15:  * Revision 3.2  87/10/18  10:21:49  narten
  16:  * Updating version numbers. Changes relative to 1.1 are actually
  17:  * relative to 3.2
  18:  *
  19:  * Revision 1.1  84/01/23  14:50:01  kcs
  20:  * Initial revision
  21:  *
  22:  * Revision 3.2  82/12/24  15:41:51  wft
  23:  * Changed curdir() such that it returns a pointer to the pathname of
  24:  * the current working directory, just as Berkeley's getcwd().
  25:  *
  26:  * Revision 3.1  82/10/18  21:16:21  wft
  27:  * curdir() now uses stdio buffering for directory reads,
  28:  * returns to working directory after done, and closes the directories.
  29:  * A testprogram was also added.
  30:  *
  31:  *******************************************************************/
  32: 
  33: 
  34: #include        "rcsbase.h"
  35: #include        <sys/param.h>
  36: #include        <sys/stat.h>
  37: #include        <sys/dir.h>
  38: #define dot     "."
  39: #define dotdot  ".."
  40: 
  41: 
  42: static char cwd[NCPPN];
  43: 
  44: char * curdir()
  45: /* Function: places the pathname of the current directory into cwd
  46:  * and returns a pointer to it. Returns NULL on failure.
  47:  */
  48: {
  49:         FILE    *file;
  50:         struct  stat    d, dd;
  51:         struct  direct  dir;
  52: 
  53:         int rdev, rino;
  54:         int off;
  55:         register i,j;
  56: 
  57:         cwd[off= 0] = '/';
  58:         cwd[1] = '\0';
  59:         stat("/", &d);
  60:         rdev = d.st_dev;
  61:         rino = d.st_ino;
  62:         for (;;) {
  63:                 if (stat(dot, &d)<0) return NULL;
  64:                 if (d.st_ino==rino && d.st_dev==rdev) {
  65:                         if (cwd[off] == '/') cwd[off] = '\0';
  66:                         chdir(cwd); /*change back to current directory*/
  67:                         return cwd;
  68:                 }
  69:                 if ((file = fopen(dotdot,"r")) == NULL) return NULL;
  70:                 if (fstat(fileno(file), &dd)<0) goto fail;
  71:                 chdir(dotdot);
  72:                 if(d.st_dev == dd.st_dev) {
  73:                         if(d.st_ino == dd.st_ino) {
  74:                             if (cwd[off] == '/') cwd[off] = '\0';
  75:                             chdir(cwd); /*change back to current directory*/
  76:                             fclose(file);
  77:                             return cwd;
  78:                         }
  79:                         do {
  80:                             if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
  81:                                 goto fail;
  82:                         } while (dir.d_ino != d.st_ino);
  83:                 }
  84:                 else do {
  85:                         if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
  86:                             goto fail;
  87:                         }
  88:                         stat(dir.d_name, &dd);
  89:                 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  90:                 fclose(file);
  91: 
  92:                 /* concatenate file name */
  93:                 i = -1;
  94:                 while (dir.d_name[++i] != 0);
  95:                 for(j=off+1; j>0; --j)
  96:                         cwd[j+i+1] = cwd[j];
  97:                 off=i+off+1;
  98:                 cwd[i+1] = '/';
  99:                 for(--i; i>=0; --i)
 100:                         cwd[i+1] = dir.d_name[i];
 101:         } /* end for */
 102: 
 103: fail:   fclose(file);
 104:         return NULL;
 105: }
 106: 
 107: 
 108: #ifdef TEST
 109: main ()
 110: {
 111:         printf ("pwd = %s\n", curdir());
 112: }
 113: #endif TEST

Defined functions

curdir defined in line 44; used 1 times
main defined in line 109; never used

Defined variables

cwd defined in line 42; used 14 times
rcsid defined in line 10; never used

Defined macros

dot defined in line 38; used 1 times
  • in line 63
dotdot defined in line 39; used 2 times
Last modified: 1987-12-18
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2474
Valid CSS Valid XHTML 1.0 Strict