blob: a4e024d84e36515b331dd4c7838b0edb096f02da [file] [log] [blame]
Herbert Xu05c10762005-09-26 18:32:28 +10001/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
Herbert Xu16399b92005-10-29 11:26:30 +10004 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
Herbert Xu05c10762005-09-26 18:32:28 +10006 *
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.
Herbert Xu16399b92005-10-29 11:26:30 +100018 * 3. Neither the name of the University nor the names of its contributors
Herbert Xu05c10762005-09-26 18:32:28 +100019 * 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
Herbert Xu05c10762005-09-26 18:32:28 +100035#include <sys/types.h>
36#include <sys/stat.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
Brian Koropoffbfcdc492011-03-15 15:35:14 +080040#include <limits.h>
Eric Blake586463c2014-10-27 11:38:43 +080041#ifdef __CYGWIN__
42#include <sys/cygwin.h>
43#endif
Herbert Xu05c10762005-09-26 18:32:28 +100044
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"
Herbert Xu4f2272f2008-05-03 14:36:08 +080059#include "main.h"
Herbert Xu05c10762005-09-26 18:32:28 +100060#include "mystring.h"
61#include "show.h"
62#include "cd.h"
63
64#define CD_PHYSICAL 1
65#define CD_PRINT 2
66
67STATIC int docd(const char *, int);
68STATIC const char *updatepwd(const char *);
69STATIC char *getpwd(void);
70STATIC int cdopt(void);
71
72STATIC char *curdir = nullstr; /* current working directory */
73STATIC char *physdir = nullstr; /* physical working directory */
74
75STATIC int
76cdopt()
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
92int
93cdcmd(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;
Herbert Xu05c10762005-09-26 18:32:28 +1000109 }
110 if (!dest)
111 dest = nullstr;
112 if (*dest == '/')
Herbert Xu78777132009-08-31 22:06:41 +1000113 goto step6;
Herbert Xu05c10762005-09-26 18:32:28 +1000114 if (*dest == '.') {
115 c = dest[1];
116dotdot:
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 = ".";
Herbert Xu78777132009-08-31 22:06:41 +1000129 path = bltinlookup("CDPATH");
130 while (path) {
Herbert Xu05c10762005-09-26 18:32:28 +1000131 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;
136docd:
137 if (!docd(p, flags))
138 goto out;
Herbert Xu78777132009-08-31 22:06:41 +1000139 goto err;
Herbert Xu05c10762005-09-26 18:32:28 +1000140 }
Herbert Xu78777132009-08-31 22:06:41 +1000141 }
142
143step6:
144 p = dest;
145 goto docd;
146
147err:
herbertdc731192005-03-02 19:46:59 +1100148 sh_error("can't cd to %s", dest);
Herbert Xu05c10762005-09-26 18:32:28 +1000149 /* NOTREACHED */
150out:
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
162STATIC int
163docd(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();
181out:
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
192STATIC const char *
193updatepwd(const char *dir)
194{
195 char *new;
196 char *p;
197 char *cdcomppath;
198 const char *lim;
199
Eric Blake586463c2014-10-27 11:38:43 +0800200#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
Herbert Xu05c10762005-09-26 18:32:28 +1000211 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
Herbert Xu05c10762005-09-26 18:32:28 +1000261/*
262 * Find out what the current directory is. If we already know the current
263 * directory, this routine returns immediately.
264 */
265inline
266STATIC char *
267getpwd()
268{
Herbert Xud2bb8972008-05-03 14:17:40 +0800269#ifdef __GLIBC__
Herbert Xu05c10762005-09-26 18:32:28 +1000270 char *dir = getcwd(0, 0);
Herbert Xu4f2272f2008-05-03 14:36:08 +0800271
272 if (dir)
273 return dir;
Herbert Xuf0f930d2008-05-02 14:23:47 +0800274#else
275 char buf[PATH_MAX];
Herbert Xu4f2272f2008-05-03 14:36:08 +0800276
Herbert Xucc0e9392008-08-05 23:50:37 +0800277 if (getcwd(buf, sizeof(buf)))
Herbert Xu4f2272f2008-05-03 14:36:08 +0800278 return savestr(buf);
Herbert Xuf0f930d2008-05-02 14:23:47 +0800279#endif
Herbert Xu4f2272f2008-05-03 14:36:08 +0800280
281 sh_warnx("getcwd() failed: %s", strerror(errno));
282 return nullstr;
Herbert Xu05c10762005-09-26 18:32:28 +1000283}
284
285int
286pwdcmd(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
301void
302setpwd(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}