This source file includes following definitions.
- simple_strtoul
- skip_atoi
- number
- vsprintf
- sprintf
1
2
3
4
5
6
7
8
9
10
11
12 #include <stdarg.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16
17 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
18 {
19 unsigned long result = 0,value;
20
21 if (!base) {
22 base = 10;
23 if (*cp == '0') {
24 base = 8;
25 cp++;
26 if ((*cp == 'x') && isxdigit(cp[1])) {
27 cp++;
28 base = 16;
29 }
30 }
31 }
32 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
33 ? toupper(*cp) : *cp)-'A'+10) < base) {
34 result = result*base + value;
35 cp++;
36 }
37 if (endp)
38 *endp = (char *)cp;
39 return result;
40 }
41
42
43 #define is_digit(c) ((c) >= '0' && (c) <= '9')
44
45 static int skip_atoi(const char **s)
46 {
47 int i=0;
48
49 while (is_digit(**s))
50 i = i*10 + *((*s)++) - '0';
51 return i;
52 }
53
54 #define ZEROPAD 1
55 #define SIGN 2
56 #define PLUS 4
57 #define SPACE 8
58 #define LEFT 16
59 #define SPECIAL 32
60 #define SMALL 64
61
62 #define do_div(n,base) ({ \
63 int __res; \
64 __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
65 __res; })
66
67 static char * number(char * str, int num, int base, int size, int precision
68 ,int type)
69 {
70 char c,sign,tmp[36];
71 const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
72 int i;
73
74 if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
75 if (type&LEFT) type &= ~ZEROPAD;
76 if (base<2 || base>36)
77 return 0;
78 c = (type & ZEROPAD) ? '0' : ' ' ;
79 if (type&SIGN && num<0) {
80 sign='-';
81 num = -num;
82 } else
83 sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
84 if (sign) size--;
85 if (type&SPECIAL)
86 if (base==16) size -= 2;
87 else if (base==8) size--;
88 i=0;
89 if (num==0)
90 tmp[i++]='0';
91 else while (num!=0)
92 tmp[i++]=digits[do_div(num,base)];
93 if (i>precision) precision=i;
94 size -= precision;
95 if (!(type&(ZEROPAD+LEFT)))
96 while(size-->0)
97 *str++ = ' ';
98 if (sign)
99 *str++ = sign;
100 if (type&SPECIAL)
101 if (base==8)
102 *str++ = '0';
103 else if (base==16) {
104 *str++ = '0';
105 *str++ = digits[33];
106 }
107 if (!(type&LEFT))
108 while(size-->0)
109 *str++ = c;
110 while(i<precision--)
111 *str++ = '0';
112 while(i-->0)
113 *str++ = tmp[i];
114 while(size-->0)
115 *str++ = ' ';
116 return str;
117 }
118
119 int vsprintf(char *buf, const char *fmt, va_list args)
120 {
121 int len;
122 int i;
123 char * str;
124 char *s;
125 int *ip;
126
127 int flags;
128
129 int field_width;
130 int precision;
131
132 int qualifier;
133
134 for (str=buf ; *fmt ; ++fmt) {
135 if (*fmt != '%') {
136 *str++ = *fmt;
137 continue;
138 }
139
140
141 flags = 0;
142 repeat:
143 ++fmt;
144 switch (*fmt) {
145 case '-': flags |= LEFT; goto repeat;
146 case '+': flags |= PLUS; goto repeat;
147 case ' ': flags |= SPACE; goto repeat;
148 case '#': flags |= SPECIAL; goto repeat;
149 case '0': flags |= ZEROPAD; goto repeat;
150 }
151
152
153 field_width = -1;
154 if (is_digit(*fmt))
155 field_width = skip_atoi(&fmt);
156 else if (*fmt == '*') {
157
158 field_width = va_arg(args, int);
159 if (field_width < 0) {
160 field_width = -field_width;
161 flags |= LEFT;
162 }
163 }
164
165
166 precision = -1;
167 if (*fmt == '.') {
168 ++fmt;
169 if (is_digit(*fmt))
170 precision = skip_atoi(&fmt);
171 else if (*fmt == '*') {
172
173 precision = va_arg(args, int);
174 }
175 if (precision < 0)
176 precision = 0;
177 }
178
179
180 qualifier = -1;
181 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
182 qualifier = *fmt;
183 ++fmt;
184 }
185
186 switch (*fmt) {
187 case 'c':
188 if (!(flags & LEFT))
189 while (--field_width > 0)
190 *str++ = ' ';
191 *str++ = (unsigned char) va_arg(args, int);
192 while (--field_width > 0)
193 *str++ = ' ';
194 break;
195
196 case 's':
197 s = va_arg(args, char *);
198 if (!s)
199 s = "<NULL>";
200 len = strlen(s);
201 if (precision < 0)
202 precision = len;
203 else if (len > precision)
204 len = precision;
205
206 if (!(flags & LEFT))
207 while (len < field_width--)
208 *str++ = ' ';
209 for (i = 0; i < len; ++i)
210 *str++ = *s++;
211 while (len < field_width--)
212 *str++ = ' ';
213 break;
214
215 case 'o':
216 str = number(str, va_arg(args, unsigned long), 8,
217 field_width, precision, flags);
218 break;
219
220 case 'p':
221 if (field_width == -1) {
222 field_width = 8;
223 flags |= ZEROPAD;
224 }
225 str = number(str,
226 (unsigned long) va_arg(args, void *), 16,
227 field_width, precision, flags);
228 break;
229
230 case 'x':
231 flags |= SMALL;
232 case 'X':
233 str = number(str, va_arg(args, unsigned long), 16,
234 field_width, precision, flags);
235 break;
236
237 case 'd':
238 case 'i':
239 flags |= SIGN;
240 case 'u':
241 str = number(str, va_arg(args, unsigned long), 10,
242 field_width, precision, flags);
243 break;
244
245 case 'n':
246 ip = va_arg(args, int *);
247 *ip = (str - buf);
248 break;
249
250 default:
251 if (*fmt != '%')
252 *str++ = '%';
253 if (*fmt)
254 *str++ = *fmt;
255 else
256 --fmt;
257 break;
258 }
259 }
260 *str = '\0';
261 return str-buf;
262 }
263
264 int sprintf(char * buf, const char *fmt, ...)
265 {
266 va_list args;
267 int i;
268
269 va_start(args, fmt);
270 i=vsprintf(buf,fmt,args);
271 va_end(args);
272 return i;
273 }
274