[Midnightbsd-cvs] src: bin/sh: Fix bugs with arithmatic expansion $((...)) was truncated

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Jun 29 20:49:38 EDT 2008


Log Message:
-----------
Fix bugs with arithmatic expansion $((...)) was truncated to type int.
Change the type for arithmatic expansion to intmax_t on all architectures.  SUSv3 requires type long, but allows for larger values.

This improves compability with other shells such as bash, zsh and NetBSD's sh.

Expand $LINENO to the current line number.  (SUSv3)
This fixes stupid autotools scripts.  (i'm looking at you graphviz)

Merged from FreeBSD CURRENT.

Modified Files:
--------------
    src/bin/sh:
        TOUR (r1.2 -> r1.3)
        arith.h (r1.2 -> r1.3)
        arith.y (r1.2 -> r1.3)
        cd.c (r1.4 -> r1.5)
        error.c (r1.2 -> r1.3)
        expand.c (r1.3 -> r1.4)
        options.c (r1.3 -> r1.4)
        shell.h (r1.2 -> r1.3)

-------------- next part --------------
Index: shell.h
===================================================================
RCS file: /home/cvs/src/bin/sh/shell.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -L bin/sh/shell.h -L bin/sh/shell.h -u -r1.2 -r1.3
--- bin/sh/shell.h
+++ bin/sh/shell.h
@@ -31,9 +31,14 @@
  * SUCH DAMAGE.
  *
  *	@(#)shell.h	8.2 (Berkeley) 5/4/95
- * $FreeBSD: src/bin/sh/shell.h,v 1.17 2004/04/06 20:06:51 markm Exp $
+ * $FreeBSD: src/bin/sh/shell.h,v 1.18 2008/04/27 20:46:45 stefanf Exp $
  */
 
+#ifndef SHELL_H_
+#define SHELL_H_
+
+#include <inttypes.h>
+
 /*
  * The follow should be set to reflect the type of system you have:
  *	JOBS -> 1 if you have Berkeley job control, 0 otherwise.
@@ -51,10 +56,10 @@
 /*
  * Type of used arithmetics. SUSv3 requires us to have at least signed long.
  */
-typedef long arith_t;
-#define	ARITH_FORMAT_STR  "%ld"
-#define	atoarith_t(arg)  strtol(arg, NULL, 0)
-#define	strtoarith_t(nptr, endptr, base)  strtol(nptr, endptr, base)
+typedef intmax_t arith_t;
+#define	ARITH_FORMAT_STR  "%" PRIdMAX
+#define	atoarith_t(arg)  strtoimax(arg, NULL, 0)
+#define	strtoarith_t(nptr, endptr, base)  strtoimax(nptr, endptr, base)
 
 typedef void *pointer;
 #define STATIC  static
@@ -69,3 +74,5 @@
 #else
 #define TRACE(param)
 #endif
+
+#endif /* !SHELL_H_ */
Index: arith.y
===================================================================
RCS file: /home/cvs/src/bin/sh/arith.y,v
retrieving revision 1.2
retrieving revision 1.3
diff -L bin/sh/arith.y -L bin/sh/arith.y -u -r1.2 -r1.3
--- bin/sh/arith.y
+++ bin/sh/arith.y
@@ -40,7 +40,7 @@
 #endif /* not lint */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/bin/sh/arith.y,v 1.19.8.1 2005/11/06 20:39:47 stefanf Exp $");
+__FBSDID("$FreeBSD: src/bin/sh/arith.y,v 1.19.8.3 2008/04/27 20:43:26 stefanf Exp $");
 
 #include <limits.h>
 #include <stdio.h>
Index: TOUR
===================================================================
RCS file: /home/cvs/src/bin/sh/TOUR,v
retrieving revision 1.2
retrieving revision 1.3
diff -L bin/sh/TOUR -L bin/sh/TOUR -u -r1.2 -r1.3
--- bin/sh/TOUR
+++ bin/sh/TOUR
@@ -23,8 +23,8 @@
 programs that generate source code.  A complete list of these
 programs is:
 
-        program         intput files        generates
-        -------         ------------        ---------
+        program         input files         generates
+        -------         -----------         ---------
         mkbuiltins      builtins            builtins.h builtins.c
         mkinit          *.c                 init.c
         mknodes         nodetypes           nodes.h nodes.c
@@ -92,7 +92,7 @@
 EXINT exception to return to the main command loop.  (Exception:
 EXINT is not raised if the user traps interrupts using the trap
 command.)  The INTOFF and INTON macros (defined in exception.h)
-provide uninterruptable critical sections.  Between the execution
+provide uninterruptible critical sections.  Between the execution
 of INTOFF and the execution of INTON, interrupt signals will be
 held for later delivery.  INTOFF and INTON can be nested.
 
@@ -111,7 +111,7 @@
         p = stackptr;
         *p++ = c;       /* repeated as many times as needed */
         stackptr = p;
-The folloing three macros (defined in memalloc.h) perform these
+The following three macros (defined in memalloc.h) perform these
 operations, but grow the stack if you run off the end:
         STARTSTACKSTR(p);
         STPUTC(c, p);   /* repeated as many times as needed */
@@ -120,7 +120,7 @@
 We now start a top-down look at the code:
 
 MAIN.C:  The main routine performs some initialization, executes
-the user's profile if necessary, and calls cmdloop.  Cmdloop is
+the user's profile if necessary, and calls cmdloop.  Cmdloop
 repeatedly parses and executes commands.
 
 OPTIONS.C:  This file contains the option processing code.  It is
@@ -179,7 +179,7 @@
         VSQUESTION          ${var?text}
         VSQUESTION|VSNUL    ${var:?text}
         VSASSIGN            ${var=text}
-        VSASSIGN|VSNUL      ${var=text}
+        VSASSIGN|VSNUL      ${var:=text}
 
 In addition, the type field will have the VSQUOTE flag set if the
 variable is enclosed in double quotes.  The name of the variable
@@ -205,7 +205,7 @@
 field can be written without any processing).  Other here docu-
 ments, and words which are not subject to splitting and file name
 generation, have the CTLESC characters removed during the vari-
-able and command substitution phase.  Words which are subject
+able and command substitution phase.  Words which are subject to
 splitting and file name generation have the CTLESC characters re-
 moved as part of the file name phase.
 
@@ -213,7 +213,7 @@
         eval.c     The top level routines.
         redir.c    Code to handle redirection of input and output.
         jobs.c     Code to handle forking, waiting, and job control.
-        exec.c     Code to to path searches and the actual exec sys call.
+        exec.c     Code to do path searches and the actual exec sys call.
         expand.c   Code to evaluate arguments.
         var.c      Maintains the variable symbol table.  Called from expand.c.
 
@@ -233,7 +233,7 @@
 REDIR.C:  Ash allows file descriptors to be redirected and then
 restored without forking off a child process.  This is accom-
 plished by duplicating the original file descriptors.  The redir-
-tab structure records where the file descriptors have be dupli-
+tab structure records where the file descriptors have been dupli-
 cated to.
 
 EXEC.C:  The routine find_command locates a command, and enters
@@ -284,8 +284,8 @@
 tered throughout the code, depending on which location appears
 most appropriate.  They can be recognized because their names al-
 ways end in "cmd".  The mapping from names to procedures is
-specified in the file builtins, which is processed by the mkbuil-
-tins command.
+specified in the file builtins, which is processed by the mkbuilt-
+ins command.
 
 A builtin command is invoked with argc and argv set up like a
 normal program.  A builtin command is allowed to overwrite its
Index: expand.c
===================================================================
RCS file: /home/cvs/src/bin/sh/expand.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -L bin/sh/expand.c -L bin/sh/expand.c -u -r1.3 -r1.4
--- bin/sh/expand.c
+++ bin/sh/expand.c
@@ -37,7 +37,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/bin/sh/expand.c,v 1.47.2.3 2006/11/22 00:28:04 stefanf Exp $");
+__FBSDID("$FreeBSD: src/bin/sh/expand.c,v 1.47.2.5 2008/04/27 20:43:26 stefanf Exp $");
 
 #include <sys/types.h>
 #include <sys/time.h>
Index: error.c
===================================================================
RCS file: /home/cvs/src/bin/sh/error.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -L bin/sh/error.c -L bin/sh/error.c -u -r1.2 -r1.3
--- bin/sh/error.c
+++ bin/sh/error.c
@@ -37,7 +37,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/bin/sh/error.c,v 1.25 2004/04/06 20:06:51 markm Exp $");
+__FBSDID("$FreeBSD: src/bin/sh/error.c,v 1.26 2006/02/04 14:37:50 schweikh Exp $");
 
 /*
  * Errors and exceptions.
@@ -119,7 +119,7 @@
 	 * This doesn't seem to be needed, since main() emits a newline.
 	 */
 #if 0
-	if (tcgetpgrp(0) == getpid())	
+	if (tcgetpgrp(0) == getpid())
 		write(STDERR_FILENO, "\n", 1);
 #endif
 	if (rootshell && iflag)
Index: cd.c
===================================================================
RCS file: /home/cvs/src/bin/sh/cd.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -L bin/sh/cd.c -L bin/sh/cd.c -u -r1.4 -r1.5
--- bin/sh/cd.c
+++ bin/sh/cd.c
@@ -37,7 +37,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/bin/sh/cd.c,v 1.34.8.1 2006/06/28 21:14:23 stefanf Exp $");
+__FBSDID("$FreeBSD: src/bin/sh/cd.c,v 1.34.8.2 2008/04/20 18:08:12 stefanf Exp $");
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -69,7 +69,9 @@
 STATIC int cdphysical(char *);
 STATIC int docd(char *, int, int);
 STATIC char *getcomponent(void);
-STATIC int updatepwd(char *);
+STATIC char *findcwd(char *);
+STATIC void updatepwd(char *);
+STATIC char *getpwd2(char *, size_t);
 
 STATIC char *curdir = NULL;	/* current working directory */
 STATIC char *prevdir;		/* previous working directory */
@@ -202,10 +204,11 @@
 	}
 
 	INTOFF;
-	if (updatepwd(badstat ? NULL : dest) < 0 || chdir(curdir) < 0) {
+	if ((p = findcwd(badstat ? NULL : dest)) == NULL || chdir(p) < 0) {
 		INTON;
 		return (-1);
 	}
+	updatepwd(p);
 	INTON;
 	return (0);
 }
@@ -213,12 +216,14 @@
 STATIC int
 cdphysical(char *dest)
 {
+	char *p;
 
 	INTOFF;
-	if (chdir(dest) < 0 || updatepwd(NULL) < 0) {
+	if (chdir(dest) < 0 || (p = findcwd(NULL)) == NULL) {
 		INTON;
 		return (-1);
 	}
+	updatepwd(p);
 	INTON;
 	return (0);
 }
@@ -248,38 +253,20 @@
 }
 
 
-/*
- * Update curdir (the name of the current directory) in response to a
- * cd command.  We also call hashcd to let the routines in exec.c know
- * that the current directory has changed.
- */
-STATIC int
-updatepwd(char *dir)
+STATIC char *
+findcwd(char *dir)
 {
 	char *new;
 	char *p;
 
-	hashcd();				/* update command hash table */
-
 	/*
 	 * If our argument is NULL, we don't know the current directory
 	 * any more because we traversed a symbolic link or something
 	 * we couldn't stat().
 	 */
 	if (dir == NULL || curdir == NULL)  {
-		if (prevdir)
-			ckfree(prevdir);
-		INTOFF;
-		prevdir = curdir;
-		curdir = NULL;
-		if (getpwd() == NULL) {
-			INTON;
-			return (-1);
-		}
-		setvar("PWD", curdir, VEXPORT);
-		setvar("OLDPWD", prevdir, VEXPORT);
-		INTON;
-		return (0);
+		p = stalloc(PATH_MAX);
+		return getpwd2(p, PATH_MAX);
 	}
 	cdcomppath = stalloc(strlen(dir) + 1);
 	scopy(dir, cdcomppath);
@@ -303,16 +290,25 @@
 	if (new == stackblock())
 		STPUTC('/', new);
 	STACKSTRNUL(new);
-	INTOFF;
+	return stackblock();
+}
+
+/*
+ * Update curdir (the name of the current directory) in response to a
+ * cd command.  We also call hashcd to let the routines in exec.c know
+ * that the current directory has changed.
+ */
+STATIC void
+updatepwd(char *dir)
+{
+	hashcd();				/* update command hash table */
+
 	if (prevdir)
 		ckfree(prevdir);
 	prevdir = curdir;
-	curdir = savestr(stackblock());
+	curdir = savestr(dir);
 	setvar("PWD", curdir, VEXPORT);
 	setvar("OLDPWD", prevdir, VEXPORT);
-	INTON;
-
-	return (0);
 }
 
 int
@@ -356,17 +352,31 @@
 }
 
 /*
- * Find out what the current directory is. If we already know the current
- * directory, this routine returns immediately.
+ * Get the current directory and cache the result in curdir.
  */
 char *
 getpwd(void)
 {
 	char buf[PATH_MAX];
+	char *p;
 
 	if (curdir)
 		return curdir;
-	if (getcwd(buf, sizeof(buf)) == NULL) {
+
+	p = getpwd2(buf, sizeof(buf));
+	if (p != NULL)
+		curdir = savestr(p);
+
+	return curdir;
+}
+
+/*
+ * Return the current directory.
+ */
+STATIC char *
+getpwd2(char *buf, size_t size)
+{
+	if (getcwd(buf, size) == NULL) {
 		char *pwd = getenv("PWD");
 		struct stat stdot, stpwd;
 
@@ -374,12 +384,9 @@
 		    stat(pwd, &stpwd) != -1 &&
 		    stdot.st_dev == stpwd.st_dev &&
 		    stdot.st_ino == stpwd.st_ino) {
-			curdir = savestr(pwd);
-			return curdir;
+			return pwd;
 		}
 		return NULL;
 	}
-	curdir = savestr(buf);
-
-	return curdir;
+	return buf;
 }
Index: arith.h
===================================================================
RCS file: /home/cvs/src/bin/sh/arith.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -L bin/sh/arith.h -L bin/sh/arith.h -u -r1.2 -r1.3
--- bin/sh/arith.h
+++ bin/sh/arith.h
@@ -28,7 +28,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)arith.h	1.1 (Berkeley) 5/4/95
- * $FreeBSD: src/bin/sh/arith.h,v 1.9.8.1 2005/11/06 20:39:47 stefanf Exp $
+ * $FreeBSD: src/bin/sh/arith.h,v 1.9.8.3 2008/04/27 20:43:26 stefanf Exp $
  */
 
 extern char *arith_buf, *arith_startbuf;
Index: options.c
===================================================================
RCS file: /home/cvs/src/bin/sh/options.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -L bin/sh/options.c -L bin/sh/options.c -u -r1.3 -r1.4
--- bin/sh/options.c
+++ bin/sh/options.c
@@ -37,7 +37,7 @@
 #endif
 #endif /* not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/bin/sh/options.c,v 1.23.2.2 2006/06/03 15:38:07 stefanf Exp $");
+__FBSDID("$FreeBSD: src/bin/sh/options.c,v 1.23.2.3 2008/04/20 18:08:46 stefanf Exp $");
 
 #include <signal.h>
 #include <unistd.h>
@@ -384,7 +384,7 @@
 	}
 	ap2 = shellparam.p;
 	while ((*ap2++ = *ap1++) != NULL);
-	shellparam.optnext = NULL;
+	shellparam.reset = 1;
 	INTON;
 	return 0;
 }
@@ -406,6 +406,7 @@
 	if (*argptr != NULL) {
 		setparam(argptr);
 	}
+	shellparam.reset = 1;
 	INTON;
 	return 0;
 }
@@ -415,7 +416,6 @@
 getoptsreset(const char *value)
 {
 	if (number(value) == 1) {
-		shellparam.optnext = NULL;
 		shellparam.reset = 1;
 	}
 }


More information about the Midnightbsd-cvs mailing list