Rev 2714 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2714 | Rev 3022 | ||
---|---|---|---|
1 | /* $NetBSD: cd.c,v 1.27 1999/07/09 03:05:49 christos Exp $ */ |
1 | /* $NetBSD: cd.c,v 1.27 1999/07/09 03:05:49 christos Exp $ */ |
2 | 2 | ||
3 | /*- |
3 | /*- |
4 | * Copyright (c) 1991, 1993 |
4 | * Copyright (c) 1991, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
5 | * The Regents of the University of California. All rights reserved. |
6 | * |
6 | * |
7 | * This code is derived from software contributed to Berkeley by |
7 | * This code is derived from software contributed to Berkeley by |
8 | * Kenneth Almquist. |
8 | * Kenneth Almquist. |
9 | * |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright |
13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. |
14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the |
16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. |
17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. All advertising materials mentioning features or use of this software |
18 | * 3. All advertising materials mentioning features or use of this software |
19 | * must display the following acknowledgement: |
19 | * must display the following acknowledgement: |
20 | * This product includes software developed by the University of |
20 | * This product includes software developed by the University of |
21 | * California, Berkeley and its contributors. |
21 | * California, Berkeley and its contributors. |
22 | * 4. Neither the name of the University nor the names of its contributors |
22 | * 4. Neither the name of the University nor the names of its contributors |
23 | * may be used to endorse or promote products derived from this software |
23 | * may be used to endorse or promote products derived from this software |
24 | * without specific prior written permission. |
24 | * without specific prior written permission. |
25 | * |
25 | * |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
36 | * SUCH DAMAGE. |
36 | * SUCH DAMAGE. |
37 | */ |
37 | */ |
38 | 38 | ||
39 | #include <sys/cdefs.h> |
39 | #include <sys/cdefs.h> |
40 | #ifndef lint |
40 | #ifndef lint |
41 | #if 0 |
41 | #if 0 |
42 | static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; |
42 | static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; |
43 | #else |
43 | #else |
44 | __RCSID("$NetBSD: cd.c,v 1.27 1999/07/09 03:05:49 christos Exp $"); |
44 | __RCSID("$NetBSD: cd.c,v 1.27 1999/07/09 03:05:49 christos Exp $"); |
45 | #endif |
45 | #endif |
46 | #endif /* not lint */ |
46 | #endif /* not lint */ |
47 | 47 | ||
48 | #include <sys/types.h> |
48 | #include <sys/types.h> |
49 | #include <sys/stat.h> |
49 | #include <sys/stat.h> |
50 | #include <stdlib.h> |
50 | #include <stdlib.h> |
51 | #include <string.h> |
51 | #include <string.h> |
52 | #include <unistd.h> |
52 | #include <unistd.h> |
53 | #include <errno.h> |
53 | #include <errno.h> |
54 | 54 | ||
55 | /* |
55 | /* |
56 | * The cd and pwd commands. |
56 | * The cd and pwd commands. |
57 | */ |
57 | */ |
58 | 58 | ||
59 | #include "shell.h" |
59 | #include "shell.h" |
60 | #include "var.h" |
60 | #include "var.h" |
61 | #include "nodes.h" /* for jobs.h */ |
61 | #include "nodes.h" /* for jobs.h */ |
62 | #include "jobs.h" |
62 | #include "jobs.h" |
63 | #include "options.h" |
63 | #include "options.h" |
64 | #include "output.h" |
64 | #include "output.h" |
65 | #include "memalloc.h" |
65 | #include "memalloc.h" |
66 | #include "error.h" |
66 | #include "error.h" |
67 | #include "exec.h" |
67 | #include "exec.h" |
68 | #include "redir.h" |
68 | #include "redir.h" |
69 | #include "mystring.h" |
69 | #include "mystring.h" |
70 | #include "show.h" |
70 | #include "show.h" |
71 | #include "cd.h" |
71 | #include "cd.h" |
72 | 72 | ||
73 | STATIC int docd (char *, int); |
73 | STATIC int docd (char *, int); |
74 | STATIC char *getcomponent (void); |
74 | STATIC char *getcomponent (void); |
75 | STATIC void updatepwd (char *); |
75 | STATIC void updatepwd (char *); |
76 | 76 | ||
77 | char *curdir = NULL; /* current working directory */ |
77 | char *curdir = NULL; /* current working directory */ |
78 | char *prevdir; /* previous working directory */ |
78 | char *prevdir; /* previous working directory */ |
79 | STATIC char *cdcomppath; |
79 | STATIC char *cdcomppath; |
80 | 80 | ||
81 | int |
81 | int |
82 | cdcmd(argc, argv) |
82 | cdcmd(argc, argv) |
83 | int argc; |
83 | int argc; |
84 | char **argv; |
84 | char **argv; |
85 | { |
85 | { |
86 | const char *dest; |
86 | const char *dest; |
87 | const char *path; |
87 | const char *path; |
88 | char *p; |
88 | char *p; |
89 | struct stat statb; |
89 | struct stat statb; |
90 | int print = 0; |
90 | int print = 0; |
91 | 91 | ||
92 | nextopt(nullstr); |
92 | nextopt(nullstr); |
93 | if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) |
93 | if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) |
94 | error("HOME not set"); |
94 | error("HOME not set"); |
95 | if (*dest == '\0') |
95 | if (*dest == '\0') |
96 | dest = "."; |
96 | dest = "."; |
97 | if (dest[0] == '-' && dest[1] == '\0') { |
97 | if (dest[0] == '-' && dest[1] == '\0') { |
98 | dest = prevdir ? prevdir : curdir; |
98 | dest = prevdir ? prevdir : curdir; |
99 | print = 1; |
99 | print = 1; |
100 | if (dest) |
100 | if (dest) |
101 | print = 1; |
101 | print = 1; |
102 | else |
102 | else |
103 | dest = "."; |
103 | dest = "."; |
104 | } |
104 | } |
105 | if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL) |
105 | if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL) |
106 | path = nullstr; |
106 | path = nullstr; |
107 | while ((p = padvance(&path, dest)) != NULL) { |
107 | while ((p = padvance(&path, dest)) != NULL) { |
108 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
108 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
109 | if (!print) { |
109 | if (!print) { |
110 | /* |
110 | /* |
111 | * XXX - rethink |
111 | * XXX - rethink |
112 | */ |
112 | */ |
113 | if (p[0] == '.' && p[1] == '/' && p[2] != '\0') |
113 | if (p[0] == '.' && p[1] == '/' && p[2] != '\0') |
114 | p += 2; |
114 | p += 2; |
115 | print = strcmp(p, dest); |
115 | print = strcmp(p, dest); |
116 | } |
116 | } |
117 | if (docd(p, print) >= 0) |
117 | if (docd(p, print) >= 0) |
118 | return 0; |
118 | return 0; |
119 | 119 | ||
120 | } |
120 | } |
121 | } |
121 | } |
122 | error("can't cd to %s", dest); |
122 | error("can't cd to %s", dest); |
123 | /* NOTREACHED */ |
123 | /* NOTREACHED */ |
124 | } |
124 | } |
125 | 125 | ||
126 | 126 | ||
127 | /* |
127 | /* |
128 | * Actually do the chdir. In an interactive shell, print the |
128 | * Actually do the chdir. In an interactive shell, print the |
129 | * directory name if "print" is nonzero. |
129 | * directory name if "print" is nonzero. |
130 | */ |
130 | */ |
131 | 131 | ||
132 | STATIC int |
132 | STATIC int |
133 | docd(dest, print) |
133 | docd(dest, print) |
134 | char *dest; |
134 | char *dest; |
135 | int print; |
135 | int print; |
136 | { |
136 | { |
137 | char *p; |
137 | char *p; |
138 | char *q; |
138 | char *q; |
139 | char *component; |
139 | char *component; |
140 | struct stat statb; |
140 | struct stat statb; |
141 | int first; |
141 | int first; |
142 | int badstat; |
142 | int badstat; |
143 | 143 | ||
144 | TRACE(("docd(\"%s\", %d) called\n", dest, print)); |
144 | TRACE(("docd(\"%s\", %d) called\n", dest, print)); |
145 | 145 | ||
146 | /* |
146 | /* |
147 | * Check each component of the path. If we find a symlink or |
147 | * Check each component of the path. If we find a symlink or |
148 | * something we can't stat, clear curdir to force a getcwd() |
148 | * something we can't stat, clear curdir to force a getcwd() |
149 | * next time we get the value of the current directory. |
149 | * next time we get the value of the current directory. |
150 | */ |
150 | */ |
151 | badstat = 0; |
151 | badstat = 0; |
152 | cdcomppath = stalloc(strlen(dest) + 1); |
152 | cdcomppath = stalloc(strlen(dest) + 1); |
153 | scopy(dest, cdcomppath); |
153 | scopy(dest, cdcomppath); |
154 | STARTSTACKSTR(p); |
154 | STARTSTACKSTR(p); |
155 | if (*dest == '/') { |
155 | if (*dest == '/') { |
156 | STPUTC('/', p); |
156 | STPUTC('/', p); |
157 | cdcomppath++; |
157 | cdcomppath++; |
158 | } |
158 | } |
159 | first = 1; |
159 | first = 1; |
160 | while ((q = getcomponent()) != NULL) { |
160 | while ((q = getcomponent()) != NULL) { |
161 | if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) |
161 | if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) |
162 | continue; |
162 | continue; |
163 | if (! first) |
163 | if (! first) |
164 | STPUTC('/', p); |
164 | STPUTC('/', p); |
165 | first = 0; |
165 | first = 0; |
166 | component = q; |
166 | component = q; |
167 | while (*q) |
167 | while (*q) |
168 | STPUTC(*q++, p); |
168 | STPUTC(*q++, p); |
169 | if (equal(component, "..")) |
169 | if (equal(component, "..")) |
170 | continue; |
170 | continue; |
171 | STACKSTRNUL(p); |
171 | STACKSTRNUL(p); |
172 | if ((lstat(stackblock(), &statb) < 0) |
172 | if ((lstat(stackblock(), &statb) < 0) |
173 | || (S_ISLNK(statb.st_mode))) { |
173 | || (S_ISLNK(statb.st_mode))) { |
174 | /* print = 1; */ |
174 | /* print = 1; */ |
175 | badstat = 1; |
175 | badstat = 1; |
176 | break; |
176 | break; |
177 | } |
177 | } |
178 | } |
178 | } |
179 | 179 | ||
180 | INTOFF; |
180 | INTOFF; |
181 | if (chdir(dest) < 0) { |
181 | if (chdir(dest) < 0) { |
182 | INTON; |
182 | INTON; |
183 | return -1; |
183 | return -1; |
184 | } |
184 | } |
185 | updatepwd(badstat ? NULL : dest); |
185 | updatepwd(badstat ? NULL : dest); |
186 | INTON; |
186 | INTON; |
187 | if (print && iflag && curdir) |
187 | if (print && iflag && curdir) |
188 | out1fmt("%s\n", curdir); |
188 | out1fmt("%s\n", curdir); |
189 | return 0; |
189 | return 0; |
190 | } |
190 | } |
191 | 191 | ||
192 | 192 | ||
193 | /* |
193 | /* |
194 | * Get the next component of the path name pointed to by cdcomppath. |
194 | * Get the next component of the path name pointed to by cdcomppath. |
195 | * This routine overwrites the string pointed to by cdcomppath. |
195 | * This routine overwrites the string pointed to by cdcomppath. |
196 | */ |
196 | */ |
197 | 197 | ||
198 | STATIC char * |
198 | STATIC char * |
199 | getcomponent() { |
199 | getcomponent() { |
200 | char *p; |
200 | char *p; |
201 | char *start; |
201 | char *start; |
202 | 202 | ||
203 | if ((p = cdcomppath) == NULL) |
203 | if ((p = cdcomppath) == NULL) |
204 | return NULL; |
204 | return NULL; |
205 | start = cdcomppath; |
205 | start = cdcomppath; |
206 | while (*p != '/' && *p != '\0') |
206 | while (*p != '/' && *p != '\0') |
207 | p++; |
207 | p++; |
208 | if (*p == '\0') { |
208 | if (*p == '\0') { |
209 | cdcomppath = NULL; |
209 | cdcomppath = NULL; |
210 | } else { |
210 | } else { |
211 | *p++ = '\0'; |
211 | *p++ = '\0'; |
212 | cdcomppath = p; |
212 | cdcomppath = p; |
213 | } |
213 | } |
214 | return start; |
214 | return start; |
215 | } |
215 | } |
216 | 216 | ||
217 | 217 | ||
218 | 218 | ||
219 | /* |
219 | /* |
220 | * Update curdir (the name of the current directory) in response to a |
220 | * Update curdir (the name of the current directory) in response to a |
221 | * cd command. We also call hashcd to let the routines in exec.c know |
221 | * cd command. We also call hashcd to let the routines in exec.c know |
222 | * that the current directory has changed. |
222 | * that the current directory has changed. |
223 | */ |
223 | */ |
224 | 224 | ||
225 | STATIC void |
225 | STATIC void |
226 | updatepwd(dir) |
226 | updatepwd(dir) |
227 | char *dir; |
227 | char *dir; |
228 | { |
228 | { |
229 | char *new; |
229 | char *new; |
230 | char *p; |
230 | char *p; |
231 | 231 | ||
232 | hashcd(); /* update command hash table */ |
232 | hashcd(); /* update command hash table */ |
233 | 233 | ||
234 | /* |
234 | /* |
235 | * If our argument is NULL, we don't know the current directory |
235 | * If our argument is NULL, we don't know the current directory |
236 | * any more because we traversed a symbolic link or something |
236 | * any more because we traversed a symbolic link or something |
237 | * we couldn't stat(). |
237 | * we couldn't stat(). |
238 | */ |
238 | */ |
239 | if (dir == NULL || curdir == NULL) { |
239 | if (dir == NULL || curdir == NULL) { |
240 | if (prevdir) |
240 | if (prevdir) |
241 | ckfree(prevdir); |
241 | ckfree(prevdir); |
242 | INTOFF; |
242 | INTOFF; |
243 | prevdir = curdir; |
243 | prevdir = curdir; |
244 | curdir = NULL; |
244 | curdir = NULL; |
245 | getpwd(); |
245 | getpwd(); |
246 | setvar("PWD", curdir, VEXPORT|VTEXTFIXED); |
246 | setvar("PWD", curdir, VEXPORT|VTEXTFIXED); |
247 | setvar("OLDPWD", prevdir, VEXPORT|VTEXTFIXED); |
247 | setvar("OLDPWD", prevdir, VEXPORT|VTEXTFIXED); |
248 | INTON; |
248 | INTON; |
249 | return; |
249 | return; |
250 | } |
250 | } |
251 | cdcomppath = stalloc(strlen(dir) + 1); |
251 | cdcomppath = stalloc(strlen(dir) + 1); |
252 | scopy(dir, cdcomppath); |
252 | scopy(dir, cdcomppath); |
253 | STARTSTACKSTR(new); |
253 | STARTSTACKSTR(new); |
254 | if (*dir != '/') { |
254 | if (*dir != '/') { |
255 | p = curdir; |
255 | p = curdir; |
256 | while (*p) |
256 | while (*p) |
257 | STPUTC(*p++, new); |
257 | STPUTC(*p++, new); |
258 | if (p[-1] == '/') |
258 | if (p[-1] == '/') |
259 | STUNPUTC(new); |
259 | STUNPUTC(new); |
260 | } |
260 | } |
261 | while ((p = getcomponent()) != NULL) { |
261 | while ((p = getcomponent()) != NULL) { |
262 | if (equal(p, "..")) { |
262 | if (equal(p, "..")) { |
263 | while (new > stackblock() && (STUNPUTC(new), *new) != '/'); |
263 | while (new > stackblock() && (STUNPUTC(new), *new) != '/'); |
264 | } else if (*p != '\0' && ! equal(p, ".")) { |
264 | } else if (*p != '\0' && ! equal(p, ".")) { |
265 | STPUTC('/', new); |
265 | STPUTC('/', new); |
266 | while (*p) |
266 | while (*p) |
267 | STPUTC(*p++, new); |
267 | STPUTC(*p++, new); |
268 | } |
268 | } |
269 | } |
269 | } |
270 | if (new == stackblock()) |
270 | if (new == stackblock()) |
271 | STPUTC('/', new); |
271 | STPUTC('/', new); |
272 | STACKSTRNUL(new); |
272 | STACKSTRNUL(new); |
273 | INTOFF; |
273 | INTOFF; |
274 | if (prevdir) |
274 | if (prevdir) |
275 | ckfree(prevdir); |
275 | ckfree(prevdir); |
276 | prevdir = curdir; |
276 | prevdir = curdir; |
277 | curdir = savestr(stackblock()); |
277 | curdir = savestr(stackblock()); |
278 | setvar("PWD", curdir, VEXPORT|VTEXTFIXED); |
278 | setvar("PWD", curdir, VEXPORT|VTEXTFIXED); |
279 | setvar("OLDPWD", prevdir, VEXPORT|VTEXTFIXED); |
279 | setvar("OLDPWD", prevdir, VEXPORT|VTEXTFIXED); |
280 | INTON; |
280 | INTON; |
281 | } |
281 | } |
282 | 282 | ||
283 | 283 | ||
284 | 284 | ||
285 | int |
285 | int |
286 | pwdcmd(argc, argv) |
286 | pwdcmd(argc, argv) |
287 | int argc; |
287 | int argc; |
288 | char **argv; |
288 | char **argv; |
289 | { |
289 | { |
290 | getpwd(); |
290 | getpwd(); |
291 | out1str(curdir); |
291 | out1str(curdir); |
292 | out1c('\n'); |
292 | out1c('\n'); |
293 | return 0; |
293 | return 0; |
294 | } |
294 | } |
295 | 295 | ||
296 | 296 | ||
297 | 297 | ||
298 | 298 | ||
299 | #define MAXPWD 256 |
299 | #define MAXPWD 256 |
300 | 300 | ||
301 | /* |
301 | /* |
302 | * Find out what the current directory is. If we already know the current |
302 | * Find out what the current directory is. If we already know the current |
303 | * directory, this routine returns immediately. |
303 | * directory, this routine returns immediately. |
304 | */ |
304 | */ |
305 | void |
305 | void |
306 | getpwd() |
306 | getpwd() |
307 | { |
307 | { |
308 | char buf[MAXPWD]; |
308 | char buf[MAXPWD]; |
309 | 309 | ||
310 | if (curdir) |
310 | if (curdir) |
311 | return; |
311 | return; |
312 | /* |
312 | /* |
313 | * Things are a bit complicated here; we could have just used |
313 | * Things are a bit complicated here; we could have just used |
314 | * getcwd, but traditionally getcwd is implemented using popen |
314 | * getcwd, but traditionally getcwd is implemented using popen |
315 | * to /bin/pwd. This creates a problem for us, since we cannot |
315 | * to /bin/pwd. This creates a problem for us, since we cannot |
316 | * keep track of the job if it is being ran behind our backs. |
316 | * keep track of the job if it is being ran behind our backs. |
317 | * So we re-implement getcwd(), and we suppress interrupts |
317 | * So we re-implement getcwd(), and we suppress interrupts |
318 | * throughout the process. This is not completely safe, since |
318 | * throughout the process. This is not completely safe, since |
319 | * the user can still break out of it by killing the pwd program. |
319 | * the user can still break out of it by killing the pwd program. |
320 | * We still try to use getcwd for systems that we know have a |
320 | * We still try to use getcwd for systems that we know have a |
321 | * c implementation of getcwd, that does not open a pipe to |
321 | * c implementation of getcwd, that does not open a pipe to |
322 | * /bin/pwd. |
322 | * /bin/pwd. |
323 | */ |
323 | */ |
324 | #if defined(__NetBSD__) || defined(__SVR4) || defined(__GLIBC__) |
324 | #if defined(__NetBSD__) || defined(__SVR4) || defined(__GLIBC__) |
325 | 325 | ||
326 | if (getcwd(buf, sizeof(buf)) == NULL) { |
326 | if (getcwd(buf, sizeof(buf)) == NULL) { |
327 | char *pwd = getenv("PWD"); |
327 | char *pwd = getenv("PWD"); |
328 | struct stat stdot, stpwd; |
328 | struct stat stdot, stpwd; |
329 | 329 | ||
330 | if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && |
330 | if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && |
331 | stat(pwd, &stpwd) != -1 && |
331 | stat(pwd, &stpwd) != -1 && |
332 | stdot.st_dev == stpwd.st_dev && |
332 | stdot.st_dev == stpwd.st_dev && |
333 | stdot.st_ino == stpwd.st_ino) { |
333 | stdot.st_ino == stpwd.st_ino) { |
334 | curdir = savestr(pwd); |
334 | curdir = savestr(pwd); |
335 | return; |
335 | return; |
336 | } |
336 | } |
337 | error("getcwd() failed: %s", strerror(errno)); |
337 | error("getcwd() failed: %s", strerror(errno)); |
338 | } |
338 | } |
339 | curdir = savestr(buf); |
339 | curdir = savestr(buf); |
340 | #else |
340 | #else |
341 | { |
341 | { |
342 | char *p; |
342 | char *p; |
343 | int i; |
343 | int i; |
344 | int status; |
344 | int status; |
345 | struct job *jp; |
345 | struct job *jp; |
346 | int pip[2]; |
346 | int pip[2]; |
347 | 347 | ||
348 | INTOFF; |
348 | INTOFF; |
349 | if (pipe(pip) < 0) |
349 | if (pipe(pip) < 0) |
350 | error("Pipe call failed"); |
350 | error("Pipe call failed"); |
351 | jp = makejob((union node *)NULL, 1); |
351 | jp = makejob((union node *)NULL, 1); |
352 | if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) { |
352 | if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) { |
353 | (void) close(pip[0]); |
353 | (void) close(pip[0]); |
354 | if (pip[1] != 1) { |
354 | if (pip[1] != 1) { |
355 | close(1); |
355 | close(1); |
356 | copyfd(pip[1], 1); |
356 | copyfd(pip[1], 1); |
357 | close(pip[1]); |
357 | close(pip[1]); |
358 | } |
358 | } |
359 | (void) execl("/bin/pwd", "pwd", (char *)0); |
359 | (void) execl("/bin/pwd", "pwd", (char *)0); |
360 | error("Cannot exec /bin/pwd"); |
360 | error("Cannot exec /bin/pwd"); |
361 | } |
361 | } |
362 | (void) close(pip[1]); |
362 | (void) close(pip[1]); |
363 | pip[1] = -1; |
363 | pip[1] = -1; |
364 | p = buf; |
364 | p = buf; |
365 | while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0 |
365 | while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0 |
366 | || (i == -1 && errno == EINTR)) { |
366 | || (i == -1 && errno == EINTR)) { |
367 | if (i > 0) |
367 | if (i > 0) |
368 | p += i; |
368 | p += i; |
369 | } |
369 | } |
370 | (void) close(pip[0]); |
370 | (void) close(pip[0]); |
371 | pip[0] = -1; |
371 | pip[0] = -1; |
372 | status = waitforjob(jp); |
372 | status = waitforjob(jp); |
373 | if (status != 0) |
373 | if (status != 0) |
374 | error((char *)0); |
374 | error((char *)0); |
375 | if (i < 0 || p == buf || p[-1] != '\n') |
375 | if (i < 0 || p == buf || p[-1] != '\n') |
376 | error("pwd command failed"); |
376 | error("pwd command failed"); |
377 | p[-1] = '\0'; |
377 | p[-1] = '\0'; |
378 | } |
378 | } |
379 | curdir = savestr(buf); |
379 | curdir = savestr(buf); |
380 | INTON; |
380 | INTON; |
381 | #endif |
381 | #endif |
382 | } |
382 | } |
383 | 383 |