1: /* pwd.c - return the current working directory */
   2: 
   3: #include "../h/mh.h"
   4: #include <stdio.h>
   5: #ifndef BSD42
   6: #include <sys/types.h>
   7: #include <sys/stat.h>
   8: #ifndef SYS5
   9: #include <ndir.h>
  10: #else   SYS5
  11: #include <dir.h>
  12: #endif	SYS5
  13: #endif	BSD42
  14: 
  15: #define MAXPATHLEN  1024
  16: 
  17: static char curwd[MAXPATHLEN];
  18: 
  19: 
  20: 
  21: char   *pwd () {
  22:     register char  *cp;
  23: 
  24: #ifndef BSD42
  25:     if (getwd (curwd) == NOTOK) {
  26:     admonish (NULL, "unable to determine working directory");
  27: #else   BSD42
  28:     if (getwd (curwd) == NULL) {
  29:     admonish (NULLCP, "unable to determine working directory: %s", curwd);
  30: #endif	BSD42
  31:     if (mypath == NULL
  32:         || *mypath == NULL
  33:         || ((void) strcpy (curwd, mypath), chdir (curwd)) == NOTOK) {
  34:         (void) strcpy (curwd, "/");
  35:         (void) chdir (curwd);
  36:     }
  37:     return curwd;
  38:     }
  39: 
  40:     if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
  41:     *cp = NULL;
  42: 
  43:     return curwd;
  44: }
  45: 
  46: /*  */
  47: 
  48: #ifndef BSD42
  49: /* getwd() - get the current working directory */
  50: 
  51: /* Algorithm from several sources, -ljobs, pwd.c, etc., etc. */
  52: 
  53: getwd (cwd)
  54: register char   *cwd;
  55: {
  56:     int     found;
  57:     char    tmp1[BUFSIZ],
  58:             tmp2[BUFSIZ];
  59:     struct stat st1,
  60:                 st2,
  61:                 root;
  62:     register struct direct *dp;
  63:     register    DIR * dd;
  64: 
  65:     (void) strcpy (cwd, "/");
  66:     (void) stat ("/", &root);
  67: 
  68:     for (;;) {
  69:     if ((dd = opendir ("..")) == NULL)
  70:         return NOTOK;
  71:     if (stat (".", &st2) == NOTOK || stat ("..", &st1) == NOTOK)
  72:         goto out;
  73:     if (st2.st_ino == root.st_ino && st2.st_dev == root.st_dev) {
  74:         closedir (dd);
  75:         return chdir (cwd);
  76:     }
  77: 
  78:     if (st2.st_ino == st1.st_ino && st2.st_dev == st1.st_dev) {
  79:         closedir (dd);
  80:         (void) chdir ("/");
  81:         if ((dd = opendir (".")) == NULL)
  82:         return NOTOK;
  83:         if (stat (".", &st1) < 0)
  84:         goto out;
  85:         if (st2.st_dev != st1.st_dev)
  86:         while (dp = readdir (dd)) {
  87:             if (stat (dp -> d_name, &st1) == NOTOK)
  88:             goto out;
  89:             if (st2.st_dev == st1.st_dev) {
  90:             (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
  91:             (void) strcpy (cwd + 1, tmp1);
  92:             closedir (dd);
  93:             return (chdir (cwd));
  94:             }
  95:         }
  96:         else {
  97:         closedir (dd);
  98:         return (chdir (cwd));
  99:         }
 100:     }
 101: 
 102:     found = 0;
 103:     while (dp = readdir (dd)) {
 104:         (void) sprintf (tmp2, "../%s", dp -> d_name);
 105:         if (stat (tmp2, &st1) != NOTOK
 106:             && st1.st_ino == st2.st_ino
 107:             && st1.st_dev == st2.st_dev) {
 108:         closedir (dd);
 109:         found++;
 110:         (void) chdir ("..");
 111:         (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
 112:         (void) strcpy (cwd + 1, tmp1);
 113:         break;
 114:         }
 115:     }
 116:     if (!found)
 117:         goto out;
 118:     }
 119: 
 120: out: ;
 121:     closedir (dd);
 122:     return NOTOK;
 123: }
 124: #endif	not BSD42

Defined functions

getwd defined in line 53; used 2 times
pwd defined in line 21; used 1 times

Defined variables

curwd defined in line 17; used 12 times

Defined macros

MAXPATHLEN defined in line 15; used 1 times
  • in line 17
Last modified: 1986-02-19
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 650
Valid CSS Valid XHTML 1.0 Strict