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