7cb996b60c0dde830e1930baac6c46866150d57e
[~emulex/for-vlad/old/compat.git] / compat / compat-2.6.25.c
1 /*
2  * Copyright 2007-2010  Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Compatibility file for Linux wireless for kernels 2.6.25.
9  */
10
11 #include <linux/miscdevice.h>
12
13 /**
14  * The following things are out of ./lib/vsprintf.c
15  * The new iwlwifi driver is using them.
16  */
17
18 /**
19  * strict_strtoul - convert a string to an unsigned long strictly
20  * @cp: The string to be converted
21  * @base: The number base to use
22  * @res: The converted result value
23  *
24  * strict_strtoul converts a string to an unsigned long only if the
25  * string is really an unsigned long string, any string containing
26  * any invalid char at the tail will be rejected and -EINVAL is returned,
27  * only a newline char at the tail is acceptible because people generally
28  * change a module parameter in the following way:
29  *
30  *      echo 1024 > /sys/module/e1000/parameters/copybreak
31  *
32  * echo will append a newline to the tail.
33  *
34  * It returns 0 if conversion is successful and *res is set to the converted
35  * value, otherwise it returns -EINVAL and *res is set to 0.
36  *
37  * simple_strtoul just ignores the successive invalid characters and
38  * return the converted value of prefix part of the string.
39  */
40 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
41
42 /**
43  * strict_strtol - convert a string to a long strictly
44  * @cp: The string to be converted
45  * @base: The number base to use
46  * @res: The converted result value
47  *
48  * strict_strtol is similiar to strict_strtoul, but it allows the first
49  * character of a string is '-'.
50  *
51  * It returns 0 if conversion is successful and *res is set to the converted
52  * value, otherwise it returns -EINVAL and *res is set to 0.
53  */
54 int strict_strtol(const char *cp, unsigned int base, long *res);
55
56 #define define_strict_strtoux(type, valtype)                            \
57 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
58 {                                                                       \
59         char *tail;                                                     \
60         valtype val;                                                    \
61         size_t len;                                                     \
62                                                                         \
63         *res = 0;                                                       \
64         len = strlen(cp);                                               \
65         if (len == 0)                                                   \
66                 return -EINVAL;                                         \
67                                                                         \
68         val = simple_strtou##type(cp, &tail, base);                     \
69         if ((*tail == '\0') ||                                          \
70                 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
71                 *res = val;                                             \
72                 return 0;                                               \
73         }                                                               \
74                                                                         \
75         return -EINVAL;                                                 \
76 }                                                                       \
77
78 #define define_strict_strtox(type, valtype)                             \
79 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
80 {                                                                       \
81         int ret;                                                        \
82         if (*cp == '-') {                                               \
83                 ret = strict_strtou##type(cp+1, base, res);             \
84                 if (!ret)                                               \
85                         *res = -(*res);                                 \
86         } else                                                          \
87                 ret = strict_strtou##type(cp, base, res);               \
88                                                                         \
89         return ret;                                                     \
90 }                                                                       \
91
92 define_strict_strtoux(l, unsigned long)
93 define_strict_strtox(l, long)
94
95 EXPORT_SYMBOL(strict_strtoul);
96 EXPORT_SYMBOL(strict_strtol);
97