allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / lib / vsprintf.c
blobf9cccce75f7db50cf2f0ebd61d14295702376db2
1 /*
2 * linux/lib/vsprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
12 /*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
19 #include <stdarg.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
28 #include <asm/page.h> /* for PAGE_SIZE */
29 #include <asm/div64.h>
31 /* Works only for digits and letters, but small and fast */
32 #define TOLOWER(x) ((x) | 0x20)
34 /**
35 * simple_strtoul - convert a string to an unsigned long
36 * @cp: The start of the string
37 * @endp: A pointer to the end of the parsed string will be placed here
38 * @base: The number base to use
40 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
42 unsigned long result = 0,value;
44 if (!base) {
45 base = 10;
46 if (*cp == '0') {
47 base = 8;
48 cp++;
49 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
50 cp++;
51 base = 16;
54 } else if (base == 16) {
55 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
56 cp += 2;
58 while (isxdigit(*cp) &&
59 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
60 result = result*base + value;
61 cp++;
63 if (endp)
64 *endp = (char *)cp;
65 return result;
68 EXPORT_SYMBOL(simple_strtoul);
70 /**
71 * simple_strtol - convert a string to a signed long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
76 long simple_strtol(const char *cp,char **endp,unsigned int base)
78 if(*cp=='-')
79 return -simple_strtoul(cp+1,endp,base);
80 return simple_strtoul(cp,endp,base);
83 EXPORT_SYMBOL(simple_strtol);
85 /**
86 * simple_strtoull - convert a string to an unsigned long long
87 * @cp: The start of the string
88 * @endp: A pointer to the end of the parsed string will be placed here
89 * @base: The number base to use
91 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
93 unsigned long long result = 0,value;
95 if (!base) {
96 base = 10;
97 if (*cp == '0') {
98 base = 8;
99 cp++;
100 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
101 cp++;
102 base = 16;
105 } else if (base == 16) {
106 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
107 cp += 2;
109 while (isxdigit(*cp)
110 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
111 result = result*base + value;
112 cp++;
114 if (endp)
115 *endp = (char *)cp;
116 return result;
119 EXPORT_SYMBOL(simple_strtoull);
122 * simple_strtoll - convert a string to a signed long long
123 * @cp: The start of the string
124 * @endp: A pointer to the end of the parsed string will be placed here
125 * @base: The number base to use
127 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
129 if(*cp=='-')
130 return -simple_strtoull(cp+1,endp,base);
131 return simple_strtoull(cp,endp,base);
134 static int skip_atoi(const char **s)
136 int i=0;
138 while (isdigit(**s))
139 i = i*10 + *((*s)++) - '0';
140 return i;
143 /* Decimal conversion is by far the most typical, and is used
144 * for /proc and /sys data. This directly impacts e.g. top performance
145 * with many processes running. We optimize it for speed
146 * using code from
147 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
148 * (with permission from the author, Douglas W. Jones). */
150 /* Formats correctly any integer in [0,99999].
151 * Outputs from one to five digits depending on input.
152 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
153 static char* put_dec_trunc(char *buf, unsigned q)
155 unsigned d3, d2, d1, d0;
156 d1 = (q>>4) & 0xf;
157 d2 = (q>>8) & 0xf;
158 d3 = (q>>12);
160 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
161 q = (d0 * 0xcd) >> 11;
162 d0 = d0 - 10*q;
163 *buf++ = d0 + '0'; /* least significant digit */
164 d1 = q + 9*d3 + 5*d2 + d1;
165 if (d1 != 0) {
166 q = (d1 * 0xcd) >> 11;
167 d1 = d1 - 10*q;
168 *buf++ = d1 + '0'; /* next digit */
170 d2 = q + 2*d2;
171 if ((d2 != 0) || (d3 != 0)) {
172 q = (d2 * 0xd) >> 7;
173 d2 = d2 - 10*q;
174 *buf++ = d2 + '0'; /* next digit */
176 d3 = q + 4*d3;
177 if (d3 != 0) {
178 q = (d3 * 0xcd) >> 11;
179 d3 = d3 - 10*q;
180 *buf++ = d3 + '0'; /* next digit */
181 if (q != 0)
182 *buf++ = q + '0'; /* most sign. digit */
186 return buf;
188 /* Same with if's removed. Always emits five digits */
189 static char* put_dec_full(char *buf, unsigned q)
191 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
192 /* but anyway, gcc produces better code with full-sized ints */
193 unsigned d3, d2, d1, d0;
194 d1 = (q>>4) & 0xf;
195 d2 = (q>>8) & 0xf;
196 d3 = (q>>12);
198 /* Possible ways to approx. divide by 10 */
199 /* gcc -O2 replaces multiply with shifts and adds */
200 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
201 // (x * 0x67) >> 10: 1100111
202 // (x * 0x34) >> 9: 110100 - same
203 // (x * 0x1a) >> 8: 11010 - same
204 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
206 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
207 q = (d0 * 0xcd) >> 11;
208 d0 = d0 - 10*q;
209 *buf++ = d0 + '0';
210 d1 = q + 9*d3 + 5*d2 + d1;
211 q = (d1 * 0xcd) >> 11;
212 d1 = d1 - 10*q;
213 *buf++ = d1 + '0';
215 d2 = q + 2*d2;
216 q = (d2 * 0xd) >> 7;
217 d2 = d2 - 10*q;
218 *buf++ = d2 + '0';
220 d3 = q + 4*d3;
221 q = (d3 * 0xcd) >> 11; /* - shorter code */
222 /* q = (d3 * 0x67) >> 10; - would also work */
223 d3 = d3 - 10*q;
224 *buf++ = d3 + '0';
225 *buf++ = q + '0';
226 return buf;
228 /* No inlining helps gcc to use registers better */
229 static noinline char* put_dec(char *buf, unsigned long long num)
231 while (1) {
232 unsigned rem;
233 if (num < 100000)
234 return put_dec_trunc(buf, num);
235 rem = do_div(num, 100000);
236 buf = put_dec_full(buf, rem);
240 #define ZEROPAD 1 /* pad with zero */
241 #define SIGN 2 /* unsigned/signed long */
242 #define PLUS 4 /* show plus */
243 #define SPACE 8 /* space if plus */
244 #define LEFT 16 /* left justified */
245 #define SMALL 32 /* Must be 32 == 0x20 */
246 #define SPECIAL 64 /* 0x */
248 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
250 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
251 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
253 char tmp[66];
254 char sign;
255 char locase;
256 int need_pfx = ((type & SPECIAL) && base != 10);
257 int i;
259 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
260 * produces same digits or (maybe lowercased) letters */
261 locase = (type & SMALL);
262 if (type & LEFT)
263 type &= ~ZEROPAD;
264 sign = 0;
265 if (type & SIGN) {
266 if ((signed long long) num < 0) {
267 sign = '-';
268 num = - (signed long long) num;
269 size--;
270 } else if (type & PLUS) {
271 sign = '+';
272 size--;
273 } else if (type & SPACE) {
274 sign = ' ';
275 size--;
278 if (need_pfx) {
279 size--;
280 if (base == 16)
281 size--;
284 /* generate full string in tmp[], in reverse order */
285 i = 0;
286 if (num == 0)
287 tmp[i++] = '0';
288 /* Generic code, for any base:
289 else do {
290 tmp[i++] = (digits[do_div(num,base)] | locase);
291 } while (num != 0);
293 else if (base != 10) { /* 8 or 16 */
294 int mask = base - 1;
295 int shift = 3;
296 if (base == 16) shift = 4;
297 do {
298 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
299 num >>= shift;
300 } while (num);
301 } else { /* base 10 */
302 i = put_dec(tmp, num) - tmp;
305 /* printing 100 using %2d gives "100", not "00" */
306 if (i > precision)
307 precision = i;
308 /* leading space padding */
309 size -= precision;
310 if (!(type & (ZEROPAD+LEFT))) {
311 while(--size >= 0) {
312 if (buf < end)
313 *buf = ' ';
314 ++buf;
317 /* sign */
318 if (sign) {
319 if (buf < end)
320 *buf = sign;
321 ++buf;
323 /* "0x" / "0" prefix */
324 if (need_pfx) {
325 if (buf < end)
326 *buf = '0';
327 ++buf;
328 if (base == 16) {
329 if (buf < end)
330 *buf = ('X' | locase);
331 ++buf;
334 /* zero or space padding */
335 if (!(type & LEFT)) {
336 char c = (type & ZEROPAD) ? '0' : ' ';
337 while (--size >= 0) {
338 if (buf < end)
339 *buf = c;
340 ++buf;
343 /* hmm even more zero padding? */
344 while (i <= --precision) {
345 if (buf < end)
346 *buf = '0';
347 ++buf;
349 /* actual digits of result */
350 while (--i >= 0) {
351 if (buf < end)
352 *buf = tmp[i];
353 ++buf;
355 /* trailing space padding */
356 while (--size >= 0) {
357 if (buf < end)
358 *buf = ' ';
359 ++buf;
361 return buf;
364 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
366 int len, i;
368 if ((unsigned long)s < PAGE_SIZE)
369 s = "<NULL>";
371 len = strnlen(s, precision);
373 if (!(flags & LEFT)) {
374 while (len < field_width--) {
375 if (buf < end)
376 *buf = ' ';
377 ++buf;
380 for (i = 0; i < len; ++i) {
381 if (buf < end)
382 *buf = *s;
383 ++buf; ++s;
385 while (len < field_width--) {
386 if (buf < end)
387 *buf = ' ';
388 ++buf;
390 return buf;
393 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
394 int precision, int flags)
396 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
397 char *p = mac_addr;
398 int i;
400 for (i = 0; i < 6; i++) {
401 p = pack_hex_byte(p, addr[i]);
402 if (!(flags & SPECIAL) && i != 5)
403 *p++ = ':';
405 *p = '\0';
407 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
411 * Show a '%p' thing. A kernel extension is that the '%p' is followed
412 * by an extra set of alphanumeric characters that are extended format
413 * specifiers.
415 * - 'M' For a 6-byte MAC address, it prints the address in the
416 * usual colon-separated hex notation
418 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
420 switch (*fmt) {
421 case 'M':
422 return mac_address_string(buf, end, ptr, field_width, precision, flags);
424 flags |= SMALL;
425 if (field_width == -1) {
426 field_width = 2*sizeof(void *);
427 flags |= ZEROPAD;
429 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
433 * vsnprintf - Format a string and place it in a buffer
434 * @buf: The buffer to place the result into
435 * @size: The size of the buffer, including the trailing null space
436 * @fmt: The format string to use
437 * @args: Arguments for the format string
439 * The return value is the number of characters which would
440 * be generated for the given input, excluding the trailing
441 * '\0', as per ISO C99. If you want to have the exact
442 * number of characters written into @buf as return value
443 * (not including the trailing '\0'), use vscnprintf(). If the
444 * return is greater than or equal to @size, the resulting
445 * string is truncated.
447 * Call this function if you are already dealing with a va_list.
448 * You probably want snprintf() instead.
450 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
452 unsigned long long num;
453 int base;
454 char *str, *end, c;
456 int flags; /* flags to number() */
458 int field_width; /* width of output field */
459 int precision; /* min. # of digits for integers; max
460 number of chars for from string */
461 int qualifier; /* 'h', 'l', or 'L' for integer fields */
462 /* 'z' support added 23/7/1999 S.H. */
463 /* 'z' changed to 'Z' --davidm 1/25/99 */
464 /* 't' added for ptrdiff_t */
466 /* Reject out-of-range values early. Large positive sizes are
467 used for unknown buffer sizes. */
468 if (unlikely((int) size < 0)) {
469 /* There can be only one.. */
470 static char warn = 1;
471 WARN_ON(warn);
472 warn = 0;
473 return 0;
476 str = buf;
477 end = buf + size;
479 /* Make sure end is always >= buf */
480 if (end < buf) {
481 end = ((void *)-1);
482 size = end - buf;
485 for (; *fmt ; ++fmt) {
486 if (*fmt != '%') {
487 if (str < end)
488 *str = *fmt;
489 ++str;
490 continue;
493 /* process flags */
494 flags = 0;
495 repeat:
496 ++fmt; /* this also skips first '%' */
497 switch (*fmt) {
498 case '-': flags |= LEFT; goto repeat;
499 case '+': flags |= PLUS; goto repeat;
500 case ' ': flags |= SPACE; goto repeat;
501 case '#': flags |= SPECIAL; goto repeat;
502 case '0': flags |= ZEROPAD; goto repeat;
505 /* get field width */
506 field_width = -1;
507 if (isdigit(*fmt))
508 field_width = skip_atoi(&fmt);
509 else if (*fmt == '*') {
510 ++fmt;
511 /* it's the next argument */
512 field_width = va_arg(args, int);
513 if (field_width < 0) {
514 field_width = -field_width;
515 flags |= LEFT;
519 /* get the precision */
520 precision = -1;
521 if (*fmt == '.') {
522 ++fmt;
523 if (isdigit(*fmt))
524 precision = skip_atoi(&fmt);
525 else if (*fmt == '*') {
526 ++fmt;
527 /* it's the next argument */
528 precision = va_arg(args, int);
530 if (precision < 0)
531 precision = 0;
534 /* get the conversion qualifier */
535 qualifier = -1;
536 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
537 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
538 qualifier = *fmt;
539 ++fmt;
540 if (qualifier == 'l' && *fmt == 'l') {
541 qualifier = 'L';
542 ++fmt;
546 /* default base */
547 base = 10;
549 switch (*fmt) {
550 case 'c':
551 if (!(flags & LEFT)) {
552 while (--field_width > 0) {
553 if (str < end)
554 *str = ' ';
555 ++str;
558 c = (unsigned char) va_arg(args, int);
559 if (str < end)
560 *str = c;
561 ++str;
562 while (--field_width > 0) {
563 if (str < end)
564 *str = ' ';
565 ++str;
567 continue;
569 case 's':
570 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
571 continue;
573 case 'p':
574 str = pointer(fmt+1, str, end,
575 va_arg(args, void *),
576 field_width, precision, flags);
577 /* Skip all alphanumeric pointer suffixes */
578 while (isalnum(fmt[1]))
579 fmt++;
580 continue;
582 case 'n':
583 /* FIXME:
584 * What does C99 say about the overflow case here? */
585 if (qualifier == 'l') {
586 long * ip = va_arg(args, long *);
587 *ip = (str - buf);
588 } else if (qualifier == 'Z' || qualifier == 'z') {
589 size_t * ip = va_arg(args, size_t *);
590 *ip = (str - buf);
591 } else {
592 int * ip = va_arg(args, int *);
593 *ip = (str - buf);
595 continue;
597 case '%':
598 if (str < end)
599 *str = '%';
600 ++str;
601 continue;
603 /* integer number formats - set up the flags and "break" */
604 case 'o':
605 base = 8;
606 break;
608 case 'x':
609 flags |= SMALL;
610 case 'X':
611 base = 16;
612 break;
614 case 'd':
615 case 'i':
616 flags |= SIGN;
617 case 'u':
618 break;
620 default:
621 if (str < end)
622 *str = '%';
623 ++str;
624 if (*fmt) {
625 if (str < end)
626 *str = *fmt;
627 ++str;
628 } else {
629 --fmt;
631 continue;
633 if (qualifier == 'L')
634 num = va_arg(args, long long);
635 else if (qualifier == 'l') {
636 num = va_arg(args, unsigned long);
637 if (flags & SIGN)
638 num = (signed long) num;
639 } else if (qualifier == 'Z' || qualifier == 'z') {
640 num = va_arg(args, size_t);
641 } else if (qualifier == 't') {
642 num = va_arg(args, ptrdiff_t);
643 } else if (qualifier == 'h') {
644 num = (unsigned short) va_arg(args, int);
645 if (flags & SIGN)
646 num = (signed short) num;
647 } else {
648 num = va_arg(args, unsigned int);
649 if (flags & SIGN)
650 num = (signed int) num;
652 str = number(str, end, num, base,
653 field_width, precision, flags);
655 if (size > 0) {
656 if (str < end)
657 *str = '\0';
658 else
659 end[-1] = '\0';
661 /* the trailing null byte doesn't count towards the total */
662 return str-buf;
665 EXPORT_SYMBOL(vsnprintf);
668 * vscnprintf - Format a string and place it in a buffer
669 * @buf: The buffer to place the result into
670 * @size: The size of the buffer, including the trailing null space
671 * @fmt: The format string to use
672 * @args: Arguments for the format string
674 * The return value is the number of characters which have been written into
675 * the @buf not including the trailing '\0'. If @size is <= 0 the function
676 * returns 0.
678 * Call this function if you are already dealing with a va_list.
679 * You probably want scnprintf() instead.
681 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
683 int i;
685 i=vsnprintf(buf,size,fmt,args);
686 return (i >= size) ? (size - 1) : i;
689 EXPORT_SYMBOL(vscnprintf);
692 * snprintf - Format a string and place it in a buffer
693 * @buf: The buffer to place the result into
694 * @size: The size of the buffer, including the trailing null space
695 * @fmt: The format string to use
696 * @...: Arguments for the format string
698 * The return value is the number of characters which would be
699 * generated for the given input, excluding the trailing null,
700 * as per ISO C99. If the return is greater than or equal to
701 * @size, the resulting string is truncated.
703 int snprintf(char * buf, size_t size, const char *fmt, ...)
705 va_list args;
706 int i;
708 va_start(args, fmt);
709 i=vsnprintf(buf,size,fmt,args);
710 va_end(args);
711 return i;
714 EXPORT_SYMBOL(snprintf);
717 * scnprintf - Format a string and place it in a buffer
718 * @buf: The buffer to place the result into
719 * @size: The size of the buffer, including the trailing null space
720 * @fmt: The format string to use
721 * @...: Arguments for the format string
723 * The return value is the number of characters written into @buf not including
724 * the trailing '\0'. If @size is <= 0 the function returns 0.
727 int scnprintf(char * buf, size_t size, const char *fmt, ...)
729 va_list args;
730 int i;
732 va_start(args, fmt);
733 i = vsnprintf(buf, size, fmt, args);
734 va_end(args);
735 return (i >= size) ? (size - 1) : i;
737 EXPORT_SYMBOL(scnprintf);
740 * vsprintf - Format a string and place it in a buffer
741 * @buf: The buffer to place the result into
742 * @fmt: The format string to use
743 * @args: Arguments for the format string
745 * The function returns the number of characters written
746 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
747 * buffer overflows.
749 * Call this function if you are already dealing with a va_list.
750 * You probably want sprintf() instead.
752 int vsprintf(char *buf, const char *fmt, va_list args)
754 return vsnprintf(buf, INT_MAX, fmt, args);
757 EXPORT_SYMBOL(vsprintf);
760 * sprintf - Format a string and place it in a buffer
761 * @buf: The buffer to place the result into
762 * @fmt: The format string to use
763 * @...: Arguments for the format string
765 * The function returns the number of characters written
766 * into @buf. Use snprintf() or scnprintf() in order to avoid
767 * buffer overflows.
769 int sprintf(char * buf, const char *fmt, ...)
771 va_list args;
772 int i;
774 va_start(args, fmt);
775 i=vsnprintf(buf, INT_MAX, fmt, args);
776 va_end(args);
777 return i;
780 EXPORT_SYMBOL(sprintf);
783 * vsscanf - Unformat a buffer into a list of arguments
784 * @buf: input buffer
785 * @fmt: format of buffer
786 * @args: arguments
788 int vsscanf(const char * buf, const char * fmt, va_list args)
790 const char *str = buf;
791 char *next;
792 char digit;
793 int num = 0;
794 int qualifier;
795 int base;
796 int field_width;
797 int is_sign = 0;
799 while(*fmt && *str) {
800 /* skip any white space in format */
801 /* white space in format matchs any amount of
802 * white space, including none, in the input.
804 if (isspace(*fmt)) {
805 while (isspace(*fmt))
806 ++fmt;
807 while (isspace(*str))
808 ++str;
811 /* anything that is not a conversion must match exactly */
812 if (*fmt != '%' && *fmt) {
813 if (*fmt++ != *str++)
814 break;
815 continue;
818 if (!*fmt)
819 break;
820 ++fmt;
822 /* skip this conversion.
823 * advance both strings to next white space
825 if (*fmt == '*') {
826 while (!isspace(*fmt) && *fmt != '%' && *fmt)
827 fmt++;
828 while (!isspace(*str) && *str)
829 str++;
830 continue;
833 /* get field width */
834 field_width = -1;
835 if (isdigit(*fmt))
836 field_width = skip_atoi(&fmt);
838 /* get conversion qualifier */
839 qualifier = -1;
840 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
841 *fmt == 'Z' || *fmt == 'z') {
842 qualifier = *fmt++;
843 if (unlikely(qualifier == *fmt)) {
844 if (qualifier == 'h') {
845 qualifier = 'H';
846 fmt++;
847 } else if (qualifier == 'l') {
848 qualifier = 'L';
849 fmt++;
853 base = 10;
854 is_sign = 0;
856 if (!*fmt || !*str)
857 break;
859 switch(*fmt++) {
860 case 'c':
862 char *s = (char *) va_arg(args,char*);
863 if (field_width == -1)
864 field_width = 1;
865 do {
866 *s++ = *str++;
867 } while (--field_width > 0 && *str);
868 num++;
870 continue;
871 case 's':
873 char *s = (char *) va_arg(args, char *);
874 if(field_width == -1)
875 field_width = INT_MAX;
876 /* first, skip leading white space in buffer */
877 while (isspace(*str))
878 str++;
880 /* now copy until next white space */
881 while (*str && !isspace(*str) && field_width--) {
882 *s++ = *str++;
884 *s = '\0';
885 num++;
887 continue;
888 case 'n':
889 /* return number of characters read so far */
891 int *i = (int *)va_arg(args,int*);
892 *i = str - buf;
894 continue;
895 case 'o':
896 base = 8;
897 break;
898 case 'x':
899 case 'X':
900 base = 16;
901 break;
902 case 'i':
903 base = 0;
904 case 'd':
905 is_sign = 1;
906 case 'u':
907 break;
908 case '%':
909 /* looking for '%' in str */
910 if (*str++ != '%')
911 return num;
912 continue;
913 default:
914 /* invalid format; stop here */
915 return num;
918 /* have some sort of integer conversion.
919 * first, skip white space in buffer.
921 while (isspace(*str))
922 str++;
924 digit = *str;
925 if (is_sign && digit == '-')
926 digit = *(str + 1);
928 if (!digit
929 || (base == 16 && !isxdigit(digit))
930 || (base == 10 && !isdigit(digit))
931 || (base == 8 && (!isdigit(digit) || digit > '7'))
932 || (base == 0 && !isdigit(digit)))
933 break;
935 switch(qualifier) {
936 case 'H': /* that's 'hh' in format */
937 if (is_sign) {
938 signed char *s = (signed char *) va_arg(args,signed char *);
939 *s = (signed char) simple_strtol(str,&next,base);
940 } else {
941 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
942 *s = (unsigned char) simple_strtoul(str, &next, base);
944 break;
945 case 'h':
946 if (is_sign) {
947 short *s = (short *) va_arg(args,short *);
948 *s = (short) simple_strtol(str,&next,base);
949 } else {
950 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
951 *s = (unsigned short) simple_strtoul(str, &next, base);
953 break;
954 case 'l':
955 if (is_sign) {
956 long *l = (long *) va_arg(args,long *);
957 *l = simple_strtol(str,&next,base);
958 } else {
959 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
960 *l = simple_strtoul(str,&next,base);
962 break;
963 case 'L':
964 if (is_sign) {
965 long long *l = (long long*) va_arg(args,long long *);
966 *l = simple_strtoll(str,&next,base);
967 } else {
968 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
969 *l = simple_strtoull(str,&next,base);
971 break;
972 case 'Z':
973 case 'z':
975 size_t *s = (size_t*) va_arg(args,size_t*);
976 *s = (size_t) simple_strtoul(str,&next,base);
978 break;
979 default:
980 if (is_sign) {
981 int *i = (int *) va_arg(args, int*);
982 *i = (int) simple_strtol(str,&next,base);
983 } else {
984 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
985 *i = (unsigned int) simple_strtoul(str,&next,base);
987 break;
989 num++;
991 if (!next)
992 break;
993 str = next;
997 * Now we've come all the way through so either the input string or the
998 * format ended. In the former case, there can be a %n at the current
999 * position in the format that needs to be filled.
1001 if (*fmt == '%' && *(fmt + 1) == 'n') {
1002 int *p = (int *)va_arg(args, int *);
1003 *p = str - buf;
1006 return num;
1009 EXPORT_SYMBOL(vsscanf);
1012 * sscanf - Unformat a buffer into a list of arguments
1013 * @buf: input buffer
1014 * @fmt: formatting of buffer
1015 * @...: resulting arguments
1017 int sscanf(const char * buf, const char * fmt, ...)
1019 va_list args;
1020 int i;
1022 va_start(args,fmt);
1023 i = vsscanf(buf,fmt,args);
1024 va_end(args);
1025 return i;
1028 EXPORT_SYMBOL(sscanf);
1031 /* Simplified asprintf. */
1032 char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
1034 unsigned int len;
1035 char *p;
1036 va_list aq;
1038 va_copy(aq, ap);
1039 len = vsnprintf(NULL, 0, fmt, aq);
1040 va_end(aq);
1042 p = kmalloc(len+1, gfp);
1043 if (!p)
1044 return NULL;
1046 vsnprintf(p, len+1, fmt, ap);
1048 return p;
1050 EXPORT_SYMBOL(kvasprintf);
1052 char *kasprintf(gfp_t gfp, const char *fmt, ...)
1054 va_list ap;
1055 char *p;
1057 va_start(ap, fmt);
1058 p = kvasprintf(gfp, fmt, ap);
1059 va_end(ap);
1061 return p;
1063 EXPORT_SYMBOL(kasprintf);