rt2x00: Release rt2x00 2.2.2
[linux-2.6/sactl.git] / lib / vsprintf.c
blob6897724ff5df0731e717305afaaca37112ec5897
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>
27 #include <linux/ioport.h>
29 #include <asm/page.h> /* for PAGE_SIZE */
30 #include <asm/div64.h>
31 #include <asm/sections.h> /* for dereference_function_descriptor() */
33 /* Works only for digits and letters, but small and fast */
34 #define TOLOWER(x) ((x) | 0x20)
36 static unsigned int simple_guess_base(const char *cp)
38 if (cp[0] == '0') {
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
40 return 16;
41 else
42 return 8;
43 } else {
44 return 10;
48 /**
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
54 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
56 unsigned long result = 0;
58 if (!base)
59 base = simple_guess_base(cp);
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
62 cp += 2;
64 while (isxdigit(*cp)) {
65 unsigned int value;
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
68 if (value >= base)
69 break;
70 result = result * base + value;
71 cp++;
74 if (endp)
75 *endp = (char *)cp;
76 return result;
78 EXPORT_SYMBOL(simple_strtoul);
80 /**
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
86 long simple_strtol(const char *cp, char **endp, unsigned int base)
88 if(*cp == '-')
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
92 EXPORT_SYMBOL(simple_strtol);
94 /**
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
102 unsigned long long result = 0;
104 if (!base)
105 base = simple_guess_base(cp);
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
110 while (isxdigit(*cp)) {
111 unsigned int value;
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
117 cp++;
120 if (endp)
121 *endp = (char *)cp;
122 return result;
124 EXPORT_SYMBOL(simple_strtoull);
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
132 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
134 if(*cp=='-')
135 return -simple_strtoull(cp + 1, endp, base);
136 return simple_strtoull(cp, endp, base);
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
153 * echo will append a newline to the tail.
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
161 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
163 char *tail;
164 unsigned long val;
165 size_t len;
167 *res = 0;
168 len = strlen(cp);
169 if (len == 0)
170 return -EINVAL;
172 val = simple_strtoul(cp, &tail, base);
173 if ((*tail == '\0') ||
174 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
175 *res = val;
176 return 0;
179 return -EINVAL;
181 EXPORT_SYMBOL(strict_strtoul);
184 * strict_strtol - convert a string to a long strictly
185 * @cp: The string to be converted
186 * @base: The number base to use
187 * @res: The converted result value
189 * strict_strtol is similiar to strict_strtoul, but it allows the first
190 * character of a string is '-'.
192 * It returns 0 if conversion is successful and *res is set to the converted
193 * value, otherwise it returns -EINVAL and *res is set to 0.
195 int strict_strtol(const char *cp, unsigned int base, long *res)
197 int ret;
198 if (*cp == '-') {
199 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
200 if (!ret)
201 *res = -(*res);
202 } else {
203 ret = strict_strtoul(cp, base, (unsigned long *)res);
206 return ret;
208 EXPORT_SYMBOL(strict_strtol);
211 * strict_strtoull - convert a string to an unsigned long long strictly
212 * @cp: The string to be converted
213 * @base: The number base to use
214 * @res: The converted result value
216 * strict_strtoull converts a string to an unsigned long long only if the
217 * string is really an unsigned long long string, any string containing
218 * any invalid char at the tail will be rejected and -EINVAL is returned,
219 * only a newline char at the tail is acceptible because people generally
220 * change a module parameter in the following way:
222 * echo 1024 > /sys/module/e1000/parameters/copybreak
224 * echo will append a newline to the tail of the string.
226 * It returns 0 if conversion is successful and *res is set to the converted
227 * value, otherwise it returns -EINVAL and *res is set to 0.
229 * simple_strtoull just ignores the successive invalid characters and
230 * return the converted value of prefix part of the string.
232 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
234 char *tail;
235 unsigned long long val;
236 size_t len;
238 *res = 0;
239 len = strlen(cp);
240 if (len == 0)
241 return -EINVAL;
243 val = simple_strtoull(cp, &tail, base);
244 if ((*tail == '\0') ||
245 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
246 *res = val;
247 return 0;
250 return -EINVAL;
252 EXPORT_SYMBOL(strict_strtoull);
255 * strict_strtoll - convert a string to a long long strictly
256 * @cp: The string to be converted
257 * @base: The number base to use
258 * @res: The converted result value
260 * strict_strtoll is similiar to strict_strtoull, but it allows the first
261 * character of a string is '-'.
263 * It returns 0 if conversion is successful and *res is set to the converted
264 * value, otherwise it returns -EINVAL and *res is set to 0.
266 int strict_strtoll(const char *cp, unsigned int base, long long *res)
268 int ret;
269 if (*cp == '-') {
270 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
271 if (!ret)
272 *res = -(*res);
273 } else {
274 ret = strict_strtoull(cp, base, (unsigned long long *)res);
277 return ret;
279 EXPORT_SYMBOL(strict_strtoll);
281 static int skip_atoi(const char **s)
283 int i=0;
285 while (isdigit(**s))
286 i = i*10 + *((*s)++) - '0';
287 return i;
290 /* Decimal conversion is by far the most typical, and is used
291 * for /proc and /sys data. This directly impacts e.g. top performance
292 * with many processes running. We optimize it for speed
293 * using code from
294 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
295 * (with permission from the author, Douglas W. Jones). */
297 /* Formats correctly any integer in [0,99999].
298 * Outputs from one to five digits depending on input.
299 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
300 static char* put_dec_trunc(char *buf, unsigned q)
302 unsigned d3, d2, d1, d0;
303 d1 = (q>>4) & 0xf;
304 d2 = (q>>8) & 0xf;
305 d3 = (q>>12);
307 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
308 q = (d0 * 0xcd) >> 11;
309 d0 = d0 - 10*q;
310 *buf++ = d0 + '0'; /* least significant digit */
311 d1 = q + 9*d3 + 5*d2 + d1;
312 if (d1 != 0) {
313 q = (d1 * 0xcd) >> 11;
314 d1 = d1 - 10*q;
315 *buf++ = d1 + '0'; /* next digit */
317 d2 = q + 2*d2;
318 if ((d2 != 0) || (d3 != 0)) {
319 q = (d2 * 0xd) >> 7;
320 d2 = d2 - 10*q;
321 *buf++ = d2 + '0'; /* next digit */
323 d3 = q + 4*d3;
324 if (d3 != 0) {
325 q = (d3 * 0xcd) >> 11;
326 d3 = d3 - 10*q;
327 *buf++ = d3 + '0'; /* next digit */
328 if (q != 0)
329 *buf++ = q + '0'; /* most sign. digit */
333 return buf;
335 /* Same with if's removed. Always emits five digits */
336 static char* put_dec_full(char *buf, unsigned q)
338 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
339 /* but anyway, gcc produces better code with full-sized ints */
340 unsigned d3, d2, d1, d0;
341 d1 = (q>>4) & 0xf;
342 d2 = (q>>8) & 0xf;
343 d3 = (q>>12);
345 /* Possible ways to approx. divide by 10 */
346 /* gcc -O2 replaces multiply with shifts and adds */
347 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
348 // (x * 0x67) >> 10: 1100111
349 // (x * 0x34) >> 9: 110100 - same
350 // (x * 0x1a) >> 8: 11010 - same
351 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
353 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
354 q = (d0 * 0xcd) >> 11;
355 d0 = d0 - 10*q;
356 *buf++ = d0 + '0';
357 d1 = q + 9*d3 + 5*d2 + d1;
358 q = (d1 * 0xcd) >> 11;
359 d1 = d1 - 10*q;
360 *buf++ = d1 + '0';
362 d2 = q + 2*d2;
363 q = (d2 * 0xd) >> 7;
364 d2 = d2 - 10*q;
365 *buf++ = d2 + '0';
367 d3 = q + 4*d3;
368 q = (d3 * 0xcd) >> 11; /* - shorter code */
369 /* q = (d3 * 0x67) >> 10; - would also work */
370 d3 = d3 - 10*q;
371 *buf++ = d3 + '0';
372 *buf++ = q + '0';
373 return buf;
375 /* No inlining helps gcc to use registers better */
376 static noinline char* put_dec(char *buf, unsigned long long num)
378 while (1) {
379 unsigned rem;
380 if (num < 100000)
381 return put_dec_trunc(buf, num);
382 rem = do_div(num, 100000);
383 buf = put_dec_full(buf, rem);
387 #define ZEROPAD 1 /* pad with zero */
388 #define SIGN 2 /* unsigned/signed long */
389 #define PLUS 4 /* show plus */
390 #define SPACE 8 /* space if plus */
391 #define LEFT 16 /* left justified */
392 #define SMALL 32 /* Must be 32 == 0x20 */
393 #define SPECIAL 64 /* 0x */
395 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
397 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
398 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
400 char tmp[66];
401 char sign;
402 char locase;
403 int need_pfx = ((type & SPECIAL) && base != 10);
404 int i;
406 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
407 * produces same digits or (maybe lowercased) letters */
408 locase = (type & SMALL);
409 if (type & LEFT)
410 type &= ~ZEROPAD;
411 sign = 0;
412 if (type & SIGN) {
413 if ((signed long long) num < 0) {
414 sign = '-';
415 num = - (signed long long) num;
416 size--;
417 } else if (type & PLUS) {
418 sign = '+';
419 size--;
420 } else if (type & SPACE) {
421 sign = ' ';
422 size--;
425 if (need_pfx) {
426 size--;
427 if (base == 16)
428 size--;
431 /* generate full string in tmp[], in reverse order */
432 i = 0;
433 if (num == 0)
434 tmp[i++] = '0';
435 /* Generic code, for any base:
436 else do {
437 tmp[i++] = (digits[do_div(num,base)] | locase);
438 } while (num != 0);
440 else if (base != 10) { /* 8 or 16 */
441 int mask = base - 1;
442 int shift = 3;
443 if (base == 16) shift = 4;
444 do {
445 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
446 num >>= shift;
447 } while (num);
448 } else { /* base 10 */
449 i = put_dec(tmp, num) - tmp;
452 /* printing 100 using %2d gives "100", not "00" */
453 if (i > precision)
454 precision = i;
455 /* leading space padding */
456 size -= precision;
457 if (!(type & (ZEROPAD+LEFT))) {
458 while(--size >= 0) {
459 if (buf < end)
460 *buf = ' ';
461 ++buf;
464 /* sign */
465 if (sign) {
466 if (buf < end)
467 *buf = sign;
468 ++buf;
470 /* "0x" / "0" prefix */
471 if (need_pfx) {
472 if (buf < end)
473 *buf = '0';
474 ++buf;
475 if (base == 16) {
476 if (buf < end)
477 *buf = ('X' | locase);
478 ++buf;
481 /* zero or space padding */
482 if (!(type & LEFT)) {
483 char c = (type & ZEROPAD) ? '0' : ' ';
484 while (--size >= 0) {
485 if (buf < end)
486 *buf = c;
487 ++buf;
490 /* hmm even more zero padding? */
491 while (i <= --precision) {
492 if (buf < end)
493 *buf = '0';
494 ++buf;
496 /* actual digits of result */
497 while (--i >= 0) {
498 if (buf < end)
499 *buf = tmp[i];
500 ++buf;
502 /* trailing space padding */
503 while (--size >= 0) {
504 if (buf < end)
505 *buf = ' ';
506 ++buf;
508 return buf;
511 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
513 int len, i;
515 if ((unsigned long)s < PAGE_SIZE)
516 s = "<NULL>";
518 len = strnlen(s, precision);
520 if (!(flags & LEFT)) {
521 while (len < field_width--) {
522 if (buf < end)
523 *buf = ' ';
524 ++buf;
527 for (i = 0; i < len; ++i) {
528 if (buf < end)
529 *buf = *s;
530 ++buf; ++s;
532 while (len < field_width--) {
533 if (buf < end)
534 *buf = ' ';
535 ++buf;
537 return buf;
540 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
542 unsigned long value = (unsigned long) ptr;
543 #ifdef CONFIG_KALLSYMS
544 char sym[KSYM_SYMBOL_LEN];
545 sprint_symbol(sym, value);
546 return string(buf, end, sym, field_width, precision, flags);
547 #else
548 field_width = 2*sizeof(void *);
549 flags |= SPECIAL | SMALL | ZEROPAD;
550 return number(buf, end, value, 16, field_width, precision, flags);
551 #endif
554 static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
556 #ifndef IO_RSRC_PRINTK_SIZE
557 #define IO_RSRC_PRINTK_SIZE 4
558 #endif
560 #ifndef MEM_RSRC_PRINTK_SIZE
561 #define MEM_RSRC_PRINTK_SIZE 8
562 #endif
564 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
565 char sym[4*sizeof(resource_size_t) + 8];
566 char *p = sym, *pend = sym + sizeof(sym);
567 int size = -1;
569 if (res->flags & IORESOURCE_IO)
570 size = IO_RSRC_PRINTK_SIZE;
571 else if (res->flags & IORESOURCE_MEM)
572 size = MEM_RSRC_PRINTK_SIZE;
574 *p++ = '[';
575 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
576 *p++ = '-';
577 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
578 *p++ = ']';
579 *p = 0;
581 return string(buf, end, sym, field_width, precision, flags);
584 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
585 int precision, int flags)
587 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
588 char *p = mac_addr;
589 int i;
591 for (i = 0; i < 6; i++) {
592 p = pack_hex_byte(p, addr[i]);
593 if (!(flags & SPECIAL) && i != 5)
594 *p++ = ':';
596 *p = '\0';
598 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
601 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
602 int precision, int flags)
604 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
605 char *p = ip6_addr;
606 int i;
608 for (i = 0; i < 8; i++) {
609 p = pack_hex_byte(p, addr[2 * i]);
610 p = pack_hex_byte(p, addr[2 * i + 1]);
611 if (!(flags & SPECIAL) && i != 7)
612 *p++ = ':';
614 *p = '\0';
616 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
619 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
620 int precision, int flags)
622 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
623 char temp[3]; /* hold each IP quad in reverse order */
624 char *p = ip4_addr;
625 int i, digits;
627 for (i = 0; i < 4; i++) {
628 digits = put_dec_trunc(temp, addr[i]) - temp;
629 /* reverse the digits in the quad */
630 while (digits--)
631 *p++ = temp[digits];
632 if (i != 3)
633 *p++ = '.';
635 *p = '\0';
637 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
641 * Show a '%p' thing. A kernel extension is that the '%p' is followed
642 * by an extra set of alphanumeric characters that are extended format
643 * specifiers.
645 * Right now we handle:
647 * - 'F' For symbolic function descriptor pointers
648 * - 'S' For symbolic direct pointers
649 * - 'R' For a struct resource pointer, it prints the range of
650 * addresses (not the name nor the flags)
651 * - 'M' For a 6-byte MAC address, it prints the address in the
652 * usual colon-separated hex notation
653 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
654 * decimal for v4 and colon separated network-order 16 bit hex for v6)
655 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
656 * currently the same
658 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
659 * function pointers are really function descriptors, which contain a
660 * pointer to the real address.
662 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
664 switch (*fmt) {
665 case 'F':
666 ptr = dereference_function_descriptor(ptr);
667 /* Fallthrough */
668 case 'S':
669 return symbol_string(buf, end, ptr, field_width, precision, flags);
670 case 'R':
671 return resource_string(buf, end, ptr, field_width, precision, flags);
672 case 'M':
673 return mac_address_string(buf, end, ptr, field_width, precision, flags);
674 case 'i':
675 flags |= SPECIAL;
676 /* Fallthrough */
677 case 'I':
678 if (fmt[1] == '6')
679 return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
680 if (fmt[1] == '4')
681 return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
682 flags &= ~SPECIAL;
683 break;
685 flags |= SMALL;
686 if (field_width == -1) {
687 field_width = 2*sizeof(void *);
688 flags |= ZEROPAD;
690 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
694 * vsnprintf - Format a string and place it in a buffer
695 * @buf: The buffer to place the result into
696 * @size: The size of the buffer, including the trailing null space
697 * @fmt: The format string to use
698 * @args: Arguments for the format string
700 * This function follows C99 vsnprintf, but has some extensions:
701 * %pS output the name of a text symbol
702 * %pF output the name of a function pointer
703 * %pR output the address range in a struct resource
705 * The return value is the number of characters which would
706 * be generated for the given input, excluding the trailing
707 * '\0', as per ISO C99. If you want to have the exact
708 * number of characters written into @buf as return value
709 * (not including the trailing '\0'), use vscnprintf(). If the
710 * return is greater than or equal to @size, the resulting
711 * string is truncated.
713 * Call this function if you are already dealing with a va_list.
714 * You probably want snprintf() instead.
716 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
718 unsigned long long num;
719 int base;
720 char *str, *end, c;
722 int flags; /* flags to number() */
724 int field_width; /* width of output field */
725 int precision; /* min. # of digits for integers; max
726 number of chars for from string */
727 int qualifier; /* 'h', 'l', or 'L' for integer fields */
728 /* 'z' support added 23/7/1999 S.H. */
729 /* 'z' changed to 'Z' --davidm 1/25/99 */
730 /* 't' added for ptrdiff_t */
732 /* Reject out-of-range values early. Large positive sizes are
733 used for unknown buffer sizes. */
734 if (unlikely((int) size < 0)) {
735 /* There can be only one.. */
736 static char warn = 1;
737 WARN_ON(warn);
738 warn = 0;
739 return 0;
742 str = buf;
743 end = buf + size;
745 /* Make sure end is always >= buf */
746 if (end < buf) {
747 end = ((void *)-1);
748 size = end - buf;
751 for (; *fmt ; ++fmt) {
752 if (*fmt != '%') {
753 if (str < end)
754 *str = *fmt;
755 ++str;
756 continue;
759 /* process flags */
760 flags = 0;
761 repeat:
762 ++fmt; /* this also skips first '%' */
763 switch (*fmt) {
764 case '-': flags |= LEFT; goto repeat;
765 case '+': flags |= PLUS; goto repeat;
766 case ' ': flags |= SPACE; goto repeat;
767 case '#': flags |= SPECIAL; goto repeat;
768 case '0': flags |= ZEROPAD; goto repeat;
771 /* get field width */
772 field_width = -1;
773 if (isdigit(*fmt))
774 field_width = skip_atoi(&fmt);
775 else if (*fmt == '*') {
776 ++fmt;
777 /* it's the next argument */
778 field_width = va_arg(args, int);
779 if (field_width < 0) {
780 field_width = -field_width;
781 flags |= LEFT;
785 /* get the precision */
786 precision = -1;
787 if (*fmt == '.') {
788 ++fmt;
789 if (isdigit(*fmt))
790 precision = skip_atoi(&fmt);
791 else if (*fmt == '*') {
792 ++fmt;
793 /* it's the next argument */
794 precision = va_arg(args, int);
796 if (precision < 0)
797 precision = 0;
800 /* get the conversion qualifier */
801 qualifier = -1;
802 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
803 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
804 qualifier = *fmt;
805 ++fmt;
806 if (qualifier == 'l' && *fmt == 'l') {
807 qualifier = 'L';
808 ++fmt;
812 /* default base */
813 base = 10;
815 switch (*fmt) {
816 case 'c':
817 if (!(flags & LEFT)) {
818 while (--field_width > 0) {
819 if (str < end)
820 *str = ' ';
821 ++str;
824 c = (unsigned char) va_arg(args, int);
825 if (str < end)
826 *str = c;
827 ++str;
828 while (--field_width > 0) {
829 if (str < end)
830 *str = ' ';
831 ++str;
833 continue;
835 case 's':
836 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
837 continue;
839 case 'p':
840 str = pointer(fmt+1, str, end,
841 va_arg(args, void *),
842 field_width, precision, flags);
843 /* Skip all alphanumeric pointer suffixes */
844 while (isalnum(fmt[1]))
845 fmt++;
846 continue;
848 case 'n':
849 /* FIXME:
850 * What does C99 say about the overflow case here? */
851 if (qualifier == 'l') {
852 long * ip = va_arg(args, long *);
853 *ip = (str - buf);
854 } else if (qualifier == 'Z' || qualifier == 'z') {
855 size_t * ip = va_arg(args, size_t *);
856 *ip = (str - buf);
857 } else {
858 int * ip = va_arg(args, int *);
859 *ip = (str - buf);
861 continue;
863 case '%':
864 if (str < end)
865 *str = '%';
866 ++str;
867 continue;
869 /* integer number formats - set up the flags and "break" */
870 case 'o':
871 base = 8;
872 break;
874 case 'x':
875 flags |= SMALL;
876 case 'X':
877 base = 16;
878 break;
880 case 'd':
881 case 'i':
882 flags |= SIGN;
883 case 'u':
884 break;
886 default:
887 if (str < end)
888 *str = '%';
889 ++str;
890 if (*fmt) {
891 if (str < end)
892 *str = *fmt;
893 ++str;
894 } else {
895 --fmt;
897 continue;
899 if (qualifier == 'L')
900 num = va_arg(args, long long);
901 else if (qualifier == 'l') {
902 num = va_arg(args, unsigned long);
903 if (flags & SIGN)
904 num = (signed long) num;
905 } else if (qualifier == 'Z' || qualifier == 'z') {
906 num = va_arg(args, size_t);
907 } else if (qualifier == 't') {
908 num = va_arg(args, ptrdiff_t);
909 } else if (qualifier == 'h') {
910 num = (unsigned short) va_arg(args, int);
911 if (flags & SIGN)
912 num = (signed short) num;
913 } else {
914 num = va_arg(args, unsigned int);
915 if (flags & SIGN)
916 num = (signed int) num;
918 str = number(str, end, num, base,
919 field_width, precision, flags);
921 if (size > 0) {
922 if (str < end)
923 *str = '\0';
924 else
925 end[-1] = '\0';
927 /* the trailing null byte doesn't count towards the total */
928 return str-buf;
930 EXPORT_SYMBOL(vsnprintf);
933 * vscnprintf - Format a string and place it in a buffer
934 * @buf: The buffer to place the result into
935 * @size: The size of the buffer, including the trailing null space
936 * @fmt: The format string to use
937 * @args: Arguments for the format string
939 * The return value is the number of characters which have been written into
940 * the @buf not including the trailing '\0'. If @size is <= 0 the function
941 * returns 0.
943 * Call this function if you are already dealing with a va_list.
944 * You probably want scnprintf() instead.
946 * See the vsnprintf() documentation for format string extensions over C99.
948 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
950 int i;
952 i=vsnprintf(buf,size,fmt,args);
953 return (i >= size) ? (size - 1) : i;
955 EXPORT_SYMBOL(vscnprintf);
958 * snprintf - Format a string and place it in a buffer
959 * @buf: The buffer to place the result into
960 * @size: The size of the buffer, including the trailing null space
961 * @fmt: The format string to use
962 * @...: Arguments for the format string
964 * The return value is the number of characters which would be
965 * generated for the given input, excluding the trailing null,
966 * as per ISO C99. If the return is greater than or equal to
967 * @size, the resulting string is truncated.
969 * See the vsnprintf() documentation for format string extensions over C99.
971 int snprintf(char * buf, size_t size, const char *fmt, ...)
973 va_list args;
974 int i;
976 va_start(args, fmt);
977 i=vsnprintf(buf,size,fmt,args);
978 va_end(args);
979 return i;
981 EXPORT_SYMBOL(snprintf);
984 * scnprintf - Format a string and place it in a buffer
985 * @buf: The buffer to place the result into
986 * @size: The size of the buffer, including the trailing null space
987 * @fmt: The format string to use
988 * @...: Arguments for the format string
990 * The return value is the number of characters written into @buf not including
991 * the trailing '\0'. If @size is <= 0 the function returns 0.
994 int scnprintf(char * buf, size_t size, const char *fmt, ...)
996 va_list args;
997 int i;
999 va_start(args, fmt);
1000 i = vsnprintf(buf, size, fmt, args);
1001 va_end(args);
1002 return (i >= size) ? (size - 1) : i;
1004 EXPORT_SYMBOL(scnprintf);
1007 * vsprintf - Format a string and place it in a buffer
1008 * @buf: The buffer to place the result into
1009 * @fmt: The format string to use
1010 * @args: Arguments for the format string
1012 * The function returns the number of characters written
1013 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1014 * buffer overflows.
1016 * Call this function if you are already dealing with a va_list.
1017 * You probably want sprintf() instead.
1019 * See the vsnprintf() documentation for format string extensions over C99.
1021 int vsprintf(char *buf, const char *fmt, va_list args)
1023 return vsnprintf(buf, INT_MAX, fmt, args);
1025 EXPORT_SYMBOL(vsprintf);
1028 * sprintf - Format a string and place it in a buffer
1029 * @buf: The buffer to place the result into
1030 * @fmt: The format string to use
1031 * @...: Arguments for the format string
1033 * The function returns the number of characters written
1034 * into @buf. Use snprintf() or scnprintf() in order to avoid
1035 * buffer overflows.
1037 * See the vsnprintf() documentation for format string extensions over C99.
1039 int sprintf(char * buf, const char *fmt, ...)
1041 va_list args;
1042 int i;
1044 va_start(args, fmt);
1045 i=vsnprintf(buf, INT_MAX, fmt, args);
1046 va_end(args);
1047 return i;
1049 EXPORT_SYMBOL(sprintf);
1052 * vsscanf - Unformat a buffer into a list of arguments
1053 * @buf: input buffer
1054 * @fmt: format of buffer
1055 * @args: arguments
1057 int vsscanf(const char * buf, const char * fmt, va_list args)
1059 const char *str = buf;
1060 char *next;
1061 char digit;
1062 int num = 0;
1063 int qualifier;
1064 int base;
1065 int field_width;
1066 int is_sign = 0;
1068 while(*fmt && *str) {
1069 /* skip any white space in format */
1070 /* white space in format matchs any amount of
1071 * white space, including none, in the input.
1073 if (isspace(*fmt)) {
1074 while (isspace(*fmt))
1075 ++fmt;
1076 while (isspace(*str))
1077 ++str;
1080 /* anything that is not a conversion must match exactly */
1081 if (*fmt != '%' && *fmt) {
1082 if (*fmt++ != *str++)
1083 break;
1084 continue;
1087 if (!*fmt)
1088 break;
1089 ++fmt;
1091 /* skip this conversion.
1092 * advance both strings to next white space
1094 if (*fmt == '*') {
1095 while (!isspace(*fmt) && *fmt)
1096 fmt++;
1097 while (!isspace(*str) && *str)
1098 str++;
1099 continue;
1102 /* get field width */
1103 field_width = -1;
1104 if (isdigit(*fmt))
1105 field_width = skip_atoi(&fmt);
1107 /* get conversion qualifier */
1108 qualifier = -1;
1109 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1110 *fmt == 'Z' || *fmt == 'z') {
1111 qualifier = *fmt++;
1112 if (unlikely(qualifier == *fmt)) {
1113 if (qualifier == 'h') {
1114 qualifier = 'H';
1115 fmt++;
1116 } else if (qualifier == 'l') {
1117 qualifier = 'L';
1118 fmt++;
1122 base = 10;
1123 is_sign = 0;
1125 if (!*fmt || !*str)
1126 break;
1128 switch(*fmt++) {
1129 case 'c':
1131 char *s = (char *) va_arg(args,char*);
1132 if (field_width == -1)
1133 field_width = 1;
1134 do {
1135 *s++ = *str++;
1136 } while (--field_width > 0 && *str);
1137 num++;
1139 continue;
1140 case 's':
1142 char *s = (char *) va_arg(args, char *);
1143 if(field_width == -1)
1144 field_width = INT_MAX;
1145 /* first, skip leading white space in buffer */
1146 while (isspace(*str))
1147 str++;
1149 /* now copy until next white space */
1150 while (*str && !isspace(*str) && field_width--) {
1151 *s++ = *str++;
1153 *s = '\0';
1154 num++;
1156 continue;
1157 case 'n':
1158 /* return number of characters read so far */
1160 int *i = (int *)va_arg(args,int*);
1161 *i = str - buf;
1163 continue;
1164 case 'o':
1165 base = 8;
1166 break;
1167 case 'x':
1168 case 'X':
1169 base = 16;
1170 break;
1171 case 'i':
1172 base = 0;
1173 case 'd':
1174 is_sign = 1;
1175 case 'u':
1176 break;
1177 case '%':
1178 /* looking for '%' in str */
1179 if (*str++ != '%')
1180 return num;
1181 continue;
1182 default:
1183 /* invalid format; stop here */
1184 return num;
1187 /* have some sort of integer conversion.
1188 * first, skip white space in buffer.
1190 while (isspace(*str))
1191 str++;
1193 digit = *str;
1194 if (is_sign && digit == '-')
1195 digit = *(str + 1);
1197 if (!digit
1198 || (base == 16 && !isxdigit(digit))
1199 || (base == 10 && !isdigit(digit))
1200 || (base == 8 && (!isdigit(digit) || digit > '7'))
1201 || (base == 0 && !isdigit(digit)))
1202 break;
1204 switch(qualifier) {
1205 case 'H': /* that's 'hh' in format */
1206 if (is_sign) {
1207 signed char *s = (signed char *) va_arg(args,signed char *);
1208 *s = (signed char) simple_strtol(str,&next,base);
1209 } else {
1210 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1211 *s = (unsigned char) simple_strtoul(str, &next, base);
1213 break;
1214 case 'h':
1215 if (is_sign) {
1216 short *s = (short *) va_arg(args,short *);
1217 *s = (short) simple_strtol(str,&next,base);
1218 } else {
1219 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1220 *s = (unsigned short) simple_strtoul(str, &next, base);
1222 break;
1223 case 'l':
1224 if (is_sign) {
1225 long *l = (long *) va_arg(args,long *);
1226 *l = simple_strtol(str,&next,base);
1227 } else {
1228 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1229 *l = simple_strtoul(str,&next,base);
1231 break;
1232 case 'L':
1233 if (is_sign) {
1234 long long *l = (long long*) va_arg(args,long long *);
1235 *l = simple_strtoll(str,&next,base);
1236 } else {
1237 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1238 *l = simple_strtoull(str,&next,base);
1240 break;
1241 case 'Z':
1242 case 'z':
1244 size_t *s = (size_t*) va_arg(args,size_t*);
1245 *s = (size_t) simple_strtoul(str,&next,base);
1247 break;
1248 default:
1249 if (is_sign) {
1250 int *i = (int *) va_arg(args, int*);
1251 *i = (int) simple_strtol(str,&next,base);
1252 } else {
1253 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1254 *i = (unsigned int) simple_strtoul(str,&next,base);
1256 break;
1258 num++;
1260 if (!next)
1261 break;
1262 str = next;
1266 * Now we've come all the way through so either the input string or the
1267 * format ended. In the former case, there can be a %n at the current
1268 * position in the format that needs to be filled.
1270 if (*fmt == '%' && *(fmt + 1) == 'n') {
1271 int *p = (int *)va_arg(args, int *);
1272 *p = str - buf;
1275 return num;
1277 EXPORT_SYMBOL(vsscanf);
1280 * sscanf - Unformat a buffer into a list of arguments
1281 * @buf: input buffer
1282 * @fmt: formatting of buffer
1283 * @...: resulting arguments
1285 int sscanf(const char * buf, const char * fmt, ...)
1287 va_list args;
1288 int i;
1290 va_start(args,fmt);
1291 i = vsscanf(buf,fmt,args);
1292 va_end(args);
1293 return i;
1295 EXPORT_SYMBOL(sscanf);