Formatting bug.
[fw/pdclib] / functions / _PDCLIB / print.c
1 /* $Id$ */
2
3 /* _PDCLIB_print( const char *, struct _PDCLIB_status_t * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <stddef.h>
14
15 /* Using an integer's bits as flags for both the conversion flags and length
16    modifiers.
17 */
18 /* FIXME: one too many flags to work on a 16-bit machine, join some (e.g. the
19           width flags) into a combined field.
20 */
21 #define E_minus    1<<0
22 #define E_plus     1<<1
23 #define E_alt      1<<2
24 #define E_space    1<<3
25 #define E_zero     1<<4
26 #define E_done     1<<5
27 #define E_char     1<<6
28 #define E_short    1<<7
29 #define E_long     1<<8
30 #define E_llong    1<<9
31 #define E_intmax   1<<10
32 #define E_size     1<<11
33 #define E_ptrdiff  1<<12
34 #define E_intptr   1<<13
35 #define E_ldouble  1<<14
36 #define E_lower    1<<15
37 #define E_unsigned 1<<16
38
39 /* This macro delivers a given character to either a memory buffer or a stream,
40    depending on the contents of 'status' (struct _PDCLIB_status_t).
41    x - the character to be delivered
42    i - pointer to number of characters already delivered in this call
43    n - pointer to maximum number of characters to be delivered in this call
44    s - the buffer into which the character shall be delivered
45    TODO: ref. fputs() for a better way to buffer handling
46 */
47 #define DELIVER( x ) \
48 do { \
49     if ( status->i < status->n ) { \
50         if ( status->stream != NULL ) \
51             putc( x, status->stream ); \
52         else \
53             status->s[status->i] = x; \
54     } \
55     ++(status->i); \
56 } while ( 0 )
57
58 /* This function recursively converts a given integer value to a character
59    stream. The conversion is done under the control of a given status struct
60    and written either to a character string or a stream, depending on that
61    same status struct. The status struct also keeps the function from exceeding
62    snprintf() limits, and enables any necessary padding / prefixing of the
63    output once the number of characters to be printed is known, which happens
64    at the lowermost recursion level.
65 */
66 static void int2base( intmax_t value, struct _PDCLIB_status_t * status )
67 {
68     /* Registering the character being printed at the end of the function here
69        already so it will be taken into account when the deepestmost recursion
70        does the prefix / padding stuff.
71     */
72     ++(status->current);
73     if ( ( value / status->base ) != 0 )
74     {
75         /* More digits to be done - recurse deeper */
76         int2base( value / status->base, status );
77     }
78     else
79     {
80         /* We reached the last digit, the deepest point of our recursion, and
81            only now know how long the number to be printed actually is. Now we
82            have to do the sign, prefix, width, and precision padding stuff
83            before printing the numbers while we resurface from the recursion.
84         */
85         /* At worst, we need two prefix characters (hex prefix). */
86         char preface[3] = "\0";
87         size_t preidx = 0;
88         if ( ( status->flags & E_alt ) && ( status->base == 16 || status->base == 8 ) )
89         {
90             /* Octal / hexadecimal prefix for "%#" conversions */
91             preface[ preidx++ ] = '0';
92             if ( status->base == 16 )
93             {
94                 preface[ preidx++ ] = ( status->flags & E_lower ) ? 'x' : 'X';
95             }
96         }
97         if ( value < 0 )
98         {
99             /* Negative sign for negative values - at all times. */
100             preface[ preidx++ ] = '-';
101         }
102         else if ( ! ( status->flags & E_unsigned ) )
103         {
104             /* plus sign / extra space are only for unsigned conversions */
105             if ( status->flags & E_plus )
106             {
107                 preface[ preidx++ ] = '+';
108             }
109             else if ( status->flags & E_space )
110             {
111                 preface[ preidx++ ] = ' ';
112             }
113         }
114         {
115         size_t prec_pads = ( status->prec > status->current ) ? ( status->prec - status->current ) : 0;
116         if ( ! ( status->flags & ( E_minus | E_zero ) ) )
117         {
118             /* Space padding is only done if no zero padding or left alignment
119                is requested. Leave space for any prefixes determined above.
120             */
121             /* The number of characters to be printed, plus prefixes if any. */
122             /* This line contained probably the most stupid, time-wasting bug
123                I've ever perpetrated. Greetings to Samface, DevL, and all
124                sceners at Breakpoint 2006.
125             */
126             size_t characters = preidx + ( ( status->current > status->prec ) ? status->current : status->prec );
127             if ( status->width > characters )
128             {
129                 for ( size_t i = 0; i < status->width - characters; ++i )
130                 {
131                     DELIVER( ' ' );
132                     /*
133                     do
134                     {
135                         if ( status->i < status->n )
136                         {
137                             if ( status->stream != 0 )
138                                 do
139                                 {
140                                     status->stream->buffer[status->stream->bufidx++] = (char)' ',
141                                     if ( ( status->stream->bufidx == status->stream->bufsize )
142                                       || ( ( status->stream->status & 2 ) && (char)' ' == '\n' )
143                                       || ( status->stream->status & 4 ) )
144                                         fflush( status->stream )
145                                 } while (0),
146                                 ' ';
147                             else status->s[status->i] = ' ';
148                         }
149                         ++(status->i);
150                     } while ( 0 );
151                     */
152                     ++(status->current);
153                 }
154             }
155         }
156         /* Now we did the padding, do the prefixes (if any). */
157         preidx = 0;
158         while ( preface[ preidx ] != '\0' )
159         {
160             DELIVER( preface[ preidx++ ] );
161             ++(status->current);
162         }
163         if ( ( ! ( status->flags & E_minus ) ) && ( status->flags & E_zero ) )
164         {
165             /* If field is not left aligned, and zero padding is requested, do
166                so.
167             */
168             while ( status->current < status->width )
169             {
170                 DELIVER( '0' );
171                 ++(status->current);
172             }
173         }
174         /* Do the precision padding if necessary. */
175         for ( size_t i = 0; i < prec_pads; ++i )
176         {
177             DELIVER( '0' );
178         }
179         }
180     }
181     /* Recursion tail - print the current digit. */
182     {
183     int digit = value % status->base;
184     if ( digit < 0 )
185     {
186         digit *= -1;
187     }
188     if ( status->flags & E_lower )
189     {
190         /* Lowercase letters. Same array used for strto...(). */
191         DELIVER( _PDCLIB_digits[ digit ] );
192     }
193     else
194     {
195         /* Uppercase letters. Array only used here, only 0-F. */
196         DELIVER( _PDCLIB_Xdigits[ digit ] );
197     }
198     }
199 }
200
201 const char * _PDCLIB_print( const char * spec, struct _PDCLIB_status_t * status )
202 {
203     const char * orig_spec = spec;
204     if ( *(++spec) == '%' )
205     {
206         /* %% -> print single '%' */
207         DELIVER( *spec );
208         return ++spec;
209     }
210     /* Initializing status structure */
211     status->flags = 0;
212     status->base  = 0;
213     status->current  = 0;
214     status->width = 0;
215     status->prec  = 0;
216
217     /* First come 0..n flags */
218     do
219     {
220         switch ( *spec )
221         {
222             case '-':
223                 /* left-aligned output */
224                 status->flags |= E_minus;
225                 ++spec;
226                 break;
227             case '+':
228                 /* positive numbers prefixed with '+' */
229                 status->flags |= E_plus;
230                 ++spec;
231                 break;
232             case '#':
233                 /* alternative format (leading 0x for hex, 0 for octal) */
234                 status->flags |= E_alt;
235                 ++spec;
236                 break;
237             case ' ':
238                 /* positive numbers prefixed with ' ' */
239                 status->flags |= E_space;
240                 ++spec;
241                 break;
242             case '0':
243                 /* right-aligned padding done with '0' instead of ' ' */
244                 status->flags |= E_zero;
245                 ++spec;
246                 break;
247             default:
248                 /* not a flag, exit flag parsing */
249                 status->flags |= E_done;
250                 break;
251         }
252     } while ( ! ( status->flags & E_done ) );
253
254     /* Optional field width */
255     if ( *spec == '*' )
256     {
257         /* Retrieve width value from argument stack */
258         int width = va_arg( status->arg, int );
259         if ( width < 0 )
260         {
261             status->flags |= E_minus;
262             status->width = abs( width );
263         }
264         else
265         {
266             status->width = width;
267         }
268         ++spec;
269     }
270     else
271     {
272         /* If a width is given, strtol() will return its value. If not given,
273            strtol() will return zero. In both cases, endptr will point to the
274            rest of the conversion specifier - just what we need.
275         */
276         status->width = (int)strtol( spec, (char**)&spec, 10 );
277     }
278
279     /* Optional precision */
280     if ( *spec == '.' )
281     {
282         ++spec;
283         if ( *spec == '*' )
284         {
285             /* Retrieve precision value from argument stack. A negative value
286                is as if no precision is given - as precision is initalized to
287                EOF (negative), there is no need for testing for negative here.
288             */
289             status->prec = va_arg( status->arg, int );
290         }
291         else
292         {
293             char * endptr;
294             status->prec = (int)strtol( spec, &endptr, 10 );
295             if ( spec == endptr )
296             {
297                 /* Decimal point but no number - bad conversion specifier. */
298                 return orig_spec;
299             }
300             spec = endptr;
301         }
302         /* Having a precision cancels out any zero flag. */
303         status->flags ^= E_zero;
304     }
305
306     /* Optional length modifier
307        We step one character ahead in any case, and step back only if we find
308        there has been no length modifier (or step ahead another character if it
309        has been "hh" or "ll").
310     */
311     switch ( *(spec++) )
312     {
313         case 'h':
314             if ( *spec == 'h' )
315             {
316                 /* hh -> char */
317                 status->flags |= E_char;
318                 ++spec;
319             }
320             else
321             {
322                 /* h -> short */
323                 status->flags |= E_short;
324             }
325             break;
326         case 'l':
327             if ( *spec == 'l' )
328             {
329                 /* ll -> long long */
330                 status->flags |= E_llong;
331                 ++spec;
332             }
333             else
334             {
335                 /* k -> long */
336                 status->flags |= E_long;
337             }
338             break;
339         case 'j':
340             /* j -> intmax_t, which might or might not be long long */
341             status->flags |= E_intmax;
342             break;
343         case 'z':
344             /* z -> size_t, which might or might not be unsigned int */
345             status->flags |= E_size;
346             break;
347         case 't':
348             /* t -> ptrdiff_t, which might or might not be long */
349             status->flags |= E_ptrdiff;
350             break;
351         case 'L':
352             /* L -> long double */
353             status->flags |= E_ldouble;
354             break;
355         default:
356             --spec;
357             break;
358     }
359
360     /* Conversion specifier */
361     switch ( *spec )
362     {
363         case 'd':
364             /* FALLTHROUGH */
365         case 'i':
366             status->base = 10;
367             break;
368         case 'o':
369             status->base = 8;
370             status->flags |= E_unsigned;
371             break;
372         case 'u':
373             status->base = 10;
374             status->flags |= E_unsigned;
375             break;
376         case 'x':
377             status->base = 16;
378             status->flags |= ( E_lower | E_unsigned );
379             break;
380         case 'X':
381             status->base = 16;
382             status->flags |= E_unsigned;
383             break;
384         case 'f':
385         case 'F':
386         case 'e':
387         case 'E':
388         case 'g':
389         case 'G':
390             break;
391         case 'a':
392         case 'A':
393             break;
394         case 'c':
395             /* TODO: Flags, wide chars. */
396             DELIVER( va_arg( status->arg, int ) );
397             return ++spec;
398         case 's':
399             /* TODO: Flags, wide chars. */
400             {
401                 char * s = va_arg( status->arg, char * );
402                 while ( *s != '\0' )
403                 {
404                     DELIVER( *(s++) );
405                 }
406                 return ++spec;
407             }
408         case 'p':
409             /* TODO: E_long -> E_intptr */
410             status->base = 16;
411             status->flags |= ( E_lower | E_unsigned | E_alt | E_long );
412             break;
413         case 'n':
414            {
415                int * val = va_arg( status->arg, int * );
416                *val = status->i;
417                return ++spec;
418            }
419         default:
420             /* No conversion specifier. Bad conversion. */
421             return orig_spec;
422     }
423
424     /* Do the actual output based on our findings */
425     if ( status->base != 0 )
426     {
427         /* Integer conversions */
428         /* TODO: Check for invalid flag combinations. */
429         if ( status->flags & E_unsigned )
430         {
431             uintmax_t value;
432             switch ( status->flags & ( E_char | E_short | E_long | E_llong | E_size ) )
433             {
434                 case E_char:
435                     value = (uintmax_t)(unsigned char)va_arg( status->arg, int );
436                     break;
437                 case E_short:
438                     value = (uintmax_t)(unsigned short)va_arg( status->arg, int );
439                     break;
440                 case 0:
441                     value = (uintmax_t)va_arg( status->arg, unsigned int );
442                     break;
443                 case E_long:
444                     value = (uintmax_t)va_arg( status->arg, unsigned long );
445                     break;
446                 case E_llong:
447                     value = (uintmax_t)va_arg( status->arg, unsigned long long );
448                     break;
449                 case E_size:
450                     value = (uintmax_t)va_arg( status->arg, size_t );
451                     break;
452             }
453             ++(status->current);
454             /* FIXME: The if clause means one-digit values do not get formatted */
455             if ( ( value / status->base ) != 0 )
456             {
457                 int2base( (intmax_t)(value / status->base), status );
458             }
459             int digit = value % status->base;
460             if ( digit < 0 )
461             {
462                 digit *= -1;
463             }
464             if ( status->flags & E_lower )
465             {
466                 DELIVER( _PDCLIB_digits[ digit ] );
467             }
468             else
469             {
470                 DELIVER( _PDCLIB_Xdigits[ digit ] );
471             }
472         }
473         else
474         {
475             switch ( status->flags & ( E_char | E_short | E_long | E_llong | E_intmax ) )
476             {
477                 case E_char:
478                     int2base( (intmax_t)(char)va_arg( status->arg, int ), status );
479                     break;
480                 case E_short:
481                     int2base( (intmax_t)(short)va_arg( status->arg, int ), status );
482                     break;
483                 case 0:
484                     int2base( (intmax_t)va_arg( status->arg, int ), status );
485                     break;
486                 case E_long:
487                     int2base( (intmax_t)va_arg( status->arg, long ), status );
488                     break;
489                 case E_llong:
490                     int2base( (intmax_t)va_arg( status->arg, long long ), status );
491                     break;
492                 case E_ptrdiff:
493                     int2base( (intmax_t)va_arg( status->arg, ptrdiff_t ), status );
494                     break;
495                 case E_intmax:
496                     int2base( va_arg( status->arg, intmax_t ), status );
497                     break;
498             }
499         }
500         if ( status->flags & E_minus )
501         {
502             while ( status->current < status->width )
503             {
504                 DELIVER( ' ' );
505                 ++(status->current);
506             }
507         }
508         if ( status->i >= status->n )
509         {
510             status->s[status->n - 1] = '\0';
511         }
512     }
513     return ++spec;
514 }
515
516 #ifdef TEST
517 #include <_PDCLIB_test.h>
518
519 #include <limits.h>
520 #include <string.h>
521
522 static int testprintf( char * buffer, size_t n, const char * format, ... )
523 {
524     /* Members: base, flags, n, i, current, s, width, prec, stream, arg      */
525     struct _PDCLIB_status_t status;
526     status.base = 0;
527     status.flags = 0;
528     status.n = n;
529     status.i = 0;
530     status.current = 0;
531     status.s = buffer;
532     status.width = 0;
533     status.prec = 0;
534     status.stream = NULL;
535     va_start( status.arg, format );
536     memset( buffer, '\0', 100 );
537     if ( *(_PDCLIB_print( format, &status )) != '\0' )
538     {
539         printf( "_PDCLIB_print() did not return end-of-specifier on '%s'.\n", format );
540         ++TEST_RESULTS;
541     }
542     va_end( status.arg );
543     return status.i;
544 }
545
546 #define TEST_CONVERSION_ONLY
547
548 #define TESTCASE_SPRINTF( x ) TESTCASE( x )
549
550 int main( void )
551 {
552     char buffer[100];
553 #include "printf_testcases.incl"
554
555 #if 0
556     char buffer[100];
557     TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
558     TESTCASE( strcmp( buffer, "-128" ) == 0 );
559     TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
560     TESTCASE( strcmp( buffer, "127" ) == 0 );
561     TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
562     TESTCASE( strcmp( buffer, "0" ) == 0 );
563     TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
564     TESTCASE( strcmp( buffer, "-32768" ) == 0 );
565     TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
566     TESTCASE( strcmp( buffer, "32767" ) == 0 );
567     TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
568     TESTCASE( strcmp( buffer, "0" ) == 0 );
569     TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
570     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
571     TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
572     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
573     TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
574     TESTCASE( strcmp( buffer, "0" ) == 0 );
575     TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
576     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
577     TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
578     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
579     TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
580     TESTCASE( strcmp( buffer, "0" ) == 0 );
581     TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
582     TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
583     TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
584     TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
585     TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
586     TESTCASE( strcmp( buffer, "0" ) == 0 );
587     TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
588     TESTCASE( strcmp( buffer, "255" ) == 0 );
589     TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
590     TESTCASE( strcmp( buffer, "255" ) == 0 );
591     TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
592     TESTCASE( strcmp( buffer, "65535" ) == 0 );
593     TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
594     TESTCASE( strcmp( buffer, "65535" ) == 0 );
595     TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
596     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
597     TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
598     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
599     TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
600     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
601     TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
602     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
603     TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
604     TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
605     TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
606     TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
607     TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
608     TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
609     TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
610     TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
611     TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
612     TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
613     TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
614     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
615     TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
616     TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
617     TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
618     TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
619     TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
620     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
621     TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
622     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
623     TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
624     TESTCASE( strcmp( buffer, "+0" ) == 0 );
625     TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
626     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
627     TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
628     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
629     TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
630     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
631     TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
632     TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
633     TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
634     TESTCASE( strcmp( buffer, " 0" ) == 0 );
635     TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
636     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
637     TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
638     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
639     TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
640     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
641     TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
642     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
643     TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
644     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
645     TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
646     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
647     TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
648     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
649     TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
650     TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
651     TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
652     TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
653     TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
654     TESTCASE( strcmp( buffer, "  2147483647" ) == 0 );
655     TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
656     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
657     TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
658     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
659     TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
660     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
661     TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
662     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
663     TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
664     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
665     TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
666     TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
667     TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
668     TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
669     TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
670     TESTCASE( strcmp( buffer, "2147483647  " ) == 0 );
671     TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
672     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
673     TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
674     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
675     TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
676     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
677     TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
678     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
679     TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
680     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
681     TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
682     TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
683     TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
684     TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
685     TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
686     TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
687     TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
688     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
689     TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
690     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
691     TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
692     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
693     TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
694     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
695     TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
696     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
697     TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
698     TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
699     TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
700     TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
701     TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
702     TESTCASE( strcmp( buffer, "2147483647  " ) == 0 );
703     TESTCASE( testprintf( buffer, 8, "%9d", INT_MAX ) == 10 );
704     TESTCASE( strcmp( buffer, "2147483" ) == 0 );
705     TESTCASE( testprintf( buffer, 8, "%9d", INT_MIN ) == 11 );
706     TESTCASE( strcmp( buffer, "-214748" ) == 0 );
707     TESTCASE( testprintf( buffer, 9, "%9d", INT_MAX ) == 10 );
708     TESTCASE( strcmp( buffer, "21474836" ) == 0 );
709     TESTCASE( testprintf( buffer, 9, "%9d", INT_MIN ) == 11 );
710     TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
711     TESTCASE( testprintf( buffer, 10, "%9d", INT_MAX ) == 10 );
712     TESTCASE( strcmp( buffer, "214748364" ) == 0 );
713     TESTCASE( testprintf( buffer, 10, "%9d", INT_MIN ) == 11 );
714     TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
715     TESTCASE( testprintf( buffer, 9, "%10d", INT_MAX ) == 10 );
716     TESTCASE( strcmp( buffer, "21474836" ) == 0 );
717     TESTCASE( testprintf( buffer, 9, "%10d", INT_MIN ) == 11 );
718     TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
719     TESTCASE( testprintf( buffer, 10, "%10d", INT_MAX ) == 10 );
720     TESTCASE( strcmp( buffer, "214748364" ) == 0 );
721     TESTCASE( testprintf( buffer, 10, "%10d", INT_MIN ) == 11 );
722     TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
723     TESTCASE( testprintf( buffer, 11, "%10d", INT_MAX ) == 10 );
724     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
725     TESTCASE( testprintf( buffer, 11, "%10d", INT_MIN ) == 11 );
726     TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
727     TESTCASE( testprintf( buffer, 10, "%11d", INT_MAX ) == 11 );
728     TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
729     TESTCASE( testprintf( buffer, 10, "%11d", INT_MIN ) == 11 );
730     TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
731     TESTCASE( testprintf( buffer, 11, "%11d", INT_MAX ) == 11 );
732     TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
733     TESTCASE( testprintf( buffer, 11, "%11d", INT_MIN ) == 11 );
734     TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
735     TESTCASE( testprintf( buffer, 12, "%11d", INT_MAX ) == 11 );
736     TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
737     TESTCASE( testprintf( buffer, 12, "%11d", INT_MIN ) == 11 );
738     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
739     TESTCASE( testprintf( buffer, 11, "%12d", INT_MAX ) == 12 );
740     TESTCASE( strcmp( buffer, "  21474836" ) == 0 );
741     TESTCASE( testprintf( buffer, 11, "%12d", INT_MIN ) == 12 );
742     TESTCASE( strcmp( buffer, " -21474836" ) == 0 );
743     TESTCASE( testprintf( buffer, 12, "%12d", INT_MAX ) == 12 );
744     TESTCASE( strcmp( buffer, "  214748364" ) == 0 );
745     TESTCASE( testprintf( buffer, 12, "%12d", INT_MIN ) == 12 );
746     TESTCASE( strcmp( buffer, " -214748364" ) == 0 );
747     TESTCASE( testprintf( buffer, 13, "%12d", INT_MAX ) == 12 );
748     TESTCASE( strcmp( buffer, "  2147483647" ) == 0 );
749     TESTCASE( testprintf( buffer, 13, "%12d", INT_MIN ) == 12 );
750     TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
751     TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
752     TESTCASE( strcmp( buffer, "          00000000002147483647" ) == 0 );
753     TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
754     TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
755     TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
756     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
757     TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
758     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
759     TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
760     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
761     TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
762     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
763     TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
764     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
765     TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
766     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
767     TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
768     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
769     TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
770     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
771     TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
772     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
773     TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
774     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
775     TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
776     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
777     TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
778     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
779     TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
780     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
781     TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
782     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
783     TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
784     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
785     TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
786     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
787     TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
788     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
789     TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
790     TESTCASE( strcmp( buffer, "x" ) == 0 );
791     TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
792     TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
793     TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
794     TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
795 #endif
796     return TEST_RESULTS;
797 }
798
799 #endif