1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1997-2005
5  *	Herbert Xu <herbert@gondor.apana.org.au>.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kenneth Almquist.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <limits.h>
41 #ifdef __CYGWIN__
42 #include <sys/cygwin.h>
43 #endif
44 
45 /*
46  * The cd and pwd commands.
47  */
48 
49 #include "shell.h"
50 #include "var.h"
51 #include "nodes.h"	/* for jobs.h */
52 #include "jobs.h"
53 #include "options.h"
54 #include "output.h"
55 #include "memalloc.h"
56 #include "error.h"
57 #include "exec.h"
58 #include "redir.h"
59 #include "main.h"
60 #include "mystring.h"
61 #include "show.h"
62 #include "cd.h"
63 
64 #define CD_PHYSICAL 1
65 #define CD_PRINT 2
66 
67 STATIC int docd(const char *, int);
68 STATIC const char *updatepwd(const char *);
69 STATIC char *getpwd(void);
70 STATIC int cdopt(void);
71 
72 STATIC char *curdir = nullstr;		/* current working directory */
73 STATIC char *physdir = nullstr;		/* physical working directory */
74 
75 STATIC int
cdopt()76 cdopt()
77 {
78 	int flags = 0;
79 	int i, j;
80 
81 	j = 'L';
82 	while ((i = nextopt("LP"))) {
83 		if (i != j) {
84 			flags ^= CD_PHYSICAL;
85 			j = i;
86 		}
87 	}
88 
89 	return flags;
90 }
91 
92 int
cdcmd(int argc,char ** argv)93 cdcmd(int argc, char **argv)
94 {
95 	const char *dest;
96 	const char *path;
97 	const char *p;
98 	char c;
99 	struct stat statb;
100 	int flags;
101 
102 	flags = cdopt();
103 	dest = *argptr;
104 	if (!dest)
105 		dest = bltinlookup(homestr);
106 	else if (dest[0] == '-' && dest[1] == '\0') {
107 		dest = bltinlookup("OLDPWD");
108 		flags |= CD_PRINT;
109 	}
110 	if (!dest)
111 		dest = nullstr;
112 	if (*dest == '/')
113 		goto step6;
114 	if (*dest == '.') {
115 		c = dest[1];
116 dotdot:
117 		switch (c) {
118 		case '\0':
119 		case '/':
120 			goto step6;
121 		case '.':
122 			c = dest[2];
123 			if (c != '.')
124 				goto dotdot;
125 		}
126 	}
127 	if (!*dest)
128 		dest = ".";
129 	path = bltinlookup("CDPATH");
130 	while (path) {
131 		c = *path;
132 		p = padvance(&path, dest);
133 		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
134 			if (c && c != ':')
135 				flags |= CD_PRINT;
136 docd:
137 			if (!docd(p, flags))
138 				goto out;
139 			goto err;
140 		}
141 	}
142 
143 step6:
144 	p = dest;
145 	goto docd;
146 
147 err:
148 	sh_error("can't cd to %s", dest);
149 	/* NOTREACHED */
150 out:
151 	if (flags & CD_PRINT)
152 		out1fmt(snlfmt, curdir);
153 	return 0;
154 }
155 
156 
157 /*
158  * Actually do the chdir.  We also call hashcd to let the routines in exec.c
159  * know that the current directory has changed.
160  */
161 
162 STATIC int
docd(const char * dest,int flags)163 docd(const char *dest, int flags)
164 {
165 	const char *dir = 0;
166 	int err;
167 
168 	TRACE(("docd(\"%s\", %d) called\n", dest, flags));
169 
170 	INTOFF;
171 	if (!(flags & CD_PHYSICAL)) {
172 		dir = updatepwd(dest);
173 		if (dir)
174 			dest = dir;
175 	}
176 	err = chdir(dest);
177 	if (err)
178 		goto out;
179 	setpwd(dir, 1);
180 	hashcd();
181 out:
182 	INTON;
183 	return err;
184 }
185 
186 
187 /*
188  * Update curdir (the name of the current directory) in response to a
189  * cd command.
190  */
191 
192 STATIC const char *
updatepwd(const char * dir)193 updatepwd(const char *dir)
194 {
195 	char *new;
196 	char *p;
197 	char *cdcomppath;
198 	const char *lim;
199 
200 #ifdef __CYGWIN__
201 	/* On cygwin, thanks to drive letters, some absolute paths do
202 	   not begin with slash; but cygwin includes a function that
203 	   forces normalization to the posix form */
204 	char pathbuf[PATH_MAX];
205 	if (cygwin_conv_path(CCP_WIN_A_TO_POSIX | CCP_RELATIVE, dir, pathbuf,
206 			     sizeof(pathbuf)) < 0)
207 		sh_error("can't normalize %s", dir);
208 	dir = pathbuf;
209 #endif
210 
211 	cdcomppath = sstrdup(dir);
212 	STARTSTACKSTR(new);
213 	if (*dir != '/') {
214 		if (curdir == nullstr)
215 			return 0;
216 		new = stputs(curdir, new);
217 	}
218 	new = makestrspace(strlen(dir) + 2, new);
219 	lim = stackblock() + 1;
220 	if (*dir != '/') {
221 		if (new[-1] != '/')
222 			USTPUTC('/', new);
223 		if (new > lim && *lim == '/')
224 			lim++;
225 	} else {
226 		USTPUTC('/', new);
227 		cdcomppath++;
228 		if (dir[1] == '/' && dir[2] != '/') {
229 			USTPUTC('/', new);
230 			cdcomppath++;
231 			lim++;
232 		}
233 	}
234 	p = strtok(cdcomppath, "/");
235 	while (p) {
236 		switch(*p) {
237 		case '.':
238 			if (p[1] == '.' && p[2] == '\0') {
239 				while (new > lim) {
240 					STUNPUTC(new);
241 					if (new[-1] == '/')
242 						break;
243 				}
244 				break;
245 			} else if (p[1] == '\0')
246 				break;
247 			/* fall through */
248 		default:
249 			new = stputs(p, new);
250 			USTPUTC('/', new);
251 		}
252 		p = strtok(0, "/");
253 	}
254 	if (new > lim)
255 		STUNPUTC(new);
256 	*new = 0;
257 	return stackblock();
258 }
259 
260 
261 /*
262  * Find out what the current directory is. If we already know the current
263  * directory, this routine returns immediately.
264  */
265 inline
266 STATIC char *
getpwd()267 getpwd()
268 {
269 #ifdef __GLIBC__
270 	char *dir = getcwd(0, 0);
271 
272 	if (dir)
273 		return dir;
274 #else
275 	char buf[PATH_MAX];
276 
277 	if (getcwd(buf, sizeof(buf)))
278 		return savestr(buf);
279 #endif
280 
281 	sh_warnx("getcwd() failed: %s", strerror(errno));
282 	return nullstr;
283 }
284 
285 int
pwdcmd(int argc,char ** argv)286 pwdcmd(int argc, char **argv)
287 {
288 	int flags;
289 	const char *dir = curdir;
290 
291 	flags = cdopt();
292 	if (flags) {
293 		if (physdir == nullstr)
294 			setpwd(dir, 0);
295 		dir = physdir;
296 	}
297 	out1fmt(snlfmt, dir);
298 	return 0;
299 }
300 
301 void
setpwd(const char * val,int setold)302 setpwd(const char *val, int setold)
303 {
304 	char *oldcur, *dir;
305 
306 	oldcur = dir = curdir;
307 
308 	if (setold) {
309 		setvar("OLDPWD", oldcur, VEXPORT);
310 	}
311 	INTOFF;
312 	if (physdir != nullstr) {
313 		if (physdir != oldcur)
314 			free(physdir);
315 		physdir = nullstr;
316 	}
317 	if (oldcur == val || !val) {
318 		char *s = getpwd();
319 		physdir = s;
320 		if (!val)
321 			dir = s;
322 	} else
323 		dir = savestr(val);
324 	if (oldcur != dir && oldcur != nullstr) {
325 		free(oldcur);
326 	}
327 	curdir = dir;
328 	INTON;
329 	setvar("PWD", dir, VEXPORT);
330 }
331