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