From 80d189ed55a9c827ef09401fd80d18219824e251 Mon Sep 17 00:00:00 2001 From: deraadt Date: Thu, 14 Dec 1995 01:22:27 +0000 Subject: [PATCH] update from netbsd, including: Fix PR/1760, where 'cd -' before any other command could cause a reference to an uninitialized pointer. Use getcwd() to get the current working directory, instead of forking /bin/pwd [per Scott's suggestion] --- bin/sh/Makefile | 7 ++++- bin/sh/cd.c | 80 +++++++++++++++++++++++++++++++++++++++---------- bin/sh/input.c | 6 ++-- bin/sh/main.c | 6 ++-- bin/sh/parser.c | 13 +++++--- 5 files changed, 87 insertions(+), 25 deletions(-) diff --git a/bin/sh/Makefile b/bin/sh/Makefile index e14db754568..07df49d324e 100644 --- a/bin/sh/Makefile +++ b/bin/sh/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.21 1995/06/10 20:19:40 mycroft Exp $ +# $NetBSD: Makefile,v 1.22 1995/10/22 00:15:02 christos Exp $ # @(#)Makefile 8.4 (Berkeley) 5/5/95 PROG= sh @@ -17,6 +17,7 @@ CLEANFILES+=\ nodes.c nodes.h printf.o syntax.c syntax.h token.def y.tab.h .depend parser.o: token.def + token.def: mktokens sh ${.CURDIR}/mktokens @@ -41,4 +42,8 @@ syntax.c syntax.h: mksyntax mksyntax: ${.CURDIR}/mksyntax.c ${.CURDIR}/parser.h ${CC} ${CFLAGS} ${.CURDIR}/mksyntax.c -o $@ +arith_lex.o: arith.o + +${OBJS}: init.c + .include diff --git a/bin/sh/cd.c b/bin/sh/cd.c index c8a13573fc8..d3e77689e91 100644 --- a/bin/sh/cd.c +++ b/bin/sh/cd.c @@ -1,4 +1,4 @@ -/* $NetBSD: cd.c,v 1.13 1995/05/11 21:28:49 christos Exp $ */ +/* $NetBSD: cd.c,v 1.14 1995/11/19 23:27:37 christos Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -40,7 +40,7 @@ #if 0 static char sccsid[] = "@(#)cd.c 8.2 (Berkeley) 5/4/95"; #else -static char rcsid[] = "$NetBSD: cd.c,v 1.13 1995/05/11 21:28:49 christos Exp $"; +static char rcsid[] = "$NetBSD: cd.c,v 1.14 1995/11/19 23:27:37 christos Exp $"; #endif #endif /* not lint */ @@ -65,13 +65,13 @@ static char rcsid[] = "$NetBSD: cd.c,v 1.13 1995/05/11 21:28:49 christos Exp $"; #include "redir.h" #include "mystring.h" #include "show.h" +#include "cd.h" STATIC int docd __P((char *, int)); STATIC char *getcomponent __P((void)); STATIC void updatepwd __P((char *)); -STATIC void getpwd __P((void)); -char *curdir; /* current working directory */ +char *curdir = NULL; /* current working directory */ char *prevdir; /* previous working directory */ STATIC char *cdcomppath; @@ -327,25 +327,75 @@ pwdcmd(argc, argv) -/* - * If we already know the current directory, this routine returns - * immediately. - */ #define MAXPWD 256 -STATIC void -getpwd() { +/* + * Find out what the current directory is. If we already know the current + * directory, this routine returns immediately. + */ +void +getpwd() +{ char buf[MAXPWD]; - char *p; - int i; - int status; - struct job *jp; - int pip[2]; if (curdir) return; + /* + * Things are a bit complicated here; we could have just used + * getcwd, but traditionally getcwd is implemented using popen + * to /bin/pwd. This creates a problem for us, since we cannot + * keep track of the job if it is being ran behind our backs. + * So we re-implement getcwd(), and we suppress interrupts + * throughout the process. This is not completely safe, since + * the user can still break out of it by killing the pwd program. + * We still try to use getcwd for systems that we know have a + * c implementation of getcwd, that does not open a pipe to + * /bin/pwd. + */ +#if defined(__NetBSD__) || defined(__svr4__) if (getcwd(buf, sizeof(buf)) == NULL) error("getcwd() failed"); +#else + { + char *p; + int i; + int status; + struct job *jp; + int pip[2]; + + INTOFF; + if (pipe(pip) < 0) + error("Pipe call failed"); + jp = makejob((union node *)NULL, 1); + if (forkshell(jp, (union node *)NULL, FORK_NOJOB) == 0) { + (void) close(pip[0]); + if (pip[1] != 1) { + close(1); + copyfd(pip[1], 1); + close(pip[1]); + } + (void) execl("/bin/pwd", "pwd", (char *)0); + error("Cannot exec /bin/pwd"); + } + (void) 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; + } + (void) 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'; + } +#endif curdir = savestr(buf); + INTON; } diff --git a/bin/sh/input.c b/bin/sh/input.c index 480082df44c..b4a26794c0a 100644 --- a/bin/sh/input.c +++ b/bin/sh/input.c @@ -1,4 +1,4 @@ -/* $NetBSD: input.c,v 1.18 1995/10/06 21:38:18 christos Exp $ */ +/* $NetBSD: input.c,v 1.19 1995/10/19 04:14:37 christos Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -40,7 +40,7 @@ #if 0 static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95"; #else -static char rcsid[] = "$NetBSD: input.c,v 1.18 1995/10/06 21:38:18 christos Exp $"; +static char rcsid[] = "$NetBSD: input.c,v 1.19 1995/10/19 04:14:37 christos Exp $"; #endif #endif /* not lint */ @@ -293,11 +293,13 @@ check: savec = *q; *q = '\0'; +#ifndef NO_HISTORY if (parsefile->fd == 0 && hist && something) { INTOFF; history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc); INTON; } +#endif if (vflag) { out2str(parsenextc); diff --git a/bin/sh/main.c b/bin/sh/main.c index f11a9a60897..5c75cff3196 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.22 1995/09/11 17:05:44 christos Exp $ */ +/* $NetBSD: main.c,v 1.23 1995/11/19 23:27:42 christos Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -46,7 +46,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95"; #else -static char rcsid[] = "$NetBSD: main.c,v 1.22 1995/09/11 17:05:44 christos Exp $"; +static char rcsid[] = "$NetBSD: main.c,v 1.23 1995/11/19 23:27:42 christos Exp $"; #endif #endif /* not lint */ @@ -76,6 +76,7 @@ static char rcsid[] = "$NetBSD: main.c,v 1.22 1995/09/11 17:05:44 christos Exp $ #include "init.h" #include "mystring.h" #include "exec.h" +#include "cd.h" #define PROFILE 0 @@ -91,7 +92,6 @@ 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 diff --git a/bin/sh/parser.c b/bin/sh/parser.c index 9614e58e9ec..d70b3ebb9c3 100644 --- a/bin/sh/parser.c +++ b/bin/sh/parser.c @@ -1,4 +1,4 @@ -/* $NetBSD: parser.c,v 1.26 1995/05/17 00:05:25 christos Exp $ */ +/* $NetBSD: parser.c,v 1.27 1995/10/19 04:14:41 christos Exp $ */ /*- * Copyright (c) 1991, 1993 @@ -40,7 +40,7 @@ #if 0 static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95"; #else -static char rcsid[] = "$NetBSD: parser.c,v 1.26 1995/05/17 00:05:25 christos Exp $"; +static char rcsid[] = "$NetBSD: parser.c,v 1.27 1995/10/19 04:14:41 christos Exp $"; #endif #endif /* not lint */ @@ -1289,9 +1289,14 @@ parsebackq: { if (!oldstyle && (readtoken() != TRP)) synexpect(TRP); (*nlpp)->n = n; - /* Start reading from old file again. */ - if (oldstyle) + if (oldstyle) { + /* + * Start reading from old file again, ignoring any pushed back + * tokens left from the backquote parsing + */ popfile(); + tokpushback = 0; + } while (stackblocksize() <= savelen) growstackblock(); STARTSTACKSTR(out); -- 2.20.1