diff options
Diffstat (limited to 'bin')
-rw-r--r-- | bin/sh/cd.c | 100 |
1 files changed, 22 insertions, 78 deletions
diff --git a/bin/sh/cd.c b/bin/sh/cd.c index b7473ba..26ad2f2 100644 --- a/bin/sh/cd.c +++ b/bin/sh/cd.c @@ -67,7 +67,7 @@ static const char rcsid[] = #include "show.h" #include "cd.h" -STATIC int docd(char *, int, int); +STATIC int docd(char *, int); STATIC char *getcomponent(void); STATIC void updatepwd(char *); @@ -76,36 +76,16 @@ char *prevdir; /* previous working directory */ STATIC char *cdcomppath; int -cdcmd(int argc, char **argv) +cdcmd(int argc __unused, char **argv __unused) { char *dest; char *path; char *p; struct stat statb; - int ch, phys, print = 0; - - optreset = 1; optind = 1; /* initialize getopt */ - phys = 0; - while ((ch = getopt(argc, argv, "LP")) != -1) { - switch (ch) { - case 'L': - phys = 0; - break; - case 'P': - phys = 1; - break; - default: - error("unknown option: -%c", optopt); - break; - } - } - argc -= optind; - argv += optind; - - if (argc > 1) - error("too many arguments"); + int print = 0; - if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) + nextopt(nullstr); + if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL) error("HOME not set"); if (*dest == '\0') dest = "."; @@ -128,8 +108,9 @@ cdcmd(int argc, char **argv) p += 2; print = strcmp(p, dest); } - if (docd(p, print, phys) >= 0) + if (docd(p, print) >= 0) return 0; + } } error("can't cd to %s", dest); @@ -143,7 +124,7 @@ cdcmd(int argc, char **argv) * directory name if "print" is nonzero. */ STATIC int -docd(char *dest, int print, int phys) +docd(char *dest, int print) { char *p; char *q; @@ -152,20 +133,7 @@ docd(char *dest, int print, int phys) int first; int badstat; - TRACE(("docd(\"%s\", %d, %d) called\n", dest, print, phys)); - - if (phys) { - INTOFF; - if (chdir(dest) < 0) { - INTON; - return -1; - } - updatepwd(NULL); - INTON; - if (print && iflag && curdir) - out1fmt("%s\n", curdir); - return 0; - } + TRACE(("docd(\"%s\", %d) called\n", dest, print)); /* * Check each component of the path. If we find a symlink or @@ -193,18 +161,20 @@ docd(char *dest, int print, int phys) if (equal(component, "..")) continue; STACKSTRNUL(p); - if (lstat(stackblock(), &statb) < 0) { + if ((lstat(stackblock(), &statb) < 0) + || (S_ISLNK(statb.st_mode))) { + /* print = 1; */ badstat = 1; break; } } INTOFF; - updatepwd(badstat ? NULL : dest); - if (chdir(curdir) < 0) { + if (chdir(dest) < 0) { INTON; return -1; } + updatepwd(badstat ? NULL : dest); INTON; if (print && iflag && curdir) out1fmt("%s\n", curdir); @@ -300,47 +270,21 @@ updatepwd(char *dir) INTON; } -#define MAXPWD 256 int pwdcmd(int argc __unused, char **argv __unused) { - char buf[MAXPWD]; - int ch, phys; - - optreset = 1; optind = 1; /* initialize getopt */ - phys = 0; - while ((ch = getopt(argc, argv, "LP")) != -1) { - switch (ch) { - case 'L': - phys = 0; - break; - case 'P': - phys = 1; - break; - default: - error("unknown option: -%c", optopt); - break; - } - } - argc -= optind; - argv += optind; + if (!getpwd()) + error("getcwd() failed: %s", strerror(errno)); + out1str(curdir); + out1c('\n'); + return 0; +} - if (argc != 0) - error("too many arguments"); - if (!phys && getpwd()) { - out1str(curdir); - out1c('\n'); - } else { - if (getcwd(buf, sizeof(buf)) == NULL) - error(".: %s", strerror(errno)); - out1str(buf); - out1c('\n'); - } - return 0; -} + +#define MAXPWD 256 /* * Find out what the current directory is. If we already know the current |