11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD$"); 401556Srgrimes 4117987Speter#include <sys/types.h> 4217987Speter#include <sys/stat.h> 4317987Speter#include <stdlib.h> 4420425Ssteve#include <string.h> 4517987Speter#include <unistd.h> 4617987Speter#include <errno.h> 47100661Stjr#include <limits.h> 4817987Speter 491556Srgrimes/* 501556Srgrimes * The cd and pwd commands. 511556Srgrimes */ 521556Srgrimes 531556Srgrimes#include "shell.h" 541556Srgrimes#include "var.h" 551556Srgrimes#include "nodes.h" /* for jobs.h */ 561556Srgrimes#include "jobs.h" 571556Srgrimes#include "options.h" 581556Srgrimes#include "output.h" 591556Srgrimes#include "memalloc.h" 601556Srgrimes#include "error.h" 6120425Ssteve#include "exec.h" 6217987Speter#include "redir.h" 631556Srgrimes#include "mystring.h" 6417987Speter#include "show.h" 6520425Ssteve#include "cd.h" 66223060Sjilles#include "builtins.h" 671556Srgrimes 68213811Sobrienstatic int cdlogical(char *); 69213811Sobrienstatic int cdphysical(char *); 70213811Sobrienstatic int docd(char *, int, int); 71213811Sobrienstatic char *getcomponent(void); 72213811Sobrienstatic char *findcwd(char *); 73213811Sobrienstatic void updatepwd(char *); 74213811Sobrienstatic char *getpwd(void); 75213811Sobrienstatic char *getpwd2(void); 761556Srgrimes 77213760Sobrienstatic char *curdir = NULL; /* current working directory */ 78213760Sobrienstatic char *prevdir; /* previous working directory */ 79213760Sobrienstatic char *cdcomppath; 801556Srgrimes 811556Srgrimesint 8297092Stjrcdcmd(int argc, char **argv) 8317987Speter{ 84201053Sjilles const char *dest; 85200956Sjilles const char *path; 861556Srgrimes char *p; 871556Srgrimes struct stat statb; 88222154Sjilles int ch, phys, print = 0, getcwderr = 0; 89222154Sjilles int rc; 90222292Sjilles int errno1 = ENOENT; 911556Srgrimes 92100663Stjr optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 93100664Stjr phys = Pflag; 94222154Sjilles while ((ch = getopt(argc, argv, "eLP")) != -1) { 9597092Stjr switch (ch) { 96222154Sjilles case 'e': 97222154Sjilles getcwderr = 1; 98222154Sjilles break; 9997092Stjr case 'L': 10097092Stjr phys = 0; 10197092Stjr break; 10297092Stjr case 'P': 10397092Stjr phys = 1; 10497092Stjr break; 10597092Stjr default: 10697092Stjr error("unknown option: -%c", optopt); 10797092Stjr break; 10897092Stjr } 10997092Stjr } 11097092Stjr argc -= optind; 11197092Stjr argv += optind; 11297092Stjr 11397092Stjr if (argc > 1) 11497092Stjr error("too many arguments"); 11597092Stjr 11697092Stjr if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) 1171556Srgrimes error("HOME not set"); 1185234Sbde if (*dest == '\0') 1195234Sbde dest = "."; 1201556Srgrimes if (dest[0] == '-' && dest[1] == '\0') { 1211556Srgrimes dest = prevdir ? prevdir : curdir; 12212273Speter if (dest) 12312273Speter print = 1; 12412273Speter else 12512273Speter dest = "."; 1261556Srgrimes } 127222381Sjilles if (dest[0] == '/' || 128222381Sjilles (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) || 129222381Sjilles (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) || 130222381Sjilles (path = bltinlookup("CDPATH", 1)) == NULL) 1311556Srgrimes path = nullstr; 1321556Srgrimes while ((p = padvance(&path, dest)) != NULL) { 133230624Sjilles if (stat(p, &statb) < 0) { 134230624Sjilles if (errno != ENOENT) 135230624Sjilles errno1 = errno; 136230624Sjilles } else if (!S_ISDIR(statb.st_mode)) 137230624Sjilles errno1 = ENOTDIR; 138230624Sjilles else { 1391556Srgrimes if (!print) { 1401556Srgrimes /* 1411556Srgrimes * XXX - rethink 1421556Srgrimes */ 14338886Stegge if (p[0] == '.' && p[1] == '/' && p[2] != '\0') 144159551Sstefanf print = strcmp(p + 2, dest); 145159551Sstefanf else 146159551Sstefanf print = strcmp(p, dest); 1471556Srgrimes } 148222154Sjilles rc = docd(p, print, phys); 149222154Sjilles if (rc >= 0) 150222154Sjilles return getcwderr ? rc : 0; 151222292Sjilles if (errno != ENOENT) 152222292Sjilles errno1 = errno; 1531556Srgrimes } 1541556Srgrimes } 155222292Sjilles error("%s: %s", dest, strerror(errno1)); 15617987Speter /*NOTREACHED*/ 15717987Speter return 0; 1581556Srgrimes} 1591556Srgrimes 1601556Srgrimes 1611556Srgrimes/* 16297092Stjr * Actually change the directory. In an interactive shell, print the 16320774Ssteve * directory name if "print" is nonzero. 1641556Srgrimes */ 165213811Sobrienstatic int 16697092Stjrdocd(char *dest, int print, int phys) 16720425Ssteve{ 168222154Sjilles int rc; 16997092Stjr 17097092Stjr TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); 17197092Stjr 17297092Stjr /* If logical cd fails, fall back to physical. */ 173222154Sjilles if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0) 17497092Stjr return (-1); 17597092Stjr 17697092Stjr if (print && iflag && curdir) 17797092Stjr out1fmt("%s\n", curdir); 17897092Stjr 179222154Sjilles return (rc); 18097092Stjr} 18197092Stjr 182213811Sobrienstatic int 18397092Stjrcdlogical(char *dest) 18497092Stjr{ 18538886Stegge char *p; 18638886Stegge char *q; 18738886Stegge char *component; 18838886Stegge struct stat statb; 18938886Stegge int first; 19038886Stegge int badstat; 19120425Ssteve 19238886Stegge /* 19338886Stegge * Check each component of the path. If we find a symlink or 19438886Stegge * something we can't stat, clear curdir to force a getcwd() 19538886Stegge * next time we get the value of the current directory. 19638886Stegge */ 19738886Stegge badstat = 0; 19838886Stegge cdcomppath = stalloc(strlen(dest) + 1); 19938886Stegge scopy(dest, cdcomppath); 20038886Stegge STARTSTACKSTR(p); 20138886Stegge if (*dest == '/') { 20238886Stegge STPUTC('/', p); 20338886Stegge cdcomppath++; 20438886Stegge } 20538886Stegge first = 1; 20638886Stegge while ((q = getcomponent()) != NULL) { 20738886Stegge if (q[0] == '\0' || (q[0] == '.' && q[1] == '\0')) 20838886Stegge continue; 20938886Stegge if (! first) 21038886Stegge STPUTC('/', p); 21138886Stegge first = 0; 21238886Stegge component = q; 213215783Sjilles STPUTS(q, p); 21438886Stegge if (equal(component, "..")) 21538886Stegge continue; 21638886Stegge STACKSTRNUL(p); 21797092Stjr if (lstat(stackblock(), &statb) < 0) { 21838886Stegge badstat = 1; 21938886Stegge break; 22038886Stegge } 22138886Stegge } 22238886Stegge 2231556Srgrimes INTOFF; 224176521Sstefanf if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) { 2251556Srgrimes INTON; 22697092Stjr return (-1); 2271556Srgrimes } 228176521Sstefanf updatepwd(p); 2291556Srgrimes INTON; 23097092Stjr return (0); 2311556Srgrimes} 2321556Srgrimes 233213811Sobrienstatic int 23497092Stjrcdphysical(char *dest) 23597092Stjr{ 236176521Sstefanf char *p; 237222154Sjilles int rc = 0; 2381556Srgrimes 23997092Stjr INTOFF; 240215727Sjilles if (chdir(dest) < 0) { 24197092Stjr INTON; 24297092Stjr return (-1); 24397092Stjr } 244215727Sjilles p = findcwd(NULL); 245222154Sjilles if (p == NULL) { 246216622Sjilles warning("warning: failed to get name of current directory"); 247222154Sjilles rc = 1; 248222154Sjilles } 249176521Sstefanf updatepwd(p); 25097092Stjr INTON; 251222154Sjilles return (rc); 25297092Stjr} 25397092Stjr 2541556Srgrimes/* 2551556Srgrimes * Get the next component of the path name pointed to by cdcomppath. 2561556Srgrimes * This routine overwrites the string pointed to by cdcomppath. 2571556Srgrimes */ 258213811Sobrienstatic char * 25990111Simpgetcomponent(void) 26020774Ssteve{ 26125222Ssteve char *p; 2621556Srgrimes char *start; 2631556Srgrimes 2641556Srgrimes if ((p = cdcomppath) == NULL) 2651556Srgrimes return NULL; 2661556Srgrimes start = cdcomppath; 2671556Srgrimes while (*p != '/' && *p != '\0') 2681556Srgrimes p++; 2691556Srgrimes if (*p == '\0') { 2701556Srgrimes cdcomppath = NULL; 2711556Srgrimes } else { 2721556Srgrimes *p++ = '\0'; 2731556Srgrimes cdcomppath = p; 2741556Srgrimes } 2751556Srgrimes return start; 2761556Srgrimes} 2771556Srgrimes 2781556Srgrimes 279213811Sobrienstatic char * 280176521Sstefanffindcwd(char *dir) 28120774Ssteve{ 2821556Srgrimes char *new; 2831556Srgrimes char *p; 2841556Srgrimes 28538886Stegge /* 28638886Stegge * If our argument is NULL, we don't know the current directory 28738886Stegge * any more because we traversed a symbolic link or something 28838886Stegge * we couldn't stat(). 28938886Stegge */ 290199631Sstefanf if (dir == NULL || curdir == NULL) 291199631Sstefanf return getpwd2(); 2921556Srgrimes cdcomppath = stalloc(strlen(dir) + 1); 2931556Srgrimes scopy(dir, cdcomppath); 2941556Srgrimes STARTSTACKSTR(new); 2951556Srgrimes if (*dir != '/') { 296215783Sjilles STPUTS(curdir, new); 297215783Sjilles if (STTOPC(new) == '/') 2981556Srgrimes STUNPUTC(new); 2991556Srgrimes } 3001556Srgrimes while ((p = getcomponent()) != NULL) { 3011556Srgrimes if (equal(p, "..")) { 3021556Srgrimes while (new > stackblock() && (STUNPUTC(new), *new) != '/'); 3031556Srgrimes } else if (*p != '\0' && ! equal(p, ".")) { 3041556Srgrimes STPUTC('/', new); 305215783Sjilles STPUTS(p, new); 3061556Srgrimes } 3071556Srgrimes } 3081556Srgrimes if (new == stackblock()) 3091556Srgrimes STPUTC('/', new); 3101556Srgrimes STACKSTRNUL(new); 311176521Sstefanf return stackblock(); 312176521Sstefanf} 313176521Sstefanf 314176521Sstefanf/* 315176521Sstefanf * Update curdir (the name of the current directory) in response to a 316176521Sstefanf * cd command. We also call hashcd to let the routines in exec.c know 317176521Sstefanf * that the current directory has changed. 318176521Sstefanf */ 319213811Sobrienstatic void 320176521Sstefanfupdatepwd(char *dir) 321176521Sstefanf{ 322176521Sstefanf hashcd(); /* update command hash table */ 323176521Sstefanf 32438886Stegge if (prevdir) 32538886Stegge ckfree(prevdir); 32638886Stegge prevdir = curdir; 327215727Sjilles curdir = dir ? savestr(dir) : NULL; 32886176Stegge setvar("PWD", curdir, VEXPORT); 32986176Stegge setvar("OLDPWD", prevdir, VEXPORT); 3301556Srgrimes} 3311556Srgrimes 3321556Srgrimesint 333100660Stjrpwdcmd(int argc, char **argv) 33417987Speter{ 335199631Sstefanf char *p; 33697092Stjr int ch, phys; 3371556Srgrimes 338100663Stjr optreset = 1; optind = 1; opterr = 0; /* initialize getopt */ 339100664Stjr phys = Pflag; 34097092Stjr while ((ch = getopt(argc, argv, "LP")) != -1) { 34197092Stjr switch (ch) { 34297092Stjr case 'L': 34397092Stjr phys = 0; 34497092Stjr break; 34597092Stjr case 'P': 34697092Stjr phys = 1; 34797092Stjr break; 34897092Stjr default: 34997092Stjr error("unknown option: -%c", optopt); 35097092Stjr break; 35197092Stjr } 35297092Stjr } 35397092Stjr argc -= optind; 35497092Stjr argv += optind; 3551556Srgrimes 35697092Stjr if (argc != 0) 35797092Stjr error("too many arguments"); 35838886Stegge 35997092Stjr if (!phys && getpwd()) { 36097092Stjr out1str(curdir); 36197092Stjr out1c('\n'); 36297092Stjr } else { 363199631Sstefanf if ((p = getpwd2()) == NULL) 36497092Stjr error(".: %s", strerror(errno)); 365199631Sstefanf out1str(p); 36697092Stjr out1c('\n'); 36797092Stjr } 36838886Stegge 36997092Stjr return 0; 37097092Stjr} 37138886Stegge 3721556Srgrimes/* 373176521Sstefanf * Get the current directory and cache the result in curdir. 3741556Srgrimes */ 375213811Sobrienstatic char * 37690111Simpgetpwd(void) 37720425Ssteve{ 378176521Sstefanf char *p; 37938886Stegge 3801556Srgrimes if (curdir) 38138886Stegge return curdir; 382176521Sstefanf 383199631Sstefanf p = getpwd2(); 384176521Sstefanf if (p != NULL) 385176521Sstefanf curdir = savestr(p); 386176521Sstefanf 387176521Sstefanf return curdir; 388176521Sstefanf} 389176521Sstefanf 390199631Sstefanf#define MAXPWD 256 391199631Sstefanf 392176521Sstefanf/* 393176521Sstefanf * Return the current directory. 394176521Sstefanf */ 395213811Sobrienstatic char * 396199631Sstefanfgetpwd2(void) 397176521Sstefanf{ 398199631Sstefanf char *pwd; 399199631Sstefanf int i; 40038886Stegge 401199631Sstefanf for (i = MAXPWD;; i *= 2) { 402199631Sstefanf pwd = stalloc(i); 403199631Sstefanf if (getcwd(pwd, i) != NULL) 404176521Sstefanf return pwd; 405199631Sstefanf stunalloc(pwd); 406199631Sstefanf if (errno != ERANGE) 407199631Sstefanf break; 40838886Stegge } 409199631Sstefanf 410206759Sjilles return NULL; 411206759Sjilles} 412206759Sjilles 413206759Sjilles/* 414206759Sjilles * Initialize PWD in a new shell. 415206759Sjilles * If the shell is interactive, we need to warn if this fails. 416206759Sjilles */ 417206759Sjillesvoid 418206759Sjillespwd_init(int warn) 419206759Sjilles{ 420206759Sjilles char *pwd; 421206759Sjilles struct stat stdot, stpwd; 422206759Sjilles 423206759Sjilles pwd = lookupvar("PWD"); 424199631Sstefanf if (pwd && *pwd == '/' && stat(".", &stdot) != -1 && 425199631Sstefanf stat(pwd, &stpwd) != -1 && 426199631Sstefanf stdot.st_dev == stpwd.st_dev && 427199631Sstefanf stdot.st_ino == stpwd.st_ino) { 428206759Sjilles if (curdir) 429206759Sjilles ckfree(curdir); 430206759Sjilles curdir = savestr(pwd); 431199631Sstefanf } 432206759Sjilles if (getpwd() == NULL && warn) 433206759Sjilles out2fmt_flush("sh: cannot determine working directory\n"); 434206759Sjilles setvar("PWD", curdir, VEXPORT); 4351556Srgrimes} 436