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
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/kernel.h>
23 #include <asm/div64.h>
27 * simple_strtoul - convert a string to an unsigned long
28 * @cp: The start of the string
29 * @endp: A pointer to the end of the parsed string will be placed here
30 * @base: The number base to use
32 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
34 unsigned long result = 0,value;
41 if ((*cp == 'x') && isxdigit(cp[1])) {
47 while (isxdigit(*cp) &&
48 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
49 result = result*base + value;
58 * simple_strtol - convert a string to a signed long
59 * @cp: The start of the string
60 * @endp: A pointer to the end of the parsed string will be placed here
61 * @base: The number base to use
63 long simple_strtol(const char *cp,char **endp,unsigned int base)
66 return -simple_strtoul(cp+1,endp,base);
67 return simple_strtoul(cp,endp,base);
71 * simple_strtoull - convert a string to an unsigned long 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 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
78 unsigned long long result = 0,value;
85 if ((*cp == 'x') && isxdigit(cp[1])) {
91 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
92 ? toupper(*cp) : *cp)-'A'+10) < base) {
93 result = result*base + value;
102 * simple_strtoll - convert a string to a signed long long
103 * @cp: The start of the string
104 * @endp: A pointer to the end of the parsed string will be placed here
105 * @base: The number base to use
107 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
110 return -simple_strtoull(cp+1,endp,base);
111 return simple_strtoull(cp,endp,base);
114 static int skip_atoi(const char **s)
119 i = i*10 + *((*s)++) - '0';
123 #define ZEROPAD 1 /* pad with zero */
124 #define SIGN 2 /* unsigned/signed long */
125 #define PLUS 4 /* show plus */
126 #define SPACE 8 /* space if plus */
127 #define LEFT 16 /* left justified */
128 #define SPECIAL 32 /* 0x */
129 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
131 static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type)
135 static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
136 static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
139 digits = (type & LARGE) ? large_digits : small_digits;
142 if (base < 2 || base > 36)
144 c = (type & ZEROPAD) ? '0' : ' ';
151 } else if (type & PLUS) {
154 } else if (type & SPACE) {
159 if (type & SPECIAL) {
168 else while (num != 0)
169 tmp[i++] = digits[do_div(num,base)];
173 if (!(type&(ZEROPAD+LEFT))) {
185 if (type & SPECIAL) {
190 } else if (base==16) {
199 if (!(type & LEFT)) {
206 while (i < precision--) {
225 * vsnprintf - Format a string and place it in a buffer
226 * @buf: The buffer to place the result into
227 * @size: The size of the buffer, including the trailing null space
228 * @fmt: The format string to use
229 * @args: Arguments for the format string
231 * Call this function if you are already dealing with a va_list.
232 * You probably want snprintf instead.
234 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
237 unsigned long long num;
242 int flags; /* flags to number() */
244 int field_width; /* width of output field */
245 int precision; /* min. # of digits for integers; max
246 number of chars for from string */
247 int qualifier; /* 'h', 'l', or 'L' for integer fields */
248 /* 'z' support added 23/7/1999 S.H. */
249 /* 'z' changed to 'Z' --davidm 1/25/99 */
251 /* Reject out-of-range values early */
252 if (unlikely((int) size < 0)) {
253 /* There can be only one.. */
256 printk(KERN_WARNING "improper call of vsnprintf!\n");
264 end = buf + size - 1;
268 size = end - buf + 1;
271 for (; *fmt ; ++fmt) {
282 ++fmt; /* this also skips first '%' */
284 case '-': flags |= LEFT; goto repeat;
285 case '+': flags |= PLUS; goto repeat;
286 case ' ': flags |= SPACE; goto repeat;
287 case '#': flags |= SPECIAL; goto repeat;
288 case '0': flags |= ZEROPAD; goto repeat;
291 /* get field width */
294 field_width = skip_atoi(&fmt);
295 else if (*fmt == '*') {
297 /* it's the next argument */
298 field_width = va_arg(args, int);
299 if (field_width < 0) {
300 field_width = -field_width;
305 /* get the precision */
310 precision = skip_atoi(&fmt);
311 else if (*fmt == '*') {
313 /* it's the next argument */
314 precision = va_arg(args, int);
320 /* get the conversion qualifier */
322 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
323 *fmt =='Z' || *fmt == 'z') {
326 if (qualifier == 'l' && *fmt == 'l') {
337 if (!(flags & LEFT)) {
338 while (--field_width > 0) {
344 c = (unsigned char) va_arg(args, int);
348 while (--field_width > 0) {
356 s = va_arg(args, char *);
357 if ((unsigned long)s < PAGE_SIZE)
360 len = strnlen(s, precision);
362 if (!(flags & LEFT)) {
363 while (len < field_width--) {
369 for (i = 0; i < len; ++i) {
374 while (len < field_width--) {
382 if (field_width == -1) {
383 field_width = 2*sizeof(void *);
386 str = number(str, end,
387 (unsigned long) va_arg(args, void *),
388 16, field_width, precision, flags);
394 * What does C99 say about the overflow case here? */
395 if (qualifier == 'l') {
396 long * ip = va_arg(args, long *);
398 } else if (qualifier == 'Z' || qualifier == 'z') {
399 size_t * ip = va_arg(args, size_t *);
402 int * ip = va_arg(args, int *);
413 /* integer number formats - set up the flags and "break" */
443 if (qualifier == 'L')
444 num = va_arg(args, long long);
445 else if (qualifier == 'l') {
446 num = va_arg(args, unsigned long);
448 num = (signed long) num;
449 } else if (qualifier == 'Z' || qualifier == 'z') {
450 num = va_arg(args, size_t);
451 } else if (qualifier == 'h') {
452 num = (unsigned short) va_arg(args, int);
454 num = (signed short) num;
456 num = va_arg(args, unsigned int);
458 num = (signed int) num;
460 str = number(str, end, num, base,
461 field_width, precision, flags);
466 /* don't write out a null byte if the buf size is zero */
468 /* the trailing null byte doesn't count towards the total
475 * snprintf - Format a string and place it in a buffer
476 * @buf: The buffer to place the result into
477 * @size: The size of the buffer, including the trailing null space
478 * @fmt: The format string to use
479 * @...: Arguments for the format string
481 int snprintf(char * buf, size_t size, const char *fmt, ...)
487 i=vsnprintf(buf,size,fmt,args);
493 * vsprintf - Format a string and place it in a buffer
494 * @buf: The buffer to place the result into
495 * @fmt: The format string to use
496 * @args: Arguments for the format string
498 * Call this function if you are already dealing with a va_list.
499 * You probably want sprintf instead.
501 int vsprintf(char *buf, const char *fmt, va_list args)
503 return vsnprintf(buf, (~0U)>>1, fmt, args);
508 * sprintf - Format a string and place it in a buffer
509 * @buf: The buffer to place the result into
510 * @fmt: The format string to use
511 * @...: Arguments for the format string
513 int sprintf(char * buf, const char *fmt, ...)
519 i=vsprintf(buf,fmt,args);
525 * vsscanf - Unformat a buffer into a list of arguments
527 * @fmt: format of buffer
530 int vsscanf(const char * buf, const char * fmt, va_list args)
532 const char *str = buf;
541 while(*fmt && *str) {
542 /* skip any white space in format */
543 /* white space in format matchs any amount of
544 * white space, including none, in the input.
547 while (isspace(*fmt))
549 while (isspace(*str))
553 /* anything that is not a conversion must match exactly */
554 if (*fmt != '%' && *fmt) {
555 if (*fmt++ != *str++)
564 /* skip this conversion.
565 * advance both strings to next white space
568 while (!isspace(*fmt) && *fmt)
570 while (!isspace(*str) && *str)
575 /* get field width */
578 field_width = skip_atoi(&fmt);
580 /* get conversion qualifier */
582 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
583 *fmt == 'Z' || *fmt == 'z') {
596 char *s = (char *) va_arg(args,char*);
597 if (field_width == -1)
601 } while (--field_width > 0 && *str);
607 char *s = (char *) va_arg(args, char *);
608 if(field_width == -1)
609 field_width = INT_MAX;
610 /* first, skip leading white space in buffer */
611 while (isspace(*str))
614 /* now copy until next white space */
615 while (*str && !isspace(*str) && field_width--) {
623 /* return number of characters read so far */
625 int *i = (int *)va_arg(args,int*);
643 /* looking for '%' in str */
648 /* invalid format; stop here */
652 /* have some sort of integer conversion.
653 * first, skip white space in buffer.
655 while (isspace(*str))
659 if (is_sign && digit == '-')
663 || (base == 16 && !isxdigit(digit))
664 || (base == 10 && !isdigit(digit))
665 || (base == 8 && (!isdigit(digit) || digit > '7'))
666 || (base == 0 && !isdigit(digit)))
672 short *s = (short *) va_arg(args,short *);
673 *s = (short) simple_strtol(str,&next,base);
675 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
676 *s = (unsigned short) simple_strtoul(str, &next, base);
681 long *l = (long *) va_arg(args,long *);
682 *l = simple_strtol(str,&next,base);
684 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
685 *l = simple_strtoul(str,&next,base);
690 long long *l = (long long*) va_arg(args,long long *);
691 *l = simple_strtoll(str,&next,base);
693 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
694 *l = simple_strtoull(str,&next,base);
700 size_t *s = (size_t*) va_arg(args,size_t*);
701 *s = (size_t) simple_strtoul(str,&next,base);
706 int *i = (int *) va_arg(args, int*);
707 *i = (int) simple_strtol(str,&next,base);
709 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
710 *i = (unsigned int) simple_strtoul(str,&next,base);
724 * sscanf - Unformat a buffer into a list of arguments
726 * @fmt: formatting of buffer
727 * @...: resulting arguments
729 int sscanf(const char * buf, const char * fmt, ...)
735 i = vsscanf(buf,fmt,args);