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 * 3. 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 #endif /* not lint */ 35 #include <sys/cdefs.h> 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <errno.h> 42 #include <limits.h> 43 44 /* 45 * The cd and pwd commands. 46 */ 47 48 #include "shell.h" 49 #include "var.h" 50 #include "nodes.h" /* for jobs.h */ 51 #include "jobs.h" 52 #include "options.h" 53 #include "output.h" 54 #include "memalloc.h" 55 #include "error.h" 56 #include "exec.h" 57 #include "redir.h" 58 #include "mystring.h" 59 #include "show.h" 60 #include "cd.h" 61 #include "builtins.h" 62 63 static int cdlogical(char *); 64 static int cdphysical(char *); 65 static int docd(char *, int, int); 66 static char *getcomponent(char **); 67 static char *findcwd(char *); 68 static void updatepwd(char *); 69 static char *getpwd(void); 70 static char *getpwd2(void); 71 72 static char *curdir = NULL; /* current working directory */ 73 74 int 75 cdcmd(int argc __unused, char **argv __unused) 76 { 77 const char *dest; 78 const char *path; 79 char *p; 80 struct stat statb; 81 int ch, phys, print = 0, getcwderr = 0; 82 int rc; 83 int errno1 = ENOENT; 84 85 phys = Pflag; 86 while ((ch = nextopt("eLP")) != '\0') { 87 switch (ch) { 88 case 'e': 89 getcwderr = 1; 90 break; 91 case 'L': 92 phys = 0; 93 break; 94 case 'P': 95 phys = 1; 96 break; 97 } 98 } 99 100 if (*argptr != NULL && argptr[1] != NULL) 101 error("too many arguments"); 102 103 if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 104 error("HOME not set"); 105 if (*dest == '\0') 106 dest = "."; 107 if (dest[0] == '-' && dest[1] == '\0') { 108 dest = bltinlookup("OLDPWD", 1); 109 if (dest == NULL) 110 error("OLDPWD not set"); 111 print = 1; 112 } 113 if (dest[0] == '/' || 114 (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) || 115 (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) || 116 (path = bltinlookup("CDPATH", 1)) == NULL) 117 path = ""; 118 while ((p = padvance(&path, NULL, dest)) != NULL) { 119 if (stat(p, &statb) < 0) { 120 if (errno != ENOENT) 121 errno1 = errno; 122 } else if (!S_ISDIR(statb.st_mode)) 123 errno1 = ENOTDIR; 124 else { 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 rc = docd(p, print, phys); 135 if (rc >= 0) 136 return getcwderr ? rc : 0; 137 if (errno != ENOENT) 138 errno1 = errno; 139 } 140 } 141 error("%s: %s", dest, strerror(errno1)); 142 /*NOTREACHED*/ 143 return 0; 144 } 145 146 147 /* 148 * Actually change the directory. In an interactive shell, print the 149 * directory name if "print" is nonzero. 150 */ 151 static int 152 docd(char *dest, int print, int phys) 153 { 154 int rc; 155 156 TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 157 158 /* If logical cd fails, fall back to physical. */ 159 if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0) 160 return (-1); 161 162 if (print && iflag && curdir) { 163 out1fmt("%s\n", curdir); 164 /* 165 * Ignore write errors to preserve the invariant that the 166 * current directory is changed iff the exit status is 0 167 * (or 1 if -e was given and the full pathname could not be 168 * determined). 169 */ 170 flushout(out1); 171 outclearerror(out1); 172 } 173 174 return (rc); 175 } 176 177 static int 178 cdlogical(char *dest) 179 { 180 char *p; 181 char *q; 182 char *component; 183 char *path; 184 struct stat statb; 185 int first; 186 int badstat; 187 188 /* 189 * Check each component of the path. If we find a symlink or 190 * something we can't stat, clear curdir to force a getcwd() 191 * next time we get the value of the current directory. 192 */ 193 badstat = 0; 194 path = stsavestr(dest); 195 STARTSTACKSTR(p); 196 if (*dest == '/') { 197 STPUTC('/', p); 198 path++; 199 } 200 first = 1; 201 while ((q = getcomponent(&path)) != NULL) { 202 if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 203 continue; 204 if (! first) 205 STPUTC('/', p); 206 first = 0; 207 component = q; 208 STPUTS(q, p); 209 if (equal(component, "..")) 210 continue; 211 STACKSTRNUL(p); 212 if (lstat(stackblock(), &statb) < 0) { 213 badstat = 1; 214 break; 215 } 216 } 217 218 INTOFF; 219 if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 220 INTON; 221 return (-1); 222 } 223 updatepwd(p); 224 INTON; 225 return (0); 226 } 227 228 static int 229 cdphysical(char *dest) 230 { 231 char *p; 232 int rc = 0; 233 234 INTOFF; 235 if (chdir(dest) < 0) { 236 INTON; 237 return (-1); 238 } 239 p = findcwd(NULL); 240 if (p == NULL) { 241 warning("warning: failed to get name of current directory"); 242 rc = 1; 243 } 244 updatepwd(p); 245 INTON; 246 return (rc); 247 } 248 249 /* 250 * Get the next component of the path name pointed to by *path. 251 * This routine overwrites *path and the string pointed to by it. 252 */ 253 static char * 254 getcomponent(char **path) 255 { 256 char *p; 257 char *start; 258 259 if ((p = *path) == NULL) 260 return NULL; 261 start = *path; 262 while (*p != '/' && *p != '\0') 263 p++; 264 if (*p == '\0') { 265 *path = NULL; 266 } else { 267 *p++ = '\0'; 268 *path = p; 269 } 270 return start; 271 } 272 273 274 static char * 275 findcwd(char *dir) 276 { 277 char *new; 278 char *p; 279 char *path; 280 281 /* 282 * If our argument is NULL, we don't know the current directory 283 * any more because we traversed a symbolic link or something 284 * we couldn't stat(). 285 */ 286 if (dir == NULL || curdir == NULL) 287 return getpwd2(); 288 path = stsavestr(dir); 289 STARTSTACKSTR(new); 290 if (*dir != '/') { 291 STPUTS(curdir, new); 292 if (STTOPC(new) == '/') 293 STUNPUTC(new); 294 } 295 while ((p = getcomponent(&path)) != NULL) { 296 if (equal(p, "..")) { 297 while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 298 } else if (*p != '\0' && ! equal(p, ".")) { 299 STPUTC('/', new); 300 STPUTS(p, new); 301 } 302 } 303 if (new == stackblock()) 304 STPUTC('/', new); 305 STACKSTRNUL(new); 306 return stackblock(); 307 } 308 309 /* 310 * Update curdir (the name of the current directory) in response to a 311 * cd command. We also call hashcd to let the routines in exec.c know 312 * that the current directory has changed. 313 */ 314 static void 315 updatepwd(char *dir) 316 { 317 char *prevdir; 318 319 hashcd(); /* update command hash table */ 320 321 setvar("PWD", dir, VEXPORT); 322 setvar("OLDPWD", curdir, VEXPORT); 323 prevdir = curdir; 324 curdir = dir ? savestr(dir) : NULL; 325 ckfree(prevdir); 326 } 327 328 int 329 pwdcmd(int argc __unused, char **argv __unused) 330 { 331 char *p; 332 int ch, phys; 333 334 phys = Pflag; 335 while ((ch = nextopt("LP")) != '\0') { 336 switch (ch) { 337 case 'L': 338 phys = 0; 339 break; 340 case 'P': 341 phys = 1; 342 break; 343 } 344 } 345 346 if (*argptr != NULL) 347 error("too many arguments"); 348 349 if (!phys && getpwd()) { 350 out1str(curdir); 351 out1c('\n'); 352 } else { 353 if ((p = getpwd2()) == NULL) 354 error(".: %s", strerror(errno)); 355 out1str(p); 356 out1c('\n'); 357 } 358 359 return 0; 360 } 361 362 /* 363 * Get the current directory and cache the result in curdir. 364 */ 365 static char * 366 getpwd(void) 367 { 368 char *p; 369 370 if (curdir) 371 return curdir; 372 373 p = getpwd2(); 374 if (p != NULL) { 375 INTOFF; 376 curdir = savestr(p); 377 INTON; 378 } 379 380 return curdir; 381 } 382 383 #define MAXPWD 256 384 385 /* 386 * Return the current directory. 387 */ 388 static char * 389 getpwd2(void) 390 { 391 char *pwd; 392 int i; 393 394 for (i = MAXPWD;; i *= 2) { 395 pwd = stalloc(i); 396 if (getcwd(pwd, i) != NULL) 397 return pwd; 398 stunalloc(pwd); 399 if (errno != ERANGE) 400 break; 401 } 402 403 return NULL; 404 } 405 406 /* 407 * Initialize PWD in a new shell. 408 * If the shell is interactive, we need to warn if this fails. 409 */ 410 void 411 pwd_init(int warn) 412 { 413 char *pwd; 414 struct stat stdot, stpwd; 415 416 pwd = lookupvar("PWD"); 417 if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 418 stat(pwd, &stpwd) != -1 && 419 stdot.st_dev == stpwd.st_dev && 420 stdot.st_ino == stpwd.st_ino) { 421 if (curdir) 422 ckfree(curdir); 423 curdir = savestr(pwd); 424 } 425 if (getpwd() == NULL && warn) 426 out2fmt_flush("sh: cannot determine working directory\n"); 427 setvar("PWD", curdir, VEXPORT); 428 } 429