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