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 __FBSDID("$FreeBSD$"); 40 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 #include <errno.h> 47 #include <limits.h> 48 49 /* 50 * The cd and pwd commands. 51 */ 52 53 #include "shell.h" 54 #include "var.h" 55 #include "nodes.h" /* for jobs.h */ 56 #include "jobs.h" 57 #include "options.h" 58 #include "output.h" 59 #include "memalloc.h" 60 #include "error.h" 61 #include "exec.h" 62 #include "redir.h" 63 #include "mystring.h" 64 #include "show.h" 65 #include "cd.h" 66 #include "builtins.h" 67 68 static int cdlogical(char *); 69 static int cdphysical(char *); 70 static int docd(char *, int, int); 71 static char *getcomponent(char **); 72 static char *findcwd(char *); 73 static void updatepwd(char *); 74 static char *getpwd(void); 75 static char *getpwd2(void); 76 77 static char *curdir = NULL; /* current working directory */ 78 79 int 80 cdcmd(int argc __unused, char **argv __unused) 81 { 82 const char *dest; 83 const char *path; 84 char *p; 85 struct stat statb; 86 int ch, phys, print = 0, getcwderr = 0; 87 int rc; 88 int errno1 = ENOENT; 89 90 phys = Pflag; 91 while ((ch = nextopt("eLP")) != '\0') { 92 switch (ch) { 93 case 'e': 94 getcwderr = 1; 95 break; 96 case 'L': 97 phys = 0; 98 break; 99 case 'P': 100 phys = 1; 101 break; 102 } 103 } 104 105 if (*argptr != NULL && argptr[1] != NULL) 106 error("too many arguments"); 107 108 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 109 error("HOME not set"); 110 if (*dest == '\0') 111 dest = "."; 112 if (dest[0] == '-' && dest[1] == '\0') { 113 dest = bltinlookup("OLDPWD", 1); 114 if (dest == NULL) 115 error("OLDPWD not set"); 116 print = 1; 117 } 118 if (dest[0] == '/' || 119 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) || 120 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) || 121 (path = bltinlookup("CDPATH", 1)) == NULL) 122 path = ""; 123 while ((p = padvance(&path, NULL, dest)) != NULL) { 124 if (stat(p, &statb) < 0) { 125 if (errno != ENOENT) 126 errno1 = errno; 127 } else if (!S_ISDIR(statb.st_mode)) 128 errno1 = ENOTDIR; 129 else { 130 if (!print) { 131 /* 132 * XXX - rethink 133 */ 134 if (p[0] == '.' && p[1] == '/' && p[2] != '\0') 135 print = strcmp(p + 2, dest); 136 else 137 print = strcmp(p, dest); 138 } 139 rc = docd(p, print, phys); 140 if (rc >= 0) 141 return getcwderr ? rc : 0; 142 if (errno != ENOENT) 143 errno1 = errno; 144 } 145 } 146 error("%s: %s", dest, strerror(errno1)); 147 /*NOTREACHED*/ 148 return 0; 149 } 150 151 152 /* 153 * Actually change the directory. In an interactive shell, print the 154 * directory name if "print" is nonzero. 155 */ 156 static int 157 docd(char *dest, int print, int phys) 158 { 159 int rc; 160 161 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 162 163 /* If logical cd fails, fall back to physical. */ 164 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0) 165 return (-1); 166 167 if (print && iflag && curdir) { 168 out1fmt("%s\n", curdir); 169 /* 170 * Ignore write errors to preserve the invariant that the 171 * current directory is changed iff the exit status is 0 172 * (or 1 if -e was given and the full pathname could not be 173 * determined). 174 */ 175 flushout(out1); 176 outclearerror(out1); 177 } 178 179 return (rc); 180 } 181 182 static int 183 cdlogical(char *dest) 184 { 185 char *p; 186 char *q; 187 char *component; 188 char *path; 189 struct stat statb; 190 int first; 191 int badstat; 192 193 /* 194 * Check each component of the path. If we find a symlink or 195 * something we can't stat, clear curdir to force a getcwd() 196 * next time we get the value of the current directory. 197 */ 198 badstat = 0; 199 path = stsavestr(dest); 200 STARTSTACKSTR(p); 201 if (*dest == '/') { 202 STPUTC('/', p); 203 path++; 204 } 205 first = 1; 206 while ((q = getcomponent(&path)) != NULL) { 207 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 208 continue; 209 if (! first) 210 STPUTC('/', p); 211 first = 0; 212 component = q; 213 STPUTS(q, p); 214 if (equal(component, "..")) 215 continue; 216 STACKSTRNUL(p); 217 if (lstat(stackblock(), &statb) < 0) { 218 badstat = 1; 219 break; 220 } 221 } 222 223 INTOFF; 224 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 225 INTON; 226 return (-1); 227 } 228 updatepwd(p); 229 INTON; 230 return (0); 231 } 232 233 static int 234 cdphysical(char *dest) 235 { 236 char *p; 237 int rc = 0; 238 239 INTOFF; 240 if (chdir(dest) < 0) { 241 INTON; 242 return (-1); 243 } 244 p = findcwd(NULL); 245 if (p == NULL) { 246 warning("warning: failed to get name of current directory"); 247 rc = 1; 248 } 249 updatepwd(p); 250 INTON; 251 return (rc); 252 } 253 254 /* 255 * Get the next component of the path name pointed to by *path. 256 * This routine overwrites *path and the string pointed to by it. 257 */ 258 static char * 259 getcomponent(char **path) 260 { 261 char *p; 262 char *start; 263 264 if ((p = *path) == NULL) 265 return NULL; 266 start = *path; 267 while (*p != '/' && *p != '\0') 268 p++; 269 if (*p == '\0') { 270 *path = NULL; 271 } else { 272 *p++ = '\0'; 273 *path = p; 274 } 275 return start; 276 } 277 278 279 static char * 280 findcwd(char *dir) 281 { 282 char *new; 283 char *p; 284 char *path; 285 286 /* 287 * If our argument is NULL, we don't know the current directory 288 * any more because we traversed a symbolic link or something 289 * we couldn't stat(). 290 */ 291 if (dir == NULL || curdir == NULL) 292 return getpwd2(); 293 path = stsavestr(dir); 294 STARTSTACKSTR(new); 295 if (*dir != '/') { 296 STPUTS(curdir, new); 297 if (STTOPC(new) == '/') 298 STUNPUTC(new); 299 } 300 while ((p = getcomponent(&path)) != NULL) { 301 if (equal(p, "..")) { 302 while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 303 } else if (*p != '\0' && ! equal(p, ".")) { 304 STPUTC('/', new); 305 STPUTS(p, new); 306 } 307 } 308 if (new == stackblock()) 309 STPUTC('/', new); 310 STACKSTRNUL(new); 311 return stackblock(); 312 } 313 314 /* 315 * Update curdir (the name of the current directory) in response to a 316 * cd command. We also call hashcd to let the routines in exec.c know 317 * that the current directory has changed. 318 */ 319 static void 320 updatepwd(char *dir) 321 { 322 char *prevdir; 323 324 hashcd(); /* update command hash table */ 325 326 setvar("PWD", dir, VEXPORT); 327 setvar("OLDPWD", curdir, VEXPORT); 328 prevdir = curdir; 329 curdir = dir ? savestr(dir) : NULL; 330 ckfree(prevdir); 331 } 332 333 int 334 pwdcmd(int argc __unused, char **argv __unused) 335 { 336 char *p; 337 int ch, phys; 338 339 phys = Pflag; 340 while ((ch = nextopt("LP")) != '\0') { 341 switch (ch) { 342 case 'L': 343 phys = 0; 344 break; 345 case 'P': 346 phys = 1; 347 break; 348 } 349 } 350 351 if (*argptr != NULL) 352 error("too many arguments"); 353 354 if (!phys && getpwd()) { 355 out1str(curdir); 356 out1c('\n'); 357 } else { 358 if ((p = getpwd2()) == NULL) 359 error(".: %s", strerror(errno)); 360 out1str(p); 361 out1c('\n'); 362 } 363 364 return 0; 365 } 366 367 /* 368 * Get the current directory and cache the result in curdir. 369 */ 370 static char * 371 getpwd(void) 372 { 373 char *p; 374 375 if (curdir) 376 return curdir; 377 378 p = getpwd2(); 379 if (p != NULL) { 380 INTOFF; 381 curdir = savestr(p); 382 INTON; 383 } 384 385 return curdir; 386 } 387 388 #define MAXPWD 256 389 390 /* 391 * Return the current directory. 392 */ 393 static char * 394 getpwd2(void) 395 { 396 char *pwd; 397 int i; 398 399 for (i = MAXPWD;; i *= 2) { 400 pwd = stalloc(i); 401 if (getcwd(pwd, i) != NULL) 402 return pwd; 403 stunalloc(pwd); 404 if (errno != ERANGE) 405 break; 406 } 407 408 return NULL; 409 } 410 411 /* 412 * Initialize PWD in a new shell. 413 * If the shell is interactive, we need to warn if this fails. 414 */ 415 void 416 pwd_init(int warn) 417 { 418 char *pwd; 419 struct stat stdot, stpwd; 420 421 pwd = lookupvar("PWD"); 422 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 423 stat(pwd, &stpwd) != -1 && 424 stdot.st_dev == stpwd.st_dev && 425 stdot.st_ino == stpwd.st_ino) { 426 if (curdir) 427 ckfree(curdir); 428 curdir = savestr(pwd); 429 } 430 if (getpwd() == NULL && warn) 431 out2fmt_flush("sh: cannot determine working directory\n"); 432 setvar("PWD", curdir, VEXPORT); 433 } 434