lsmod: repair indentation
[oweals/busybox.git] / libbb / xgetularg.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * xgetularg* implementations for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <assert.h>
16 #include "libbb.h"
17
18 #ifdef L_xgetularg_bnd_sfx
19 unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
20                                                                    unsigned long lower,
21                                                                    unsigned long upper,
22                                                                    const struct suffix_mult *suffixes)
23 {
24         unsigned long r;
25         int old_errno;
26         char *e;
27
28         assert(arg);
29
30         /* Disallow '-' and any leading whitespace.  Speed isn't critical here
31          * since we're parsing commandline args.  So make sure we get the
32          * actual isspace function rather than a larger macro implementaion. */
33         if ((*arg == '-') || (isspace)(*arg)) {
34                 bb_show_usage();
35         }
36
37         /* Since this is a lib function, we're not allowed to reset errno to 0.
38          * Doing so could break an app that is deferring checking of errno.
39          * So, save the old value so that we can restore it if successful. */
40         old_errno = errno;
41         errno = 0;
42         r = strtoul(arg, &e, base);
43         /* Do the initial validity check.  Note: The standards do not
44          * guarantee that errno is set if no digits were found.  So we
45          * must test for this explicitly. */
46         if (errno || (arg == e)) {      /* error or no digits */
47                 bb_show_usage();
48         }
49         errno = old_errno;      /* Ok.  So restore errno. */
50
51         /* Do optional suffix parsing.  Allow 'empty' suffix tables.
52          * Note that we also all nul suffixes with associated multipliers,
53          * to allow for scaling of the arg by some default multiplier. */
54
55         if (suffixes) {
56                 while (suffixes->suffix) {
57                         if (strcmp(suffixes->suffix, e) == 0) {
58                                 if (ULONG_MAX / suffixes->mult < r) {   /* Overflow! */
59                                         bb_show_usage();
60                                 }
61                                 ++e;
62                                 r *= suffixes->mult;
63                                 break;
64                         }
65                         ++suffixes;
66                 }
67         }
68
69         /* Finally, check for illegal trailing chars and range limits. */
70         /* Note: although we allow leading space (via stroul), trailing space
71          * is an error.  It would be easy enough to allow though if desired. */
72         if (*e || (r < lower) || (r > upper)) {
73                 bb_show_usage();
74         }
75
76         return r;
77 }
78 #endif
79
80 #ifdef L_xgetlarg_bnd_sfx
81 long bb_xgetlarg_bnd_sfx(const char *arg, int base,
82                                                  long lower,
83                                                  long upper,
84                                                  const struct suffix_mult *suffixes)
85 {
86         unsigned long u = LONG_MAX;
87         long r;
88         const char *p = arg;
89
90         if ((*p == '-') && (p[1] != '+')) {
91                 ++p;
92                 ++u;    /* two's complement */
93         }
94
95         r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
96
97         if (*arg == '-') {
98                 r = -r;
99         }
100
101         if ((r < lower) || (r > upper)) {
102                 bb_show_usage();
103         }
104
105         return r;
106 }
107 #endif
108
109 #ifdef L_getlarg10_sfx
110 long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
111 {
112         return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
113 }
114 #endif
115
116 #ifdef L_xgetularg_bnd
117 unsigned long bb_xgetularg_bnd(const char *arg, int base,
118                                                            unsigned long lower,
119                                                            unsigned long upper)
120 {
121         return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
122 }
123 #endif
124
125 #ifdef L_xgetularg10_bnd
126 unsigned long bb_xgetularg10_bnd(const char *arg,
127                                                                  unsigned long lower,
128                                                                  unsigned long upper)
129 {
130         return bb_xgetularg_bnd(arg, 10, lower, upper);
131 }
132 #endif
133
134 #ifdef L_xgetularg10
135 unsigned long bb_xgetularg10(const char *arg)
136 {
137         return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
138 }
139 #endif