summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenis Vlasenko2006-11-27 14:43:21 +0000
committerDenis Vlasenko2006-11-27 14:43:21 +0000
commitd686a045c8134d3a42fa5cc6b2e09118e08d603f (patch)
tree38f509fc9556f68f758c77b06b480cc33b2725eb /coreutils
parent8a0a83d503a7971895254efa9e79cf15ba1850d4 (diff)
downloadbusybox-d686a045c8134d3a42fa5cc6b2e09118e08d603f.zip
busybox-d686a045c8134d3a42fa5cc6b2e09118e08d603f.tar.gz
safe_strtoXX interface proved to be a bit unconvenient.
Remove it, introduce saner bb_strtoXX. Saved ~350 bytes.
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/expr.c2
-rw-r--r--coreutils/printf.c31
2 files changed, 26 insertions, 7 deletions
diff --git a/coreutils/expr.c b/coreutils/expr.c
index 854a657..1914734 100644
--- a/coreutils/expr.c
+++ b/coreutils/expr.c
@@ -49,6 +49,8 @@ typedef long arith_t;
#define STRTOL(s, e, b) strtol(s, e, b)
#endif
+/* TODO: use bb_strtol[l]? It's easier to check for errors... */
+
/* A value is.... */
struct valinfo {
TYPE type; /* Which kind. */
diff --git a/coreutils/printf.c b/coreutils/printf.c
index 1511034..0e81835 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -44,38 +44,55 @@ static int print_formatted(char *format, int argc, char **argv);
static void print_direc(char *start, size_t length,
int field_width, int precision, char *argument);
-typedef int (*converter)(char *arg, void *result);
+typedef void (*converter)(char *arg, void *result);
static void multiconvert(char *arg, void *result, converter convert)
{
char s[16];
if (*arg == '"' || *arg == '\'') {
- sprintf(s, "%d", (unsigned)arg[1]);
+ sprintf(s, "%d", (unsigned char)arg[1]);
arg = s;
}
- if (convert(arg, result))
+ convert(arg, result);
+ if (errno) /* Huh, looks strange... bug? */
fputs(arg, stderr);
}
+static void conv_strtoul(char *arg, void *result)
+{
+ *(unsigned long*)result = bb_strtoul(arg, NULL, 10);
+}
+static void conv_strtol(char *arg, void *result)
+{
+ *(long*)result = bb_strtol(arg, NULL, 10);
+}
+static void conv_strtod(char *arg, void *result)
+{
+ char *end;
+ /* Well, this one allows leading whitespace... so what */
+ /* What I like much less is that "-" is accepted too! :( */
+ *(double*)result = strtod(arg, &end);
+ if (end[0]) errno = ERANGE;
+}
+
static unsigned long my_xstrtoul(char *arg)
{
unsigned long result;
-
- multiconvert(arg, &result, (converter)safe_strtoul);
+ multiconvert(arg, &result, conv_strtoul);
return result;
}
static long my_xstrtol(char *arg)
{
long result;
- multiconvert(arg, &result, (converter)safe_strtol);
+ multiconvert(arg, &result, conv_strtol);
return result;
}
static double my_xstrtod(char *arg)
{
double result;
- multiconvert(arg, &result, (converter)safe_strtod);
+ multiconvert(arg, &result, conv_strtod);
return result;
}