paths changed
[kk_librfid.git] / firmware / lib / vsprintf.c
blob5da7c0242e7b2e24e12cbb4145fe2c7c40b79108
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 <sys/types.h>
21 #include <string.h>
22 #include <asm/ctype.h>
24 #include <asm/div64.h>
26 /**
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;
36 if (!base) {
37 base = 10;
38 if (*cp == '0') {
39 base = 8;
40 cp++;
41 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
42 cp++;
43 base = 16;
46 } else if (base == 16) {
47 if (cp[0] == '0' && toupper(cp[1]) == 'X')
48 cp += 2;
50 while (isxdigit(*cp) &&
51 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
52 result = result*base + value;
53 cp++;
55 if (endp)
56 *endp = (char *)cp;
57 return result;
61 /**
62 * simple_strtol - convert a string to a signed long
63 * @cp: The start of the string
64 * @endp: A pointer to the end of the parsed string will be placed here
65 * @base: The number base to use
67 long simple_strtol(const char *cp,char **endp,unsigned int base)
69 if(*cp=='-')
70 return -simple_strtoul(cp+1,endp,base);
71 return simple_strtoul(cp,endp,base);
75 /**
76 * simple_strtoull - convert a string to an unsigned long long
77 * @cp: The start of the string
78 * @endp: A pointer to the end of the parsed string will be placed here
79 * @base: The number base to use
81 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
83 unsigned long long result = 0,value;
85 if (!base) {
86 base = 10;
87 if (*cp == '0') {
88 base = 8;
89 cp++;
90 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
91 cp++;
92 base = 16;
95 } else if (base == 16) {
96 if (cp[0] == '0' && toupper(cp[1]) == 'X')
97 cp += 2;
99 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
100 ? toupper(*cp) : *cp)-'A'+10) < base) {
101 result = result*base + value;
102 cp++;
104 if (endp)
105 *endp = (char *)cp;
106 return result;
111 * simple_strtoll - convert a string to a signed long long
112 * @cp: The start of the string
113 * @endp: A pointer to the end of the parsed string will be placed here
114 * @base: The number base to use
116 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
118 if(*cp=='-')
119 return -simple_strtoull(cp+1,endp,base);
120 return simple_strtoull(cp,endp,base);
123 static int skip_atoi(const char **s)
125 int i=0;
127 while (isdigit(**s))
128 i = i*10 + *((*s)++) - '0';
129 return i;
132 #define ZEROPAD 1 /* pad with zero */
133 #define SIGN 2 /* unsigned/signed long */
134 #define PLUS 4 /* show plus */
135 #define SPACE 8 /* space if plus */
136 #define LEFT 16 /* left justified */
137 #define SPECIAL 32 /* 0x */
138 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
140 static char * number(char * buf, char * end, unsigned long long num, int base, int size, int precision, int type)
142 char c,sign,tmp[66];
143 const char *digits;
144 static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
145 static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
146 int i;
148 digits = (type & LARGE) ? large_digits : small_digits;
149 if (type & LEFT)
150 type &= ~ZEROPAD;
151 if (base < 2 || base > 36)
152 return NULL;
153 c = (type & ZEROPAD) ? '0' : ' ';
154 sign = 0;
155 if (type & SIGN) {
156 if ((signed long long) num < 0) {
157 sign = '-';
158 num = - (signed long long) num;
159 size--;
160 } else if (type & PLUS) {
161 sign = '+';
162 size--;
163 } else if (type & SPACE) {
164 sign = ' ';
165 size--;
168 if (type & SPECIAL) {
169 if (base == 16)
170 size -= 2;
171 else if (base == 8)
172 size--;
174 i = 0;
175 if (num == 0)
176 tmp[i++]='0';
177 else while (num != 0)
178 tmp[i++] = digits[do_div(num,base)];
179 if (i > precision)
180 precision = i;
181 size -= precision;
182 if (!(type&(ZEROPAD+LEFT))) {
183 while(size-->0) {
184 if (buf <= end)
185 *buf = ' ';
186 ++buf;
189 if (sign) {
190 if (buf <= end)
191 *buf = sign;
192 ++buf;
194 if (type & SPECIAL) {
195 if (base==8) {
196 if (buf <= end)
197 *buf = '0';
198 ++buf;
199 } else if (base==16) {
200 if (buf <= end)
201 *buf = '0';
202 ++buf;
203 if (buf <= end)
204 *buf = digits[33];
205 ++buf;
208 if (!(type & LEFT)) {
209 while (size-- > 0) {
210 if (buf <= end)
211 *buf = c;
212 ++buf;
215 while (i < precision--) {
216 if (buf <= end)
217 *buf = '0';
218 ++buf;
220 while (i-- > 0) {
221 if (buf <= end)
222 *buf = tmp[i];
223 ++buf;
225 while (size-- > 0) {
226 if (buf <= end)
227 *buf = ' ';
228 ++buf;
230 return buf;
234 * vsnprintf - Format a string and place it in a buffer
235 * @buf: The buffer to place the result into
236 * @size: The size of the buffer, including the trailing null space
237 * @fmt: The format string to use
238 * @args: Arguments for the format string
240 * The return value is the number of characters which would
241 * be generated for the given input, excluding the trailing
242 * '\0', as per ISO C99. If you want to have the exact
243 * number of characters written into @buf as return value
244 * (not including the trailing '\0'), use vscnprintf. If the
245 * return is greater than or equal to @size, the resulting
246 * string is truncated.
248 * Call this function if you are already dealing with a va_list.
249 * You probably want snprintf instead.
251 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
253 int len;
254 unsigned long long num;
255 int i, base;
256 char *str, *end, c;
257 const char *s;
259 int flags; /* flags to number() */
261 int field_width; /* width of output field */
262 int precision; /* min. # of digits for integers; max
263 number of chars for from string */
264 int qualifier; /* 'h', 'l', or 'L' for integer fields */
265 /* 'z' support added 23/7/1999 S.H. */
266 /* 'z' changed to 'Z' --davidm 1/25/99 */
267 /* 't' added for ptrdiff_t */
269 /* Reject out-of-range values early */
270 if ((int) size < 0) {
271 return 0;
274 str = buf;
275 end = buf + size - 1;
277 if (end < buf - 1) {
278 end = ((void *) -1);
279 size = end - buf + 1;
282 for (; *fmt ; ++fmt) {
283 if (*fmt != '%') {
284 if (str <= end)
285 *str = *fmt;
286 ++str;
287 continue;
290 /* process flags */
291 flags = 0;
292 repeat:
293 ++fmt; /* this also skips first '%' */
294 switch (*fmt) {
295 case '-': flags |= LEFT; goto repeat;
296 case '+': flags |= PLUS; goto repeat;
297 case ' ': flags |= SPACE; goto repeat;
298 case '#': flags |= SPECIAL; goto repeat;
299 case '0': flags |= ZEROPAD; goto repeat;
302 /* get field width */
303 field_width = -1;
304 if (isdigit(*fmt))
305 field_width = skip_atoi(&fmt);
306 else if (*fmt == '*') {
307 ++fmt;
308 /* it's the next argument */
309 field_width = va_arg(args, int);
310 if (field_width < 0) {
311 field_width = -field_width;
312 flags |= LEFT;
316 /* get the precision */
317 precision = -1;
318 if (*fmt == '.') {
319 ++fmt;
320 if (isdigit(*fmt))
321 precision = skip_atoi(&fmt);
322 else if (*fmt == '*') {
323 ++fmt;
324 /* it's the next argument */
325 precision = va_arg(args, int);
327 if (precision < 0)
328 precision = 0;
331 /* get the conversion qualifier */
332 qualifier = -1;
333 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
334 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
335 qualifier = *fmt;
336 ++fmt;
337 if (qualifier == 'l' && *fmt == 'l') {
338 qualifier = 'L';
339 ++fmt;
343 /* default base */
344 base = 10;
346 switch (*fmt) {
347 case 'c':
348 if (!(flags & LEFT)) {
349 while (--field_width > 0) {
350 if (str <= end)
351 *str = ' ';
352 ++str;
355 c = (unsigned char) va_arg(args, int);
356 if (str <= end)
357 *str = c;
358 ++str;
359 while (--field_width > 0) {
360 if (str <= end)
361 *str = ' ';
362 ++str;
364 continue;
366 case 's':
367 s = va_arg(args, char *);
369 len = strnlen(s, precision);
371 if (!(flags & LEFT)) {
372 while (len < field_width--) {
373 if (str <= end)
374 *str = ' ';
375 ++str;
378 for (i = 0; i < len; ++i) {
379 if (str <= end)
380 *str = *s;
381 ++str; ++s;
383 while (len < field_width--) {
384 if (str <= end)
385 *str = ' ';
386 ++str;
388 continue;
390 case 'p':
391 if (field_width == -1) {
392 field_width = 2*sizeof(void *);
393 flags |= ZEROPAD;
395 str = number(str, end,
396 (unsigned long) va_arg(args, void *),
397 16, field_width, precision, flags);
398 continue;
401 case 'n':
402 /* FIXME:
403 * What does C99 say about the overflow case here? */
404 if (qualifier == 'l') {
405 long * ip = va_arg(args, long *);
406 *ip = (str - buf);
407 } else if (qualifier == 'Z' || qualifier == 'z') {
408 size_t * ip = va_arg(args, size_t *);
409 *ip = (str - buf);
410 } else {
411 int * ip = va_arg(args, int *);
412 *ip = (str - buf);
414 continue;
416 case '%':
417 if (str <= end)
418 *str = '%';
419 ++str;
420 continue;
422 /* integer number formats - set up the flags and "break" */
423 case 'o':
424 base = 8;
425 break;
427 case 'X':
428 flags |= LARGE;
429 case 'x':
430 base = 16;
431 break;
433 case 'd':
434 case 'i':
435 flags |= SIGN;
436 case 'u':
437 break;
439 default:
440 if (str <= end)
441 *str = '%';
442 ++str;
443 if (*fmt) {
444 if (str <= end)
445 *str = *fmt;
446 ++str;
447 } else {
448 --fmt;
450 continue;
452 if (qualifier == 'L')
453 num = va_arg(args, long long);
454 else if (qualifier == 'l') {
455 num = va_arg(args, unsigned long);
456 if (flags & SIGN)
457 num = (signed long) num;
458 } else if (qualifier == 'Z' || qualifier == 'z') {
459 num = va_arg(args, size_t);
460 } else if (qualifier == 't') {
461 num = va_arg(args, ptrdiff_t);
462 } else if (qualifier == 'h') {
463 num = (unsigned short) va_arg(args, int);
464 if (flags & SIGN)
465 num = (signed short) num;
466 } else {
467 num = va_arg(args, unsigned int);
468 if (flags & SIGN)
469 num = (signed int) num;
471 str = number(str, end, num, base,
472 field_width, precision, flags);
474 if (str <= end)
475 *str = '\0';
476 else if (size > 0)
477 /* don't write out a null byte if the buf size is zero */
478 *end = '\0';
479 /* the trailing null byte doesn't count towards the total
480 * ++str;
482 return str-buf;
487 * vscnprintf - Format a string and place it in a buffer
488 * @buf: The buffer to place the result into
489 * @size: The size of the buffer, including the trailing null space
490 * @fmt: The format string to use
491 * @args: Arguments for the format string
493 * The return value is the number of characters which have been written into
494 * the @buf not including the trailing '\0'. If @size is <= 0 the function
495 * returns 0.
497 * Call this function if you are already dealing with a va_list.
498 * You probably want scnprintf instead.
500 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
502 unsigned int i;
504 i=vsnprintf(buf,size,fmt,args);
505 return (i >= size) ? (size - 1) : i;
510 * snprintf - Format a string and place it in a buffer
511 * @buf: The buffer to place the result into
512 * @size: The size of the buffer, including the trailing null space
513 * @fmt: The format string to use
514 * @...: Arguments for the format string
516 * The return value is the number of characters which would be
517 * generated for the given input, excluding the trailing null,
518 * as per ISO C99. If the return is greater than or equal to
519 * @size, the resulting string is truncated.
521 int snprintf(char * buf, size_t size, const char *fmt, ...)
523 va_list args;
524 int i;
526 va_start(args, fmt);
527 i=vsnprintf(buf,size,fmt,args);
528 va_end(args);
529 return i;
534 * scnprintf - Format a string and place it in a buffer
535 * @buf: The buffer to place the result into
536 * @size: The size of the buffer, including the trailing null space
537 * @fmt: The format string to use
538 * @...: Arguments for the format string
540 * The return value is the number of characters written into @buf not including
541 * the trailing '\0'. If @size is <= 0 the function returns 0. If the return is
542 * greater than or equal to @size, the resulting string is truncated.
545 int scnprintf(char * buf, size_t size, const char *fmt, ...)
547 va_list args;
548 unsigned int i;
550 va_start(args, fmt);
551 i = vsnprintf(buf, size, fmt, args);
552 va_end(args);
553 return (i >= size) ? (size - 1) : i;
557 * vsprintf - Format a string and place it in a buffer
558 * @buf: The buffer to place the result into
559 * @fmt: The format string to use
560 * @args: Arguments for the format string
562 * The function returns the number of characters written
563 * into @buf. Use vsnprintf or vscnprintf in order to avoid
564 * buffer overflows.
566 * Call this function if you are already dealing with a va_list.
567 * You probably want sprintf instead.
569 int vsprintf(char *buf, const char *fmt, va_list args)
571 return vsnprintf(buf, INT_MAX, fmt, args);
576 * sprintf - Format a string and place it in a buffer
577 * @buf: The buffer to place the result into
578 * @fmt: The format string to use
579 * @...: Arguments for the format string
581 * The function returns the number of characters written
582 * into @buf. Use snprintf or scnprintf in order to avoid
583 * buffer overflows.
585 int sprintf(char * buf, const char *fmt, ...)
587 va_list args;
588 int i;
590 va_start(args, fmt);
591 i=vsnprintf(buf, INT_MAX, fmt, args);
592 va_end(args);
593 return i;
598 * vsscanf - Unformat a buffer into a list of arguments
599 * @buf: input buffer
600 * @fmt: format of buffer
601 * @args: arguments
603 int vsscanf(const char * buf, const char * fmt, va_list args)
605 const char *str = buf;
606 char *next;
607 char digit;
608 int num = 0;
609 int qualifier;
610 int base;
611 int field_width;
612 int is_sign = 0;
614 while(*fmt && *str) {
615 /* skip any white space in format */
616 /* white space in format matchs any amount of
617 * white space, including none, in the input.
619 if (isspace(*fmt)) {
620 while (isspace(*fmt))
621 ++fmt;
622 while (isspace(*str))
623 ++str;
626 /* anything that is not a conversion must match exactly */
627 if (*fmt != '%' && *fmt) {
628 if (*fmt++ != *str++)
629 break;
630 continue;
633 if (!*fmt)
634 break;
635 ++fmt;
637 /* skip this conversion.
638 * advance both strings to next white space
640 if (*fmt == '*') {
641 while (!isspace(*fmt) && *fmt)
642 fmt++;
643 while (!isspace(*str) && *str)
644 str++;
645 continue;
648 /* get field width */
649 field_width = -1;
650 if (isdigit(*fmt))
651 field_width = skip_atoi(&fmt);
653 /* get conversion qualifier */
654 qualifier = -1;
655 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
656 *fmt == 'Z' || *fmt == 'z') {
657 qualifier = *fmt++;
658 if (qualifier == *fmt) {
659 if (qualifier == 'h') {
660 qualifier = 'H';
661 fmt++;
662 } else if (qualifier == 'l') {
663 qualifier = 'L';
664 fmt++;
668 base = 10;
669 is_sign = 0;
671 if (!*fmt || !*str)
672 break;
674 switch(*fmt++) {
675 case 'c':
677 char *s = (char *) va_arg(args,char*);
678 if (field_width == -1)
679 field_width = 1;
680 do {
681 *s++ = *str++;
682 } while (--field_width > 0 && *str);
683 num++;
685 continue;
686 case 's':
688 char *s = (char *) va_arg(args, char *);
689 if(field_width == -1)
690 field_width = INT_MAX;
691 /* first, skip leading white space in buffer */
692 while (isspace(*str))
693 str++;
695 /* now copy until next white space */
696 while (*str && !isspace(*str) && field_width--) {
697 *s++ = *str++;
699 *s = '\0';
700 num++;
702 continue;
703 case 'n':
704 /* return number of characters read so far */
706 int *i = (int *)va_arg(args,int*);
707 *i = str - buf;
709 continue;
710 case 'o':
711 base = 8;
712 break;
713 case 'x':
714 case 'X':
715 base = 16;
716 break;
717 case 'i':
718 base = 0;
719 case 'd':
720 is_sign = 1;
721 case 'u':
722 break;
723 case '%':
724 /* looking for '%' in str */
725 if (*str++ != '%')
726 return num;
727 continue;
728 default:
729 /* invalid format; stop here */
730 return num;
733 /* have some sort of integer conversion.
734 * first, skip white space in buffer.
736 while (isspace(*str))
737 str++;
739 digit = *str;
740 if (is_sign && digit == '-')
741 digit = *(str + 1);
743 if (!digit
744 || (base == 16 && !isxdigit(digit))
745 || (base == 10 && !isdigit(digit))
746 || (base == 8 && (!isdigit(digit) || digit > '7'))
747 || (base == 0 && !isdigit(digit)))
748 break;
750 switch(qualifier) {
751 case 'H': /* that's 'hh' in format */
752 if (is_sign) {
753 signed char *s = (signed char *) va_arg(args,signed char *);
754 *s = (signed char) simple_strtol(str,&next,base);
755 } else {
756 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
757 *s = (unsigned char) simple_strtoul(str, &next, base);
759 break;
760 case 'h':
761 if (is_sign) {
762 short *s = (short *) va_arg(args,short *);
763 *s = (short) simple_strtol(str,&next,base);
764 } else {
765 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
766 *s = (unsigned short) simple_strtoul(str, &next, base);
768 break;
769 case 'l':
770 if (is_sign) {
771 long *l = (long *) va_arg(args,long *);
772 *l = simple_strtol(str,&next,base);
773 } else {
774 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
775 *l = simple_strtoul(str,&next,base);
777 break;
778 case 'L':
779 if (is_sign) {
780 long long *l = (long long*) va_arg(args,long long *);
781 *l = simple_strtoll(str,&next,base);
782 } else {
783 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
784 *l = simple_strtoull(str,&next,base);
786 break;
787 case 'Z':
788 case 'z':
790 size_t *s = (size_t*) va_arg(args,size_t*);
791 *s = (size_t) simple_strtoul(str,&next,base);
793 break;
794 default:
795 if (is_sign) {
796 int *i = (int *) va_arg(args, int*);
797 *i = (int) simple_strtol(str,&next,base);
798 } else {
799 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
800 *i = (unsigned int) simple_strtoul(str,&next,base);
802 break;
804 num++;
806 if (!next)
807 break;
808 str = next;
810 return num;
815 * sscanf - Unformat a buffer into a list of arguments
816 * @buf: input buffer
817 * @fmt: formatting of buffer
818 * @...: resulting arguments
820 int sscanf(const char * buf, const char * fmt, ...)
822 va_list args;
823 int i;
825 va_start(args,fmt);
826 i = vsscanf(buf,fmt,args);
827 va_end(args);
828 return i;