xref: /linux-6.15/lib/vsprintf.c (revision f7275650)
1 /*
2  *  linux/lib/vsprintf.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6 
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9  * Wirzenius wrote this portably, Torvalds fucked it up :-)
10  */
11 
12 /*
13  * Fri Jul 13 2001 Crutcher Dunnavant <[email protected]>
14  * - changed to provide snprintf and vsnprintf functions
15  * So Feb  1 16:51:32 CET 2004 Juergen Quade <[email protected]>
16  * - scnprintf and vscnprintf
17  */
18 
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 
28 #include <asm/page.h>		/* for PAGE_SIZE */
29 #include <asm/div64.h>
30 #include <asm/sections.h>	/* for dereference_function_descriptor() */
31 
32 /* Works only for digits and letters, but small and fast */
33 #define TOLOWER(x) ((x) | 0x20)
34 
35 static unsigned int simple_guess_base(const char *cp)
36 {
37 	if (cp[0] == '0') {
38 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
39 			return 16;
40 		else
41 			return 8;
42 	} else {
43 		return 10;
44 	}
45 }
46 
47 /**
48  * simple_strtoul - convert a string to an unsigned long
49  * @cp: The start of the string
50  * @endp: A pointer to the end of the parsed string will be placed here
51  * @base: The number base to use
52  */
53 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
54 {
55 	unsigned long result = 0;
56 
57 	if (!base)
58 		base = simple_guess_base(cp);
59 
60 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
61 		cp += 2;
62 
63 	while (isxdigit(*cp)) {
64 		unsigned int value;
65 
66 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
67 		if (value >= base)
68 			break;
69 		result = result * base + value;
70 		cp++;
71 	}
72 
73 	if (endp)
74 		*endp = (char *)cp;
75 	return result;
76 }
77 EXPORT_SYMBOL(simple_strtoul);
78 
79 /**
80  * simple_strtol - convert a string to a signed 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
84  */
85 long simple_strtol(const char *cp, char **endp, unsigned int base)
86 {
87 	if(*cp == '-')
88 		return -simple_strtoul(cp + 1, endp, base);
89 	return simple_strtoul(cp, endp, base);
90 }
91 EXPORT_SYMBOL(simple_strtol);
92 
93 /**
94  * simple_strtoull - convert a string to an unsigned long long
95  * @cp: The start of the string
96  * @endp: A pointer to the end of the parsed string will be placed here
97  * @base: The number base to use
98  */
99 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
100 {
101 	unsigned long long result = 0;
102 
103 	if (!base)
104 		base = simple_guess_base(cp);
105 
106 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
107 		cp += 2;
108 
109 	while (isxdigit(*cp)) {
110 		unsigned int value;
111 
112 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
113 		if (value >= base)
114 			break;
115 		result = result * base + value;
116 		cp++;
117 	}
118 
119 	if (endp)
120 		*endp = (char *)cp;
121 	return result;
122 }
123 EXPORT_SYMBOL(simple_strtoull);
124 
125 /**
126  * simple_strtoll - convert a string to a signed long long
127  * @cp: The start of the string
128  * @endp: A pointer to the end of the parsed string will be placed here
129  * @base: The number base to use
130  */
131 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
132 {
133 	if(*cp=='-')
134 		return -simple_strtoull(cp + 1, endp, base);
135 	return simple_strtoull(cp, endp, base);
136 }
137 
138 /**
139  * strict_strtoul - convert a string to an unsigned long strictly
140  * @cp: The string to be converted
141  * @base: The number base to use
142  * @res: The converted result value
143  *
144  * strict_strtoul converts a string to an unsigned long only if the
145  * string is really an unsigned long string, any string containing
146  * any invalid char at the tail will be rejected and -EINVAL is returned,
147  * only a newline char at the tail is acceptible because people generally
148  * change a module parameter in the following way:
149  *
150  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
151  *
152  * echo will append a newline to the tail.
153  *
154  * It returns 0 if conversion is successful and *res is set to the converted
155  * value, otherwise it returns -EINVAL and *res is set to 0.
156  *
157  * simple_strtoul just ignores the successive invalid characters and
158  * return the converted value of prefix part of the string.
159  */
160 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
161 {
162 	char *tail;
163 	unsigned long val;
164 	size_t len;
165 
166 	*res = 0;
167 	len = strlen(cp);
168 	if (len == 0)
169 		return -EINVAL;
170 
171 	val = simple_strtoul(cp, &tail, base);
172 	if ((*tail == '\0') ||
173 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
174 		*res = val;
175 		return 0;
176 	}
177 
178 	return -EINVAL;
179 }
180 EXPORT_SYMBOL(strict_strtoul);
181 
182 /**
183  * strict_strtol - convert a string to a long strictly
184  * @cp: The string to be converted
185  * @base: The number base to use
186  * @res: The converted result value
187  *
188  * strict_strtol is similiar to strict_strtoul, but it allows the first
189  * character of a string is '-'.
190  *
191  * It returns 0 if conversion is successful and *res is set to the converted
192  * value, otherwise it returns -EINVAL and *res is set to 0.
193  */
194 int strict_strtol(const char *cp, unsigned int base, long *res)
195 {
196 	int ret;
197 	if (*cp == '-') {
198 		ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
199 		if (!ret)
200 			*res = -(*res);
201 	} else {
202 		ret = strict_strtoul(cp, base, (unsigned long *)res);
203 	}
204 
205 	return ret;
206 }
207 EXPORT_SYMBOL(strict_strtol);
208 
209 /**
210  * strict_strtoull - convert a string to an unsigned long long strictly
211  * @cp: The string to be converted
212  * @base: The number base to use
213  * @res: The converted result value
214  *
215  * strict_strtoull converts a string to an unsigned long long only if the
216  * string is really an unsigned long long string, any string containing
217  * any invalid char at the tail will be rejected and -EINVAL is returned,
218  * only a newline char at the tail is acceptible because people generally
219  * change a module parameter in the following way:
220  *
221  * 	echo 1024 > /sys/module/e1000/parameters/copybreak
222  *
223  * echo will append a newline to the tail of the string.
224  *
225  * It returns 0 if conversion is successful and *res is set to the converted
226  * value, otherwise it returns -EINVAL and *res is set to 0.
227  *
228  * simple_strtoull just ignores the successive invalid characters and
229  * return the converted value of prefix part of the string.
230  */
231 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
232 {
233 	char *tail;
234 	unsigned long long val;
235 	size_t len;
236 
237 	*res = 0;
238 	len = strlen(cp);
239 	if (len == 0)
240 		return -EINVAL;
241 
242 	val = simple_strtoull(cp, &tail, base);
243 	if ((*tail == '\0') ||
244 		((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
245 		*res = val;
246 		return 0;
247 	}
248 
249 	return -EINVAL;
250 }
251 EXPORT_SYMBOL(strict_strtoull);
252 
253 /**
254  * strict_strtoll - convert a string to a long long strictly
255  * @cp: The string to be converted
256  * @base: The number base to use
257  * @res: The converted result value
258  *
259  * strict_strtoll is similiar to strict_strtoull, but it allows the first
260  * character of a string is '-'.
261  *
262  * It returns 0 if conversion is successful and *res is set to the converted
263  * value, otherwise it returns -EINVAL and *res is set to 0.
264  */
265 int strict_strtoll(const char *cp, unsigned int base, long long *res)
266 {
267 	int ret;
268 	if (*cp == '-') {
269 		ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
270 		if (!ret)
271 			*res = -(*res);
272 	} else {
273 		ret = strict_strtoull(cp, base, (unsigned long long *)res);
274 	}
275 
276 	return ret;
277 }
278 EXPORT_SYMBOL(strict_strtoll);
279 
280 static int skip_atoi(const char **s)
281 {
282 	int i=0;
283 
284 	while (isdigit(**s))
285 		i = i*10 + *((*s)++) - '0';
286 	return i;
287 }
288 
289 /* Decimal conversion is by far the most typical, and is used
290  * for /proc and /sys data. This directly impacts e.g. top performance
291  * with many processes running. We optimize it for speed
292  * using code from
293  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
294  * (with permission from the author, Douglas W. Jones). */
295 
296 /* Formats correctly any integer in [0,99999].
297  * Outputs from one to five digits depending on input.
298  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
299 static char* put_dec_trunc(char *buf, unsigned q)
300 {
301 	unsigned d3, d2, d1, d0;
302 	d1 = (q>>4) & 0xf;
303 	d2 = (q>>8) & 0xf;
304 	d3 = (q>>12);
305 
306 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
307 	q = (d0 * 0xcd) >> 11;
308 	d0 = d0 - 10*q;
309 	*buf++ = d0 + '0'; /* least significant digit */
310 	d1 = q + 9*d3 + 5*d2 + d1;
311 	if (d1 != 0) {
312 		q = (d1 * 0xcd) >> 11;
313 		d1 = d1 - 10*q;
314 		*buf++ = d1 + '0'; /* next digit */
315 
316 		d2 = q + 2*d2;
317 		if ((d2 != 0) || (d3 != 0)) {
318 			q = (d2 * 0xd) >> 7;
319 			d2 = d2 - 10*q;
320 			*buf++ = d2 + '0'; /* next digit */
321 
322 			d3 = q + 4*d3;
323 			if (d3 != 0) {
324 				q = (d3 * 0xcd) >> 11;
325 				d3 = d3 - 10*q;
326 				*buf++ = d3 + '0';  /* next digit */
327 				if (q != 0)
328 					*buf++ = q + '0';  /* most sign. digit */
329 			}
330 		}
331 	}
332 	return buf;
333 }
334 /* Same with if's removed. Always emits five digits */
335 static char* put_dec_full(char *buf, unsigned q)
336 {
337 	/* BTW, if q is in [0,9999], 8-bit ints will be enough, */
338 	/* but anyway, gcc produces better code with full-sized ints */
339 	unsigned d3, d2, d1, d0;
340 	d1 = (q>>4) & 0xf;
341 	d2 = (q>>8) & 0xf;
342 	d3 = (q>>12);
343 
344 	/* Possible ways to approx. divide by 10 */
345 	/* gcc -O2 replaces multiply with shifts and adds */
346 	// (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
347 	// (x * 0x67) >> 10:  1100111
348 	// (x * 0x34) >> 9:    110100 - same
349 	// (x * 0x1a) >> 8:     11010 - same
350 	// (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
351 
352 	d0 = 6*(d3 + d2 + d1) + (q & 0xf);
353 	q = (d0 * 0xcd) >> 11;
354 	d0 = d0 - 10*q;
355 	*buf++ = d0 + '0';
356 	d1 = q + 9*d3 + 5*d2 + d1;
357 		q = (d1 * 0xcd) >> 11;
358 		d1 = d1 - 10*q;
359 		*buf++ = d1 + '0';
360 
361 		d2 = q + 2*d2;
362 			q = (d2 * 0xd) >> 7;
363 			d2 = d2 - 10*q;
364 			*buf++ = d2 + '0';
365 
366 			d3 = q + 4*d3;
367 				q = (d3 * 0xcd) >> 11; /* - shorter code */
368 				/* q = (d3 * 0x67) >> 10; - would also work */
369 				d3 = d3 - 10*q;
370 				*buf++ = d3 + '0';
371 					*buf++ = q + '0';
372 	return buf;
373 }
374 /* No inlining helps gcc to use registers better */
375 static noinline char* put_dec(char *buf, unsigned long long num)
376 {
377 	while (1) {
378 		unsigned rem;
379 		if (num < 100000)
380 			return put_dec_trunc(buf, num);
381 		rem = do_div(num, 100000);
382 		buf = put_dec_full(buf, rem);
383 	}
384 }
385 
386 #define ZEROPAD	1		/* pad with zero */
387 #define SIGN	2		/* unsigned/signed long */
388 #define PLUS	4		/* show plus */
389 #define SPACE	8		/* space if plus */
390 #define LEFT	16		/* left justified */
391 #define SMALL	32		/* Must be 32 == 0x20 */
392 #define SPECIAL	64		/* 0x */
393 
394 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
395 {
396 	/* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
397 	static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
398 
399 	char tmp[66];
400 	char sign;
401 	char locase;
402 	int need_pfx = ((type & SPECIAL) && base != 10);
403 	int i;
404 
405 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
406 	 * produces same digits or (maybe lowercased) letters */
407 	locase = (type & SMALL);
408 	if (type & LEFT)
409 		type &= ~ZEROPAD;
410 	sign = 0;
411 	if (type & SIGN) {
412 		if ((signed long long) num < 0) {
413 			sign = '-';
414 			num = - (signed long long) num;
415 			size--;
416 		} else if (type & PLUS) {
417 			sign = '+';
418 			size--;
419 		} else if (type & SPACE) {
420 			sign = ' ';
421 			size--;
422 		}
423 	}
424 	if (need_pfx) {
425 		size--;
426 		if (base == 16)
427 			size--;
428 	}
429 
430 	/* generate full string in tmp[], in reverse order */
431 	i = 0;
432 	if (num == 0)
433 		tmp[i++] = '0';
434 	/* Generic code, for any base:
435 	else do {
436 		tmp[i++] = (digits[do_div(num,base)] | locase);
437 	} while (num != 0);
438 	*/
439 	else if (base != 10) { /* 8 or 16 */
440 		int mask = base - 1;
441 		int shift = 3;
442 		if (base == 16) shift = 4;
443 		do {
444 			tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
445 			num >>= shift;
446 		} while (num);
447 	} else { /* base 10 */
448 		i = put_dec(tmp, num) - tmp;
449 	}
450 
451 	/* printing 100 using %2d gives "100", not "00" */
452 	if (i > precision)
453 		precision = i;
454 	/* leading space padding */
455 	size -= precision;
456 	if (!(type & (ZEROPAD+LEFT))) {
457 		while(--size >= 0) {
458 			if (buf < end)
459 				*buf = ' ';
460 			++buf;
461 		}
462 	}
463 	/* sign */
464 	if (sign) {
465 		if (buf < end)
466 			*buf = sign;
467 		++buf;
468 	}
469 	/* "0x" / "0" prefix */
470 	if (need_pfx) {
471 		if (buf < end)
472 			*buf = '0';
473 		++buf;
474 		if (base == 16) {
475 			if (buf < end)
476 				*buf = ('X' | locase);
477 			++buf;
478 		}
479 	}
480 	/* zero or space padding */
481 	if (!(type & LEFT)) {
482 		char c = (type & ZEROPAD) ? '0' : ' ';
483 		while (--size >= 0) {
484 			if (buf < end)
485 				*buf = c;
486 			++buf;
487 		}
488 	}
489 	/* hmm even more zero padding? */
490 	while (i <= --precision) {
491 		if (buf < end)
492 			*buf = '0';
493 		++buf;
494 	}
495 	/* actual digits of result */
496 	while (--i >= 0) {
497 		if (buf < end)
498 			*buf = tmp[i];
499 		++buf;
500 	}
501 	/* trailing space padding */
502 	while (--size >= 0) {
503 		if (buf < end)
504 			*buf = ' ';
505 		++buf;
506 	}
507 	return buf;
508 }
509 
510 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
511 {
512 	int len, i;
513 
514 	if ((unsigned long)s < PAGE_SIZE)
515 		s = "<NULL>";
516 
517 	len = strnlen(s, precision);
518 
519 	if (!(flags & LEFT)) {
520 		while (len < field_width--) {
521 			if (buf < end)
522 				*buf = ' ';
523 			++buf;
524 		}
525 	}
526 	for (i = 0; i < len; ++i) {
527 		if (buf < end)
528 			*buf = *s;
529 		++buf; ++s;
530 	}
531 	while (len < field_width--) {
532 		if (buf < end)
533 			*buf = ' ';
534 		++buf;
535 	}
536 	return buf;
537 }
538 
539 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
540 {
541 	unsigned long value = (unsigned long) ptr;
542 #ifdef CONFIG_KALLSYMS
543 	char sym[KSYM_SYMBOL_LEN];
544 	sprint_symbol(sym, value);
545 	return string(buf, end, sym, field_width, precision, flags);
546 #else
547 	field_width = 2*sizeof(void *);
548 	flags |= SPECIAL | SMALL | ZEROPAD;
549 	return number(buf, end, value, 16, field_width, precision, flags);
550 #endif
551 }
552 
553 /*
554  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
555  * by an extra set of alphanumeric characters that are extended format
556  * specifiers.
557  *
558  * Right now we just handle 'F' (for symbolic Function descriptor pointers)
559  * and 'S' (for Symbolic direct pointers), but this can easily be
560  * extended in the future (network address types etc).
561  *
562  * The difference between 'S' and 'F' is that on ia64 and ppc64 function
563  * pointers are really function descriptors, which contain a pointer the
564  * real address.
565  */
566 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
567 {
568 	switch (*fmt) {
569 	case 'F':
570 		ptr = dereference_function_descriptor(ptr);
571 		/* Fallthrough */
572 	case 'S':
573 		return symbol_string(buf, end, ptr, field_width, precision, flags);
574 	}
575 	flags |= SMALL;
576 	if (field_width == -1) {
577 		field_width = 2*sizeof(void *);
578 		flags |= ZEROPAD;
579 	}
580 	return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
581 }
582 
583 /**
584  * vsnprintf - Format a string and place it in a buffer
585  * @buf: The buffer to place the result into
586  * @size: The size of the buffer, including the trailing null space
587  * @fmt: The format string to use
588  * @args: Arguments for the format string
589  *
590  * This function follows C99 vsnprintf, but has some extensions:
591  * %pS output the name of a text symbol
592  * %pF output the name of a function pointer
593  *
594  * The return value is the number of characters which would
595  * be generated for the given input, excluding the trailing
596  * '\0', as per ISO C99. If you want to have the exact
597  * number of characters written into @buf as return value
598  * (not including the trailing '\0'), use vscnprintf(). If the
599  * return is greater than or equal to @size, the resulting
600  * string is truncated.
601  *
602  * Call this function if you are already dealing with a va_list.
603  * You probably want snprintf() instead.
604  */
605 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
606 {
607 	unsigned long long num;
608 	int base;
609 	char *str, *end, c;
610 
611 	int flags;		/* flags to number() */
612 
613 	int field_width;	/* width of output field */
614 	int precision;		/* min. # of digits for integers; max
615 				   number of chars for from string */
616 	int qualifier;		/* 'h', 'l', or 'L' for integer fields */
617 				/* 'z' support added 23/7/1999 S.H.    */
618 				/* 'z' changed to 'Z' --davidm 1/25/99 */
619 				/* 't' added for ptrdiff_t */
620 
621 	/* Reject out-of-range values early.  Large positive sizes are
622 	   used for unknown buffer sizes. */
623 	if (unlikely((int) size < 0)) {
624 		/* There can be only one.. */
625 		static char warn = 1;
626 		WARN_ON(warn);
627 		warn = 0;
628 		return 0;
629 	}
630 
631 	str = buf;
632 	end = buf + size;
633 
634 	/* Make sure end is always >= buf */
635 	if (end < buf) {
636 		end = ((void *)-1);
637 		size = end - buf;
638 	}
639 
640 	for (; *fmt ; ++fmt) {
641 		if (*fmt != '%') {
642 			if (str < end)
643 				*str = *fmt;
644 			++str;
645 			continue;
646 		}
647 
648 		/* process flags */
649 		flags = 0;
650 		repeat:
651 			++fmt;		/* this also skips first '%' */
652 			switch (*fmt) {
653 				case '-': flags |= LEFT; goto repeat;
654 				case '+': flags |= PLUS; goto repeat;
655 				case ' ': flags |= SPACE; goto repeat;
656 				case '#': flags |= SPECIAL; goto repeat;
657 				case '0': flags |= ZEROPAD; goto repeat;
658 			}
659 
660 		/* get field width */
661 		field_width = -1;
662 		if (isdigit(*fmt))
663 			field_width = skip_atoi(&fmt);
664 		else if (*fmt == '*') {
665 			++fmt;
666 			/* it's the next argument */
667 			field_width = va_arg(args, int);
668 			if (field_width < 0) {
669 				field_width = -field_width;
670 				flags |= LEFT;
671 			}
672 		}
673 
674 		/* get the precision */
675 		precision = -1;
676 		if (*fmt == '.') {
677 			++fmt;
678 			if (isdigit(*fmt))
679 				precision = skip_atoi(&fmt);
680 			else if (*fmt == '*') {
681 				++fmt;
682 				/* it's the next argument */
683 				precision = va_arg(args, int);
684 			}
685 			if (precision < 0)
686 				precision = 0;
687 		}
688 
689 		/* get the conversion qualifier */
690 		qualifier = -1;
691 		if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
692 		    *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
693 			qualifier = *fmt;
694 			++fmt;
695 			if (qualifier == 'l' && *fmt == 'l') {
696 				qualifier = 'L';
697 				++fmt;
698 			}
699 		}
700 
701 		/* default base */
702 		base = 10;
703 
704 		switch (*fmt) {
705 			case 'c':
706 				if (!(flags & LEFT)) {
707 					while (--field_width > 0) {
708 						if (str < end)
709 							*str = ' ';
710 						++str;
711 					}
712 				}
713 				c = (unsigned char) va_arg(args, int);
714 				if (str < end)
715 					*str = c;
716 				++str;
717 				while (--field_width > 0) {
718 					if (str < end)
719 						*str = ' ';
720 					++str;
721 				}
722 				continue;
723 
724 			case 's':
725 				str = string(str, end, va_arg(args, char *), field_width, precision, flags);
726 				continue;
727 
728 			case 'p':
729 				str = pointer(fmt+1, str, end,
730 						va_arg(args, void *),
731 						field_width, precision, flags);
732 				/* Skip all alphanumeric pointer suffixes */
733 				while (isalnum(fmt[1]))
734 					fmt++;
735 				continue;
736 
737 			case 'n':
738 				/* FIXME:
739 				* What does C99 say about the overflow case here? */
740 				if (qualifier == 'l') {
741 					long * ip = va_arg(args, long *);
742 					*ip = (str - buf);
743 				} else if (qualifier == 'Z' || qualifier == 'z') {
744 					size_t * ip = va_arg(args, size_t *);
745 					*ip = (str - buf);
746 				} else {
747 					int * ip = va_arg(args, int *);
748 					*ip = (str - buf);
749 				}
750 				continue;
751 
752 			case '%':
753 				if (str < end)
754 					*str = '%';
755 				++str;
756 				continue;
757 
758 				/* integer number formats - set up the flags and "break" */
759 			case 'o':
760 				base = 8;
761 				break;
762 
763 			case 'x':
764 				flags |= SMALL;
765 			case 'X':
766 				base = 16;
767 				break;
768 
769 			case 'd':
770 			case 'i':
771 				flags |= SIGN;
772 			case 'u':
773 				break;
774 
775 			default:
776 				if (str < end)
777 					*str = '%';
778 				++str;
779 				if (*fmt) {
780 					if (str < end)
781 						*str = *fmt;
782 					++str;
783 				} else {
784 					--fmt;
785 				}
786 				continue;
787 		}
788 		if (qualifier == 'L')
789 			num = va_arg(args, long long);
790 		else if (qualifier == 'l') {
791 			num = va_arg(args, unsigned long);
792 			if (flags & SIGN)
793 				num = (signed long) num;
794 		} else if (qualifier == 'Z' || qualifier == 'z') {
795 			num = va_arg(args, size_t);
796 		} else if (qualifier == 't') {
797 			num = va_arg(args, ptrdiff_t);
798 		} else if (qualifier == 'h') {
799 			num = (unsigned short) va_arg(args, int);
800 			if (flags & SIGN)
801 				num = (signed short) num;
802 		} else {
803 			num = va_arg(args, unsigned int);
804 			if (flags & SIGN)
805 				num = (signed int) num;
806 		}
807 		str = number(str, end, num, base,
808 				field_width, precision, flags);
809 	}
810 	if (size > 0) {
811 		if (str < end)
812 			*str = '\0';
813 		else
814 			end[-1] = '\0';
815 	}
816 	/* the trailing null byte doesn't count towards the total */
817 	return str-buf;
818 }
819 EXPORT_SYMBOL(vsnprintf);
820 
821 /**
822  * vscnprintf - Format a string and place it in a buffer
823  * @buf: The buffer to place the result into
824  * @size: The size of the buffer, including the trailing null space
825  * @fmt: The format string to use
826  * @args: Arguments for the format string
827  *
828  * The return value is the number of characters which have been written into
829  * the @buf not including the trailing '\0'. If @size is <= 0 the function
830  * returns 0.
831  *
832  * Call this function if you are already dealing with a va_list.
833  * You probably want scnprintf() instead.
834  *
835  * See the vsnprintf() documentation for format string extensions over C99.
836  */
837 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
838 {
839 	int i;
840 
841 	i=vsnprintf(buf,size,fmt,args);
842 	return (i >= size) ? (size - 1) : i;
843 }
844 EXPORT_SYMBOL(vscnprintf);
845 
846 /**
847  * snprintf - Format a string and place it in a buffer
848  * @buf: The buffer to place the result into
849  * @size: The size of the buffer, including the trailing null space
850  * @fmt: The format string to use
851  * @...: Arguments for the format string
852  *
853  * The return value is the number of characters which would be
854  * generated for the given input, excluding the trailing null,
855  * as per ISO C99.  If the return is greater than or equal to
856  * @size, the resulting string is truncated.
857  *
858  * See the vsnprintf() documentation for format string extensions over C99.
859  */
860 int snprintf(char * buf, size_t size, const char *fmt, ...)
861 {
862 	va_list args;
863 	int i;
864 
865 	va_start(args, fmt);
866 	i=vsnprintf(buf,size,fmt,args);
867 	va_end(args);
868 	return i;
869 }
870 EXPORT_SYMBOL(snprintf);
871 
872 /**
873  * scnprintf - Format a string and place it in a buffer
874  * @buf: The buffer to place the result into
875  * @size: The size of the buffer, including the trailing null space
876  * @fmt: The format string to use
877  * @...: Arguments for the format string
878  *
879  * The return value is the number of characters written into @buf not including
880  * the trailing '\0'. If @size is <= 0 the function returns 0.
881  */
882 
883 int scnprintf(char * buf, size_t size, const char *fmt, ...)
884 {
885 	va_list args;
886 	int i;
887 
888 	va_start(args, fmt);
889 	i = vsnprintf(buf, size, fmt, args);
890 	va_end(args);
891 	return (i >= size) ? (size - 1) : i;
892 }
893 EXPORT_SYMBOL(scnprintf);
894 
895 /**
896  * vsprintf - Format a string and place it in a buffer
897  * @buf: The buffer to place the result into
898  * @fmt: The format string to use
899  * @args: Arguments for the format string
900  *
901  * The function returns the number of characters written
902  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
903  * buffer overflows.
904  *
905  * Call this function if you are already dealing with a va_list.
906  * You probably want sprintf() instead.
907  *
908  * See the vsnprintf() documentation for format string extensions over C99.
909  */
910 int vsprintf(char *buf, const char *fmt, va_list args)
911 {
912 	return vsnprintf(buf, INT_MAX, fmt, args);
913 }
914 EXPORT_SYMBOL(vsprintf);
915 
916 /**
917  * sprintf - Format a string and place it in a buffer
918  * @buf: The buffer to place the result into
919  * @fmt: The format string to use
920  * @...: Arguments for the format string
921  *
922  * The function returns the number of characters written
923  * into @buf. Use snprintf() or scnprintf() in order to avoid
924  * buffer overflows.
925  *
926  * See the vsnprintf() documentation for format string extensions over C99.
927  */
928 int sprintf(char * buf, const char *fmt, ...)
929 {
930 	va_list args;
931 	int i;
932 
933 	va_start(args, fmt);
934 	i=vsnprintf(buf, INT_MAX, fmt, args);
935 	va_end(args);
936 	return i;
937 }
938 EXPORT_SYMBOL(sprintf);
939 
940 /**
941  * vsscanf - Unformat a buffer into a list of arguments
942  * @buf:	input buffer
943  * @fmt:	format of buffer
944  * @args:	arguments
945  */
946 int vsscanf(const char * buf, const char * fmt, va_list args)
947 {
948 	const char *str = buf;
949 	char *next;
950 	char digit;
951 	int num = 0;
952 	int qualifier;
953 	int base;
954 	int field_width;
955 	int is_sign = 0;
956 
957 	while(*fmt && *str) {
958 		/* skip any white space in format */
959 		/* white space in format matchs any amount of
960 		 * white space, including none, in the input.
961 		 */
962 		if (isspace(*fmt)) {
963 			while (isspace(*fmt))
964 				++fmt;
965 			while (isspace(*str))
966 				++str;
967 		}
968 
969 		/* anything that is not a conversion must match exactly */
970 		if (*fmt != '%' && *fmt) {
971 			if (*fmt++ != *str++)
972 				break;
973 			continue;
974 		}
975 
976 		if (!*fmt)
977 			break;
978 		++fmt;
979 
980 		/* skip this conversion.
981 		 * advance both strings to next white space
982 		 */
983 		if (*fmt == '*') {
984 			while (!isspace(*fmt) && *fmt)
985 				fmt++;
986 			while (!isspace(*str) && *str)
987 				str++;
988 			continue;
989 		}
990 
991 		/* get field width */
992 		field_width = -1;
993 		if (isdigit(*fmt))
994 			field_width = skip_atoi(&fmt);
995 
996 		/* get conversion qualifier */
997 		qualifier = -1;
998 		if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
999 		    *fmt == 'Z' || *fmt == 'z') {
1000 			qualifier = *fmt++;
1001 			if (unlikely(qualifier == *fmt)) {
1002 				if (qualifier == 'h') {
1003 					qualifier = 'H';
1004 					fmt++;
1005 				} else if (qualifier == 'l') {
1006 					qualifier = 'L';
1007 					fmt++;
1008 				}
1009 			}
1010 		}
1011 		base = 10;
1012 		is_sign = 0;
1013 
1014 		if (!*fmt || !*str)
1015 			break;
1016 
1017 		switch(*fmt++) {
1018 		case 'c':
1019 		{
1020 			char *s = (char *) va_arg(args,char*);
1021 			if (field_width == -1)
1022 				field_width = 1;
1023 			do {
1024 				*s++ = *str++;
1025 			} while (--field_width > 0 && *str);
1026 			num++;
1027 		}
1028 		continue;
1029 		case 's':
1030 		{
1031 			char *s = (char *) va_arg(args, char *);
1032 			if(field_width == -1)
1033 				field_width = INT_MAX;
1034 			/* first, skip leading white space in buffer */
1035 			while (isspace(*str))
1036 				str++;
1037 
1038 			/* now copy until next white space */
1039 			while (*str && !isspace(*str) && field_width--) {
1040 				*s++ = *str++;
1041 			}
1042 			*s = '\0';
1043 			num++;
1044 		}
1045 		continue;
1046 		case 'n':
1047 			/* return number of characters read so far */
1048 		{
1049 			int *i = (int *)va_arg(args,int*);
1050 			*i = str - buf;
1051 		}
1052 		continue;
1053 		case 'o':
1054 			base = 8;
1055 			break;
1056 		case 'x':
1057 		case 'X':
1058 			base = 16;
1059 			break;
1060 		case 'i':
1061                         base = 0;
1062 		case 'd':
1063 			is_sign = 1;
1064 		case 'u':
1065 			break;
1066 		case '%':
1067 			/* looking for '%' in str */
1068 			if (*str++ != '%')
1069 				return num;
1070 			continue;
1071 		default:
1072 			/* invalid format; stop here */
1073 			return num;
1074 		}
1075 
1076 		/* have some sort of integer conversion.
1077 		 * first, skip white space in buffer.
1078 		 */
1079 		while (isspace(*str))
1080 			str++;
1081 
1082 		digit = *str;
1083 		if (is_sign && digit == '-')
1084 			digit = *(str + 1);
1085 
1086 		if (!digit
1087                     || (base == 16 && !isxdigit(digit))
1088                     || (base == 10 && !isdigit(digit))
1089                     || (base == 8 && (!isdigit(digit) || digit > '7'))
1090                     || (base == 0 && !isdigit(digit)))
1091 				break;
1092 
1093 		switch(qualifier) {
1094 		case 'H':	/* that's 'hh' in format */
1095 			if (is_sign) {
1096 				signed char *s = (signed char *) va_arg(args,signed char *);
1097 				*s = (signed char) simple_strtol(str,&next,base);
1098 			} else {
1099 				unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1100 				*s = (unsigned char) simple_strtoul(str, &next, base);
1101 			}
1102 			break;
1103 		case 'h':
1104 			if (is_sign) {
1105 				short *s = (short *) va_arg(args,short *);
1106 				*s = (short) simple_strtol(str,&next,base);
1107 			} else {
1108 				unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1109 				*s = (unsigned short) simple_strtoul(str, &next, base);
1110 			}
1111 			break;
1112 		case 'l':
1113 			if (is_sign) {
1114 				long *l = (long *) va_arg(args,long *);
1115 				*l = simple_strtol(str,&next,base);
1116 			} else {
1117 				unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1118 				*l = simple_strtoul(str,&next,base);
1119 			}
1120 			break;
1121 		case 'L':
1122 			if (is_sign) {
1123 				long long *l = (long long*) va_arg(args,long long *);
1124 				*l = simple_strtoll(str,&next,base);
1125 			} else {
1126 				unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1127 				*l = simple_strtoull(str,&next,base);
1128 			}
1129 			break;
1130 		case 'Z':
1131 		case 'z':
1132 		{
1133 			size_t *s = (size_t*) va_arg(args,size_t*);
1134 			*s = (size_t) simple_strtoul(str,&next,base);
1135 		}
1136 		break;
1137 		default:
1138 			if (is_sign) {
1139 				int *i = (int *) va_arg(args, int*);
1140 				*i = (int) simple_strtol(str,&next,base);
1141 			} else {
1142 				unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1143 				*i = (unsigned int) simple_strtoul(str,&next,base);
1144 			}
1145 			break;
1146 		}
1147 		num++;
1148 
1149 		if (!next)
1150 			break;
1151 		str = next;
1152 	}
1153 
1154 	/*
1155 	 * Now we've come all the way through so either the input string or the
1156 	 * format ended. In the former case, there can be a %n at the current
1157 	 * position in the format that needs to be filled.
1158 	 */
1159 	if (*fmt == '%' && *(fmt + 1) == 'n') {
1160 		int *p = (int *)va_arg(args, int *);
1161 		*p = str - buf;
1162 	}
1163 
1164 	return num;
1165 }
1166 EXPORT_SYMBOL(vsscanf);
1167 
1168 /**
1169  * sscanf - Unformat a buffer into a list of arguments
1170  * @buf:	input buffer
1171  * @fmt:	formatting of buffer
1172  * @...:	resulting arguments
1173  */
1174 int sscanf(const char * buf, const char * fmt, ...)
1175 {
1176 	va_list args;
1177 	int i;
1178 
1179 	va_start(args,fmt);
1180 	i = vsscanf(buf,fmt,args);
1181 	va_end(args);
1182 	return i;
1183 }
1184 EXPORT_SYMBOL(sscanf);
1185