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/div64.h>
29 * simple_strtoul - convert a string to an unsigned long
30 * @cp: The start of the string
31 * @endp: A pointer to the end of the parsed string will be placed here
32 * @base: The number base to use
34 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
36 unsigned long result = 0,value;
43 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
48 } else if (base == 16) {
49 if (cp[0] == '0' && toupper(cp[1]) == 'X')
52 while (isxdigit(*cp) &&
53 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
54 result = result*base + value;
62 EXPORT_SYMBOL(simple_strtoul);
65 * simple_strtol - convert a string to a signed long
66 * @cp: The start of the string
67 * @endp: A pointer to the end of the parsed string will be placed here
68 * @base: The number base to use
70 long simple_strtol(const char *cp,char **endp,unsigned int base)
73 return -simple_strtoul(cp+1,endp,base);
74 return simple_strtoul(cp,endp,base);
77 EXPORT_SYMBOL(simple_strtol);
80 * simple_strtoull - convert a string to an unsigned long long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
85 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
87 unsigned long long result = 0,value;
94 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
99 } else if (base == 16) {
100 if (cp[0] == '0' && toupper(cp[1]) == 'X')
103 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
104 ? toupper(*cp) : *cp)-'A'+10) < base) {
105 result = result*base + value;
113 EXPORT_SYMBOL(simple_strtoull);
116 * simple_strtoll - convert a string to a signed long long
117 * @cp: The start of the string
118 * @endp: A pointer to the end of the parsed string will be placed here
119 * @base: The number base to use
121 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
124 return -simple_strtoull(cp+1,endp,base);
125 return simple_strtoull(cp,endp,base);
128 static int skip_atoi(const char **s)
133 i = i*10 + *((*s)++) - '0';
137 #define ZEROPAD 1 /* pad with zero */
138 #define SIGN 2 /* unsigned/signed long */
139 #define PLUS 4 /* show plus */
140 #define SPACE 8 /* space if plus */
141 #define LEFT 16 /* left justified */
142 #define SPECIAL 32 /* 0x */
143 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
145 static char * number(char * buf, char * end, unsigned long long num, int base, int size, int precision, int type)
149 static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
150 static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
153 digits = (type & LARGE) ? large_digits : small_digits;
156 if (base < 2 || base > 36)
158 c = (type & ZEROPAD) ? '0' : ' ';
161 if ((signed long long) num < 0) {
163 num = - (signed long long) num;
165 } else if (type & PLUS) {
168 } else if (type & SPACE) {
173 if (type & SPECIAL) {
182 else while (num != 0)
183 tmp[i++] = digits[do_div(num,base)];
187 if (!(type&(ZEROPAD+LEFT))) {
199 if (type & SPECIAL) {
204 } else if (base==16) {
213 if (!(type & LEFT)) {
220 while (i < precision--) {
239 * vsnprintf - Format a string and place it in a buffer
240 * @buf: The buffer to place the result into
241 * @size: The size of the buffer, including the trailing null space
242 * @fmt: The format string to use
243 * @args: Arguments for the format string
245 * The return value is the number of characters which would
246 * be generated for the given input, excluding the trailing
247 * '\0', as per ISO C99. If you want to have the exact
248 * number of characters written into @buf as return value
249 * (not including the trailing '\0'), use vscnprintf. If the
250 * return is greater than or equal to @size, the resulting
251 * string is truncated.
253 * Call this function if you are already dealing with a va_list.
254 * You probably want snprintf instead.
256 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
259 unsigned long long num;
264 int flags; /* flags to number() */
266 int field_width; /* width of output field */
267 int precision; /* min. # of digits for integers; max
268 number of chars for from string */
269 int qualifier; /* 'h', 'l', or 'L' for integer fields */
270 /* 'z' support added 23/7/1999 S.H. */
271 /* 'z' changed to 'Z' --davidm 1/25/99 */
273 /* Reject out-of-range values early */
274 if (unlikely((int) size < 0)) {
275 /* There can be only one.. */
283 end = buf + size - 1;
287 size = end - buf + 1;
290 for (; *fmt ; ++fmt) {
301 ++fmt; /* this also skips first '%' */
303 case '-': flags |= LEFT; goto repeat;
304 case '+': flags |= PLUS; goto repeat;
305 case ' ': flags |= SPACE; goto repeat;
306 case '#': flags |= SPECIAL; goto repeat;
307 case '0': flags |= ZEROPAD; goto repeat;
310 /* get field width */
313 field_width = skip_atoi(&fmt);
314 else if (*fmt == '*') {
316 /* it's the next argument */
317 field_width = va_arg(args, int);
318 if (field_width < 0) {
319 field_width = -field_width;
324 /* get the precision */
329 precision = skip_atoi(&fmt);
330 else if (*fmt == '*') {
332 /* it's the next argument */
333 precision = va_arg(args, int);
339 /* get the conversion qualifier */
341 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
342 *fmt =='Z' || *fmt == 'z') {
345 if (qualifier == 'l' && *fmt == 'l') {
356 if (!(flags & LEFT)) {
357 while (--field_width > 0) {
363 c = (unsigned char) va_arg(args, int);
367 while (--field_width > 0) {
375 s = va_arg(args, char *);
376 if ((unsigned long)s < PAGE_SIZE)
379 len = strnlen(s, precision);
381 if (!(flags & LEFT)) {
382 while (len < field_width--) {
388 for (i = 0; i < len; ++i) {
393 while (len < field_width--) {
401 if (field_width == -1) {
402 field_width = 2*sizeof(void *);
405 str = number(str, end,
406 (unsigned long) va_arg(args, void *),
407 16, field_width, precision, flags);
413 * What does C99 say about the overflow case here? */
414 if (qualifier == 'l') {
415 long * ip = va_arg(args, long *);
417 } else if (qualifier == 'Z' || qualifier == 'z') {
418 size_t * ip = va_arg(args, size_t *);
421 int * ip = va_arg(args, int *);
432 /* integer number formats - set up the flags and "break" */
462 if (qualifier == 'L')
463 num = va_arg(args, long long);
464 else if (qualifier == 'l') {
465 num = va_arg(args, unsigned long);
467 num = (signed long) num;
468 } else if (qualifier == 'Z' || qualifier == 'z') {
469 num = va_arg(args, size_t);
470 } else if (qualifier == 'h') {
471 num = (unsigned short) va_arg(args, int);
473 num = (signed short) num;
475 num = va_arg(args, unsigned int);
477 num = (signed int) num;
479 str = number(str, end, num, base,
480 field_width, precision, flags);
485 /* don't write out a null byte if the buf size is zero */
487 /* the trailing null byte doesn't count towards the total
493 EXPORT_SYMBOL(vsnprintf);
496 * vscnprintf - Format a string and place it in a buffer
497 * @buf: The buffer to place the result into
498 * @size: The size of the buffer, including the trailing null space
499 * @fmt: The format string to use
500 * @args: Arguments for the format string
502 * The return value is the number of characters which have been written into
503 * the @buf not including the trailing '\0'. If @size is <= 0 the function
506 * Call this function if you are already dealing with a va_list.
507 * You probably want scnprintf instead.
509 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
513 i=vsnprintf(buf,size,fmt,args);
514 return (i >= size) ? (size - 1) : i;
517 EXPORT_SYMBOL(vscnprintf);
520 * snprintf - Format a string and place it in a buffer
521 * @buf: The buffer to place the result into
522 * @size: The size of the buffer, including the trailing null space
523 * @fmt: The format string to use
524 * @...: Arguments for the format string
526 * The return value is the number of characters which would be
527 * generated for the given input, excluding the trailing null,
528 * as per ISO C99. If the return is greater than or equal to
529 * @size, the resulting string is truncated.
531 int snprintf(char * buf, size_t size, const char *fmt, ...)
537 i=vsnprintf(buf,size,fmt,args);
542 EXPORT_SYMBOL(snprintf);
545 * scnprintf - Format a string and place it in a buffer
546 * @buf: The buffer to place the result into
547 * @size: The size of the buffer, including the trailing null space
548 * @fmt: The format string to use
549 * @...: Arguments for the format string
551 * The return value is the number of characters written into @buf not including
552 * the trailing '\0'. If @size is <= 0 the function returns 0. If the return is
553 * greater than or equal to @size, the resulting string is truncated.
556 int scnprintf(char * buf, size_t size, const char *fmt, ...)
562 i = vsnprintf(buf, size, fmt, args);
564 return (i >= size) ? (size - 1) : i;
566 EXPORT_SYMBOL(scnprintf);
569 * vsprintf - Format a string and place it in a buffer
570 * @buf: The buffer to place the result into
571 * @fmt: The format string to use
572 * @args: Arguments for the format string
574 * The function returns the number of characters written
575 * into @buf. Use vsnprintf or vscnprintf in order to avoid
578 * Call this function if you are already dealing with a va_list.
579 * You probably want sprintf instead.
581 int vsprintf(char *buf, const char *fmt, va_list args)
583 return vsnprintf(buf, INT_MAX, fmt, args);
586 EXPORT_SYMBOL(vsprintf);
589 * sprintf - Format a string and place it in a buffer
590 * @buf: The buffer to place the result into
591 * @fmt: The format string to use
592 * @...: Arguments for the format string
594 * The function returns the number of characters written
595 * into @buf. Use snprintf or scnprintf in order to avoid
598 int sprintf(char * buf, const char *fmt, ...)
604 i=vsnprintf(buf, INT_MAX, fmt, args);
609 EXPORT_SYMBOL(sprintf);
612 * vsscanf - Unformat a buffer into a list of arguments
614 * @fmt: format of buffer
617 int vsscanf(const char * buf, const char * fmt, va_list args)
619 const char *str = buf;
628 while(*fmt && *str) {
629 /* skip any white space in format */
630 /* white space in format matchs any amount of
631 * white space, including none, in the input.
634 while (isspace(*fmt))
636 while (isspace(*str))
640 /* anything that is not a conversion must match exactly */
641 if (*fmt != '%' && *fmt) {
642 if (*fmt++ != *str++)
651 /* skip this conversion.
652 * advance both strings to next white space
655 while (!isspace(*fmt) && *fmt)
657 while (!isspace(*str) && *str)
662 /* get field width */
665 field_width = skip_atoi(&fmt);
667 /* get conversion qualifier */
669 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
670 *fmt == 'Z' || *fmt == 'z') {
672 if (unlikely(qualifier == *fmt)) {
673 if (qualifier == 'h') {
676 } else if (qualifier == 'l') {
691 char *s = (char *) va_arg(args,char*);
692 if (field_width == -1)
696 } while (--field_width > 0 && *str);
702 char *s = (char *) va_arg(args, char *);
703 if(field_width == -1)
704 field_width = INT_MAX;
705 /* first, skip leading white space in buffer */
706 while (isspace(*str))
709 /* now copy until next white space */
710 while (*str && !isspace(*str) && field_width--) {
718 /* return number of characters read so far */
720 int *i = (int *)va_arg(args,int*);
738 /* looking for '%' in str */
743 /* invalid format; stop here */
747 /* have some sort of integer conversion.
748 * first, skip white space in buffer.
750 while (isspace(*str))
754 if (is_sign && digit == '-')
758 || (base == 16 && !isxdigit(digit))
759 || (base == 10 && !isdigit(digit))
760 || (base == 8 && (!isdigit(digit) || digit > '7'))
761 || (base == 0 && !isdigit(digit)))
765 case 'H': /* that's 'hh' in format */
767 signed char *s = (signed char *) va_arg(args,signed char *);
768 *s = (signed char) simple_strtol(str,&next,base);
770 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
771 *s = (unsigned char) simple_strtoul(str, &next, base);
776 short *s = (short *) va_arg(args,short *);
777 *s = (short) simple_strtol(str,&next,base);
779 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
780 *s = (unsigned short) simple_strtoul(str, &next, base);
785 long *l = (long *) va_arg(args,long *);
786 *l = simple_strtol(str,&next,base);
788 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
789 *l = simple_strtoul(str,&next,base);
794 long long *l = (long long*) va_arg(args,long long *);
795 *l = simple_strtoll(str,&next,base);
797 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
798 *l = simple_strtoull(str,&next,base);
804 size_t *s = (size_t*) va_arg(args,size_t*);
805 *s = (size_t) simple_strtoul(str,&next,base);
810 int *i = (int *) va_arg(args, int*);
811 *i = (int) simple_strtol(str,&next,base);
813 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
814 *i = (unsigned int) simple_strtoul(str,&next,base);
827 EXPORT_SYMBOL(vsscanf);
830 * sscanf - Unformat a buffer into a list of arguments
832 * @fmt: formatting of buffer
833 * @...: resulting arguments
835 int sscanf(const char * buf, const char * fmt, ...)
841 i = vsscanf(buf,fmt,args);
846 EXPORT_SYMBOL(sscanf);