Remove atoi()

Mostly use number() (no longer implemented using atoi()) when an
unsigned integer is required, but use strtoXXX() when a conversion
is wanted, without the possibility or error (like setting OPTIND
and RANDOM).   Always init OPTIND to 1 when sh starts (overriding
anything in environ.)
This commit is contained in:
kre 2018-07-13 22:43:44 +00:00
parent 09ca79d6f2
commit c6c29888c4
5 changed files with 37 additions and 20 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $ */
/* $NetBSD: histedit.c,v 1.53 2018/07/13 22:43:44 kre Exp $ */
/*-
* Copyright (c) 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $");
__RCSID("$NetBSD: histedit.c,v 1.53 2018/07/13 22:43:44 kre Exp $");
#endif
#endif /* not lint */
@ -210,8 +210,8 @@ sethistsize(const char *hs)
HistEvent he;
if (hist != NULL) {
if (hs == NULL || *hs == '\0' ||
(histsize = atoi(hs)) < 0)
if (hs == NULL || *hs == '\0' || *hs == '-' ||
(histsize = number(hs)) < 0)
histsize = 100;
history(hist, &he, H_SETSIZE, histsize);
history(hist, &he, H_SETUNIQUE, 1);
@ -529,7 +529,7 @@ str_to_event(const char *str, int last)
s++;
}
if (is_number(s)) {
i = atoi(s);
i = number(s);
if (relative) {
while (retval != -1 && i--) {
retval = history(hist, &he, H_NEXT);

View File

@ -1,4 +1,4 @@
/* $NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $ */
/* $NetBSD: mystring.c,v 1.18 2018/07/13 22:43:44 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)mystring.c 8.2 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $");
__RCSID("$NetBSD: mystring.c,v 1.18 2018/07/13 22:43:44 kre Exp $");
#endif
#endif /* not lint */
@ -51,6 +51,8 @@ __RCSID("$NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $");
* is_number(s) Return true if s is a string of digits.
*/
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include "shell.h"
#include "syntax.h"
@ -110,10 +112,15 @@ prefix(const char *pfx, const char *string)
int
number(const char *s)
{
char *ep = NULL;
intmax_t n;
if (! is_number(s))
error("Illegal number: %s", s);
return atoi(s);
if (!is_digit(*s) || ((n = strtoimax(s, &ep, 10)),
(ep == NULL || ep == s || *ep != '\0')))
error("Illegal number: '%s'", s);
if (n < INT_MIN || n > INT_MAX)
error("Number out of range: %s", s);
return (int)n;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: options.c,v 1.52 2017/11/21 03:42:39 kre Exp $ */
/* $NetBSD: options.c,v 1.53 2018/07/13 22:43:44 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: options.c,v 1.52 2017/11/21 03:42:39 kre Exp $");
__RCSID("$NetBSD: options.c,v 1.53 2018/07/13 22:43:44 kre Exp $");
#endif
#endif /* not lint */
@ -60,6 +60,7 @@ __RCSID("$NetBSD: options.c,v 1.52 2017/11/21 03:42:39 kre Exp $");
#include "memalloc.h"
#include "error.h"
#include "mystring.h"
#include "syntax.h"
#ifndef SMALL
#include "myhistedit.h"
#endif
@ -456,7 +457,12 @@ setcmd(int argc, char **argv)
void
getoptsreset(const char *value)
{
if (number(value) == 1) {
/*
* This is just to detect the case where OPTIND=1
* is executed. Any other string assigned to OPTIND
* is OK, but is not a reset. No errors, so cannot use number()
*/
if (is_digit(*value) && strtol(value, NULL, 10) == 1) {
shellparam.optnext = NULL;
shellparam.reset = 1;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: parser.c,v 1.146 2018/04/21 21:32:14 kre Exp $ */
/* $NetBSD: parser.c,v 1.147 2018/07/13 22:43:44 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
#else
__RCSID("$NetBSD: parser.c,v 1.146 2018/04/21 21:32:14 kre Exp $");
__RCSID("$NetBSD: parser.c,v 1.147 2018/07/13 22:43:44 kre Exp $");
#endif
#endif /* not lint */
@ -1506,7 +1506,7 @@ parseredir(const char *out, int c)
union node *np;
int fd;
fd = (*out == '\0') ? -1 : atoi(out);
fd = (*out == '\0') ? -1 : number(out);
np = stalloc(sizeof(struct nfile));
if (c == '>') {

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.69 2017/11/19 03:23:01 kre Exp $ */
/* $NetBSD: var.c,v 1.70 2018/07/13 22:43:44 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: var.c,v 1.69 2017/11/19 03:23:01 kre Exp $");
__RCSID("$NetBSD: var.c,v 1.70 2018/07/13 22:43:44 kre Exp $");
#endif
#endif /* not lint */
@ -50,6 +50,7 @@ __RCSID("$NetBSD: var.c,v 1.69 2017/11/19 03:23:01 kre Exp $");
#include <time.h>
#include <pwd.h>
#include <fcntl.h>
#include <inttypes.h>
/*
* Shell variables.
@ -230,14 +231,17 @@ INIT {
*
* PPID is readonly
* Always default IFS
* POSIX: "Whenever the shell is invoked, OPTIND shall
* be initialized to 1."
* PSc indicates the root/non-root status of this shell.
* NETBSD_SHELL is a constant (readonly), and is never exported
* START_TIME belongs only to this shell.
* NETBSD_SHELL is a constant (readonly), and is never exported
* LINENO is simply magic...
*/
snprintf(buf, sizeof(buf), "%d", (int)getppid());
setvar("PPID", buf, VREADONLY);
setvar("IFS", ifs_default, VTEXTFIXED);
setvar("OPTIND", "1", VTEXTFIXED);
setvar("PSc", (geteuid() == 0 ? "#" : "$"), VTEXTFIXED);
#ifndef SMALL
@ -1427,7 +1431,7 @@ get_random(struct var *vp)
}
} else
/* good enough for today */
random_val = atoi(vp->text + vp->name_len + 1);
random_val = strtoimax(vp->text+vp->name_len+1,NULL,0);
srandom((long)random_val);
}