Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/bin/sh Remove atoi()
details: https://anonhg.NetBSD.org/src/rev/49c1b15c3c5a
branches: trunk
changeset: 324055:49c1b15c3c5a
user: kre <kre%NetBSD.org@localhost>
date: Fri Jul 13 22:43:44 2018 +0000
description:
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.)
diffstat:
bin/sh/histedit.c | 10 +++++-----
bin/sh/mystring.c | 17 ++++++++++++-----
bin/sh/options.c | 12 +++++++++---
bin/sh/parser.c | 6 +++---
bin/sh/var.c | 12 ++++++++----
5 files changed, 37 insertions(+), 20 deletions(-)
diffs (205 lines):
diff -r 56cbfce14eba -r 49c1b15c3c5a bin/sh/histedit.c
--- a/bin/sh/histedit.c Fri Jul 13 21:46:58 2018 +0000
+++ b/bin/sh/histedit.c Fri Jul 13 22:43:44 2018 +0000
@@ -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 @@
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 @@
s++;
}
if (is_number(s)) {
- i = atoi(s);
+ i = number(s);
if (relative) {
while (retval != -1 && i--) {
retval = history(hist, &he, H_NEXT);
diff -r 56cbfce14eba -r 49c1b15c3c5a bin/sh/mystring.c
--- a/bin/sh/mystring.c Fri Jul 13 21:46:58 2018 +0000
+++ b/bin/sh/mystring.c Fri Jul 13 22:43:44 2018 +0000
@@ -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 @@
* 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 @@
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;
}
diff -r 56cbfce14eba -r 49c1b15c3c5a bin/sh/options.c
--- a/bin/sh/options.c Fri Jul 13 21:46:58 2018 +0000
+++ b/bin/sh/options.c Fri Jul 13 22:43:44 2018 +0000
@@ -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 @@
#include "memalloc.h"
#include "error.h"
#include "mystring.h"
+#include "syntax.h"
#ifndef SMALL
#include "myhistedit.h"
#endif
@@ -456,7 +457,12 @@
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;
}
diff -r 56cbfce14eba -r 49c1b15c3c5a bin/sh/parser.c
--- a/bin/sh/parser.c Fri Jul 13 21:46:58 2018 +0000
+++ b/bin/sh/parser.c Fri Jul 13 22:43:44 2018 +0000
@@ -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 @@
union node *np;
int fd;
- fd = (*out == '\0') ? -1 : atoi(out);
+ fd = (*out == '\0') ? -1 : number(out);
np = stalloc(sizeof(struct nfile));
if (c == '>') {
diff -r 56cbfce14eba -r 49c1b15c3c5a bin/sh/var.c
--- a/bin/sh/var.c Fri Jul 13 21:46:58 2018 +0000
+++ b/bin/sh/var.c Fri Jul 13 22:43:44 2018 +0000
@@ -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 @@
#include <time.h>
#include <pwd.h>
#include <fcntl.h>
+#include <inttypes.h>
/*
* Shell variables.
@@ -230,14 +231,17 @@
*
* 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.
+ * START_TIME belongs only to this shell.
* NETBSD_SHELL is a constant (readonly), and is never exported
- * START_TIME belongs only to this shell.
* 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 @@
}
} 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);
}
Home |
Main Index |
Thread Index |
Old Index