cd.c revision 176521
1108287Stjr/*- 2108287Stjr * Copyright (c) 1991, 1993 3108287Stjr * The Regents of the University of California. All rights reserved. 4108287Stjr * 5108287Stjr * This code is derived from software contributed to Berkeley by 6108287Stjr * Kenneth Almquist. 7108287Stjr * 8108287Stjr * Redistribution and use in source and binary forms, with or without 9108287Stjr * modification, are permitted provided that the following conditions 10108287Stjr * are met: 11108287Stjr * 1. Redistributions of source code must retain the above copyright 12108287Stjr * notice, this list of conditions and the following disclaimer. 13108287Stjr * 2. Redistributions in binary form must reproduce the above copyright 14108287Stjr * notice, this list of conditions and the following disclaimer in the 15108287Stjr * documentation and/or other materials provided with the distribution. 16108287Stjr * 4. Neither the name of the University nor the names of its contributors 17108287Stjr * may be used to endorse or promote products derived from this software 18108287Stjr * without specific prior written permission. 19108287Stjr * 20108287Stjr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21108287Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22108287Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23108287Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24108287Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25108287Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26108287Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27108287Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28108287Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29108287Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30108287Stjr * SUCH DAMAGE. 31108287Stjr */ 32108287Stjr 33108287Stjr#ifndef lint 34108287Stjr#if 0 35108287Stjrstatic char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 36108287Stjr#endif 37108287Stjr#endif /* not lint */ 38108287Stjr#include <sys/cdefs.h> 39108287Stjr__FBSDID("$FreeBSD: head/bin/sh/cd.c 176521 2008-02-24 16:50:55Z stefanf $"); 40108287Stjr 41108291Stjr#include <sys/types.h> 42108291Stjr#include <sys/stat.h> 43108291Stjr#include <stdlib.h> 44108291Stjr#include <string.h> 45108291Stjr#include <unistd.h> 46108287Stjr#include <errno.h> 47108287Stjr#include <limits.h> 48108287Stjr 49108287Stjr/* 50108287Stjr * The cd and pwd commands. 51108287Stjr */ 52131331Stjr 53108596Stjr#include "shell.h" 54108287Stjr#include "var.h" 55108287Stjr#include "nodes.h" /* for jobs.h */ 56108287Stjr#include "jobs.h" 57108287Stjr#include "options.h" 58108287Stjr#include "output.h" 59108287Stjr#include "memalloc.h" 60108287Stjr#include "error.h" 61108287Stjr#include "exec.h" 62108287Stjr#include "redir.h" 63108287Stjr#include "mystring.h" 64108287Stjr#include "show.h" 65108287Stjr#include "cd.h" 66108287Stjr 67108287StjrSTATIC int cdlogical(char *); 68108287StjrSTATIC int cdphysical(char *); 69108287StjrSTATIC int docd(char *, int, int); 70108287StjrSTATIC char *getcomponent(void); 71108290StjrSTATIC char *findcwd(char *); 72108287StjrSTATIC void updatepwd(char *); 73108287StjrSTATIC char *getpwd2(char *, size_t); 74108287Stjr 75108287StjrSTATIC char *curdir = NULL; /* current working directory */ 76STATIC char *prevdir; /* previous working directory */ 77STATIC char *cdcomppath; 78 79int 80cdcmd(int argc, char **argv) 81{ 82 char *dest; 83 char *path; 84 char *p; 85 struct stat statb; 86 int ch, phys, print = 0; 87 88 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 89 phys = Pflag; 90 while ((ch = getopt(argc, argv, "LP")) != -1) { 91 switch (ch) { 92 case 'L': 93 phys = 0; 94 break; 95 case 'P': 96 phys = 1; 97 break; 98 default: 99 error("unknown option: -%c", optopt); 100 break; 101 } 102 } 103 argc -= optind; 104 argv += optind; 105 106 if (argc > 1) 107 error("too many arguments"); 108 109 if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 110 error("HOME not set"); 111 if (*dest == '\0') 112 dest = "."; 113 if (dest[0] == '-' && dest[1] == '\0') { 114 dest = prevdir ? prevdir : curdir; 115 if (dest) 116 print = 1; 117 else 118 dest = "."; 119 } 120 if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL) 121 path = nullstr; 122 while ((p = padvance(&path, dest)) != NULL) { 123 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { 124 if (!print) { 125 /* 126 * XXX - rethink 127 */ 128 if (p[0] == '.' && p[1] == '/' && p[2] != '\0') 129 print = strcmp(p + 2, dest); 130 else 131 print = strcmp(p, dest); 132 } 133 if (docd(p, print, phys) >= 0) 134 return 0; 135 } 136 } 137 error("can't cd to %s", dest); 138 /*NOTREACHED*/ 139 return 0; 140} 141 142 143/* 144 * Actually change the directory. In an interactive shell, print the 145 * directory name if "print" is nonzero. 146 */ 147STATIC int 148docd(char *dest, int print, int phys) 149{ 150 151 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 152 153 /* If logical cd fails, fall back to physical. */ 154 if ((phys || cdlogical(dest) < 0) && cdphysical(dest) < 0) 155 return (-1); 156 157 if (print && iflag && curdir) 158 out1fmt("%s\n", curdir); 159 160 return 0; 161} 162 163STATIC int 164cdlogical(char *dest) 165{ 166 char *p; 167 char *q; 168 char *component; 169 struct stat statb; 170 int first; 171 int badstat; 172 173 /* 174 * Check each component of the path. If we find a symlink or 175 * something we can't stat, clear curdir to force a getcwd() 176 * next time we get the value of the current directory. 177 */ 178 badstat = 0; 179 cdcomppath = stalloc(strlen(dest) + 1); 180 scopy(dest, cdcomppath); 181 STARTSTACKSTR(p); 182 if (*dest == '/') { 183 STPUTC('/', p); 184 cdcomppath++; 185 } 186 first = 1; 187 while ((q = getcomponent()) != NULL) { 188 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 189 continue; 190 if (! first) 191 STPUTC('/', p); 192 first = 0; 193 component = q; 194 while (*q) 195 STPUTC(*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 215STATIC int 216cdphysical(char *dest) 217{ 218 char *p; 219 220 INTOFF; 221 if (chdir(dest) < 0 || (p = findcwd(NULL)) == NULL) { 222 INTON; 223 return (-1); 224 } 225 updatepwd(p); 226 INTON; 227 return (0); 228} 229 230/* 231 * Get the next component of the path name pointed to by cdcomppath. 232 * This routine overwrites the string pointed to by cdcomppath. 233 */ 234STATIC char * 235getcomponent(void) 236{ 237 char *p; 238 char *start; 239 240 if ((p = cdcomppath) == NULL) 241 return NULL; 242 start = cdcomppath; 243 while (*p != '/' && *p != '\0') 244 p++; 245 if (*p == '\0') { 246 cdcomppath = NULL; 247 } else { 248 *p++ = '\0'; 249 cdcomppath = p; 250 } 251 return start; 252} 253 254 255STATIC char * 256findcwd(char *dir) 257{ 258 char *new; 259 char *p; 260 261 /* 262 * If our argument is NULL, we don't know the current directory 263 * any more because we traversed a symbolic link or something 264 * we couldn't stat(). 265 */ 266 if (dir == NULL || curdir == NULL) { 267 p = stalloc(PATH_MAX); 268 return getpwd2(p, PATH_MAX); 269 } 270 cdcomppath = stalloc(strlen(dir) + 1); 271 scopy(dir, cdcomppath); 272 STARTSTACKSTR(new); 273 if (*dir != '/') { 274 p = curdir; 275 while (*p) 276 STPUTC(*p++, new); 277 if (p[-1] == '/') 278 STUNPUTC(new); 279 } 280 while ((p = getcomponent()) != NULL) { 281 if (equal(p, "..")) { 282 while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 283 } else if (*p != '\0' && ! equal(p, ".")) { 284 STPUTC('/', new); 285 while (*p) 286 STPUTC(*p++, new); 287 } 288 } 289 if (new == stackblock()) 290 STPUTC('/', new); 291 STACKSTRNUL(new); 292 return stackblock(); 293} 294 295/* 296 * Update curdir (the name of the current directory) in response to a 297 * cd command. We also call hashcd to let the routines in exec.c know 298 * that the current directory has changed. 299 */ 300STATIC void 301updatepwd(char *dir) 302{ 303 hashcd(); /* update command hash table */ 304 305 if (prevdir) 306 ckfree(prevdir); 307 prevdir = curdir; 308 curdir = savestr(dir); 309 setvar("PWD", curdir, VEXPORT); 310 setvar("OLDPWD", prevdir, VEXPORT); 311} 312 313int 314pwdcmd(int argc, char **argv) 315{ 316 char buf[PATH_MAX]; 317 int ch, phys; 318 319 optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 320 phys = Pflag; 321 while ((ch = getopt(argc, argv, "LP")) != -1) { 322 switch (ch) { 323 case 'L': 324 phys = 0; 325 break; 326 case 'P': 327 phys = 1; 328 break; 329 default: 330 error("unknown option: -%c", optopt); 331 break; 332 } 333 } 334 argc -= optind; 335 argv += optind; 336 337 if (argc != 0) 338 error("too many arguments"); 339 340 if (!phys && getpwd()) { 341 out1str(curdir); 342 out1c('\n'); 343 } else { 344 if (getcwd(buf, sizeof(buf)) == NULL) 345 error(".: %s", strerror(errno)); 346 out1str(buf); 347 out1c('\n'); 348 } 349 350 return 0; 351} 352 353/* 354 * Get the current directory and cache the result in curdir. 355 */ 356char * 357getpwd(void) 358{ 359 char buf[PATH_MAX]; 360 char *p; 361 362 if (curdir) 363 return curdir; 364 365 p = getpwd2(buf, sizeof(buf)); 366 if (p != NULL) 367 curdir = savestr(p); 368 369 return curdir; 370} 371 372/* 373 * Return the current directory. 374 */ 375STATIC char * 376getpwd2(char *buf, size_t size) 377{ 378 if (getcwd(buf, size) == NULL) { 379 char *pwd = getenv("PWD"); 380 struct stat stdot, stpwd; 381 382 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 383 stat(pwd, &stpwd) != -1 && 384 stdot.st_dev == stpwd.st_dev && 385 stdot.st_ino == stpwd.st_ino) { 386 return pwd; 387 } 388 return NULL; 389 } 390 return buf; 391} 392