1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 #include <errno.h> 45 #include <limits.h> 46 47 /* 48 * The cd and pwd commands. 49 */ 50 51 #include "shell.h" 52 #include "var.h" 53 #include "nodes.h" /* for jobs.h */ 54 #include "jobs.h" 55 #include "options.h" 56 #include "output.h" 57 #include "memalloc.h" 58 #include "error.h" 59 #include "exec.h" 60 #include "redir.h" 61 #include "mystring.h" 62 #include "show.h" 63 #include "cd.h" 64 #include "builtins.h" 65 66 static int cdlogical(char *); 67 static int cdphysical(char *); 68 static int docd(char *, int, int); 69 static char *getcomponent(char **); 70 static char *findcwd(char *); 71 static void updatepwd(char *); 72 static char *getpwd(void); 73 static char *getpwd2(void); 74 75 static char *curdir = NULL; /* current working directory */ 76 77 int 78 cdcmd(int argc __unused, char **argv __unused) 79 { 80 const char *dest; 81 const char *path; 82 char *p; 83 struct stat statb; 84 int ch, phys, print = 0, getcwderr = 0; 85 int rc; 86 int errno1 = ENOENT; 87 88 phys = Pflag; 89 while ((ch = nextopt("eLP")) != '\0') { 90 switch (ch) { 91 case 'e': 92 getcwderr = 1; 93 break; 94 case 'L': 95 phys = 0; 96 break; 97 case 'P': 98 phys = 1; 99 break; 100 } 101 } 102 103 if (*argptr != NULL && argptr[1] != NULL) 104 error("too many arguments"); 105 106 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 107 error("HOME not set"); 108 if (*dest == '\0') 109 dest = "."; 110 if (dest[0] == '-' && dest[1] == '\0') { 111 dest = bltinlookup("OLDPWD", 1); 112 if (dest == NULL) 113 error("OLDPWD not set"); 114 print = 1; 115 } 116 if (dest[0] == '/' || 117 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) || 118 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) || 119 (path = bltinlookup("CDPATH", 1)) == NULL) 120 path = ""; 121 while ((p = padvance(&path, NULL, dest)) != NULL) { 122 if (stat(p, &statb) < 0) { 123 if (errno != ENOENT) 124 errno1 = errno; 125 } else if (!S_ISDIR(statb.st_mode)) 126 errno1 = ENOTDIR; 127 else { 128 if (!print) { 129 /* 130 * XXX - rethink 131 */ 132 if (p[0] == '.' && p[1] == '/' && p[2] != '\0') 133 print = strcmp(p + 2, dest); 134 else 135 print = strcmp(p, dest); 136 } 137 rc = docd(p, print, phys); 138 if (rc >= 0) 139 return getcwderr ? rc : 0; 140 if (errno != ENOENT) 141 errno1 = errno; 142 } 143 } 144 error("%s: %s", dest, strerror(errno1)); 145 /*NOTREACHED*/ 146 return 0; 147 } 148 149 150 /* 151 * Actually change the directory. In an interactive shell, print the 152 * directory name if "print" is nonzero. 153 */ 154 static int 155 docd(char *dest, int print, int phys) 156 { 157 int rc; 158 159 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 160 161 /* If logical cd fails, fall back to physical. */ 162 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0) 163 return (-1); 164 165 if (print && iflag && curdir) { 166 out1fmt("%s\n", curdir); 167 /* 168 * Ignore write errors to preserve the invariant that the 169 * current directory is changed iff the exit status is 0 170 * (or 1 if -e was given and the full pathname could not be 171 * determined). 172 */ 173 flushout(out1); 174 outclearerror(out1); 175 } 176 177 return (rc); 178 } 179 180 static int 181 cdlogical(char *dest) 182 { 183 char *p; 184 char *q; 185 char *component; 186 char *path; 187 struct stat statb; 188 int first; 189 int badstat; 190 191 /* 192 * Check each component of the path. If we find a symlink or 193 * something we can't stat, clear curdir to force a getcwd() 194 * next time we get the value of the current directory. 195 */ 196 badstat = 0; 197 path = stsavestr(dest); 198 STARTSTACKSTR(p); 199 if (*dest == '/') { 200 STPUTC('/', p); 201 path++; 202 } 203 first = 1; 204 while ((q = getcomponent(&path)) != NULL) { 205 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 206 continue; 207 if (! first) 208 STPUTC('/', p); 209 first = 0; 210 component = q; 211 STPUTS(q, p); 212 if (equal(component, "..")) 213 continue; 214 STACKSTRNUL(p); 215 if (lstat(stackblock(), &statb) < 0) { 216 badstat = 1; 217 break; 218 } 219 } 220 221 INTOFF; 222 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 223 INTON; 224 return (-1); 225 } 226 updatepwd(p); 227 INTON; 228 return (0); 229 } 230 231 static int 232 cdphysical(char *dest) 233 { 234 char *p; 235 int rc = 0; 236 237 INTOFF; 238 if (chdir(dest) < 0) { 239 INTON; 240 return (-1); 241 } 242 p = findcwd(NULL); 243 if (p == NULL) { 244 warning("warning: failed to get name of current directory"); 245 rc = 1; 246 } 247 updatepwd(p); 248 INTON; 249 return (rc); 250 } 251 252 /* 253 * Get the next component of the path name pointed to by *path. 254 * This routine overwrites *path and the string pointed to by it. 255 */ 256 static char * 257 getcomponent(char **path) 258 { 259 char *p; 260 char *start; 261 262 if ((p = *path) == NULL) 263 return NULL; 264 start = *path; 265 while (*p != '/' && *p != '\0') 266 p++; 267 if (*p == '\0') { 268 *path = NULL; 269 } else { 270 *p++ = '\0'; 271 *path = p; 272 } 273 return start; 274 } 275 276 277 static char * 278 findcwd(char *dir) 279 { 280 char *new; 281 char *p; 282 char *path; 283 284 /* 285 * If our argument is NULL, we don't know the current directory 286 * any more because we traversed a symbolic link or something 287 * we couldn't stat(). 288 */ 289 if (dir == NULL || curdir == NULL) 290 return getpwd2(); 291 path = stsavestr(dir); 292 STARTSTACKSTR(new); 293 if (*dir != '/') { 294 STPUTS(curdir, new); 295 if (STTOPC(new) == '/') 296 STUNPUTC(new); 297 } 298 while ((p = getcomponent(&path)) != NULL) { 299 if (equal(p, "..")) { 300 while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 301 } else if (*p != '\0' && ! equal(p, ".")) { 302 STPUTC('/', new); 303 STPUTS(p, new); 304 } 305 } 306 if (new == stackblock()) 307 STPUTC('/', new); 308 STACKSTRNUL(new); 309 return stackblock(); 310 } 311 312 /* 313 * Update curdir (the name of the current directory) in response to a 314 * cd command. We also call hashcd to let the routines in exec.c know 315 * that the current directory has changed. 316 */ 317 static void 318 updatepwd(char *dir) 319 { 320 char *prevdir; 321 322 hashcd(); /* update command hash table */ 323 324 setvar("PWD", dir, VEXPORT); 325 setvar("OLDPWD", curdir, VEXPORT); 326 prevdir = curdir; 327 curdir = dir ? savestr(dir) : NULL; 328 ckfree(prevdir); 329 } 330 331 int 332 pwdcmd(int argc __unused, char **argv __unused) 333 { 334 char *p; 335 int ch, phys; 336 337 phys = Pflag; 338 while ((ch = nextopt("LP")) != '\0') { 339 switch (ch) { 340 case 'L': 341 phys = 0; 342 break; 343 case 'P': 344 phys = 1; 345 break; 346 } 347 } 348 349 if (*argptr != NULL) 350 error("too many arguments"); 351 352 if (!phys && getpwd()) { 353 out1str(curdir); 354 out1c('\n'); 355 } else { 356 if ((p = getpwd2()) == NULL) 357 error(".: %s", strerror(errno)); 358 out1str(p); 359 out1c('\n'); 360 } 361 362 return 0; 363 } 364 365 /* 366 * Get the current directory and cache the result in curdir. 367 */ 368 static char * 369 getpwd(void) 370 { 371 char *p; 372 373 if (curdir) 374 return curdir; 375 376 p = getpwd2(); 377 if (p != NULL) { 378 INTOFF; 379 curdir = savestr(p); 380 INTON; 381 } 382 383 return curdir; 384 } 385 386 #define MAXPWD 256 387 388 /* 389 * Return the current directory. 390 */ 391 static char * 392 getpwd2(void) 393 { 394 char *pwd; 395 int i; 396 397 for (i = MAXPWD;; i *= 2) { 398 pwd = stalloc(i); 399 if (getcwd(pwd, i) != NULL) 400 return pwd; 401 stunalloc(pwd); 402 if (errno != ERANGE) 403 break; 404 } 405 406 return NULL; 407 } 408 409 /* 410 * Initialize PWD in a new shell. 411 * If the shell is interactive, we need to warn if this fails. 412 */ 413 void 414 pwd_init(int warn) 415 { 416 char *pwd; 417 struct stat stdot, stpwd; 418 419 pwd = lookupvar("PWD"); 420 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 421 stat(pwd, &stpwd) != -1 && 422 stdot.st_dev == stpwd.st_dev && 423 stdot.st_ino == stpwd.st_ino) { 424 if (curdir) 425 ckfree(curdir); 426 curdir = savestr(pwd); 427 } 428 if (getpwd() == NULL && warn) 429 out2fmt_flush("sh: cannot determine working directory\n"); 430 setvar("PWD", curdir, VEXPORT); 431 } 432