1 /* snprintf - compatibility implementation of snprintf, vsnprintf
3 * Copyright (c) 2013, NLnet Labs. All rights reserved.
5 * This software is open source.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * Neither the name of the NLNET LABS nor the names of its contributors may
19 * be used to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 /* #define SNPRINTF_TEST 1 */
50 #define snprintf my_snprintf
51 #define vsnprintf my_vsnprintf
52 #endif /* SNPRINTF_TEST */
54 int snprintf(char* str, size_t size, const char* format, ...);
55 int vsnprintf(char* str, size_t size, const char* format, va_list arg);
58 * Very portable snprintf implementation, limited in functionality,
59 * esp. for %[capital] %[nonportable] and so on. Reduced float functionality,
60 * mostly in formatting and range (e+-16), for %f and %g.
62 * %s, %d, %u, %i, %x, %c, %n and %% are fully supported.
63 * This includes width, precision, flags 0- +, and *(arg for wid,prec).
64 * %f, %g, %m, %p have reduced support, support for wid,prec,flags,*, but
65 * less floating point range, no %e formatting for %g.
67 int snprintf(char* str, size_t size, const char* format, ...)
71 va_start(args, format);
72 r = vsnprintf(str, size, format, args);
77 /** add padding to string */
79 print_pad(char** at, size_t* left, int* ret, char p, int num)
90 /** get negative symbol, 0 if none */
92 get_negsign(int negative, int plus, int space)
103 #define PRINT_DEC_BUFSZ 32 /* 20 is enough for 64 bit decimals */
104 /** print decimal into buffer, returns length */
106 print_dec(char* buf, int max, unsigned int value)
114 } else while(value && i < max) {
115 buf[i++] = '0' + value % 10;
121 /** print long decimal into buffer, returns length */
123 print_dec_l(char* buf, int max, unsigned long value)
131 } else while(value && i < max) {
132 buf[i++] = '0' + value % 10;
138 /** print long decimal into buffer, returns length */
140 print_dec_ll(char* buf, int max, unsigned long long value)
148 } else while(value && i < max) {
149 buf[i++] = '0' + value % 10;
155 /** print hex into buffer, returns length */
157 print_hex(char* buf, int max, unsigned int value)
159 const char* h = "0123456789abcdef";
166 } else while(value && i < max) {
167 buf[i++] = h[value & 0x0f];
173 /** print long hex into buffer, returns length */
175 print_hex_l(char* buf, int max, unsigned long value)
177 const char* h = "0123456789abcdef";
184 } else while(value && i < max) {
185 buf[i++] = h[value & 0x0f];
191 /** print long long hex into buffer, returns length */
193 print_hex_ll(char* buf, int max, unsigned long long value)
195 const char* h = "0123456789abcdef";
202 } else while(value && i < max) {
203 buf[i++] = h[value & 0x0f];
209 /** copy string into result, reversed */
211 spool_str_rev(char** at, size_t* left, int* ret, const char* buf, int len)
223 /** copy string into result */
225 spool_str(char** at, size_t* left, int* ret, const char* buf, int len)
228 for(i=0; i<len; i++) {
237 /** print number formatted */
239 print_num(char** at, size_t* left, int* ret, int minw, int precision,
240 int prgiven, int zeropad, int minus, int plus, int space,
241 int zero, int negative, char* buf, int len)
243 int w = len; /* excludes minus sign */
244 char s = get_negsign(negative, plus, space);
246 /* left adjust the number into the field, space padding */
247 /* calc numw = [sign][zeroes][number] */
249 if(precision == 0 && zero) numw = 0;
250 if(numw < precision) numw = precision;
254 if(s) print_pad(at, left, ret, s, 1);
257 if(precision == 0 && zero) {
258 /* "" for the number */
261 print_pad(at, left, ret, '0', precision - w);
262 spool_str_rev(at, left, ret, buf, len);
266 print_pad(at, left, ret, ' ', minw - numw);
268 /* pad on the left of the number */
269 /* calculate numw has width of [sign][zeroes][number] */
271 if(precision == 0 && zero) numw = 0;
272 if(numw < precision) numw = precision;
273 if(!prgiven && zeropad && numw < minw) numw = minw;
276 /* pad with spaces */
278 print_pad(at, left, ret, ' ', minw - numw);
279 /* print sign (and one less zeropad if so) */
281 print_pad(at, left, ret, s, 1);
284 /* pad with zeroes */
286 print_pad(at, left, ret, '0', numw - w);
287 if(precision == 0 && zero)
289 /* print the characters for the value */
290 spool_str_rev(at, left, ret, buf, len);
294 /** print %d and %i */
296 print_num_d(char** at, size_t* left, int* ret, int value,
297 int minw, int precision, int prgiven, int zeropad, int minus,
300 char buf[PRINT_DEC_BUFSZ];
301 int negative = (value < 0);
302 int zero = (value == 0);
303 int len = print_dec(buf, (int)sizeof(buf),
304 (unsigned int)(negative?-value:value));
305 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
306 plus, space, zero, negative, buf, len);
309 /** print %ld and %li */
311 print_num_ld(char** at, size_t* left, int* ret, long value,
312 int minw, int precision, int prgiven, int zeropad, int minus,
315 char buf[PRINT_DEC_BUFSZ];
316 int negative = (value < 0);
317 int zero = (value == 0);
318 int len = print_dec_l(buf, (int)sizeof(buf),
319 (unsigned long)(negative?-value:value));
320 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
321 plus, space, zero, negative, buf, len);
324 /** print %lld and %lli */
326 print_num_lld(char** at, size_t* left, int* ret, long long value,
327 int minw, int precision, int prgiven, int zeropad, int minus,
330 char buf[PRINT_DEC_BUFSZ];
331 int negative = (value < 0);
332 int zero = (value == 0);
333 int len = print_dec_ll(buf, (int)sizeof(buf),
334 (unsigned long long)(negative?-value:value));
335 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
336 plus, space, zero, negative, buf, len);
341 print_num_u(char** at, size_t* left, int* ret, unsigned int value,
342 int minw, int precision, int prgiven, int zeropad, int minus,
345 char buf[PRINT_DEC_BUFSZ];
347 int zero = (value == 0);
348 int len = print_dec(buf, (int)sizeof(buf), value);
349 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
350 plus, space, zero, negative, buf, len);
355 print_num_lu(char** at, size_t* left, int* ret, unsigned long value,
356 int minw, int precision, int prgiven, int zeropad, int minus,
359 char buf[PRINT_DEC_BUFSZ];
361 int zero = (value == 0);
362 int len = print_dec_l(buf, (int)sizeof(buf), value);
363 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
364 plus, space, zero, negative, buf, len);
369 print_num_llu(char** at, size_t* left, int* ret, unsigned long long value,
370 int minw, int precision, int prgiven, int zeropad, int minus,
373 char buf[PRINT_DEC_BUFSZ];
375 int zero = (value == 0);
376 int len = print_dec_ll(buf, (int)sizeof(buf), value);
377 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
378 plus, space, zero, negative, buf, len);
383 print_num_x(char** at, size_t* left, int* ret, unsigned int value,
384 int minw, int precision, int prgiven, int zeropad, int minus,
387 char buf[PRINT_DEC_BUFSZ];
389 int zero = (value == 0);
390 int len = print_hex(buf, (int)sizeof(buf), value);
391 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
392 plus, space, zero, negative, buf, len);
397 print_num_lx(char** at, size_t* left, int* ret, unsigned long value,
398 int minw, int precision, int prgiven, int zeropad, int minus,
401 char buf[PRINT_DEC_BUFSZ];
403 int zero = (value == 0);
404 int len = print_hex_l(buf, (int)sizeof(buf), value);
405 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
406 plus, space, zero, negative, buf, len);
411 print_num_llx(char** at, size_t* left, int* ret, unsigned long long value,
412 int minw, int precision, int prgiven, int zeropad, int minus,
415 char buf[PRINT_DEC_BUFSZ];
417 int zero = (value == 0);
418 int len = print_hex_ll(buf, (int)sizeof(buf), value);
419 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
420 plus, space, zero, negative, buf, len);
425 print_num_llp(char** at, size_t* left, int* ret, void* value,
426 int minw, int precision, int prgiven, int zeropad, int minus,
429 char buf[PRINT_DEC_BUFSZ];
431 int zero = (value == 0);
432 #if defined(SIZE_MAX) && defined(UINT32_MAX) && (UINT32_MAX == SIZE_MAX || INT32_MAX == SIZE_MAX)
433 /* avoid warning about upcast on 32bit systems */
434 unsigned long long llvalue = (unsigned long)value;
436 unsigned long long llvalue = (unsigned long long)value;
438 int len = print_hex_ll(buf, (int)sizeof(buf), llvalue);
447 /* put '0x' in front of the (reversed) buffer result */
448 if(len < PRINT_DEC_BUFSZ)
450 if(len < PRINT_DEC_BUFSZ)
453 print_num(at, left, ret, minw, precision, prgiven, zeropad, minus,
454 plus, space, zero, negative, buf, len);
457 #define PRINT_FLOAT_BUFSZ 64 /* xx.yy with 20.20 about the max */
458 /** spool remainder after the decimal point to buffer, in reverse */
460 print_remainder(char* buf, int max, double r, int prec)
462 unsigned long long cap = 1;
463 unsigned long long value;
465 if(prec > 19) prec = 19; /* max we can do */
466 if(max < prec) return 0;
467 for(i=0; i<prec; i++) {
471 value = (unsigned long long)r;
472 /* see if we need to round up */
473 if(((unsigned long long)((r - (double)value)*10.0)) >= 5) {
475 /* that might carry to numbers before the comma, if so,
476 * just ignore that rounding. failure because 64bitprintout */
480 len = print_dec_ll(buf, max, value);
481 while(len < prec) { /* pad with zeroes, e.g. if 0.0012 */
489 /** spool floating point to buffer */
491 print_float(char* buf, int max, double value, int prec)
493 /* as xxx.xxx if prec==0, no '.', with prec decimals after . */
494 /* no conversion for NAN and INF, because we do not want to require
496 /* Thus, the conversions use 64bit integers to convert the numbers,
497 * which makes 19 digits before and after the decimal point the max */
498 unsigned long long whole = (unsigned long long)value;
499 double remain = value - (double)whole;
502 len = print_remainder(buf, max, remain, prec);
503 len += print_dec_ll(buf+len, max-len, whole);
509 print_num_f(char** at, size_t* left, int* ret, double value,
510 int minw, int precision, int prgiven, int zeropad, int minus,
513 char buf[PRINT_FLOAT_BUFSZ];
514 int negative = (value < 0);
517 if(!prgiven) precision = 6;
518 len = print_float(buf, (int)sizeof(buf), negative?-value:value,
520 print_num(at, left, ret, minw, 1, 0, zeropad, minus,
521 plus, space, zero, negative, buf, len);
524 /* rudimentary %g support */
526 print_float_g(char* buf, int max, double value, int prec)
528 unsigned long long whole = (unsigned long long)value;
529 double remain = value - (double)whole;
533 /* number of digits before the decimal point */
538 whole = (unsigned long long)value;
540 if(prec > before && remain != 0.0) {
541 /* see if the last decimals are zero, if so, skip them */
542 len = print_remainder(buf, max, remain, prec-before);
543 while(len > 0 && buf[0]=='0') {
544 memmove(buf, buf+1, --len);
547 len += print_dec_ll(buf+len, max-len, whole);
554 print_num_g(char** at, size_t* left, int* ret, double value,
555 int minw, int precision, int prgiven, int zeropad, int minus,
558 char buf[PRINT_FLOAT_BUFSZ];
559 int negative = (value < 0);
562 if(!prgiven) precision = 6;
563 if(precision == 0) precision = 1;
564 len = print_float_g(buf, (int)sizeof(buf), negative?-value:value,
566 print_num(at, left, ret, minw, 1, 0, zeropad, minus,
567 plus, space, zero, negative, buf, len);
571 /** strnlen (compat implementation) */
573 my_strnlen(const char* s, int max)
584 print_str(char** at, size_t* left, int* ret, char* s,
585 int minw, int precision, int prgiven, int minus)
588 /* with prec: no more than x characters from this string, stop at 0 */
590 w = my_strnlen(s, precision);
591 else w = (int)strlen(s); /* up to the nul */
592 if(w < minw && !minus)
593 print_pad(at, left, ret, ' ', minw - w);
594 spool_str(at, left, ret, s, w);
595 if(w < minw && minus)
596 print_pad(at, left, ret, ' ', minw - w);
601 print_char(char** at, size_t* left, int* ret, int c,
604 if(1 < minw && !minus)
605 print_pad(at, left, ret, ' ', minw - 1);
606 print_pad(at, left, ret, c, 1);
607 if(1 < minw && minus)
608 print_pad(at, left, ret, ' ', minw - 1);
614 * str: string buffer for result. result will be null terminated.
615 * size: size of the buffer. null is put inside buffer.
616 * format: printf format string.
617 * arg: '...' arguments to print.
618 * returns number of characters. a null is printed after this.
619 * return number of bytes that would have been written
620 * if the buffer had been large enough.
622 * supported format specifiers:
623 * %s, %u, %d, %x, %i, %f, %g, %c, %p, %n.
624 * length: l, ll (for d, u, x).
625 * precision: 6.6d (for d, u, x)
626 * %f, %g precisions, 0.3f
630 int vsnprintf(char* str, size_t size, const char* format, va_list arg)
635 const char* fmt = format;
636 int conv, minw, precision, prgiven, zeropad, minus, plus, space, length;
638 /* copy string before % */
639 while(*fmt && *fmt!='%') {
647 /* see if we are at end */
650 /* fetch next argument % designation from format string */
651 fmt++; /* skip the '%' */
653 /********************************/
654 /* get the argument designation */
655 /********************************/
656 /* we must do this vararg stuff inside this function for
657 * portability. Hence, get_designation, and print_designation
658 * are not their own functions. */
660 /* printout designation:
661 * conversion specifier: x, d, u, s, c, n, m, p
662 * flags: # not supported
663 * 0 zeropad (on the left)
664 * - left adjust (right by default)
665 * ' ' printspace for positive number (in - position).
667 * fieldwidth: [1-9][0-9]* minimum field width.
668 * if this is * then type int next argument specifies the minwidth.
669 * if this is negative, the - flag is set (with positive width).
670 * precision: period[digits]*, %.2x.
671 * if this is * then type int next argument specifies the precision.
672 * just '.' or negative value means precision=0.
673 * this is mindigits to print for d, i, u, x
674 * this is aftercomma digits for f
675 * this is max number significant digits for g
676 * maxnumber characters to be printed for s
677 * length: 0-none (int), 1-l (long), 2-ll (long long)
678 * notsupported: hh (char), h (short), L (long double), q, j, z, t
679 * Does not support %m$ and *m$ argument designation as array indices.
680 * Does not support %#x
692 /* get flags in any order */
708 fmt++; /* skip char */
709 minw = va_arg(arg, int);
714 } else while(*fmt >= '0' && *fmt <= '9') {
715 minw = minw*10 + (*fmt++)-'0';
720 fmt++; /* skip period */
724 fmt++; /* skip char */
725 precision = va_arg(arg, int);
728 } else while(*fmt >= '0' && *fmt <= '9') {
729 precision = precision*10 + (*fmt++)-'0';
735 fmt++; /* skip char */
738 fmt++; /* skip char */
743 /* get the conversion */
747 /***********************************/
748 /* print that argument designation */
749 /***********************************/
754 print_num_d(&at, &left, &ret, va_arg(arg, int),
755 minw, precision, prgiven, zeropad, minus, plus, space);
757 print_num_ld(&at, &left, &ret, va_arg(arg, long),
758 minw, precision, prgiven, zeropad, minus, plus, space);
760 print_num_lld(&at, &left, &ret,
761 va_arg(arg, long long),
762 minw, precision, prgiven, zeropad, minus, plus, space);
766 print_num_u(&at, &left, &ret,
767 va_arg(arg, unsigned int),
768 minw, precision, prgiven, zeropad, minus, plus, space);
770 print_num_lu(&at, &left, &ret,
771 va_arg(arg, unsigned long),
772 minw, precision, prgiven, zeropad, minus, plus, space);
774 print_num_llu(&at, &left, &ret,
775 va_arg(arg, unsigned long long),
776 minw, precision, prgiven, zeropad, minus, plus, space);
780 print_num_x(&at, &left, &ret,
781 va_arg(arg, unsigned int),
782 minw, precision, prgiven, zeropad, minus, plus, space);
784 print_num_lx(&at, &left, &ret,
785 va_arg(arg, unsigned long),
786 minw, precision, prgiven, zeropad, minus, plus, space);
788 print_num_llx(&at, &left, &ret,
789 va_arg(arg, unsigned long long),
790 minw, precision, prgiven, zeropad, minus, plus, space);
793 print_str(&at, &left, &ret, va_arg(arg, char*),
794 minw, precision, prgiven, minus);
797 print_char(&at, &left, &ret, va_arg(arg, int),
801 *va_arg(arg, int*) = ret;
804 print_str(&at, &left, &ret, strerror(errno),
805 minw, precision, prgiven, minus);
808 print_num_llp(&at, &left, &ret, va_arg(arg, void*),
809 minw, precision, prgiven, zeropad, minus, plus, space);
812 print_pad(&at, &left, &ret, '%', 1);
815 print_num_f(&at, &left, &ret, va_arg(arg, double),
816 minw, precision, prgiven, zeropad, minus, plus, space);
819 print_num_g(&at, &left, &ret, va_arg(arg, double),
820 minw, precision, prgiven, zeropad, minus, plus, space);
838 #define DOTEST(bufsz, result, retval, ...) do { \
840 printf("now test %s\n", #__VA_ARGS__); \
841 int r=my_snprintf(buf, sizeof(buf), __VA_ARGS__); \
842 if(r != retval || strcmp(buf, result) != 0) { \
843 printf("error test(%s) was \"%s\":%d\n", \
844 ""#bufsz", "#result", "#retval", "#__VA_ARGS__, \
848 r=snprintf(buf, sizeof(buf), __VA_ARGS__); \
849 if(r != retval || strcmp(buf, result) != 0) { \
850 printf("error test(%s) differs with system, \"%s\":%d\n", \
851 ""#bufsz", "#result", "#retval", "#__VA_ARGS__, \
855 printf("test(\"%s\":%d) passed\n", buf, r); \
863 /* bufsize, expectedstring, expectedretval, snprintf arguments */
864 DOTEST(1024, "hello", 5, "hello");
865 DOTEST(1024, "h", 1, "h");
866 /* warning from gcc for format string, but it does work
867 * DOTEST(1024, "", 0, ""); */
869 DOTEST(3, "he", 5, "hello");
870 DOTEST(1, "", 7, "%d", 7823089);
872 /* test positive numbers */
873 DOTEST(1024, "0", 1, "%d", 0);
874 DOTEST(1024, "1", 1, "%d", 1);
875 DOTEST(1024, "9", 1, "%d", 9);
876 DOTEST(1024, "15", 2, "%d", 15);
877 DOTEST(1024, "ab15cd", 6, "ab%dcd", 15);
878 DOTEST(1024, "167", 3, "%d", 167);
879 DOTEST(1024, "7823089", 7, "%d", 7823089);
880 DOTEST(1024, " 12", 3, "%3d", 12);
881 DOTEST(1024, "012", 3, "%.3d", 12);
882 DOTEST(1024, "012", 3, "%3.3d", 12);
883 DOTEST(1024, "012", 3, "%03d", 12);
884 DOTEST(1024, " 012", 4, "%4.3d", 12);
885 DOTEST(1024, "", 0, "%.0d", 0);
887 /* test negative numbers */
888 DOTEST(1024, "-1", 2, "%d", -1);
889 DOTEST(1024, "-12", 3, "%3d", -12);
890 DOTEST(1024, " -2", 3, "%3d", -2);
891 DOTEST(1024, "-012", 4, "%.3d", -12);
892 DOTEST(1024, "-012", 4, "%3.3d", -12);
893 DOTEST(1024, "-012", 4, "%4.3d", -12);
894 DOTEST(1024, " -012", 5, "%5.3d", -12);
895 DOTEST(1024, "-12", 3, "%03d", -12);
896 DOTEST(1024, "-02", 3, "%03d", -2);
897 DOTEST(1024, "-15", 3, "%d", -15);
898 DOTEST(1024, "-7307", 5, "%d", -7307);
899 DOTEST(1024, "-12 ", 5, "%-5d", -12);
900 DOTEST(1024, "-00012", 6, "%-.5d", -12);
902 /* test + and space flags */
903 DOTEST(1024, "+12", 3, "%+d", 12);
904 DOTEST(1024, " 12", 3, "% d", 12);
907 DOTEST(1024, "12", 2, "%u", 12);
908 DOTEST(1024, "0", 1, "%u", 0);
909 DOTEST(1024, "4294967295", 10, "%u", 0xffffffff);
912 DOTEST(1024, "0", 1, "%x", 0);
913 DOTEST(1024, "c", 1, "%x", 12);
914 DOTEST(1024, "12ab34cd", 8, "%x", 0x12ab34cd);
916 /* test %llu, %lld */
917 DOTEST(1024, "18446744073709551615", 20, "%llu",
918 (long long)0xffffffffffffffff);
919 DOTEST(1024, "-9223372036854775808", 20, "%lld",
920 (long long)0x8000000000000000);
921 DOTEST(1024, "9223372036854775808", 19, "%llu",
922 (long long)0x8000000000000000);
925 DOTEST(1024, "hello", 5, "%s", "hello");
926 DOTEST(1024, " hello", 10, "%10s", "hello");
927 DOTEST(1024, "hello ", 10, "%-10s", "hello");
928 DOTEST(1024, "he", 2, "%.2s", "hello");
929 DOTEST(1024, " he", 4, "%4.2s", "hello");
930 DOTEST(1024, " h", 4, "%4.2s", "h");
933 DOTEST(1024, "a", 1, "%c", 'a');
934 /* warning from gcc for format string, but it does work
935 DOTEST(1024, " a", 5, "%5c", 'a');
936 DOTEST(1024, "a", 1, "%.0c", 'a'); */
939 DOTEST(1024, "hello", 5, "hello%n", &x);
940 if(x != 5) { printf("the %%n failed\n"); exit(1); }
944 DOTEST(1024, "Success", 7, "%m");
947 DOTEST(1024, "0x10", 4, "%p", (void*)0x10);
948 DOTEST(1024, "(nil)", 5, "%p", (void*)0x0);
951 DOTEST(1024, "%", 1, "%%");
954 DOTEST(1024, "0.000000", 8, "%f", 0.0);
955 DOTEST(1024, "0.00", 4, "%.2f", 0.0);
956 /* differs, "-0.00" DOTEST(1024, "0.00", 4, "%.2f", -0.0); */
957 DOTEST(1024, "234.00", 6, "%.2f", 234.005);
958 DOTEST(1024, "8973497.1246", 12, "%.4f", 8973497.12456);
959 DOTEST(1024, "-12.000000", 10, "%f", -12.0);
960 DOTEST(1024, "6", 1, "%.0f", 6.0);
962 DOTEST(1024, "6", 1, "%g", 6.0);
963 DOTEST(1024, "6.1", 3, "%g", 6.1);
964 DOTEST(1024, "6.15", 4, "%g", 6.15);
966 /* These format strings are from the code of NSD, Unbound, ldns */
968 DOTEST(1024, "abcdef", 6, "%s", "abcdef");
969 DOTEST(1024, "005", 3, "%03u", 5);
970 DOTEST(1024, "12345", 5, "%03u", 12345);
971 DOTEST(1024, "5", 1, "%d", 5);
972 DOTEST(1024, "(nil)", 5, "%p", NULL);
973 DOTEST(1024, "12345", 5, "%ld", (long)12345);
974 DOTEST(1024, "12345", 5, "%lu", (long)12345);
975 DOTEST(1024, " 12345", 12, "%12u", (unsigned)12345);
976 DOTEST(1024, "12345", 5, "%u", (unsigned)12345);
977 DOTEST(1024, "12345", 5, "%llu", (unsigned long long)12345);
978 DOTEST(1024, "12345", 5, "%x", 0x12345);
979 DOTEST(1024, "12345", 5, "%llx", (long long)0x12345);
980 DOTEST(1024, "012345", 6, "%6.6d", 12345);
981 DOTEST(1024, "012345", 6, "%6.6u", 12345);
982 DOTEST(1024, "1234.54", 7, "%g", 1234.54);
983 DOTEST(1024, "123456789.54", 12, "%.12g", 123456789.54);
984 DOTEST(1024, "3456789123456.54", 16, "%.16g", 3456789123456.54);
985 /* %24g does not work with 24 digits, not enough accuracy,
986 * the first 16 digits are correct */
987 DOTEST(1024, "12345", 5, "%3.3d", 12345);
988 DOTEST(1024, "000", 3, "%3.3d", 0);
989 DOTEST(1024, "001", 3, "%3.3d", 1);
990 DOTEST(1024, "012", 3, "%3.3d", 12);
991 DOTEST(1024, "-012", 4, "%3.3d", -12);
992 DOTEST(1024, "he", 2, "%.2s", "hello");
993 DOTEST(1024, "helloworld", 10, "%s%s", "hello", "world");
994 DOTEST(1024, "he", 2, "%.*s", 2, "hello");
995 DOTEST(1024, " hello", 7, "%*s", 7, "hello");
996 DOTEST(1024, "hello ", 7, "%*s", -7, "hello");
997 DOTEST(1024, "0", 1, "%c", '0');
998 DOTEST(1024, "A", 1, "%c", 'A');
999 DOTEST(1024, "", 1, "%c", 0);
1000 DOTEST(1024, "\010", 1, "%c", 8);
1001 DOTEST(1024, "%", 1, "%%");
1002 DOTEST(1024, "0a", 2, "%02x", 0x0a);
1003 DOTEST(1024, "bd", 2, "%02x", 0xbd);
1004 DOTEST(1024, "12", 2, "%02ld", (long)12);
1005 DOTEST(1024, "02", 2, "%02ld", (long)2);
1006 DOTEST(1024, "02", 2, "%02u", (unsigned)2);
1007 DOTEST(1024, "765432", 6, "%05u", (unsigned)765432);
1008 DOTEST(1024, "10.234", 6, "%0.3f", 10.23421);
1009 DOTEST(1024, "123456.234", 10, "%0.3f", 123456.23421);
1010 DOTEST(1024, "123456789.234", 13, "%0.3f", 123456789.23421);
1011 DOTEST(1024, "123456.23", 9, "%.2f", 123456.23421);
1012 DOTEST(1024, "123456", 6, "%.0f", 123456.23421);
1013 DOTEST(1024, "0123", 4, "%.4x", 0x0123);
1014 DOTEST(1024, "00000123", 8, "%.8x", 0x0123);
1015 DOTEST(1024, "ffeb0cde", 8, "%.8x", 0xffeb0cde);
1016 DOTEST(1024, " 987654321", 10, "%10lu", (unsigned long)987654321);
1017 DOTEST(1024, " 987654321", 12, "%12lu", (unsigned long)987654321);
1018 DOTEST(1024, "987654321", 9, "%i", 987654321);
1019 DOTEST(1024, "-87654321", 9, "%i", -87654321);
1020 DOTEST(1024, "hello ", 16, "%-16s", "hello");
1021 DOTEST(1024, " ", 16, "%-16s", "");
1022 DOTEST(1024, "a ", 16, "%-16s", "a");
1023 DOTEST(1024, "foobarfoobar ", 16, "%-16s", "foobarfoobar");
1024 DOTEST(1024, "foobarfoobarfoobar", 18, "%-16s", "foobarfoobarfoobar");
1026 /* combined expressions */
1027 DOTEST(1024, "foo 1.0 size 512 edns", 21,
1028 "foo %s size %d %s%s", "1.0", 512, "", "edns");
1029 DOTEST(15, "foo 1.0 size 5", 21,
1030 "foo %s size %d %s%s", "1.0", 512, "", "edns");
1031 DOTEST(1024, "packet 1203ceff id", 18,
1032 "packet %2.2x%2.2x%2.2x%2.2x id", 0x12, 0x03, 0xce, 0xff);
1033 DOTEST(1024, "/tmp/testbound_123abcd.tmp", 26, "/tmp/testbound_%u%s%s.tmp", 123, "ab", "cd");
1037 #endif /* SNPRINTF_TEST */