Commit 28210862 authored by Adam Wujek's avatar Adam Wujek

lib: remove atoi

Use fromdec in atoi. Not the way round, because fromdec can return a pointer
to non recognized character (atoi cannot).
Signed-off-by: 's avatarAdam Wujek <adam.wujek@creotech.pl>
parent ffa3f294
/*
* This work is part of the White Rabbit project
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* Released according to the GNU GPL, version 2 or any later version.
*/
#include <stdlib.h>
/*
* This is a minimal atoi, that doesn't call strtol. Since we are only
* calling atoi, it saves XXXX bytes of library code
*/
int atoi(const char *s)
{
int sign = 1, res = 0;
/* as (mis)designed in atoi, we make no error check */
if (*s == '-') {
sign = -1;
s++;
}
while (*s >= '0' && *s <= '9')
res = res * 10 + *s++ - '0';
return res * sign;
}
#ifdef TEST_ATOI
/*
* You can try this:
*
make atoi CFLAGS="-DTEST_ATOI -Wall" && \
./atoi foo 0 45 -45 9876543210 -9345.2 97.5 666devils
*
*/
#include <stdio.h>
/* just to be sure I call mine and not a #define somewhere in the headers */
int __local_atoi (const char *s) __attribute__((alias("atoi")));
int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
printf("%i\n", __local_atoi(argv[i]));
return 0;
}
#endif /* TEST_ATOI */
......@@ -281,7 +281,7 @@ static int cmd_ltest(const char *args[])
int v = 0, v1 = 0;
if (args[1]) {
fromdec(args[1], &v1); /* ms */
v1 = atoi(args[1]); /* ms */
}
if (args[0]) {
if (HAS_SYSLOG && !strcmp(args[0], "verbose"))
......@@ -289,9 +289,9 @@ static int cmd_ltest(const char *args[])
else if (HAS_SYSLOG && !strcmp(args[0], "quiet"))
lat_verbose = 0;
else if (!strcmp(args[0], "fake"))
fromdec(args[1], &ltest_fake_delay_ns);
ltest_fake_delay_ns = atoi(args[1]);
else {
fromdec(args[0], &v);
v = atoi(args[0]);
latency_period_ms = v * 1000 + v1;
lastt = 0; /* reset, so it fires immediately */
}
......
obj-y += lib/util.o lib/wrc-tasks.o
obj-$(CONFIG_LM32) += \
lib/atoi.o \
lib/assert.o \
lib/usleep.o \
lib/event.o
......
......@@ -210,3 +210,17 @@ const char *fromdec(const char *dec, int *v)
*v = o * sign;
return dec;
}
/*
* This is a minimal atoi, that doesn't call strtol. Since we are only
* calling atoi, it saves XXXX bytes of library code
* Use fromdec in atoi. Not the way round, because fromdec can return a pointer
* to non recognized character (atoi cannot).
*/
int atoi(const char *s)
{
int res;
fromdec(s, &res);
return res;
}
......@@ -49,8 +49,8 @@ static int cmd_delays(const char *args[])
return 0;
}
if (args[1]) {
fromdec(args[0], &tx);
fromdec(args[1], &rx);
tx = atoi(args[0]);
rx = atoi(args[1]);
sfp_deltaTx = tx;
sfp_deltaRx = rx;
/* Change the active value too (add bislide here) */
......
......@@ -19,7 +19,7 @@ static int cmd_refresh(const char *args[])
int sec;
if (args[0] && !args[1]) {
fromdec(args[0], &sec);
sec = atoi(args[0]);
}
else {
pp_printf("Usage: refresh <seconds>\n");
......
......@@ -14,7 +14,7 @@ static int cmd_sleep(const char *args[])
int sec = 1;
if (args[0])
fromdec(args[0], &sec);
sec = atoi(args[0]);
while (sec--)
usleep(1000 * 1000);
return 0;
......
......@@ -19,7 +19,7 @@ static int cmd_vlan(const char *args[])
if (!args[0] || !strcasecmp(args[0], "get")) {
/* nothing... */
} else if (!strcasecmp(args[0], "set") && args[1]) {
fromdec(args[1], &i);
i = atoi(args[1]);
if (i < 1 || i > 4095) {
pp_printf("%i (\"%s\") out of range\n", i, args[1]);
return -EINVAL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment