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