2 * String functions for logger.
8 * Copyright (C) 1991, 1992 Linus Torvalds
11 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
13 * Wirzenius wrote this portably, Torvalds fucked it up :-)
17 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
18 * - changed to provide snprintf and vsnprintf functions
22 #include "libc/string.h"
23 #include "libc/vsprintf.h"
25 static int skip_atoi(const char **s)
30 i = i*10 + *((*s)++) - '0';
34 #define ZEROPAD 1 /* pad with zero */
35 #define SIGN 2 /* unsigned/signed long */
36 #define PLUS 4 /* show plus */
37 #define SPACE 8 /* space if plus */
38 #define LEFT 16 /* left justified */
39 #define SPECIAL 32 /* 0x */
40 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
42 #define do_div(n,base) ({ \
44 __res = ((unsigned long long) n) % (unsigned) base; \
45 n = ((unsigned long long) n) / (unsigned) base; \
48 static int mstrlen( const char *str );
51 #define PAGE_SIZE 4096
54 static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type)
58 static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
59 static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
62 digits = (type & LARGE) ? large_digits : small_digits;
65 if (base < 2 || base > 36)
67 c = (type & ZEROPAD) ? '0' : ' ';
74 } else if (type & PLUS) {
77 } else if (type & SPACE) {
92 tmp[i++] = digits[do_div(num,base)];
96 if (!(type&(ZEROPAD+LEFT))) {
108 if (type & SPECIAL) {
113 } else if (base==16) {
122 if (!(type & LEFT)) {
129 while (i < precision--) {
148 * vsnprintf - Format a string and place it in a buffer
149 * @buf: The buffer to place the result into
150 * @size: The size of the buffer, including the trailing null space
151 * @fmt: The format string to use
152 * @args: Arguments for the format string
154 * Call this function if you are already dealing with a va_list.
155 * You probably want snprintf instead.
157 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
160 unsigned long long num;
165 int flags; /* flags to number() */
167 int field_width; /* width of output field */
168 int precision; /* min. # of digits for integers; max
169 number of chars for from string */
170 int qualifier; /* 'h', 'l', or 'L' for integer fields */
171 /* 'z' support added 23/7/1999 S.H. */
172 /* 'z' changed to 'Z' --davidm 1/25/99 */
175 end = buf + size - 1;
179 size = end - buf + 1;
182 for (; *fmt ; ++fmt) {
193 ++fmt; /* this also skips first '%' */
195 case '-': flags |= LEFT; goto repeat;
196 case '+': flags |= PLUS; goto repeat;
197 case ' ': flags |= SPACE; goto repeat;
198 case '#': flags |= SPECIAL; goto repeat;
199 case '0': flags |= ZEROPAD; goto repeat;
202 /* get field width */
205 field_width = skip_atoi(&fmt);
206 else if (*fmt == '*') {
208 /* it's the next argument */
209 field_width = va_arg(args, int);
210 if (field_width < 0) {
211 field_width = -field_width;
216 /* get the precision */
221 precision = skip_atoi(&fmt);
222 else if (*fmt == '*') {
224 /* it's the next argument */
225 precision = va_arg(args, int);
231 /* get the conversion qualifier */
233 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
234 *fmt =='Z' || *fmt == 'z') {
237 if (qualifier == 'l' && *fmt == 'l') {
248 if (!(flags & LEFT)) {
249 while (--field_width > 0) {
255 c = (unsigned char) va_arg(args, int);
259 while (--field_width > 0) {
267 s = va_arg(args, char *);
268 if ((unsigned long)s < PAGE_SIZE)
272 len = strnlen(s, precision);
275 if( precision > len )
278 if (!(flags & LEFT)) {
279 while (len < field_width--) {
285 for (i = 0; i < len; ++i) {
290 while (len < field_width--) {
298 if (field_width == -1) {
299 field_width = 2*sizeof(void *);
302 str = number(str, end,
303 (unsigned long) va_arg(args, void *),
304 16, field_width, precision, flags);
310 * What does C99 say about the overflow case here? */
311 if (qualifier == 'l') {
312 long * ip = va_arg(args, long *);
314 } else if (qualifier == 'Z' || qualifier == 'z') {
315 size_t * ip = va_arg(args, size_t *);
318 int * ip = va_arg(args, int *);
329 /* integer number formats - set up the flags and "break" */
359 if (qualifier == 'L')
360 num = va_arg(args, long long);
361 else if (qualifier == 'l') {
362 num = va_arg(args, unsigned long);
364 num = (signed long) num;
365 } else if (qualifier == 'Z' || qualifier == 'z') {
366 num = va_arg(args, size_t);
367 } else if (qualifier == 'h') {
368 num = (unsigned short) va_arg(args, int);
370 num = (signed short) num;
372 num = va_arg(args, unsigned int);
374 num = (signed int) num;
376 str = number(str, end, num, base,
377 field_width, precision, flags);
382 /* don't write out a null byte if the buf size is zero */
384 /* the trailing null byte doesn't count towards the total
391 * snprintf - Format a string and place it in a buffer
392 * @buf: The buffer to place the result into
393 * @size: The size of the buffer, including the trailing null space
394 * @fmt: The format string to use
395 * @...: Arguments for the format string
397 int snprintf(char * buf, size_t size, const char *fmt, ...)
403 i=vsnprintf(buf,size,fmt,args);
409 * vsprintf - Format a string and place it in a buffer
410 * @buf: The buffer to place the result into
411 * @fmt: The format string to use
412 * @args: Arguments for the format string
414 * Call this function if you are already dealing with a va_list.
415 * You probably want sprintf instead.
417 int vsprintf(char *buf, const char *fmt, va_list args)
419 return vsnprintf(buf, (~0U)>>1, fmt, args);
424 * sprintf - Format a string and place it in a buffer
425 * @buf: The buffer to place the result into
426 * @fmt: The format string to use
427 * @...: Arguments for the format string
429 int sprintf(char * buf, const char *fmt, ...)
435 i=vsprintf(buf,fmt,args);
440 static int mstrlen( const char *str )