- Fix bugs where the value of arithmetic expansion$((...)) was trucated

to type int.
- Change the type used for arithmetic expansion to intmax_t (ie. 64 bit on all
  currently supported FreeBSD architectures).  SUSv3 requires at least type
  long but allows for larger types.  Other shells (eg. bash, zsh, NetBSD's sh)
  do that too.

PR:		122659
Submitted by:	Jaakko Heinonen (minor modifications by me)
This commit is contained in:
Stefan Farfeleder 2008-04-27 20:46:45 +00:00
parent 9d5a22b928
commit 39376f45e2
3 changed files with 29 additions and 14 deletions

View File

@ -30,8 +30,12 @@
* $FreeBSD$
*/
#include "shell.h"
#define DIGITS(var) (3 + (2 + CHAR_BIT * sizeof((var))) / 3)
extern char *arith_buf, *arith_startbuf;
int arith(char *);
arith_t arith(char *);
void arith_lex_reset(void);
int expcmd(int, char **);

View File

@ -75,7 +75,10 @@ __FBSDID("$FreeBSD$");
exp:
expr
{ return ($1); }
{
*YYPARSE_PARAM = $1;
return (0);
}
;
expr:
@ -259,12 +262,13 @@ expr:
#include "output.h"
#include "memalloc.h"
#define lstrlen(var) (3 + (2 + CHAR_BIT * sizeof((var))) / 3)
#define YYPARSE_PARAM_TYPE arith_t *
#define YYPARSE_PARAM result
char *arith_buf, *arith_startbuf;
int yylex(void);
int yyparse(void);
int yyparse(YYPARSE_PARAM_TYPE);
static int
arith_assign(char *name, arith_t value)
@ -272,22 +276,22 @@ arith_assign(char *name, arith_t value)
char *str;
int ret;
str = (char *)ckmalloc(lstrlen(value));
str = (char *)ckmalloc(DIGITS(value));
sprintf(str, ARITH_FORMAT_STR, value);
ret = setvarsafe(name, str, 0);
free(str);
return ret;
}
int
arith_t
arith(char *s)
{
long result;
arith_t result;
arith_buf = arith_startbuf = s;
INTOFF;
result = yyparse();
yyparse(&result);
arith_lex_reset(); /* Reprime lex. */
INTON;
@ -313,7 +317,7 @@ expcmd(int argc, char **argv)
char *p;
char *concat;
char **ap;
long i;
arith_t i;
if (argc > 1) {
p = argv[1];
@ -338,7 +342,7 @@ expcmd(int argc, char **argv)
i = arith(p);
out1fmt("%ld\n", i);
out1fmt(ARITH_FORMAT_STR "\n", i);
return !i;
}

View File

@ -33,6 +33,11 @@
* $FreeBSD$
*/
#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.
@ -50,10 +55,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
@ -68,3 +73,5 @@ extern char nullstr[1]; /* null string */
#else
#define TRACE(param)
#endif
#endif /* !SHELL_H_ */