cd.c revision 230624
1219019Sgabor/*- 2219019Sgabor * Copyright (c) 1991, 1993 3219019Sgabor * The Regents of the University of California. All rights reserved. 4219019Sgabor * 5219019Sgabor * This code is derived from software contributed to Berkeley by 6219019Sgabor * Kenneth Almquist. 7219019Sgabor * 8219019Sgabor * Redistribution and use in source and binary forms, with or without 9219019Sgabor * modification, are permitted provided that the following conditions 10219019Sgabor * are met: 11219019Sgabor * 1. Redistributions of source code must retain the above copyright 12219019Sgabor * notice, this list of conditions and the following disclaimer. 13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14219019Sgabor * notice, this list of conditions and the following disclaimer in the 15219019Sgabor * documentation and/or other materials provided with the distribution. 16219019Sgabor * 4. Neither the name of the University nor the names of its contributors 17219019Sgabor * may be used to endorse or promote products derived from this software 18219019Sgabor * without specific prior written permission. 19219019Sgabor * 20219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23219019Sgabor * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30219019Sgabor * SUCH DAMAGE. 31219019Sgabor */ 32219019Sgabor 33219019Sgabor#ifndef lint 34219019Sgabor#if 0 35219019Sgaborstatic char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 36219019Sgabor#endif 37219019Sgabor#endif /* not lint */ 38219019Sgabor#include <sys/cdefs.h> 39219019Sgabor__FBSDID("$FreeBSD: stable/9/bin/sh/cd.c 230624 2012-01-27 20:53:37Z jilles $"); 40219019Sgabor 41219019Sgabor#include <sys/types.h> 42219019Sgabor#include <sys/stat.h> 43219019Sgabor#include <stdlib.h> 44219019Sgabor#include <string.h> 45219019Sgabor#include <unistd.h> 46219019Sgabor#include <errno.h> 47219019Sgabor#include <limits.h> 48219019Sgabor 49219019Sgabor/* 50219019Sgabor * The cd and pwd commands. 51219019Sgabor */ 52219019Sgabor 53219019Sgabor#include "shell.h" 54219019Sgabor#include "var.h" 55219019Sgabor#include "nodes.h" /* for jobs.h */ 56219019Sgabor#include "jobs.h" 57219019Sgabor#include "options.h" 58219019Sgabor#include "output.h" 59219019Sgabor#include "memalloc.h" 60219019Sgabor#include "error.h" 61219019Sgabor#include "exec.h" 62219019Sgabor#include "redir.h" 63219019Sgabor#include "mystring.h" 64219019Sgabor#include "show.h" 65219019Sgabor#include "cd.h" 66219019Sgabor#include "builtins.h" 67219019Sgabor 68219019Sgaborstatic int cdlogical(char *); 69219019Sgaborstatic int cdphysical(char *); 70219019Sgaborstatic int docd(char *, int, int); 71219019Sgaborstatic char *getcomponent(void); 72219019Sgaborstatic char *findcwd(char *); 73219019Sgaborstatic void updatepwd(char *); 74219019Sgaborstatic char *getpwd(void); 75219019Sgaborstatic char *getpwd2(void); 76219019Sgabor 77219019Sgaborstatic char *curdir = NULL; /* current working directory */ 78219019Sgaborstatic char *prevdir; /* previous working directory */ 79219019Sgaborstatic char *cdcomppath; 80219019Sgabor 81219019Sgaborint 82219019Sgaborcdcmd(int argc, char **argv) 83219019Sgabor{ 84219019Sgabor const char *dest; 85219019Sgabor const char *path; 86219019Sgabor char *p; 87219019Sgabor struct stat statb; 88219019Sgabor int ch, phys, print = 0, getcwderr = 0; 89219019Sgabor int rc; 90219019Sgabor int errno1 = ENOENT; 91219019Sgabor 92219019Sgabor optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 93219019Sgabor phys = Pflag; 94219019Sgabor while ((ch = getopt(argc, argv, "eLP")) != -1) { 95219019Sgabor switch (ch) { 96219019Sgabor case 'e': 97219019Sgabor getcwderr = 1; 98219019Sgabor break; 99219019Sgabor case 'L': 100219019Sgabor phys = 0; 101219019Sgabor break; 102219019Sgabor case 'P': 103219019Sgabor phys = 1; 104219019Sgabor break; 105219019Sgabor default: 106219019Sgabor error("unknown option: -%c", optopt); 107219019Sgabor break; 108219019Sgabor } 109219019Sgabor } 110219019Sgabor argc -= optind; 111219019Sgabor argv += optind; 112219019Sgabor 113219019Sgabor if (argc > 1) 114219019Sgabor error("too many arguments"); 115219019Sgabor 116219019Sgabor if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 117219019Sgabor error("HOME not set"); 118219019Sgabor if (*dest == '\0') 119219019Sgabor dest = "."; 120219019Sgabor if (dest[0] == '-' && dest[1] == '\0') { 121219019Sgabor dest = prevdir ? prevdir : curdir; 122219019Sgabor if (dest) 123219019Sgabor print = 1; 124219019Sgabor else 125219019Sgabor dest = "."; 126219019Sgabor } 127219019Sgabor if (dest[0] == '/' || 128219019Sgabor (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) || 129219019Sgabor (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) || 130219019Sgabor (path = bltinlookup("CDPATH", 1)) == NULL) 131219019Sgabor path = nullstr; 132219019Sgabor while ((p = padvance(&path, dest)) != NULL) { 133219019Sgabor if (stat(p, &statb) < 0) { 134219019Sgabor if (errno != ENOENT) 135219019Sgabor errno1 = errno; 136219019Sgabor } else if (!S_ISDIR(statb.st_mode)) 137219019Sgabor errno1 = ENOTDIR; 138219019Sgabor else { 139219019Sgabor if (!print) { 140219019Sgabor /* 141219019Sgabor * XXX - rethink 142219019Sgabor */ 143219019Sgabor if (p[0] == '.' && p[1] == '/' && p[2] != '\0') 144219019Sgabor print = strcmp(p + 2, dest); 145219019Sgabor else 146219019Sgabor print = strcmp(p, dest); 147219019Sgabor } 148219019Sgabor rc = docd(p, print, phys); 149219019Sgabor if (rc >= 0) 150219019Sgabor return getcwderr ? rc : 0; 151219019Sgabor if (errno != ENOENT) 152219019Sgabor errno1 = errno; 153219019Sgabor } 154219019Sgabor } 155219019Sgabor error("%s: %s", dest, strerror(errno1)); 156219019Sgabor /*NOTREACHED*/ 157219019Sgabor return 0; 158219019Sgabor} 159219019Sgabor 160219019Sgabor 161219019Sgabor/* 162219019Sgabor * Actually change the directory. In an interactive shell, print the 163219019Sgabor * directory name if "print" is nonzero. 164219019Sgabor */ 165219019Sgaborstatic int 166219019Sgabordocd(char *dest, int print, int phys) 167219019Sgabor{ 168219019Sgabor int rc; 169219019Sgabor 170219019Sgabor TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 171219019Sgabor 172219019Sgabor /* If logical cd fails, fall back to physical. */ 173219019Sgabor if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0) 174219019Sgabor return (-1); 175219019Sgabor 176219019Sgabor if (print && iflag && curdir) 177219019Sgabor out1fmt("%s\n", curdir); 178219019Sgabor 179219019Sgabor return (rc); 180219019Sgabor} 181219019Sgabor 182219019Sgaborstatic int 183219019Sgaborcdlogical(char *dest) 184219019Sgabor{ 185219019Sgabor char *p; 186219019Sgabor char *q; 187219019Sgabor char *component; 188219019Sgabor struct stat statb; 189219019Sgabor int first; 190219019Sgabor int badstat; 191219019Sgabor 192219019Sgabor /* 193219019Sgabor * Check each component of the path. If we find a symlink or 194219019Sgabor * something we can't stat, clear curdir to force a getcwd() 195219019Sgabor * next time we get the value of the current directory. 196219019Sgabor */ 197219019Sgabor badstat = 0; 198219019Sgabor cdcomppath = stalloc(strlen(dest) + 1); 199219019Sgabor scopy(dest, cdcomppath); 200225678Sgabor STARTSTACKSTR(p); 201219019Sgabor if (*dest == '/') { 202219019Sgabor STPUTC('/', p); 203219019Sgabor cdcomppath++; 204219019Sgabor } 205219019Sgabor first = 1; 206219019Sgabor while ((q = getcomponent()) != NULL) { 207219019Sgabor if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 208219019Sgabor continue; 209219019Sgabor if (! first) 210219019Sgabor STPUTC('/', p); 211219019Sgabor first = 0; 212219019Sgabor component = q; 213219019Sgabor STPUTS(q, p); 214219019Sgabor if (equal(component, "..")) 215219019Sgabor continue; 216219019Sgabor STACKSTRNUL(p); 217219019Sgabor if (lstat(stackblock(), &statb) < 0) { 218219019Sgabor badstat = 1; 219219019Sgabor break; 220219019Sgabor } 221219019Sgabor } 222219019Sgabor 223219019Sgabor INTOFF; 224219019Sgabor if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 225219019Sgabor INTON; 226219019Sgabor return (-1); 227219019Sgabor } 228219019Sgabor updatepwd(p); 229219019Sgabor INTON; 230219019Sgabor return (0); 231219019Sgabor} 232219019Sgabor 233219019Sgaborstatic int 234219019Sgaborcdphysical(char *dest) 235219019Sgabor{ 236219019Sgabor char *p; 237219019Sgabor 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 cdcomppath. 256 * This routine overwrites the string pointed to by cdcomppath. 257 */ 258static char * 259getcomponent(void) 260{ 261 char *p; 262 char *start; 263 264 if ((p = cdcomppath) == NULL) 265 return NULL; 266 start = cdcomppath; 267 while (*p != '/' && *p != '\0') 268 p++; 269 if (*p == '\0') { 270 cdcomppath = NULL; 271 } else { 272 *p++ = '\0'; 273 cdcomppath = p; 274 } 275 return start; 276} 277 278 279static char * 280findcwd(char *dir) 281{ 282 char *new; 283 char *p; 284 285 /* 286 * If our argument is NULL, we don't know the current directory 287 * any more because we traversed a symbolic link or something 288 * we couldn't stat(). 289 */ 290 if (dir == NULL || curdir == NULL) 291 return getpwd2(); 292 cdcomppath = stalloc(strlen(dir) + 1); 293 scopy(dir, cdcomppath); 294 STARTSTACKSTR(new); 295 if (*dir != '/') { 296 STPUTS(curdir, new); 297 if (STTOPC(new) == '/') 298 STUNPUTC(new); 299 } 300 while ((p = getcomponent()) != 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 */ 319static void 320updatepwd(char *dir) 321{ 322 hashcd(); /* update command hash table */ 323 324 if (prevdir) 325 ckfree(prevdir); 326 prevdir = curdir; 327 curdir = dir ? savestr(dir) : NULL; 328 setvar("PWD", curdir, VEXPORT); 329 setvar("OLDPWD", prevdir, VEXPORT); 330} 331 332int 333pwdcmd(int argc, char **argv) 334{ 335 char *p; 336 int ch, phys; 337 338 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 339 phys = Pflag; 340 while ((ch = getopt(argc, argv, "LP")) != -1) { 341 switch (ch) { 342 case 'L': 343 phys = 0; 344 break; 345 case 'P': 346 phys = 1; 347 break; 348 default: 349 error("unknown option: -%c", optopt); 350 break; 351 } 352 } 353 argc -= optind; 354 argv += optind; 355 356 if (argc != 0) 357 error("too many arguments"); 358 359 if (!phys && getpwd()) { 360 out1str(curdir); 361 out1c('\n'); 362 } else { 363 if ((p = getpwd2()) == NULL) 364 error(".: %s", strerror(errno)); 365 out1str(p); 366 out1c('\n'); 367 } 368 369 return 0; 370} 371 372/* 373 * Get the current directory and cache the result in curdir. 374 */ 375static char * 376getpwd(void) 377{ 378 char *p; 379 380 if (curdir) 381 return curdir; 382 383 p = getpwd2(); 384 if (p != NULL) 385 curdir = savestr(p); 386 387 return curdir; 388} 389 390#define MAXPWD 256 391 392/* 393 * Return the current directory. 394 */ 395static char * 396getpwd2(void) 397{ 398 char *pwd; 399 int i; 400 401 for (i = MAXPWD;; i *= 2) { 402 pwd = stalloc(i); 403 if (getcwd(pwd, i) != NULL) 404 return pwd; 405 stunalloc(pwd); 406 if (errno != ERANGE) 407 break; 408 } 409 410 return NULL; 411} 412 413/* 414 * Initialize PWD in a new shell. 415 * If the shell is interactive, we need to warn if this fails. 416 */ 417void 418pwd_init(int warn) 419{ 420 char *pwd; 421 struct stat stdot, stpwd; 422 423 pwd = lookupvar("PWD"); 424 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 425 stat(pwd, &stpwd) != -1 && 426 stdot.st_dev == stpwd.st_dev && 427 stdot.st_ino == stpwd.st_ino) { 428 if (curdir) 429 ckfree(curdir); 430 curdir = savestr(pwd); 431 } 432 if (getpwd() == NULL && warn) 433 out2fmt_flush("sh: cannot determine working directory\n"); 434 setvar("PWD", curdir, VEXPORT); 435} 436