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