NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

kern/49084: Unify stand's atoi() to MI libsa



>Number:         49084
>Category:       kern
>Synopsis:       Unify stand's atoi() to MI libsa
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Aug 07 08:35:00 +0000 2014
>Originator:     Tetsuya Isaki
>Release:        NetBSD-current
>Organization:
>Environment:
N/A
>Description:
Some arch/*/stand/ have their own atoi() copy.  In the past,
it's not the best way, but was a better way.  However, MI libsa
also have their own atoi() copy (in bootcfg.c) now.
They should be unified..

I wrote a patch.  I confirmed that all 'make depends' in arch/*/stand
succeeded for each arch I modified.  I tested it only on x68k but
I think it will also work for the other arch.
While here, I also replaced some isnum() macro related to atoi()
with libkern's isdigit() macro.
>How-To-Repeat:
N/A
>Fix:
--- /dev/null   2014-08-07 16:42:35.000000000 +0900
+++ sys/lib/libsa/atoi.c        2014-08-07 16:41:38.000000000 +0900
@@ -0,0 +1,49 @@
+/*     $NetBSD$        */
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/reboot.h>
+
+#include <lib/libsa/stand.h>
+#include <lib/libkern/libkern.h>
+
+int
+atoi(const char *in)
+{
+       char *c;
+       int ret;
+
+       ret = 0;
+       c = (char *)in;
+       if (*c == '-')
+               c++;
+       for (; isdigit(*c); c++)
+               ret = (ret * 10) + (*c - '0');
+
+       return (*in == '-') ? -ret : ret;
+}
Index: sys/lib/libsa/Makefile
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/Makefile,v
retrieving revision 1.83
diff -u -r1.83 Makefile
--- sys/lib/libsa/Makefile      28 Jun 2014 09:16:18 -0000      1.83
+++ sys/lib/libsa/Makefile      7 Aug 2014 07:37:03 -0000
@@ -23,7 +23,7 @@
 .PATH.c: ${SADIR} ${.PARSEDIR}/../../../common/lib/libc/string
 
 # stand routines
-SRCS+= alloc.c errno.c exit.c files.c \
+SRCS+= alloc.c atoi.c errno.c exit.c files.c \
        getfile.c gets.c globals.c \
        panic.c printf.c qsort.c snprintf.c strerror.c \
        subr_prf.c twiddle.c checkpasswd.c
Index: sys/lib/libsa/bootcfg.c
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/bootcfg.c,v
retrieving revision 1.1
diff -u -r1.1 bootcfg.c
--- sys/lib/libsa/bootcfg.c     28 Jun 2014 09:16:18 -0000      1.1
+++ sys/lib/libsa/bootcfg.c     7 Aug 2014 08:27:22 -0000
@@ -33,10 +33,6 @@
 #include <lib/libsa/bootcfg.h>
 #include <lib/libkern/libkern.h>
 
-static int atoi(const char *);
-
-#define isnum(c) ((c) >= '0' && (c) <= '9')
-
 #define MENUFORMAT_AUTO   0
 #define MENUFORMAT_NUMBER 1
 #define MENUFORMAT_LETTER 2
@@ -46,22 +42,6 @@
 
 struct bootcfg_def bootcfg_info;
 
-int
-atoi(const char *in)
-{
-       char *c;
-       int ret;
-
-       ret = 0;
-       c = (char *)in;
-       if (*c == '-')
-               c++;
-       for (; isnum(*c); c++)
-               ret = (ret * 10) + (*c - '0');
-
-       return (*in == '-') ? -ret : ret;
-}
-
 void
 bootcfg_do_noop(const char *cmd, char *arg)
 {
@@ -231,7 +211,7 @@
                        if (cbanner < BOOTCFG_MAXBANNER)
                                bootcfg_info.banner[cbanner++] = value;
                } else if (!strncmp(key, "timeout", 7)) {
-                       if (!isnum(*value))
+                       if (!isdigit(*value))
                                bootcfg_info.timeout = -1;
                        else
                                bootcfg_info.timeout = atoi(value);
Index: sys/lib/libsa/stand.h
===================================================================
RCS file: /cvsroot/src/sys/lib/libsa/stand.h,v
retrieving revision 1.78
diff -u -r1.78 stand.h
--- sys/lib/libsa/stand.h       26 Mar 2014 18:02:24 -0000      1.78
+++ sys/lib/libsa/stand.h       7 Aug 2014 07:37:03 -0000
@@ -319,4 +319,6 @@
 void   bcopy(const void *, void *, size_t);
 void   bzero(void *, size_t);
 
+int    atoi(const char *);
+
 #endif /* _LIBSA_STAND_H_ */
Index: sys/arch/hp300/stand/common/devopen.c
===================================================================
RCS file: /cvsroot/src/sys/arch/hp300/stand/common/devopen.c,v
retrieving revision 1.10
diff -u -r1.10 devopen.c
--- sys/arch/hp300/stand/common/devopen.c       28 Apr 2008 20:23:19 -0000      
1.10
+++ sys/arch/hp300/stand/common/devopen.c       7 Aug 2014 07:36:46 -0000
@@ -73,16 +73,6 @@
 static int devlookup(const char * ,int);
 static int devparse(const char *, int *, int*, int*, int*, int*, char **);
 
-int
-atoi(char *cp)
-{
-       int val = 0;
-
-       while (isdigit((unsigned char)*cp))
-               val = val * 10 + (*cp++ - '0');
-       return val;
-}
-
 void
 usage(void)
 {
Index: sys/arch/hp300/stand/common/samachdep.h
===================================================================
RCS file: /cvsroot/src/sys/arch/hp300/stand/common/samachdep.h,v
retrieving revision 1.17
diff -u -r1.17 samachdep.h
--- sys/arch/hp300/stand/common/samachdep.h     19 Apr 2014 06:04:58 -0000      
1.17
+++ sys/arch/hp300/stand/common/samachdep.h     7 Aug 2014 07:36:46 -0000
@@ -74,7 +74,6 @@
 
 /* devopen.c */
 extern u_int opendev;
-int atoi(char *);
 
 /* exec.c */
 void exec_hp300(char *, u_long, int);
Index: sys/arch/hp300/stand/inst/inst.c
===================================================================
RCS file: /cvsroot/src/sys/arch/hp300/stand/inst/inst.c,v
retrieving revision 1.19
diff -u -r1.19 inst.c
--- sys/arch/hp300/stand/inst/inst.c    21 Jun 2014 02:01:21 -0000      1.19
+++ sys/arch/hp300/stand/inst/inst.c    7 Aug 2014 07:36:46 -0000
@@ -760,18 +760,11 @@
        __asm("stop #0x2700");
 }
 
-/*
- * XXX Should have a generic atoi for libkern/libsa.
- */
 int
 a2int(char *cp)
 {
-       int i = 0;
-
        if (*cp == '\0')
-               return (-1);
+               return -1;
 
-       while (*cp != '\0')
-               i = i * 10 + *cp++ - '0';
-       return (i);
+       return atoi(cp);
 }
Index: sys/arch/i386/stand/lib/bootmenu.c
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/stand/lib/bootmenu.c,v
retrieving revision 1.13
diff -u -r1.13 bootmenu.c
--- sys/arch/i386/stand/lib/bootmenu.c  28 Jun 2014 09:16:18 -0000      1.13
+++ sys/arch/i386/stand/lib/bootmenu.c  7 Aug 2014 07:36:46 -0000
@@ -40,8 +40,6 @@
 #include <libi386.h>
 #include <bootmenu.h>
 
-#define isnum(c) ((c) >= '0' && (c) <= '9')
-
 static void docommandchoice(int);
 
 extern struct x86_boot_params boot_params;
@@ -51,22 +49,6 @@
 #define MENUFORMAT_NUMBER 1
 #define MENUFORMAT_LETTER 2
 
-int
-atoi(const char *in)
-{
-       char *c;
-       int ret;
-
-       ret = 0;
-       c = (char *)in;
-       if (*c == '-')
-               c++;
-       for (; isnum(*c); c++)
-               ret = (ret * 10) + (*c - '0');
-
-       return (*in == '-') ? -ret : ret;
-}
-
 /*
  * XXX
  * if module_add, userconf_add are strictly mi they can be folded back
@@ -105,14 +87,14 @@
                choice = (*input) - 'A';
        else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
                choice = (*input) - 'a';
-       else if (isnum(*input)) {
+       else if (isdigit(*input)) {
                choice = atoi(input) - 1;
                if (choice < 0 || choice >= bootcfg_info.nummenu)
                        choice = -1;
        }
 
        if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
-           !isnum(*input) && !usedef)
+           !isdigit(*input) && !usedef)
                choice = -1;
 
        return choice;
Index: sys/arch/i386/stand/lib/bootmenu.h
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/stand/lib/bootmenu.h,v
retrieving revision 1.4
diff -u -r1.4 bootmenu.h
--- sys/arch/i386/stand/lib/bootmenu.h  28 Jun 2014 09:16:18 -0000      1.4
+++ sys/arch/i386/stand/lib/bootmenu.h  7 Aug 2014 07:36:46 -0000
@@ -34,6 +34,5 @@
 void parsebootconf(const char *);
 void doboottypemenu(void);
 void bootdefault(void);
-int atoi(const char *);
 
 #endif /* !_BOOTMENU_H */
Index: sys/arch/next68k/stand/boot/devopen.c
===================================================================
RCS file: /cvsroot/src/sys/arch/next68k/stand/boot/devopen.c,v
retrieving revision 1.5
diff -u -r1.5 devopen.c
--- sys/arch/next68k/stand/boot/devopen.c       11 Dec 2005 12:18:29 -0000      
1.5
+++ sys/arch/next68k/stand/boot/devopen.c       7 Aug 2014 07:36:47 -0000
@@ -32,21 +32,10 @@
 #include <lib/libsa/stand.h>
 #include <lib/libkern/libkern.h>
 
-int atoi(const char *);
 int devlookup(const char *, int);
 int devparse(const char *, int *, char *, char *, char *, char **);
 
 int
-atoi(const char *cp)
-{
-    int val = 0;
-
-    while(isdigit((unsigned char)*cp))
-       val = val * 10 + (*cp++ - '0');
-    return val;
-}
-
-int
 devlookup(const char *d, int len)
 {
     struct devsw *dp = devsw;
Index: sys/arch/prep/stand/boot/devopen.c
===================================================================
RCS file: /cvsroot/src/sys/arch/prep/stand/boot/devopen.c,v
retrieving revision 1.4
diff -u -r1.4 devopen.c
--- sys/arch/prep/stand/boot/devopen.c  19 May 2012 14:40:13 -0000      1.4
+++ sys/arch/prep/stand/boot/devopen.c  7 Aug 2014 07:36:47 -0000
@@ -33,21 +33,10 @@
 
 #define        ispart(c)       ((c) >= 'a' && (c) <= 'h')
 
-int atoi(char *);
 int devlookup(char *);
 int devparse(const char *, int *, int *, int *, int *, int *, char **);
 
 int
-atoi(char *cp)
-{
-       int val = 0;
-
-       while (isdigit(*cp))
-               val = val * 10 + (*cp++ - '0');
-       return (val);
-}
-
-int
 devlookup(char *d)
 {
        struct devsw *dp = devsw;
Index: sys/arch/rs6000/stand/boot/devopen.c
===================================================================
RCS file: /cvsroot/src/sys/arch/rs6000/stand/boot/devopen.c,v
retrieving revision 1.1
diff -u -r1.1 devopen.c
--- sys/arch/rs6000/stand/boot/devopen.c        17 Dec 2007 19:09:48 -0000      
1.1
+++ sys/arch/rs6000/stand/boot/devopen.c        7 Aug 2014 07:36:47 -0000
@@ -33,21 +33,10 @@
 
 #define        ispart(c)       ((c) >= 'a' && (c) <= 'h')
 
-int atoi(char *);
 int devlookup(char *);
 int devparse(const char *, int *, int *, int *, int *, int *, char **);
 
 int
-atoi(char *cp)
-{
-       int val = 0;
-
-       while (isdigit(*cp))
-               val = val * 10 + (*cp++ - '0');
-       return (val);
-}
-
-int
 devlookup(char *d)
 {
        struct devsw *dp = devsw;
Index: sys/arch/x68k/stand/boot/switch.c
===================================================================
RCS file: /cvsroot/src/sys/arch/x68k/stand/boot/switch.c,v
retrieving revision 1.1
diff -u -r1.1 switch.c
--- sys/arch/x68k/stand/boot/switch.c   5 Aug 2014 13:49:04 -0000       1.1
+++ sys/arch/x68k/stand/boot/switch.c   7 Aug 2014 07:36:47 -0000
@@ -63,22 +63,6 @@
 }
 
 static int
-atoi(const char *in)
-{
-       char *c;
-       int ret;
-
-       ret = 0;
-       c = (char *)in;
-       if (*c == '-')
-               c++;
-       for (; isdigit(*c); c++)
-               ret = (ret * 10) + (*c - '0');
-
-       return (*in == '-') ? -ret : ret;
-}
-
-static int
 hextoi(const char *in)
 {
        char *c;
Index: sys/arch/zaurus/stand/zboot/bootmenu.c
===================================================================
RCS file: /cvsroot/src/sys/arch/zaurus/stand/zboot/bootmenu.c,v
retrieving revision 1.3
diff -u -r1.3 bootmenu.c
--- sys/arch/zaurus/stand/zboot/bootmenu.c      28 Jun 2014 09:16:18 -0000      
1.3
+++ sys/arch/zaurus/stand/zboot/bootmenu.c      7 Aug 2014 07:36:47 -0000
@@ -35,28 +35,10 @@
 #include "bootmenu.h"
 #include "pathnames.h"
 
-#define isnum(c) ((c) >= '0' && (c) <= '9')
-
 #define MENUFORMAT_AUTO          0
 #define MENUFORMAT_NUMBER 1
 #define MENUFORMAT_LETTER 2
 
-int
-atoi(const char *in)
-{
-       char *c;
-       int ret;
-
-       ret = 0;
-       c = (char *)in;
-       if (*c == '-')
-               c++;
-       for (; isnum(*c); c++)
-               ret = (ret * 10) + (*c - '0');
-
-       return (*in == '-') ? -ret : ret;
-}
-
 void
 parsebootconf(const char *conf)
 {
@@ -77,7 +59,7 @@
                choice = (*input) - 'A';
        else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
                choice = (*input) - 'a';
-       else if (isnum(*input)) {
+       else if (isdigit(*input)) {
                choice = atoi(input) - 1;
                if (choice < 0 || choice >= bootcfg_info.nummenu)
                        choice = -1;
Index: sys/arch/zaurus/stand/zboot/bootmenu.h
===================================================================
RCS file: /cvsroot/src/sys/arch/zaurus/stand/zboot/bootmenu.h,v
retrieving revision 1.2
diff -u -r1.2 bootmenu.h
--- sys/arch/zaurus/stand/zboot/bootmenu.h      28 Jun 2014 09:16:18 -0000      
1.2
+++ sys/arch/zaurus/stand/zboot/bootmenu.h      7 Aug 2014 07:36:47 -0000
@@ -33,6 +33,5 @@
 
 void parsebootconf(const char *);
 void doboottypemenu(void);
-int atoi(const char *);
 
 #endif /* !_BOOTMENU_H */



Home | Main Index | Thread Index | Old Index