cd.c revision 222292
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 35static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 36#endif 37#endif /* not lint */ 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/bin/sh/cd.c 222292 2011-05-25 21:38:16Z jilles $"); 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 67static int cdlogical(char *); 68static int cdphysical(char *); 69static int docd(char *, int, int); 70static char *getcomponent(void); 71static char *findcwd(char *); 72static void updatepwd(char *); 73static char *getpwd(void); 74static char *getpwd2(void); 75 76static char *curdir = NULL; /* current working directory */ 77static char *prevdir; /* previous working directory */ 78static char *cdcomppath; 79 80int 81cdcmd(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 == '/' || (path = bltinlookup("CDPATH", 1)) == NULL) 127 path = nullstr; 128 while ((p = padvance(&path, dest)) != NULL) { 129 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { 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 */ 156static int 157docd(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 return (rc); 171} 172 173static int 174cdlogical(char *dest) 175{ 176 char *p; 177 char *q; 178 char *component; 179 struct stat statb; 180 int first; 181 int badstat; 182 183 /* 184 * Check each component of the path. If we find a symlink or 185 * something we can't stat, clear curdir to force a getcwd() 186 * next time we get the value of the current directory. 187 */ 188 badstat = 0; 189 cdcomppath = stalloc(strlen(dest) + 1); 190 scopy(dest, cdcomppath); 191 STARTSTACKSTR(p); 192 if (*dest == '/') { 193 STPUTC('/', p); 194 cdcomppath++; 195 } 196 first = 1; 197 while ((q = getcomponent()) != NULL) { 198 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 199 continue; 200 if (! first) 201 STPUTC('/', p); 202 first = 0; 203 component = q; 204 STPUTS(q, p); 205 if (equal(component, "..")) 206 continue; 207 STACKSTRNUL(p); 208 if (lstat(stackblock(), &statb) < 0) { 209 badstat = 1; 210 break; 211 } 212 } 213 214 INTOFF; 215 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 216 INTON; 217 return (-1); 218 } 219 updatepwd(p); 220 INTON; 221 return (0); 222} 223 224static int 225cdphysical(char *dest) 226{ 227 char *p; 228 int rc = 0; 229 230 INTOFF; 231 if (chdir(dest) < 0) { 232 INTON; 233 return (-1); 234 } 235 p = findcwd(NULL); 236 if (p == NULL) { 237 warning("warning: failed to get name of current directory"); 238 rc = 1; 239 } 240 updatepwd(p); 241 INTON; 242 return (rc); 243} 244 245/* 246 * Get the next component of the path name pointed to by cdcomppath. 247 * This routine overwrites the string pointed to by cdcomppath. 248 */ 249static char * 250getcomponent(void) 251{ 252 char *p; 253 char *start; 254 255 if ((p = cdcomppath) == NULL) 256 return NULL; 257 start = cdcomppath; 258 while (*p != '/' && *p != '\0') 259 p++; 260 if (*p == '\0') { 261 cdcomppath = NULL; 262 } else { 263 *p++ = '\0'; 264 cdcomppath = p; 265 } 266 return start; 267} 268 269 270static char * 271findcwd(char *dir) 272{ 273 char *new; 274 char *p; 275 276 /* 277 * If our argument is NULL, we don't know the current directory 278 * any more because we traversed a symbolic link or something 279 * we couldn't stat(). 280 */ 281 if (dir == NULL || curdir == NULL) 282 return getpwd2(); 283 cdcomppath = stalloc(strlen(dir) + 1); 284 scopy(dir, cdcomppath); 285 STARTSTACKSTR(new); 286 if (*dir != '/') { 287 STPUTS(curdir, new); 288 if (STTOPC(new) == '/') 289 STUNPUTC(new); 290 } 291 while ((p = getcomponent()) != NULL) { 292 if (equal(p, "..")) { 293 while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 294 } else if (*p != '\0' && ! equal(p, ".")) { 295 STPUTC('/', new); 296 STPUTS(p, new); 297 } 298 } 299 if (new == stackblock()) 300 STPUTC('/', new); 301 STACKSTRNUL(new); 302 return stackblock(); 303} 304 305/* 306 * Update curdir (the name of the current directory) in response to a 307 * cd command. We also call hashcd to let the routines in exec.c know 308 * that the current directory has changed. 309 */ 310static void 311updatepwd(char *dir) 312{ 313 hashcd(); /* update command hash table */ 314 315 if (prevdir) 316 ckfree(prevdir); 317 prevdir = curdir; 318 curdir = dir ? savestr(dir) : NULL; 319 setvar("PWD", curdir, VEXPORT); 320 setvar("OLDPWD", prevdir, VEXPORT); 321} 322 323int 324pwdcmd(int argc, char **argv) 325{ 326 char *p; 327 int ch, phys; 328 329 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 330 phys = Pflag; 331 while ((ch = getopt(argc, argv, "LP")) != -1) { 332 switch (ch) { 333 case 'L': 334 phys = 0; 335 break; 336 case 'P': 337 phys = 1; 338 break; 339 default: 340 error("unknown option: -%c", optopt); 341 break; 342 } 343 } 344 argc -= optind; 345 argv += optind; 346 347 if (argc != 0) 348 error("too many arguments"); 349 350 if (!phys && getpwd()) { 351 out1str(curdir); 352 out1c('\n'); 353 } else { 354 if ((p = getpwd2()) == NULL) 355 error(".: %s", strerror(errno)); 356 out1str(p); 357 out1c('\n'); 358 } 359 360 return 0; 361} 362 363/* 364 * Get the current directory and cache the result in curdir. 365 */ 366static char * 367getpwd(void) 368{ 369 char *p; 370 371 if (curdir) 372 return curdir; 373 374 p = getpwd2(); 375 if (p != NULL) 376 curdir = savestr(p); 377 378 return curdir; 379} 380 381#define MAXPWD 256 382 383/* 384 * Return the current directory. 385 */ 386static char * 387getpwd2(void) 388{ 389 char *pwd; 390 int i; 391 392 for (i = MAXPWD;; i *= 2) { 393 pwd = stalloc(i); 394 if (getcwd(pwd, i) != NULL) 395 return pwd; 396 stunalloc(pwd); 397 if (errno != ERANGE) 398 break; 399 } 400 401 return NULL; 402} 403 404/* 405 * Initialize PWD in a new shell. 406 * If the shell is interactive, we need to warn if this fails. 407 */ 408void 409pwd_init(int warn) 410{ 411 char *pwd; 412 struct stat stdot, stpwd; 413 414 pwd = lookupvar("PWD"); 415 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 416 stat(pwd, &stpwd) != -1 && 417 stdot.st_dev == stpwd.st_dev && 418 stdot.st_ino == stpwd.st_ino) { 419 if (curdir) 420 ckfree(curdir); 421 curdir = savestr(pwd); 422 } 423 if (getpwd() == NULL && warn) 424 out2fmt_flush("sh: cannot determine working directory\n"); 425 setvar("PWD", curdir, VEXPORT); 426} 427