4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
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
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>
26 #include <asm/page.h> /* for PAGE_SIZE */
27 #include <asm/div64.h>
30 * simple_strtoul - convert a string to an unsigned long
31 * @cp: The start of the string
32 * @endp: A pointer to the end of the parsed string will be placed here
33 * @base: The number base to use
35 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
37 unsigned long result = 0,value;
44 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
49 } else if (base == 16) {
50 if (cp[0] == '0' && toupper(cp[1]) == 'X')
53 while (isxdigit(*cp) &&
54 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
55 result = result*base + value;
63 EXPORT_SYMBOL(simple_strtoul);
66 * simple_strtol - convert a string to a signed long
67 * @cp: The start of the string
68 * @endp: A pointer to the end of the parsed string will be placed here
69 * @base: The number base to use
71 long simple_strtol(const char *cp,char **endp,unsigned int base)
74 return -simple_strtoul(cp+1,endp,base);
75 return simple_strtoul(cp,endp,base);
78 EXPORT_SYMBOL(simple_strtol);
81 * simple_strtoull - convert a string to an unsigned long 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 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
88 unsigned long long result = 0,value;
95 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
100 } else if (base == 16) {
101 if (cp[0] == '0' && toupper(cp[1]) == 'X')
104 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
105 ? toupper(*cp) : *cp)-'A'+10) < base) {
106 result = result*base + value;
114 EXPORT_SYMBOL(simple_strtoull);
117 * simple_strtoll - convert a string to a signed long long
118 * @cp: The start of the string
119 * @endp: A pointer to the end of the parsed string will be placed here
120 * @base: The number base to use
122 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
125 return -simple_strtoull(cp+1,endp,base);
126 return simple_strtoull(cp,endp,base);
129 static int skip_atoi(const char **s)
134 i = i*10 + *((*s)++) - '0';
138 #define ZEROPAD 1 /* pad with zero */
139 #define SIGN 2 /* unsigned/signed long */
140 #define PLUS 4 /* show plus */
141 #define SPACE 8 /* space if plus */
142 #define LEFT 16 /* left justified */
143 #define SPECIAL 32 /* 0x */
144 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
146 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
150 /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
151 static const char small_digits[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
152 static const char large_digits[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
153 int need_pfx = ((type & SPECIAL) && base != 10);
156 digits = (type & LARGE) ? large_digits : small_digits;
159 if (base < 2 || base > 36)
163 if ((signed long long) num < 0) {
165 num = - (signed long long) num;
167 } else if (type & PLUS) {
170 } else if (type & SPACE) {
181 /* generate full string in tmp[], in reverse order */
185 else if (base != 10) { /* 8 or 16 */
188 if (base == 16) shift = 4;
190 tmp[i++] = digits[((unsigned char)num) & mask];
193 } else do { /* generic code, works for any base */
194 tmp[i++] = digits[do_div(num,10 /*base*/)];
197 /* printing 100 using %2d gives "100", not "00" */
200 /* leading space padding */
202 if (!(type & (ZEROPAD+LEFT))) {
215 /* "0x" / "0" prefix */
222 *buf = digits[16]; /* for arbitrary base: digits[33]; */
226 /* zero or space padding */
227 if (!(type & LEFT)) {
228 char c = (type & ZEROPAD) ? '0' : ' ';
229 while (--size >= 0) {
235 /* hmm even more zero padding? */
236 while (i <= --precision) {
241 /* actual digits of result */
247 /* trailing space padding */
248 while (--size >= 0) {
257 * vsnprintf - Format a string and place it in a buffer
258 * @buf: The buffer to place the result into
259 * @size: The size of the buffer, including the trailing null space
260 * @fmt: The format string to use
261 * @args: Arguments for the format string
263 * The return value is the number of characters which would
264 * be generated for the given input, excluding the trailing
265 * '\0', as per ISO C99. If you want to have the exact
266 * number of characters written into @buf as return value
267 * (not including the trailing '\0'), use vscnprintf(). If the
268 * return is greater than or equal to @size, the resulting
269 * string is truncated.
271 * Call this function if you are already dealing with a va_list.
272 * You probably want snprintf() instead.
274 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
277 unsigned long long num;
282 int flags; /* flags to number() */
284 int field_width; /* width of output field */
285 int precision; /* min. # of digits for integers; max
286 number of chars for from string */
287 int qualifier; /* 'h', 'l', or 'L' for integer fields */
288 /* 'z' support added 23/7/1999 S.H. */
289 /* 'z' changed to 'Z' --davidm 1/25/99 */
290 /* 't' added for ptrdiff_t */
292 /* Reject out-of-range values early. Large positive sizes are
293 used for unknown buffer sizes. */
294 if (unlikely((int) size < 0)) {
295 /* There can be only one.. */
296 static char warn = 1;
305 /* Make sure end is always >= buf */
311 for (; *fmt ; ++fmt) {
322 ++fmt; /* this also skips first '%' */
324 case '-': flags |= LEFT; goto repeat;
325 case '+': flags |= PLUS; goto repeat;
326 case ' ': flags |= SPACE; goto repeat;
327 case '#': flags |= SPECIAL; goto repeat;
328 case '0': flags |= ZEROPAD; goto repeat;
331 /* get field width */
334 field_width = skip_atoi(&fmt);
335 else if (*fmt == '*') {
337 /* it's the next argument */
338 field_width = va_arg(args, int);
339 if (field_width < 0) {
340 field_width = -field_width;
345 /* get the precision */
350 precision = skip_atoi(&fmt);
351 else if (*fmt == '*') {
353 /* it's the next argument */
354 precision = va_arg(args, int);
360 /* get the conversion qualifier */
362 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
363 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
366 if (qualifier == 'l' && *fmt == 'l') {
377 if (!(flags & LEFT)) {
378 while (--field_width > 0) {
384 c = (unsigned char) va_arg(args, int);
388 while (--field_width > 0) {
396 s = va_arg(args, char *);
397 if ((unsigned long)s < PAGE_SIZE)
400 len = strnlen(s, precision);
402 if (!(flags & LEFT)) {
403 while (len < field_width--) {
409 for (i = 0; i < len; ++i) {
414 while (len < field_width--) {
422 if (field_width == -1) {
423 field_width = 2*sizeof(void *);
426 str = number(str, end,
427 (unsigned long) va_arg(args, void *),
428 16, field_width, precision, flags);
434 * What does C99 say about the overflow case here? */
435 if (qualifier == 'l') {
436 long * ip = va_arg(args, long *);
438 } else if (qualifier == 'Z' || qualifier == 'z') {
439 size_t * ip = va_arg(args, size_t *);
442 int * ip = va_arg(args, int *);
453 /* integer number formats - set up the flags and "break" */
483 if (qualifier == 'L')
484 num = va_arg(args, long long);
485 else if (qualifier == 'l') {
486 num = va_arg(args, unsigned long);
488 num = (signed long) num;
489 } else if (qualifier == 'Z' || qualifier == 'z') {
490 num = va_arg(args, size_t);
491 } else if (qualifier == 't') {
492 num = va_arg(args, ptrdiff_t);
493 } else if (qualifier == 'h') {
494 num = (unsigned short) va_arg(args, int);
496 num = (signed short) num;
498 num = va_arg(args, unsigned int);
500 num = (signed int) num;
502 str = number(str, end, num, base,
503 field_width, precision, flags);
511 /* the trailing null byte doesn't count towards the total */
515 EXPORT_SYMBOL(vsnprintf);
518 * vscnprintf - Format a string and place it in a buffer
519 * @buf: The buffer to place the result into
520 * @size: The size of the buffer, including the trailing null space
521 * @fmt: The format string to use
522 * @args: Arguments for the format string
524 * The return value is the number of characters which have been written into
525 * the @buf not including the trailing '\0'. If @size is <= 0 the function
528 * Call this function if you are already dealing with a va_list.
529 * You probably want scnprintf() instead.
531 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
535 i=vsnprintf(buf,size,fmt,args);
536 return (i >= size) ? (size - 1) : i;
539 EXPORT_SYMBOL(vscnprintf);
542 * snprintf - Format a string and place it in a buffer
543 * @buf: The buffer to place the result into
544 * @size: The size of the buffer, including the trailing null space
545 * @fmt: The format string to use
546 * @...: Arguments for the format string
548 * The return value is the number of characters which would be
549 * generated for the given input, excluding the trailing null,
550 * as per ISO C99. If the return is greater than or equal to
551 * @size, the resulting string is truncated.
553 int snprintf(char * buf, size_t size, const char *fmt, ...)
559 i=vsnprintf(buf,size,fmt,args);
564 EXPORT_SYMBOL(snprintf);
567 * scnprintf - Format a string and place it in a buffer
568 * @buf: The buffer to place the result into
569 * @size: The size of the buffer, including the trailing null space
570 * @fmt: The format string to use
571 * @...: Arguments for the format string
573 * The return value is the number of characters written into @buf not including
574 * the trailing '\0'. If @size is <= 0 the function returns 0.
577 int scnprintf(char * buf, size_t size, const char *fmt, ...)
583 i = vsnprintf(buf, size, fmt, args);
585 return (i >= size) ? (size - 1) : i;
587 EXPORT_SYMBOL(scnprintf);
590 * vsprintf - Format a string and place it in a buffer
591 * @buf: The buffer to place the result into
592 * @fmt: The format string to use
593 * @args: Arguments for the format string
595 * The function returns the number of characters written
596 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
599 * Call this function if you are already dealing with a va_list.
600 * You probably want sprintf() instead.
602 int vsprintf(char *buf, const char *fmt, va_list args)
604 return vsnprintf(buf, INT_MAX, fmt, args);
607 EXPORT_SYMBOL(vsprintf);
610 * sprintf - Format a string and place it in a buffer
611 * @buf: The buffer to place the result into
612 * @fmt: The format string to use
613 * @...: Arguments for the format string
615 * The function returns the number of characters written
616 * into @buf. Use snprintf() or scnprintf() in order to avoid
619 int sprintf(char * buf, const char *fmt, ...)
625 i=vsnprintf(buf, INT_MAX, fmt, args);
630 EXPORT_SYMBOL(sprintf);
633 * vsscanf - Unformat a buffer into a list of arguments
635 * @fmt: format of buffer
638 int vsscanf(const char * buf, const char * fmt, va_list args)
640 const char *str = buf;
649 while(*fmt && *str) {
650 /* skip any white space in format */
651 /* white space in format matchs any amount of
652 * white space, including none, in the input.
655 while (isspace(*fmt))
657 while (isspace(*str))
661 /* anything that is not a conversion must match exactly */
662 if (*fmt != '%' && *fmt) {
663 if (*fmt++ != *str++)
672 /* skip this conversion.
673 * advance both strings to next white space
676 while (!isspace(*fmt) && *fmt)
678 while (!isspace(*str) && *str)
683 /* get field width */
686 field_width = skip_atoi(&fmt);
688 /* get conversion qualifier */
690 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
691 *fmt == 'Z' || *fmt == 'z') {
693 if (unlikely(qualifier == *fmt)) {
694 if (qualifier == 'h') {
697 } else if (qualifier == 'l') {
712 char *s = (char *) va_arg(args,char*);
713 if (field_width == -1)
717 } while (--field_width > 0 && *str);
723 char *s = (char *) va_arg(args, char *);
724 if(field_width == -1)
725 field_width = INT_MAX;
726 /* first, skip leading white space in buffer */
727 while (isspace(*str))
730 /* now copy until next white space */
731 while (*str && !isspace(*str) && field_width--) {
739 /* return number of characters read so far */
741 int *i = (int *)va_arg(args,int*);
759 /* looking for '%' in str */
764 /* invalid format; stop here */
768 /* have some sort of integer conversion.
769 * first, skip white space in buffer.
771 while (isspace(*str))
775 if (is_sign && digit == '-')
779 || (base == 16 && !isxdigit(digit))
780 || (base == 10 && !isdigit(digit))
781 || (base == 8 && (!isdigit(digit) || digit > '7'))
782 || (base == 0 && !isdigit(digit)))
786 case 'H': /* that's 'hh' in format */
788 signed char *s = (signed char *) va_arg(args,signed char *);
789 *s = (signed char) simple_strtol(str,&next,base);
791 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
792 *s = (unsigned char) simple_strtoul(str, &next, base);
797 short *s = (short *) va_arg(args,short *);
798 *s = (short) simple_strtol(str,&next,base);
800 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
801 *s = (unsigned short) simple_strtoul(str, &next, base);
806 long *l = (long *) va_arg(args,long *);
807 *l = simple_strtol(str,&next,base);
809 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
810 *l = simple_strtoul(str,&next,base);
815 long long *l = (long long*) va_arg(args,long long *);
816 *l = simple_strtoll(str,&next,base);
818 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
819 *l = simple_strtoull(str,&next,base);
825 size_t *s = (size_t*) va_arg(args,size_t*);
826 *s = (size_t) simple_strtoul(str,&next,base);
831 int *i = (int *) va_arg(args, int*);
832 *i = (int) simple_strtol(str,&next,base);
834 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
835 *i = (unsigned int) simple_strtoul(str,&next,base);
847 * Now we've come all the way through so either the input string or the
848 * format ended. In the former case, there can be a %n at the current
849 * position in the format that needs to be filled.
851 if (*fmt == '%' && *(fmt + 1) == 'n') {
852 int *p = (int *)va_arg(args, int *);
859 EXPORT_SYMBOL(vsscanf);
862 * sscanf - Unformat a buffer into a list of arguments
864 * @fmt: formatting of buffer
865 * @...: resulting arguments
867 int sscanf(const char * buf, const char * fmt, ...)
873 i = vsscanf(buf,fmt,args);
878 EXPORT_SYMBOL(sscanf);
881 /* Simplified asprintf. */
882 char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
889 len = vsnprintf(NULL, 0, fmt, aq);
892 p = kmalloc(len+1, gfp);
896 vsnprintf(p, len+1, fmt, ap);
900 EXPORT_SYMBOL(kvasprintf);
902 char *kasprintf(gfp_t gfp, const char *fmt, ...)
908 p = kvasprintf(gfp, fmt, ap);
913 EXPORT_SYMBOL(kasprintf);