Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / sbr / RCS / pwd.c,v
1 head    2.5;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 2.5
9 date    92.12.15.00.20.22;      author jromine; state Exp;
10 branches;
11 next    2.4;
12
13 2.4
14 date    92.01.31.21.56.45;      author jromine; state Exp;
15 branches;
16 next    2.3;
17
18 2.3
19 date    90.04.05.15.30.25;      author sources; state Exp;
20 branches;
21 next    2.2;
22
23 2.2
24 date    90.04.05.14.44.11;      author sources; state Exp;
25 branches;
26 next    2.1;
27
28 2.1
29 date    90.02.05.14.38.27;      author sources; state Exp;
30 branches;
31 next    2.0;
32
33 2.0
34 date    89.11.17.15.57.25;      author sources; state Exp;
35 branches;
36 next    1.1;
37
38 1.1
39 date    89.06.26.14.31.50;      author sources; state Exp;
40 branches;
41 next    ;
42
43
44 desc
45 @@
46
47
48 2.5
49 log
50 @endif sugar
51 @
52 text
53 @/* pwd.c - return the current working directory */
54 #ifndef lint
55 static char ident[] = "@@(#)$Id: pwd.c,v 2.4 1992/01/31 21:56:45 jromine Exp jromine $";
56 #endif  /* lint */
57
58 #include "../h/mh.h"
59 #if     !defined (BSD42) && !defined (SYS5DIR)
60 #include "../h/local.h"
61 #endif  /* not BSD42 and not SYS5DIR */
62 #include <stdio.h>
63
64 #define MAXPATHLEN      1024
65
66 static char curwd[MAXPATHLEN];
67
68
69
70 char   *pwd () {
71     register char  *cp;
72
73 #ifndef BSD42
74 #ifndef SYS5DIR
75     if (getwd (curwd) == NOTOK) {
76         admonish (NULL, "unable to determine working directory");
77 #else   /* SYS5DIR */
78     if (getcwd (curwd, MAXPATHLEN) == NULL) {
79         admonish (NULL, "unable to determine working directory");
80 #endif  /* SYS5DIR */
81 #else   /* BSD42 */
82     if (getwd (curwd) == 0) {
83         admonish (NULLCP, "unable to determine working directory: %s", curwd);
84 #endif  /* BSD42 */
85         if (mypath == NULL
86                 || *mypath == 0
87                 || ((void) strcpy (curwd, mypath), chdir (curwd)) == NOTOK) {
88             (void) strcpy (curwd, "/");
89             (void) chdir (curwd);
90         }
91         return curwd;
92     }
93
94     if ((cp = curwd + strlen (curwd) - 1) > curwd && *cp == '/')
95         *cp = 0;
96
97     return curwd;
98 }
99
100 /* \f */
101
102 #if     !defined (BSD42) && !defined (SYS5DIR)
103 /* getwd() - get the current working directory */
104
105 /* Algorithm from several sources, -ljobs, pwd.c, etc., etc. */
106
107 getwd (cwd)
108 register char   *cwd;
109 {
110     int     found;
111     char    tmp1[BUFSIZ],
112             tmp2[BUFSIZ];
113     struct stat st1,
114                 st2,
115                 root;
116     register struct direct *dp;
117     register    DIR * dd;
118
119     (void) strcpy (cwd, "/");
120     (void) stat ("/", &root);
121
122     for (;;) {
123         if ((dd = opendir ("..")) == NULL)
124             return NOTOK;
125         if (stat (".", &st2) == NOTOK || stat ("..", &st1) == NOTOK)
126             goto out;
127         if (st2.st_ino == root.st_ino && st2.st_dev == root.st_dev) {
128             closedir (dd);
129             return chdir (cwd);
130         }
131
132         if (st2.st_ino == st1.st_ino && st2.st_dev == st1.st_dev) {
133             closedir (dd);
134             (void) chdir ("/");
135             if ((dd = opendir (".")) == NULL)
136                 return NOTOK;
137             if (stat (".", &st1) < 0)
138                 goto out;
139             if (st2.st_dev != st1.st_dev)
140                 while (dp = readdir (dd)) {
141                     if (stat (dp -> d_name, &st1) == NOTOK)
142                         goto out;
143                     if (st2.st_dev == st1.st_dev) {
144                         (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
145                         (void) strcpy (cwd + 1, tmp1);
146                         closedir (dd);
147                         return (chdir (cwd));
148                     }
149                 }
150             else {
151                 closedir (dd);
152                 return (chdir (cwd));
153             }
154         }
155
156         found = 0;
157         while (dp = readdir (dd)) {
158             (void) sprintf (tmp2, "../%s", dp -> d_name);
159             if (stat (tmp2, &st1) != NOTOK
160                     && st1.st_ino == st2.st_ino
161                     && st1.st_dev == st2.st_dev) {
162                 closedir (dd);
163                 found++;
164                 (void) chdir ("..");
165                 (void) sprintf (tmp1, "%s%s", dp -> d_name, cwd);
166                 (void) strcpy (cwd + 1, tmp1);
167                 break;
168             }
169         }
170         if (!found)
171             goto out;
172     }
173
174 out: ;
175     closedir (dd);
176     return NOTOK;
177 }
178 #endif  /* not BSD42 and not SYS5DIR */
179 @
180
181
182 2.4
183 log
184 @kerberos
185 @
186 text
187 @d3 2
188 a4 2
189 static char ident[] = "@@(#)$Id: pwd.c,v 2.3 1990/04/05 15:30:25 sources Exp jromine $";
190 #endif  lint
191 d9 1
192 a9 1
193 #endif  not BSD42 and not SYS5DIR
194 d25 1
195 a25 1
196 #else   SYS5DIR
197 d28 2
198 a29 2
199 #endif  SYS5DIR
200 #else   BSD42
201 d32 1
202 a32 1
203 #endif  BSD42
204 d126 1
205 a126 1
206 #endif  not BSD42 and not SYS5DIR
207 @
208
209
210 2.3
211 log
212 @add ID
213 @
214 text
215 @d3 1
216 a3 1
217 static char ident[] = "@@(#)$Id:$";
218 d30 1
219 a30 1
220     if (getwd (curwd) == NULL) {
221 d34 1
222 a34 1
223                 || *mypath == NULL
224 d43 1
225 a43 1
226         *cp = NULL;
227 @
228
229
230 2.2
231 log
232 @add ID
233 @
234 text
235 @d3 1
236 a3 1
237 static char ident[] = "$Id:";
238 @
239
240
241 2.1
242 log
243 @new SYS5DIR routine
244 @
245 text
246 @d2 3
247 @
248
249
250 2.0
251 log
252 @changes for SUN40 shared libraries and NNTP under bbc
253 @
254 text
255 @d4 1
256 a4 1
257 #ifndef BSD42
258 d6 1
259 a6 1
260 #endif  not BSD42
261 d19 1
262 d22 4
263 d47 1
264 a47 1
265 #ifndef BSD42
266 a60 4
267 #ifdef SYS5DIR
268     register struct dirent *dp;
269                 DIR * dd;
270 #else  SYS5DIR
271 a62 1
272 #endif SYS5DIR
273 d123 1
274 a123 1
275 #endif  not BSD42
276 @
277
278
279 1.1
280 log
281 @Initial revision
282 @
283 text
284 @d56 4
285 d62 1
286 @