Home | History | Annotate | Line # | Download | only in compat
snprintf.c revision 1.1
      1  1.1  christos /* snprintf - compatibility implementation of snprintf, vsnprintf
      2  1.1  christos  *
      3  1.1  christos  * Copyright (c) 2013, NLnet Labs. All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * This software is open source.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  *
     11  1.1  christos  * Redistributions of source code must retain the above copyright notice,
     12  1.1  christos  * this list of conditions and the following disclaimer.
     13  1.1  christos  *
     14  1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     15  1.1  christos  * this list of conditions and the following disclaimer in the documentation
     16  1.1  christos  * and/or other materials provided with the distribution.
     17  1.1  christos  *
     18  1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     19  1.1  christos  * be used to endorse or promote products derived from this software without
     20  1.1  christos  * specific prior written permission.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     23  1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     24  1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     25  1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     26  1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     29  1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     30  1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     31  1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     32  1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include "config.h"
     36  1.1  christos #include <stdio.h>
     37  1.1  christos #include <ctype.h>
     38  1.1  christos #include <string.h>
     39  1.1  christos #include <stdarg.h>
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #include <errno.h>
     42  1.1  christos #ifdef HAVE_STDINT_H
     43  1.1  christos #include <stdint.h>
     44  1.1  christos #endif
     45  1.1  christos #include <limits.h>
     46  1.1  christos 
     47  1.1  christos /* for test */
     48  1.1  christos /* #define SNPRINTF_TEST 1 */
     49  1.1  christos #ifdef SNPRINTF_TEST
     50  1.1  christos #define snprintf my_snprintf
     51  1.1  christos #define vsnprintf my_vsnprintf
     52  1.1  christos #endif /* SNPRINTF_TEST */
     53  1.1  christos 
     54  1.1  christos int snprintf(char* str, size_t size, const char* format, ...);
     55  1.1  christos int vsnprintf(char* str, size_t size, const char* format, va_list arg);
     56  1.1  christos 
     57  1.1  christos /**
     58  1.1  christos  * Very portable snprintf implementation, limited in functionality,
     59  1.1  christos  * esp. for %[capital] %[nonportable] and so on.  Reduced float functionality,
     60  1.1  christos  * mostly in formatting and range (e+-16), for %f and %g.
     61  1.1  christos  *
     62  1.1  christos  * %s, %d, %u, %i, %x, %c, %n and %% are fully supported.
     63  1.1  christos  *   This includes width, precision, flags 0- +, and *(arg for wid,prec).
     64  1.1  christos  * %f, %g, %m, %p have reduced support, support for wid,prec,flags,*, but
     65  1.1  christos  *   less floating point range, no %e formatting for %g.
     66  1.1  christos  */
     67  1.1  christos int snprintf(char* str, size_t size, const char* format, ...)
     68  1.1  christos {
     69  1.1  christos 	int r;
     70  1.1  christos 	va_list args;
     71  1.1  christos 	va_start(args, format);
     72  1.1  christos 	r = vsnprintf(str, size, format, args);
     73  1.1  christos 	va_end(args);
     74  1.1  christos 	return r;
     75  1.1  christos }
     76  1.1  christos 
     77  1.1  christos /** add padding to string */
     78  1.1  christos static void
     79  1.1  christos print_pad(char** at, size_t* left, int* ret, char p, int num)
     80  1.1  christos {
     81  1.1  christos 	while(num--) {
     82  1.1  christos 		if(*left > 1) {
     83  1.1  christos 			*(*at)++ = p;
     84  1.1  christos 			(*left)--;
     85  1.1  christos 		}
     86  1.1  christos 		(*ret)++;
     87  1.1  christos 	}
     88  1.1  christos }
     89  1.1  christos 
     90  1.1  christos /** get negative symbol, 0 if none */
     91  1.1  christos static char
     92  1.1  christos get_negsign(int negative, int plus, int space)
     93  1.1  christos {
     94  1.1  christos 	if(negative)
     95  1.1  christos 		return '-';
     96  1.1  christos 	if(plus)
     97  1.1  christos 		return '+';
     98  1.1  christos 	if(space)
     99  1.1  christos 		return ' ';
    100  1.1  christos 	return 0;
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos #define PRINT_DEC_BUFSZ 32 /* 20 is enough for 64 bit decimals */
    104  1.1  christos /** print decimal into buffer, returns length */
    105  1.1  christos static int
    106  1.1  christos print_dec(char* buf, int max, unsigned int value)
    107  1.1  christos {
    108  1.1  christos 	int i = 0;
    109  1.1  christos 	if(value == 0) {
    110  1.1  christos 		if(max > 0) {
    111  1.1  christos 			buf[0] = '0';
    112  1.1  christos 			i = 1;
    113  1.1  christos 		}
    114  1.1  christos 	} else while(value && i < max) {
    115  1.1  christos 		buf[i++] = '0' + value % 10;
    116  1.1  christos 		value /= 10;
    117  1.1  christos 	}
    118  1.1  christos 	return i;
    119  1.1  christos }
    120  1.1  christos 
    121  1.1  christos /** print long decimal into buffer, returns length */
    122  1.1  christos static int
    123  1.1  christos print_dec_l(char* buf, int max, unsigned long value)
    124  1.1  christos {
    125  1.1  christos 	int i = 0;
    126  1.1  christos 	if(value == 0) {
    127  1.1  christos 		if(max > 0) {
    128  1.1  christos 			buf[0] = '0';
    129  1.1  christos 			i = 1;
    130  1.1  christos 		}
    131  1.1  christos 	} else while(value && i < max) {
    132  1.1  christos 		buf[i++] = '0' + value % 10;
    133  1.1  christos 		value /= 10;
    134  1.1  christos 	}
    135  1.1  christos 	return i;
    136  1.1  christos }
    137  1.1  christos 
    138  1.1  christos /** print long decimal into buffer, returns length */
    139  1.1  christos static int
    140  1.1  christos print_dec_ll(char* buf, int max, unsigned long long value)
    141  1.1  christos {
    142  1.1  christos 	int i = 0;
    143  1.1  christos 	if(value == 0) {
    144  1.1  christos 		if(max > 0) {
    145  1.1  christos 			buf[0] = '0';
    146  1.1  christos 			i = 1;
    147  1.1  christos 		}
    148  1.1  christos 	} else while(value && i < max) {
    149  1.1  christos 		buf[i++] = '0' + value % 10;
    150  1.1  christos 		value /= 10;
    151  1.1  christos 	}
    152  1.1  christos 	return i;
    153  1.1  christos }
    154  1.1  christos 
    155  1.1  christos /** print hex into buffer, returns length */
    156  1.1  christos static int
    157  1.1  christos print_hex(char* buf, int max, unsigned int value)
    158  1.1  christos {
    159  1.1  christos 	const char* h = "0123456789abcdef";
    160  1.1  christos 	int i = 0;
    161  1.1  christos 	if(value == 0) {
    162  1.1  christos 		if(max > 0) {
    163  1.1  christos 			buf[0] = '0';
    164  1.1  christos 			i = 1;
    165  1.1  christos 		}
    166  1.1  christos 	} else while(value && i < max) {
    167  1.1  christos 		buf[i++] = h[value & 0x0f];
    168  1.1  christos 		value >>= 4;
    169  1.1  christos 	}
    170  1.1  christos 	return i;
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos /** print long hex into buffer, returns length */
    174  1.1  christos static int
    175  1.1  christos print_hex_l(char* buf, int max, unsigned long value)
    176  1.1  christos {
    177  1.1  christos 	const char* h = "0123456789abcdef";
    178  1.1  christos 	int i = 0;
    179  1.1  christos 	if(value == 0) {
    180  1.1  christos 		if(max > 0) {
    181  1.1  christos 			buf[0] = '0';
    182  1.1  christos 			i = 1;
    183  1.1  christos 		}
    184  1.1  christos 	} else while(value && i < max) {
    185  1.1  christos 		buf[i++] = h[value & 0x0f];
    186  1.1  christos 		value >>= 4;
    187  1.1  christos 	}
    188  1.1  christos 	return i;
    189  1.1  christos }
    190  1.1  christos 
    191  1.1  christos /** print long long hex into buffer, returns length */
    192  1.1  christos static int
    193  1.1  christos print_hex_ll(char* buf, int max, unsigned long long value)
    194  1.1  christos {
    195  1.1  christos 	const char* h = "0123456789abcdef";
    196  1.1  christos 	int i = 0;
    197  1.1  christos 	if(value == 0) {
    198  1.1  christos 		if(max > 0) {
    199  1.1  christos 			buf[0] = '0';
    200  1.1  christos 			i = 1;
    201  1.1  christos 		}
    202  1.1  christos 	} else while(value && i < max) {
    203  1.1  christos 		buf[i++] = h[value & 0x0f];
    204  1.1  christos 		value >>= 4;
    205  1.1  christos 	}
    206  1.1  christos 	return i;
    207  1.1  christos }
    208  1.1  christos 
    209  1.1  christos /** copy string into result, reversed */
    210  1.1  christos static void
    211  1.1  christos spool_str_rev(char** at, size_t* left, int* ret, const char* buf, int len)
    212  1.1  christos {
    213  1.1  christos 	int i = len;
    214  1.1  christos 	while(i) {
    215  1.1  christos 		if(*left > 1) {
    216  1.1  christos 			*(*at)++ = buf[--i];
    217  1.1  christos 			(*left)--;
    218  1.1  christos 		} else --i;
    219  1.1  christos 		(*ret)++;
    220  1.1  christos 	}
    221  1.1  christos }
    222  1.1  christos 
    223  1.1  christos /** copy string into result */
    224  1.1  christos static void
    225  1.1  christos spool_str(char** at, size_t* left, int* ret, const char* buf, int len)
    226  1.1  christos {
    227  1.1  christos 	int i;
    228  1.1  christos 	for(i=0; i<len; i++) {
    229  1.1  christos 		if(*left > 1) {
    230  1.1  christos 			*(*at)++ = buf[i];
    231  1.1  christos 			(*left)--;
    232  1.1  christos 		}
    233  1.1  christos 		(*ret)++;
    234  1.1  christos 	}
    235  1.1  christos }
    236  1.1  christos 
    237  1.1  christos /** print number formatted */
    238  1.1  christos static void
    239  1.1  christos print_num(char** at, size_t* left, int* ret, int minw, int precision,
    240  1.1  christos 	int prgiven, int zeropad, int minus, int plus, int space,
    241  1.1  christos 	int zero, int negative, char* buf, int len)
    242  1.1  christos {
    243  1.1  christos 	int w = len; /* excludes minus sign */
    244  1.1  christos 	char s = get_negsign(negative, plus, space);
    245  1.1  christos 	if(minus) {
    246  1.1  christos 		/* left adjust the number into the field, space padding */
    247  1.1  christos 		/* calc numw = [sign][zeroes][number] */
    248  1.1  christos 		int numw = w;
    249  1.1  christos 		if(precision == 0 && zero) numw = 0;
    250  1.1  christos 		if(numw < precision) numw = precision;
    251  1.1  christos 		if(s) numw++;
    252  1.1  christos 
    253  1.1  christos 		/* sign */
    254  1.1  christos 		if(s) print_pad(at, left, ret, s, 1);
    255  1.1  christos 
    256  1.1  christos 		/* number */
    257  1.1  christos 		if(precision == 0 && zero) {
    258  1.1  christos 			/* "" for the number */
    259  1.1  christos 		} else {
    260  1.1  christos 			if(w < precision)
    261  1.1  christos 				print_pad(at, left, ret, '0', precision - w);
    262  1.1  christos 			spool_str_rev(at, left, ret, buf, len);
    263  1.1  christos 		}
    264  1.1  christos 		/* spaces */
    265  1.1  christos 		if(numw < minw)
    266  1.1  christos 			print_pad(at, left, ret, ' ', minw - numw);
    267  1.1  christos 	} else {
    268  1.1  christos 		/* pad on the left of the number */
    269  1.1  christos 		/* calculate numw has width of [sign][zeroes][number] */
    270  1.1  christos 		int numw = w;
    271  1.1  christos 		if(precision == 0 && zero) numw = 0;
    272  1.1  christos 		if(numw < precision) numw = precision;
    273  1.1  christos 		if(!prgiven && zeropad && numw < minw) numw = minw;
    274  1.1  christos 		else if(s) numw++;
    275  1.1  christos 
    276  1.1  christos 		/* pad with spaces */
    277  1.1  christos 		if(numw < minw)
    278  1.1  christos 			print_pad(at, left, ret, ' ', minw - numw);
    279  1.1  christos 		/* print sign (and one less zeropad if so) */
    280  1.1  christos 		if(s) {
    281  1.1  christos 			print_pad(at, left, ret, s, 1);
    282  1.1  christos 			numw--;
    283  1.1  christos 		}
    284  1.1  christos 		/* pad with zeroes */
    285  1.1  christos 		if(w < numw)
    286  1.1  christos 			print_pad(at, left, ret, '0', numw - w);
    287  1.1  christos 		if(precision == 0 && zero)
    288  1.1  christos 			return;
    289  1.1  christos 		/* print the characters for the value */
    290  1.1  christos 		spool_str_rev(at, left, ret, buf, len);
    291  1.1  christos 	}
    292  1.1  christos }
    293  1.1  christos 
    294  1.1  christos /** print %d and %i */
    295  1.1  christos static void
    296  1.1  christos print_num_d(char** at, size_t* left, int* ret, int value,
    297  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    298  1.1  christos 	int plus, int space)
    299  1.1  christos {
    300  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    301  1.1  christos 	int negative = (value < 0);
    302  1.1  christos 	int zero = (value == 0);
    303  1.1  christos 	int len = print_dec(buf, (int)sizeof(buf),
    304  1.1  christos 		(unsigned int)(negative?-value:value));
    305  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    306  1.1  christos 		plus, space, zero, negative, buf, len);
    307  1.1  christos }
    308  1.1  christos 
    309  1.1  christos /** print %ld and %li */
    310  1.1  christos static void
    311  1.1  christos print_num_ld(char** at, size_t* left, int* ret, long value,
    312  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    313  1.1  christos 	int plus, int space)
    314  1.1  christos {
    315  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    316  1.1  christos 	int negative = (value < 0);
    317  1.1  christos 	int zero = (value == 0);
    318  1.1  christos 	int len = print_dec_l(buf, (int)sizeof(buf),
    319  1.1  christos 		(unsigned long)(negative?-value:value));
    320  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    321  1.1  christos 		plus, space, zero, negative, buf, len);
    322  1.1  christos }
    323  1.1  christos 
    324  1.1  christos /** print %lld and %lli */
    325  1.1  christos static void
    326  1.1  christos print_num_lld(char** at, size_t* left, int* ret, long long value,
    327  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    328  1.1  christos 	int plus, int space)
    329  1.1  christos {
    330  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    331  1.1  christos 	int negative = (value < 0);
    332  1.1  christos 	int zero = (value == 0);
    333  1.1  christos 	int len = print_dec_ll(buf, (int)sizeof(buf),
    334  1.1  christos 		(unsigned long long)(negative?-value:value));
    335  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    336  1.1  christos 		plus, space, zero, negative, buf, len);
    337  1.1  christos }
    338  1.1  christos 
    339  1.1  christos /** print %u */
    340  1.1  christos static void
    341  1.1  christos print_num_u(char** at, size_t* left, int* ret, unsigned int value,
    342  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    343  1.1  christos 	int plus, int space)
    344  1.1  christos {
    345  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    346  1.1  christos 	int negative = 0;
    347  1.1  christos 	int zero = (value == 0);
    348  1.1  christos 	int len = print_dec(buf, (int)sizeof(buf), value);
    349  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    350  1.1  christos 		plus, space, zero, negative, buf, len);
    351  1.1  christos }
    352  1.1  christos 
    353  1.1  christos /** print %lu */
    354  1.1  christos static void
    355  1.1  christos print_num_lu(char** at, size_t* left, int* ret, unsigned long value,
    356  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    357  1.1  christos 	int plus, int space)
    358  1.1  christos {
    359  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    360  1.1  christos 	int negative = 0;
    361  1.1  christos 	int zero = (value == 0);
    362  1.1  christos 	int len = print_dec_l(buf, (int)sizeof(buf), value);
    363  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    364  1.1  christos 		plus, space, zero, negative, buf, len);
    365  1.1  christos }
    366  1.1  christos 
    367  1.1  christos /** print %llu */
    368  1.1  christos static void
    369  1.1  christos print_num_llu(char** at, size_t* left, int* ret, unsigned long long value,
    370  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    371  1.1  christos 	int plus, int space)
    372  1.1  christos {
    373  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    374  1.1  christos 	int negative = 0;
    375  1.1  christos 	int zero = (value == 0);
    376  1.1  christos 	int len = print_dec_ll(buf, (int)sizeof(buf), value);
    377  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    378  1.1  christos 		plus, space, zero, negative, buf, len);
    379  1.1  christos }
    380  1.1  christos 
    381  1.1  christos /** print %x */
    382  1.1  christos static void
    383  1.1  christos print_num_x(char** at, size_t* left, int* ret, unsigned int value,
    384  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    385  1.1  christos 	int plus, int space)
    386  1.1  christos {
    387  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    388  1.1  christos 	int negative = 0;
    389  1.1  christos 	int zero = (value == 0);
    390  1.1  christos 	int len = print_hex(buf, (int)sizeof(buf), value);
    391  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    392  1.1  christos 		plus, space, zero, negative, buf, len);
    393  1.1  christos }
    394  1.1  christos 
    395  1.1  christos /** print %lx */
    396  1.1  christos static void
    397  1.1  christos print_num_lx(char** at, size_t* left, int* ret, unsigned long value,
    398  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    399  1.1  christos 	int plus, int space)
    400  1.1  christos {
    401  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    402  1.1  christos 	int negative = 0;
    403  1.1  christos 	int zero = (value == 0);
    404  1.1  christos 	int len = print_hex_l(buf, (int)sizeof(buf), value);
    405  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    406  1.1  christos 		plus, space, zero, negative, buf, len);
    407  1.1  christos }
    408  1.1  christos 
    409  1.1  christos /** print %llx */
    410  1.1  christos static void
    411  1.1  christos print_num_llx(char** at, size_t* left, int* ret, unsigned long long value,
    412  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    413  1.1  christos 	int plus, int space)
    414  1.1  christos {
    415  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    416  1.1  christos 	int negative = 0;
    417  1.1  christos 	int zero = (value == 0);
    418  1.1  christos 	int len = print_hex_ll(buf, (int)sizeof(buf), value);
    419  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    420  1.1  christos 		plus, space, zero, negative, buf, len);
    421  1.1  christos }
    422  1.1  christos 
    423  1.1  christos /** print %llp */
    424  1.1  christos static void
    425  1.1  christos print_num_llp(char** at, size_t* left, int* ret, void* value,
    426  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    427  1.1  christos 	int plus, int space)
    428  1.1  christos {
    429  1.1  christos 	char buf[PRINT_DEC_BUFSZ];
    430  1.1  christos 	int negative = 0;
    431  1.1  christos 	int zero = (value == 0);
    432  1.1  christos #if defined(SIZE_MAX) && defined(UINT32_MAX) && (UINT32_MAX == SIZE_MAX || INT32_MAX == SIZE_MAX)
    433  1.1  christos 	/* avoid warning about upcast on 32bit systems */
    434  1.1  christos 	unsigned long long llvalue = (unsigned long)value;
    435  1.1  christos #else
    436  1.1  christos 	unsigned long long llvalue = (unsigned long long)value;
    437  1.1  christos #endif
    438  1.1  christos 	int len = print_hex_ll(buf, (int)sizeof(buf), llvalue);
    439  1.1  christos 	if(zero) {
    440  1.1  christos 		buf[0]=')';
    441  1.1  christos 		buf[1]='l';
    442  1.1  christos 		buf[2]='i';
    443  1.1  christos 		buf[3]='n';
    444  1.1  christos 		buf[4]='(';
    445  1.1  christos 		len = 5;
    446  1.1  christos 	} else {
    447  1.1  christos 		/* put '0x' in front of the (reversed) buffer result */
    448  1.1  christos 		if(len < PRINT_DEC_BUFSZ)
    449  1.1  christos 			buf[len++] = 'x';
    450  1.1  christos 		if(len < PRINT_DEC_BUFSZ)
    451  1.1  christos 			buf[len++] = '0';
    452  1.1  christos 	}
    453  1.1  christos 	print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
    454  1.1  christos 		plus, space, zero, negative, buf, len);
    455  1.1  christos }
    456  1.1  christos 
    457  1.1  christos #define PRINT_FLOAT_BUFSZ 64 /* xx.yy with 20.20 about the max */
    458  1.1  christos /** spool remainder after the decimal point to buffer, in reverse */
    459  1.1  christos static int
    460  1.1  christos print_remainder(char* buf, int max, double r, int prec)
    461  1.1  christos {
    462  1.1  christos 	unsigned long long cap = 1;
    463  1.1  christos 	unsigned long long value;
    464  1.1  christos 	int len, i;
    465  1.1  christos 	if(prec > 19) prec = 19; /* max we can do */
    466  1.1  christos 	if(max < prec) return 0;
    467  1.1  christos 	for(i=0; i<prec; i++) {
    468  1.1  christos 		cap *= 10;
    469  1.1  christos 	}
    470  1.1  christos 	r *= (double)cap;
    471  1.1  christos 	value = (unsigned long long)r;
    472  1.1  christos 	/* see if we need to round up */
    473  1.1  christos 	if(((unsigned long long)((r - (double)value)*10.0)) >= 5) {
    474  1.1  christos 		value++;
    475  1.1  christos 		/* that might carry to numbers before the comma, if so,
    476  1.1  christos 		 * just ignore that rounding. failure because 64bitprintout */
    477  1.1  christos 		if(value >= cap)
    478  1.1  christos 			value = cap-1;
    479  1.1  christos 	}
    480  1.1  christos 	len = print_dec_ll(buf, max, value);
    481  1.1  christos 	while(len < prec) { /* pad with zeroes, e.g. if 0.0012 */
    482  1.1  christos 		buf[len++] = '0';
    483  1.1  christos 	}
    484  1.1  christos 	if(len < max)
    485  1.1  christos 		buf[len++] = '.';
    486  1.1  christos 	return len;
    487  1.1  christos }
    488  1.1  christos 
    489  1.1  christos /** spool floating point to buffer */
    490  1.1  christos static int
    491  1.1  christos print_float(char* buf, int max, double value, int prec)
    492  1.1  christos {
    493  1.1  christos 	/* as xxx.xxx  if prec==0, no '.', with prec decimals after . */
    494  1.1  christos 	/* no conversion for NAN and INF, because we do not want to require
    495  1.1  christos 	   linking with -lm. */
    496  1.1  christos 	/* Thus, the conversions use 64bit integers to convert the numbers,
    497  1.1  christos 	 * which makes 19 digits before and after the decimal point the max */
    498  1.1  christos 	unsigned long long whole = (unsigned long long)value;
    499  1.1  christos 	double remain = value - (double)whole;
    500  1.1  christos 	int len = 0;
    501  1.1  christos 	if(prec != 0)
    502  1.1  christos 		len = print_remainder(buf, max, remain, prec);
    503  1.1  christos 	len += print_dec_ll(buf+len, max-len, whole);
    504  1.1  christos 	return len;
    505  1.1  christos }
    506  1.1  christos 
    507  1.1  christos /** print %f */
    508  1.1  christos static void
    509  1.1  christos print_num_f(char** at, size_t* left, int* ret, double value,
    510  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    511  1.1  christos 	int plus, int space)
    512  1.1  christos {
    513  1.1  christos 	char buf[PRINT_FLOAT_BUFSZ];
    514  1.1  christos 	int negative = (value < 0);
    515  1.1  christos 	int zero = 0;
    516  1.1  christos 	int len;
    517  1.1  christos 	if(!prgiven) precision = 6;
    518  1.1  christos 	len = print_float(buf, (int)sizeof(buf), negative?-value:value,
    519  1.1  christos 		precision);
    520  1.1  christos 	print_num(at, left, ret, minw, 1, 0, zeropad, minus,
    521  1.1  christos 		plus, space, zero, negative, buf, len);
    522  1.1  christos }
    523  1.1  christos 
    524  1.1  christos /* rudimentary %g support */
    525  1.1  christos static int
    526  1.1  christos print_float_g(char* buf, int max, double value, int prec)
    527  1.1  christos {
    528  1.1  christos 	unsigned long long whole = (unsigned long long)value;
    529  1.1  christos 	double remain = value - (double)whole;
    530  1.1  christos 	int before = 0;
    531  1.1  christos 	int len = 0;
    532  1.1  christos 
    533  1.1  christos 	/* number of digits before the decimal point */
    534  1.1  christos 	while(whole > 0) {
    535  1.1  christos 		before++;
    536  1.1  christos 		whole /= 10;
    537  1.1  christos 	}
    538  1.1  christos 	whole = (unsigned long long)value;
    539  1.1  christos 
    540  1.1  christos 	if(prec > before && remain != 0.0) {
    541  1.1  christos 		/* see if the last decimals are zero, if so, skip them */
    542  1.1  christos 		len = print_remainder(buf, max, remain, prec-before);
    543  1.1  christos 		while(len > 0 && buf[0]=='0') {
    544  1.1  christos 			memmove(buf, buf+1, --len);
    545  1.1  christos 		}
    546  1.1  christos 	}
    547  1.1  christos 	len += print_dec_ll(buf+len, max-len, whole);
    548  1.1  christos 	return len;
    549  1.1  christos }
    550  1.1  christos 
    551  1.1  christos 
    552  1.1  christos /** print %g */
    553  1.1  christos static void
    554  1.1  christos print_num_g(char** at, size_t* left, int* ret, double value,
    555  1.1  christos 	int minw, int precision, int prgiven, int zeropad, int minus,
    556  1.1  christos 	int plus, int space)
    557  1.1  christos {
    558  1.1  christos 	char buf[PRINT_FLOAT_BUFSZ];
    559  1.1  christos 	int negative = (value < 0);
    560  1.1  christos 	int zero = 0;
    561  1.1  christos 	int len;
    562  1.1  christos 	if(!prgiven) precision = 6;
    563  1.1  christos 	if(precision == 0) precision = 1;
    564  1.1  christos 	len = print_float_g(buf, (int)sizeof(buf), negative?-value:value,
    565  1.1  christos 		precision);
    566  1.1  christos 	print_num(at, left, ret, minw, 1, 0, zeropad, minus,
    567  1.1  christos 		plus, space, zero, negative, buf, len);
    568  1.1  christos }
    569  1.1  christos 
    570  1.1  christos 
    571  1.1  christos /** strnlen (compat implementation) */
    572  1.1  christos static int
    573  1.1  christos my_strnlen(const char* s, int max)
    574  1.1  christos {
    575  1.1  christos 	int i;
    576  1.1  christos 	for(i=0; i<max; i++)
    577  1.1  christos 		if(s[i]==0)
    578  1.1  christos 			return i;
    579  1.1  christos 	return max;
    580  1.1  christos }
    581  1.1  christos 
    582  1.1  christos /** print %s */
    583  1.1  christos static void
    584  1.1  christos print_str(char** at, size_t* left, int* ret, char* s,
    585  1.1  christos 	int minw, int precision, int prgiven, int minus)
    586  1.1  christos {
    587  1.1  christos 	int w;
    588  1.1  christos 	/* with prec: no more than x characters from this string, stop at 0 */
    589  1.1  christos 	if(prgiven)
    590  1.1  christos 		w = my_strnlen(s, precision);
    591  1.1  christos 	else	w = (int)strlen(s); /* up to the nul */
    592  1.1  christos 	if(w < minw && !minus)
    593  1.1  christos 		print_pad(at, left, ret, ' ', minw - w);
    594  1.1  christos 	spool_str(at, left, ret, s, w);
    595  1.1  christos 	if(w < minw && minus)
    596  1.1  christos 		print_pad(at, left, ret, ' ', minw - w);
    597  1.1  christos }
    598  1.1  christos 
    599  1.1  christos /** print %c */
    600  1.1  christos static void
    601  1.1  christos print_char(char** at, size_t* left, int* ret, int c,
    602  1.1  christos 	int minw, int minus)
    603  1.1  christos {
    604  1.1  christos 	if(1 < minw && !minus)
    605  1.1  christos 		print_pad(at, left, ret, ' ', minw - 1);
    606  1.1  christos 	print_pad(at, left, ret, c, 1);
    607  1.1  christos 	if(1 < minw && minus)
    608  1.1  christos 		print_pad(at, left, ret, ' ', minw - 1);
    609  1.1  christos }
    610  1.1  christos 
    611  1.1  christos 
    612  1.1  christos /**
    613  1.1  christos  * Print to string.
    614  1.1  christos  * str: string buffer for result. result will be null terminated.
    615  1.1  christos  * size: size of the buffer. null is put inside buffer.
    616  1.1  christos  * format: printf format string.
    617  1.1  christos  * arg: '...' arguments to print.
    618  1.1  christos  * returns number of characters. a null is printed after this.
    619  1.1  christos  * return number of bytes that would have been written
    620  1.1  christos  *	   if the buffer had been large enough.
    621  1.1  christos  *
    622  1.1  christos  * supported format specifiers:
    623  1.1  christos  * 	%s, %u, %d, %x, %i, %f, %g, %c, %p, %n.
    624  1.1  christos  * 	length: l, ll (for d, u, x).
    625  1.1  christos  * 	precision: 6.6d (for d, u, x)
    626  1.1  christos  * 		%f, %g precisions, 0.3f
    627  1.1  christos  * 		%20s, '.*s'
    628  1.1  christos  * 	and %%.
    629  1.1  christos  */
    630  1.1  christos int vsnprintf(char* str, size_t size, const char* format, va_list arg)
    631  1.1  christos {
    632  1.1  christos 	char* at = str;
    633  1.1  christos 	size_t left = size;
    634  1.1  christos 	int ret = 0;
    635  1.1  christos 	const char* fmt = format;
    636  1.1  christos 	int conv, minw, precision, prgiven, zeropad, minus, plus, space, length;
    637  1.1  christos 	while(*fmt) {
    638  1.1  christos 		/* copy string before % */
    639  1.1  christos 		while(*fmt && *fmt!='%') {
    640  1.1  christos 			if(left > 1) {
    641  1.1  christos 				*at++ = *fmt++;
    642  1.1  christos 				left--;
    643  1.1  christos 			} else fmt++;
    644  1.1  christos 			ret++;
    645  1.1  christos 		}
    646  1.1  christos 
    647  1.1  christos 		/* see if we are at end */
    648  1.1  christos 		if(!*fmt) break;
    649  1.1  christos 
    650  1.1  christos 		/* fetch next argument % designation from format string */
    651  1.1  christos 		fmt++; /* skip the '%' */
    652  1.1  christos 
    653  1.1  christos 		/********************************/
    654  1.1  christos 		/* get the argument designation */
    655  1.1  christos 		/********************************/
    656  1.1  christos 		/* we must do this vararg stuff inside this function for
    657  1.1  christos 		 * portability.  Hence, get_designation, and print_designation
    658  1.1  christos 		 * are not their own functions. */
    659  1.1  christos 
    660  1.1  christos 		/* printout designation:
    661  1.1  christos 		 * conversion specifier: x, d, u, s, c, n, m, p
    662  1.1  christos 		 * flags: # not supported
    663  1.1  christos 		 *        0 zeropad (on the left)
    664  1.1  christos 		 *	  - left adjust (right by default)
    665  1.1  christos 		 *	  ' ' printspace for positive number (in - position).
    666  1.1  christos 		 *	  + alwayssign
    667  1.1  christos 		 * fieldwidth: [1-9][0-9]* minimum field width.
    668  1.1  christos 		 * 	if this is * then type int next argument specifies the minwidth.
    669  1.1  christos 		 * 	if this is negative, the - flag is set (with positive width).
    670  1.1  christos 		 * precision: period[digits]*, %.2x.
    671  1.1  christos 		 * 	if this is * then type int next argument specifies the precision.
    672  1.1  christos 		 *	just '.' or negative value means precision=0.
    673  1.1  christos 		 *		this is mindigits to print for d, i, u, x
    674  1.1  christos 		 *		this is aftercomma digits for f
    675  1.1  christos 		 *		this is max number significant digits for g
    676  1.1  christos 		 *		maxnumber characters to be printed for s
    677  1.1  christos 		 * length: 0-none (int), 1-l (long), 2-ll (long long)
    678  1.1  christos 		 * 	notsupported: hh (char), h (short), L (long double), q, j, z, t
    679  1.1  christos 		 * Does not support %m$ and *m$ argument designation as array indices.
    680  1.1  christos 		 * Does not support %#x
    681  1.1  christos 		 *
    682  1.1  christos 		 */
    683  1.1  christos 		minw = 0;
    684  1.1  christos 		precision = 1;
    685  1.1  christos 		prgiven = 0;
    686  1.1  christos 		zeropad = 0;
    687  1.1  christos 		minus = 0;
    688  1.1  christos 		plus = 0;
    689  1.1  christos 		space = 0;
    690  1.1  christos 		length = 0;
    691  1.1  christos 
    692  1.1  christos 		/* get flags in any order */
    693  1.1  christos 		for(;;) {
    694  1.1  christos 			if(*fmt == '0')
    695  1.1  christos 				zeropad = 1;
    696  1.1  christos 			else if(*fmt == '-')
    697  1.1  christos 				minus = 1;
    698  1.1  christos 			else if(*fmt == '+')
    699  1.1  christos 				plus = 1;
    700  1.1  christos 			else if(*fmt == ' ')
    701  1.1  christos 				space = 1;
    702  1.1  christos 			else break;
    703  1.1  christos 			fmt++;
    704  1.1  christos 		}
    705  1.1  christos 
    706  1.1  christos 		/* field width */
    707  1.1  christos 		if(*fmt == '*') {
    708  1.1  christos 			fmt++; /* skip char */
    709  1.1  christos 			minw = va_arg(arg, int);
    710  1.1  christos 			if(minw < 0) {
    711  1.1  christos 				minus = 1;
    712  1.1  christos 				minw = -minw;
    713  1.1  christos 			}
    714  1.1  christos 		} else while(*fmt >= '0' && *fmt <= '9') {
    715  1.1  christos 			minw = minw*10 + (*fmt++)-'0';
    716  1.1  christos 		}
    717  1.1  christos 
    718  1.1  christos 		/* precision */
    719  1.1  christos 		if(*fmt == '.') {
    720  1.1  christos 			fmt++; /* skip period */
    721  1.1  christos 			prgiven = 1;
    722  1.1  christos 			precision = 0;
    723  1.1  christos 			if(*fmt == '*') {
    724  1.1  christos 				fmt++; /* skip char */
    725  1.1  christos 				precision = va_arg(arg, int);
    726  1.1  christos 				if(precision < 0)
    727  1.1  christos 					precision = 0;
    728  1.1  christos 			} else while(*fmt >= '0' && *fmt <= '9') {
    729  1.1  christos 				precision = precision*10 + (*fmt++)-'0';
    730  1.1  christos 			}
    731  1.1  christos 		}
    732  1.1  christos 
    733  1.1  christos 		/* length */
    734  1.1  christos 		if(*fmt == 'l') {
    735  1.1  christos 			fmt++; /* skip char */
    736  1.1  christos 			length = 1;
    737  1.1  christos 			if(*fmt == 'l') {
    738  1.1  christos 				fmt++; /* skip char */
    739  1.1  christos 				length = 2;
    740  1.1  christos 			}
    741  1.1  christos 		}
    742  1.1  christos 
    743  1.1  christos 		/* get the conversion */
    744  1.1  christos 		if(!*fmt) conv = 0;
    745  1.1  christos 		else	conv = *fmt++;
    746  1.1  christos 
    747  1.1  christos 		/***********************************/
    748  1.1  christos 		/* print that argument designation */
    749  1.1  christos 		/***********************************/
    750  1.1  christos 		switch(conv) {
    751  1.1  christos 		case 'i':
    752  1.1  christos 		case 'd':
    753  1.1  christos 			if(length == 0)
    754  1.1  christos 			    print_num_d(&at, &left, &ret, va_arg(arg, int),
    755  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    756  1.1  christos 			else if(length == 1)
    757  1.1  christos 			    print_num_ld(&at, &left, &ret, va_arg(arg, long),
    758  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    759  1.1  christos 			else if(length == 2)
    760  1.1  christos 			    print_num_lld(&at, &left, &ret,
    761  1.1  christos 				va_arg(arg, long long),
    762  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    763  1.1  christos 			break;
    764  1.1  christos 		case 'u':
    765  1.1  christos 			if(length == 0)
    766  1.1  christos 			    print_num_u(&at, &left, &ret,
    767  1.1  christos 				va_arg(arg, unsigned int),
    768  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    769  1.1  christos 			else if(length == 1)
    770  1.1  christos 			    print_num_lu(&at, &left, &ret,
    771  1.1  christos 				va_arg(arg, unsigned long),
    772  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    773  1.1  christos 			else if(length == 2)
    774  1.1  christos 			    print_num_llu(&at, &left, &ret,
    775  1.1  christos 				va_arg(arg, unsigned long long),
    776  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    777  1.1  christos 			break;
    778  1.1  christos 		case 'x':
    779  1.1  christos 			if(length == 0)
    780  1.1  christos 			    print_num_x(&at, &left, &ret,
    781  1.1  christos 				va_arg(arg, unsigned int),
    782  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    783  1.1  christos 			else if(length == 1)
    784  1.1  christos 			    print_num_lx(&at, &left, &ret,
    785  1.1  christos 				va_arg(arg, unsigned long),
    786  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    787  1.1  christos 			else if(length == 2)
    788  1.1  christos 			    print_num_llx(&at, &left, &ret,
    789  1.1  christos 				va_arg(arg, unsigned long long),
    790  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    791  1.1  christos 			break;
    792  1.1  christos 		case 's':
    793  1.1  christos 			print_str(&at, &left, &ret, va_arg(arg, char*),
    794  1.1  christos 				minw, precision, prgiven, minus);
    795  1.1  christos 			break;
    796  1.1  christos 		case 'c':
    797  1.1  christos 			print_char(&at, &left, &ret, va_arg(arg, int),
    798  1.1  christos 				minw, minus);
    799  1.1  christos 			break;
    800  1.1  christos 		case 'n':
    801  1.1  christos 			*va_arg(arg, int*) = ret;
    802  1.1  christos 			break;
    803  1.1  christos 		case 'm':
    804  1.1  christos 			print_str(&at, &left, &ret, strerror(errno),
    805  1.1  christos 				minw, precision, prgiven, minus);
    806  1.1  christos 			break;
    807  1.1  christos 		case 'p':
    808  1.1  christos 			print_num_llp(&at, &left, &ret, va_arg(arg, void*),
    809  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    810  1.1  christos 			break;
    811  1.1  christos 		case '%':
    812  1.1  christos 			print_pad(&at, &left, &ret, '%', 1);
    813  1.1  christos 			break;
    814  1.1  christos 		case 'f':
    815  1.1  christos 			print_num_f(&at, &left, &ret, va_arg(arg, double),
    816  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    817  1.1  christos 			break;
    818  1.1  christos 		case 'g':
    819  1.1  christos 			print_num_g(&at, &left, &ret, va_arg(arg, double),
    820  1.1  christos 				minw, precision, prgiven, zeropad, minus, plus, space);
    821  1.1  christos 			break;
    822  1.1  christos 		/* unknown */
    823  1.1  christos 		default:
    824  1.1  christos 		case 0: break;
    825  1.1  christos 		}
    826  1.1  christos 	}
    827  1.1  christos 
    828  1.1  christos 	/* zero terminate */
    829  1.1  christos 	if(left > 0)
    830  1.1  christos 		*at = 0;
    831  1.1  christos 	return ret;
    832  1.1  christos }
    833  1.1  christos 
    834  1.1  christos #ifdef SNPRINTF_TEST
    835  1.1  christos 
    836  1.1  christos /** do tests */
    837  1.1  christos #undef snprintf
    838  1.1  christos #define DOTEST(bufsz, result, retval, ...) do { \
    839  1.1  christos 	char buf[bufsz]; \
    840  1.1  christos 	printf("now test %s\n", #__VA_ARGS__); \
    841  1.1  christos 	int r=my_snprintf(buf, sizeof(buf), __VA_ARGS__); \
    842  1.1  christos 	if(r != retval || strcmp(buf, result) != 0) { \
    843  1.1  christos 		printf("error test(%s) was \"%s\":%d\n", \
    844  1.1  christos 			""#bufsz", "#result", "#retval", "#__VA_ARGS__, \
    845  1.1  christos 			buf, r); \
    846  1.1  christos 		exit(1); \
    847  1.1  christos 		} \
    848  1.1  christos 	r=snprintf(buf, sizeof(buf), __VA_ARGS__); \
    849  1.1  christos 	if(r != retval || strcmp(buf, result) != 0) { \
    850  1.1  christos 		printf("error test(%s) differs with system, \"%s\":%d\n", \
    851  1.1  christos 			""#bufsz", "#result", "#retval", "#__VA_ARGS__, \
    852  1.1  christos 			buf, r); \
    853  1.1  christos 		exit(1); \
    854  1.1  christos 		} \
    855  1.1  christos 	printf("test(\"%s\":%d) passed\n", buf, r); \
    856  1.1  christos 	} while(0);
    857  1.1  christos 
    858  1.1  christos /** test program */
    859  1.1  christos int main(void)
    860  1.1  christos {
    861  1.1  christos 	int x = 0;
    862  1.1  christos 
    863  1.1  christos 	/* bufsize, expectedstring, expectedretval, snprintf arguments */
    864  1.1  christos 	DOTEST(1024, "hello", 5, "hello");
    865  1.1  christos 	DOTEST(1024, "h", 1, "h");
    866  1.1  christos 	/* warning from gcc for format string, but it does work
    867  1.1  christos 	 * DOTEST(1024, "", 0, ""); */
    868  1.1  christos 
    869  1.1  christos 	DOTEST(3, "he", 5, "hello");
    870  1.1  christos 	DOTEST(1, "", 7, "%d", 7823089);
    871  1.1  christos 
    872  1.1  christos 	/* test positive numbers */
    873  1.1  christos 	DOTEST(1024, "0", 1, "%d", 0);
    874  1.1  christos 	DOTEST(1024, "1", 1, "%d", 1);
    875  1.1  christos 	DOTEST(1024, "9", 1, "%d", 9);
    876  1.1  christos 	DOTEST(1024, "15", 2, "%d", 15);
    877  1.1  christos 	DOTEST(1024, "ab15cd", 6, "ab%dcd", 15);
    878  1.1  christos 	DOTEST(1024, "167", 3, "%d", 167);
    879  1.1  christos 	DOTEST(1024, "7823089", 7, "%d", 7823089);
    880  1.1  christos 	DOTEST(1024, " 12", 3, "%3d", 12);
    881  1.1  christos 	DOTEST(1024, "012", 3, "%.3d", 12);
    882  1.1  christos 	DOTEST(1024, "012", 3, "%3.3d", 12);
    883  1.1  christos 	DOTEST(1024, "012", 3, "%03d", 12);
    884  1.1  christos 	DOTEST(1024, " 012", 4, "%4.3d", 12);
    885  1.1  christos 	DOTEST(1024, "", 0, "%.0d", 0);
    886  1.1  christos 
    887  1.1  christos 	/* test negative numbers */
    888  1.1  christos 	DOTEST(1024, "-1", 2, "%d", -1);
    889  1.1  christos 	DOTEST(1024, "-12", 3, "%3d", -12);
    890  1.1  christos 	DOTEST(1024, " -2", 3, "%3d", -2);
    891  1.1  christos 	DOTEST(1024, "-012", 4, "%.3d", -12);
    892  1.1  christos 	DOTEST(1024, "-012", 4, "%3.3d", -12);
    893  1.1  christos 	DOTEST(1024, "-012", 4, "%4.3d", -12);
    894  1.1  christos 	DOTEST(1024, " -012", 5, "%5.3d", -12);
    895  1.1  christos 	DOTEST(1024, "-12", 3, "%03d", -12);
    896  1.1  christos 	DOTEST(1024, "-02", 3, "%03d", -2);
    897  1.1  christos 	DOTEST(1024, "-15", 3, "%d", -15);
    898  1.1  christos 	DOTEST(1024, "-7307", 5, "%d", -7307);
    899  1.1  christos 	DOTEST(1024, "-12  ", 5, "%-5d", -12);
    900  1.1  christos 	DOTEST(1024, "-00012", 6, "%-.5d", -12);
    901  1.1  christos 
    902  1.1  christos 	/* test + and space flags */
    903  1.1  christos 	DOTEST(1024, "+12", 3, "%+d", 12);
    904  1.1  christos 	DOTEST(1024, " 12", 3, "% d", 12);
    905  1.1  christos 
    906  1.1  christos 	/* test %u */
    907  1.1  christos 	DOTEST(1024, "12", 2, "%u", 12);
    908  1.1  christos 	DOTEST(1024, "0", 1, "%u", 0);
    909  1.1  christos 	DOTEST(1024, "4294967295", 10, "%u", 0xffffffff);
    910  1.1  christos 
    911  1.1  christos 	/* test %x */
    912  1.1  christos 	DOTEST(1024, "0", 1, "%x", 0);
    913  1.1  christos 	DOTEST(1024, "c", 1, "%x", 12);
    914  1.1  christos 	DOTEST(1024, "12ab34cd", 8, "%x", 0x12ab34cd);
    915  1.1  christos 
    916  1.1  christos 	/* test %llu, %lld */
    917  1.1  christos 	DOTEST(1024, "18446744073709551615", 20, "%llu",
    918  1.1  christos 		(long long)0xffffffffffffffff);
    919  1.1  christos 	DOTEST(1024, "-9223372036854775808", 20, "%lld",
    920  1.1  christos 		(long long)0x8000000000000000);
    921  1.1  christos 	DOTEST(1024, "9223372036854775808", 19, "%llu",
    922  1.1  christos 		(long long)0x8000000000000000);
    923  1.1  christos 
    924  1.1  christos 	/* test %s */
    925  1.1  christos 	DOTEST(1024, "hello", 5, "%s", "hello");
    926  1.1  christos 	DOTEST(1024, "     hello", 10, "%10s", "hello");
    927  1.1  christos 	DOTEST(1024, "hello     ", 10, "%-10s", "hello");
    928  1.1  christos 	DOTEST(1024, "he", 2, "%.2s", "hello");
    929  1.1  christos 	DOTEST(1024, "  he", 4, "%4.2s", "hello");
    930  1.1  christos 	DOTEST(1024, "   h", 4, "%4.2s", "h");
    931  1.1  christos 
    932  1.1  christos 	/* test %c */
    933  1.1  christos 	DOTEST(1024, "a", 1, "%c", 'a');
    934  1.1  christos 	/* warning from gcc for format string, but it does work
    935  1.1  christos 	   DOTEST(1024, "    a", 5, "%5c", 'a');
    936  1.1  christos 	   DOTEST(1024, "a", 1, "%.0c", 'a'); */
    937  1.1  christos 
    938  1.1  christos 	/* test %n */
    939  1.1  christos 	DOTEST(1024, "hello", 5, "hello%n", &x);
    940  1.1  christos 	if(x != 5) { printf("the %%n failed\n"); exit(1); }
    941  1.1  christos 
    942  1.1  christos 	/* test %m */
    943  1.1  christos 	errno = 0;
    944  1.1  christos 	DOTEST(1024, "Success", 7, "%m");
    945  1.1  christos 
    946  1.1  christos 	/* test %p */
    947  1.1  christos 	DOTEST(1024, "0x10", 4, "%p", (void*)0x10);
    948  1.1  christos 	DOTEST(1024, "(nil)", 5, "%p", (void*)0x0);
    949  1.1  christos 
    950  1.1  christos 	/* test %% */
    951  1.1  christos 	DOTEST(1024, "%", 1, "%%");
    952  1.1  christos 
    953  1.1  christos 	/* test %f */
    954  1.1  christos 	DOTEST(1024, "0.000000", 8, "%f", 0.0);
    955  1.1  christos 	DOTEST(1024, "0.00", 4, "%.2f", 0.0);
    956  1.1  christos 	/* differs, "-0.00" DOTEST(1024, "0.00", 4, "%.2f", -0.0); */
    957  1.1  christos 	DOTEST(1024, "234.00", 6, "%.2f", 234.005);
    958  1.1  christos 	DOTEST(1024, "8973497.1246", 12, "%.4f", 8973497.12456);
    959  1.1  christos 	DOTEST(1024, "-12.000000", 10, "%f", -12.0);
    960  1.1  christos 	DOTEST(1024, "6", 1, "%.0f", 6.0);
    961  1.1  christos 
    962  1.1  christos 	DOTEST(1024, "6", 1, "%g", 6.0);
    963  1.1  christos 	DOTEST(1024, "6.1", 3, "%g", 6.1);
    964  1.1  christos 	DOTEST(1024, "6.15", 4, "%g", 6.15);
    965  1.1  christos 
    966  1.1  christos 	/* These format strings are from the code of NSD, Unbound, ldns */
    967  1.1  christos 
    968  1.1  christos 	DOTEST(1024, "abcdef", 6, "%s", "abcdef");
    969  1.1  christos 	DOTEST(1024, "005", 3, "%03u", 5);
    970  1.1  christos 	DOTEST(1024, "12345", 5, "%03u", 12345);
    971  1.1  christos 	DOTEST(1024, "5", 1, "%d", 5);
    972  1.1  christos 	DOTEST(1024, "(nil)", 5, "%p", NULL);
    973  1.1  christos 	DOTEST(1024, "12345", 5, "%ld", (long)12345);
    974  1.1  christos 	DOTEST(1024, "12345", 5, "%lu", (long)12345);
    975  1.1  christos 	DOTEST(1024, "       12345", 12, "%12u", (unsigned)12345);
    976  1.1  christos 	DOTEST(1024, "12345", 5, "%u", (unsigned)12345);
    977  1.1  christos 	DOTEST(1024, "12345", 5, "%llu", (unsigned long long)12345);
    978  1.1  christos 	DOTEST(1024, "12345", 5, "%x", 0x12345);
    979  1.1  christos 	DOTEST(1024, "12345", 5, "%llx", (long long)0x12345);
    980  1.1  christos 	DOTEST(1024, "012345", 6, "%6.6d", 12345);
    981  1.1  christos 	DOTEST(1024, "012345", 6, "%6.6u", 12345);
    982  1.1  christos 	DOTEST(1024, "1234.54", 7, "%g", 1234.54);
    983  1.1  christos 	DOTEST(1024, "123456789.54", 12, "%.12g", 123456789.54);
    984  1.1  christos 	DOTEST(1024, "3456789123456.54", 16, "%.16g", 3456789123456.54);
    985  1.1  christos 	/* %24g does not work with 24 digits, not enough accuracy,
    986  1.1  christos 	 * the first 16 digits are correct */
    987  1.1  christos 	DOTEST(1024, "12345", 5, "%3.3d", 12345);
    988  1.1  christos 	DOTEST(1024, "000", 3, "%3.3d", 0);
    989  1.1  christos 	DOTEST(1024, "001", 3, "%3.3d", 1);
    990  1.1  christos 	DOTEST(1024, "012", 3, "%3.3d", 12);
    991  1.1  christos 	DOTEST(1024, "-012", 4, "%3.3d", -12);
    992  1.1  christos 	DOTEST(1024, "he", 2, "%.2s", "hello");
    993  1.1  christos 	DOTEST(1024, "helloworld", 10, "%s%s", "hello", "world");
    994  1.1  christos 	DOTEST(1024, "he", 2, "%.*s", 2, "hello");
    995  1.1  christos 	DOTEST(1024, "  hello", 7, "%*s", 7, "hello");
    996  1.1  christos 	DOTEST(1024, "hello  ", 7, "%*s", -7, "hello");
    997  1.1  christos 	DOTEST(1024, "0", 1, "%c", '0');
    998  1.1  christos 	DOTEST(1024, "A", 1, "%c", 'A');
    999  1.1  christos 	DOTEST(1024, "", 1, "%c", 0);
   1000  1.1  christos 	DOTEST(1024, "\010", 1, "%c", 8);
   1001  1.1  christos 	DOTEST(1024, "%", 1, "%%");
   1002  1.1  christos 	DOTEST(1024, "0a", 2, "%02x", 0x0a);
   1003  1.1  christos 	DOTEST(1024, "bd", 2, "%02x", 0xbd);
   1004  1.1  christos 	DOTEST(1024, "12", 2, "%02ld", (long)12);
   1005  1.1  christos 	DOTEST(1024, "02", 2, "%02ld", (long)2);
   1006  1.1  christos 	DOTEST(1024, "02", 2, "%02u", (unsigned)2);
   1007  1.1  christos 	DOTEST(1024, "765432", 6, "%05u", (unsigned)765432);
   1008  1.1  christos 	DOTEST(1024, "10.234", 6, "%0.3f", 10.23421);
   1009  1.1  christos 	DOTEST(1024, "123456.234", 10, "%0.3f", 123456.23421);
   1010  1.1  christos 	DOTEST(1024, "123456789.234", 13, "%0.3f", 123456789.23421);
   1011  1.1  christos 	DOTEST(1024, "123456.23", 9, "%.2f", 123456.23421);
   1012  1.1  christos 	DOTEST(1024, "123456", 6, "%.0f", 123456.23421);
   1013  1.1  christos 	DOTEST(1024, "0123", 4, "%.4x", 0x0123);
   1014  1.1  christos 	DOTEST(1024, "00000123", 8, "%.8x", 0x0123);
   1015  1.1  christos 	DOTEST(1024, "ffeb0cde", 8, "%.8x", 0xffeb0cde);
   1016  1.1  christos 	DOTEST(1024, " 987654321", 10, "%10lu", (unsigned long)987654321);
   1017  1.1  christos 	DOTEST(1024, "   987654321", 12, "%12lu", (unsigned long)987654321);
   1018  1.1  christos 	DOTEST(1024, "987654321", 9, "%i", 987654321);
   1019  1.1  christos 	DOTEST(1024, "-87654321", 9, "%i", -87654321);
   1020  1.1  christos 	DOTEST(1024, "hello           ", 16, "%-16s", "hello");
   1021  1.1  christos 	DOTEST(1024, "                ", 16, "%-16s", "");
   1022  1.1  christos 	DOTEST(1024, "a               ", 16, "%-16s", "a");
   1023  1.1  christos 	DOTEST(1024, "foobarfoobar    ", 16, "%-16s", "foobarfoobar");
   1024  1.1  christos 	DOTEST(1024, "foobarfoobarfoobar", 18, "%-16s", "foobarfoobarfoobar");
   1025  1.1  christos 
   1026  1.1  christos 	/* combined expressions */
   1027  1.1  christos 	DOTEST(1024, "foo 1.0 size 512 edns", 21,
   1028  1.1  christos 		"foo %s size %d %s%s", "1.0", 512, "", "edns");
   1029  1.1  christos 	DOTEST(15, "foo 1.0 size 5", 21,
   1030  1.1  christos 		"foo %s size %d %s%s", "1.0", 512, "", "edns");
   1031  1.1  christos 	DOTEST(1024, "packet 1203ceff id", 18,
   1032  1.1  christos 		"packet %2.2x%2.2x%2.2x%2.2x id", 0x12, 0x03, 0xce, 0xff);
   1033  1.1  christos 	DOTEST(1024, "/tmp/testbound_123abcd.tmp", 26, "/tmp/testbound_%u%s%s.tmp", 123, "ab", "cd");
   1034  1.1  christos 
   1035  1.1  christos 	return 0;
   1036  1.1  christos }
   1037  1.1  christos #endif /* SNPRINTF_TEST */
   1038