From ae8f68c4a7c2f557bbb94bc357693d44edbb57dd Mon Sep 17 00:00:00 2001 From: deraadt Date: Wed, 22 Nov 1995 10:32:52 +0000 Subject: [PATCH] handle "cd -" causing crash if used as first sh command; from scottr@Plexus.COM; netbsd pr#1760 --- bin/sh/cd.c | 39 ++++----------------------------------- bin/sh/main.c | 2 ++ 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/bin/sh/cd.c b/bin/sh/cd.c index f11b9ad4d10..c8a13573fc8 100644 --- a/bin/sh/cd.c +++ b/bin/sh/cd.c @@ -328,10 +328,8 @@ pwdcmd(argc, argv) /* - * Run /bin/pwd to find out what the current directory is. We suppress - * interrupts throughout most of this, but the user can still break out - * of it by killing the pwd program. If we already know the current - * directory, this routine returns immediately. + * If we already know the current directory, this routine returns + * immediately. */ #define MAXPWD 256 @@ -347,36 +345,7 @@ getpwd() { if (curdir) return; - INTOFF; - if (pipe(pip) < 0) - error("Pipe call failed"); - jp = makejob((union node *)NULL, 1); - if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) { - close(pip[0]); - if (pip[1] != 1) { - close(1); - copyfd(pip[1], 1); - close(pip[1]); - } - execl("/bin/pwd", "pwd", (char *)0); - error("Cannot exec /bin/pwd"); - } - close(pip[1]); - pip[1] = -1; - p = buf; - while ((i = read(pip[0], p, buf + MAXPWD - p)) > 0 - || (i == -1 && errno == EINTR)) { - if (i > 0) - p += i; - } - close(pip[0]); - pip[0] = -1; - status = waitforjob(jp); - if (status != 0) - error((char *)0); - if (i < 0 || p == buf || p[-1] != '\n') - error("pwd command failed"); - p[-1] = '\0'; + if (getcwd(buf, sizeof(buf)) == NULL) + error("getcwd() failed"); curdir = savestr(buf); - INTON; } diff --git a/bin/sh/main.c b/bin/sh/main.c index caa4bdf99de..f11a9a60897 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -91,6 +91,7 @@ extern int etext(); STATIC void read_profile __P((char *)); STATIC char *find_dot_file __P((char *)); +STATIC void getpwd __P((void)); /* * Main routine. We initialize things, parse the arguments, execute @@ -159,6 +160,7 @@ main(argc, argv) init(); setstackmark(&smark); procargs(argc, argv); + getpwd(); if (argv[0] && argv[0][0] == '-') { state = 1; read_profile("/etc/profile"); -- 2.20.1