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 while (*q) 196 STPUTC(*q++, p); 197 if (equal(component, "..")) 198 continue; 199 STACKSTRNUL(p); 200 if (lstat(stackblock(), &statb) < 0) { 201 badstat = 1; 202 break; 203 } 204 } 205 206 INTOFF; 207 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 208 INTON; 209 return (-1); 210 } 211 updatepwd(p); 212 INTON; 213 return (0); 214 } 215 216 static int 217 cdphysical(char *dest) 218 { 219 char *p; 220 221 INTOFF; 222 if (chdir(dest) < 0 || (p = findcwd(NULL)) == NULL) { 223 INTON; 224 return (-1); 225 } 226 updatepwd(p); 227 INTON; 228 return (0); 229 } 230 231 /* 232 * Get the next component of the path name pointed to by cdcomppath. 233 * This routine overwrites the string pointed to by cdcomppath. 234 */ 235 static char * 236 getcomponent(void) 237 { 238 char *p; 239 char *start; 240 241 if ((p = cdcomppath) == NULL) 242 return NULL; 243 start = cdcomppath; 244 while (*p != '/' && *p != '\0') 245 p++; 246 if (*p == '\0') { 247 cdcomppath = NULL; 248 } else { 249 *p++ = '\0'; 250 cdcomppath = p; 251 } 252 return start; 253 } 254 255 256 static char * 257 findcwd(char *dir) 258 { 259 char *new; 260 char *p; 261 262 /* 263 * If our argument is NULL, we don't know the current directory 264 * any more because we traversed a symbolic link or something 265 * we couldn't stat(). 266 */ 267 if (dir == NULL || curdir == NULL) 268 return getpwd2(); 269 cdcomppath = stalloc(strlen(dir) + 1); 270 scopy(dir, cdcomppath); 271 STARTSTACKSTR(new); 272 if (*dir != '/') { 273 p = curdir; 274 while (*p) 275 STPUTC(*p++, new); 276 if (p[-1] == '/') 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 while (*p) 285 STPUTC(*p++, new); 286 } 287 } 288 if (new == stackblock()) 289 STPUTC('/', new); 290 STACKSTRNUL(new); 291 return stackblock(); 292 } 293 294 /* 295 * Update curdir (the name of the current directory) in response to a 296 * cd command. We also call hashcd to let the routines in exec.c know 297 * that the current directory has changed. 298 */ 299 static void 300 updatepwd(char *dir) 301 { 302 hashcd(); /* update command hash table */ 303 304 if (prevdir) 305 ckfree(prevdir); 306 prevdir = curdir; 307 curdir = savestr(dir); 308 setvar("PWD", curdir, VEXPORT); 309 setvar("OLDPWD", prevdir, VEXPORT); 310 } 311 312 int 313 pwdcmd(int argc, char **argv) 314 { 315 char *p; 316 int ch, phys; 317 318 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 319 phys = Pflag; 320 while ((ch = getopt(argc, argv, "LP")) != -1) { 321 switch (ch) { 322 case 'L': 323 phys = 0; 324 break; 325 case 'P': 326 phys = 1; 327 break; 328 default: 329 error("unknown option: -%c", optopt); 330 break; 331 } 332 } 333 argc -= optind; 334 argv += optind; 335 336 if (argc != 0) 337 error("too many arguments"); 338 339 if (!phys && getpwd()) { 340 out1str(curdir); 341 out1c('\n'); 342 } else { 343 if ((p = getpwd2()) == NULL) 344 error(".: %s", strerror(errno)); 345 out1str(p); 346 out1c('\n'); 347 } 348 349 return 0; 350 } 351 352 /* 353 * Get the current directory and cache the result in curdir. 354 */ 355 static char * 356 getpwd(void) 357 { 358 char *p; 359 360 if (curdir) 361 return curdir; 362 363 p = getpwd2(); 364 if (p != NULL) 365 curdir = savestr(p); 366 367 return curdir; 368 } 369 370 #define MAXPWD 256 371 372 /* 373 * Return the current directory. 374 */ 375 static char * 376 getpwd2(void) 377 { 378 char *pwd; 379 int i; 380 381 for (i = MAXPWD;; i *= 2) { 382 pwd = stalloc(i); 383 if (getcwd(pwd, i) != NULL) 384 return pwd; 385 stunalloc(pwd); 386 if (errno != ERANGE) 387 break; 388 } 389 390 return NULL; 391 } 392 393 /* 394 * Initialize PWD in a new shell. 395 * If the shell is interactive, we need to warn if this fails. 396 */ 397 void 398 pwd_init(int warn) 399 { 400 char *pwd; 401 struct stat stdot, stpwd; 402 403 pwd = lookupvar("PWD"); 404 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 405 stat(pwd, &stpwd) != -1 && 406 stdot.st_dev == stpwd.st_dev && 407 stdot.st_ino == stpwd.st_ino) { 408 if (curdir) 409 ckfree(curdir); 410 curdir = savestr(pwd); 411 } 412 if (getpwd() == NULL && warn) 413 out2fmt_flush("sh: cannot determine working directory\n"); 414 setvar("PWD", curdir, VEXPORT); 415 } 416