]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/print.c
Minor cleanups.
[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 #define E_minus    1<<0
19 #define E_plus     1<<1
20 #define E_alt      1<<2
21 #define E_space    1<<3
22 #define E_zero     1<<4
23 #define E_done     1<<5
24 #define E_char     1<<6
25 #define E_short    1<<7
26 #define E_long     1<<8
27 #define E_llong    1<<9
28 #define E_intmax   1<<10
29 #define E_size     1<<11
30 #define E_ptrdiff  1<<12
31 #define E_intptr   1<<13
32 #define E_ldouble  1<<14
33 #define E_lower    1<<15
34 #define E_unsigned 1<<16
35
36 /* This macro delivers a given character to either a memory buffer or a stream,
37    depending on the contents of 'status' (struct _PDCLIB_status_t).
38    x - the character to be delivered
39    i - pointer to number of characters already delivered in this call
40    n - pointer to maximum number of characters to be delivered in this call
41    s - the buffer into which the character shall be delivered
42 */
43 #define DELIVER( x ) \
44 do { \
45     if ( status->i < status->n ) { \
46         if ( status->stream != NULL ) { \
47             status->stream->buffer[status->stream->bufidx++] = x; \
48             if ( ( status->stream->bufidx == status->stream->bufsize ) \
49               || ( ( status->stream->status & _IOLBF ) && ( x == '\n' ) ) \
50               || ( status->stream->status & _IONBF ) ) \
51                 fflush( 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->this);
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->this ) ? ( status->prec - status->this ) : 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->this > status->prec ) ? status->this : status->prec );
127             if ( status->width > characters )
128             {
129                 for ( int 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->this);
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->this);
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->this < status->width )
169             {
170                 DELIVER( '0' );
171                 ++(status->this);
172             }
173         }
174         /* Do the precision padding if necessary. */
175         for ( int 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->this  = 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         if ( ( status->width = va_arg( status->arg, int ) ) < 0 )
259         {
260             /* Negative value is '-' flag plus absolute value */
261             status->flags |= E_minus;
262             status->width *= -1;
263         }
264         ++spec;
265     }
266     else
267     {
268         /* If a width is given, strtol() will return its value. If not given,
269            strtol() will return zero. In both cases, endptr will point to the
270            rest of the conversion specifier - just what we need.
271         */
272         status->width = (int)strtol( spec, (char**)&spec, 10 );
273     }
274
275     /* Optional precision */
276     if ( *spec == '.' )
277     {
278         ++spec;
279         if ( *spec == '*' )
280         {
281             /* Retrieve precision value from argument stack. A negative value
282                is as if no precision is given - as precision is initalized to
283                EOF (negative), there is no need for testing for negative here.
284             */
285             status->prec = va_arg( status->arg, int );
286         }
287         else
288         {
289             char * endptr;
290             status->prec = (int)strtol( spec, &endptr, 10 );
291             if ( spec == endptr )
292             {
293                 /* Decimal point but no number - bad conversion specifier. */
294                 return orig_spec;
295             }
296             spec = endptr;
297         }
298         /* Having a precision cancels out any zero flag. */
299         status->flags ^= E_zero;
300     }
301
302     /* Optional length modifier
303        We step one character ahead in any case, and step back only if we find
304        there has been no length modifier (or step ahead another character if it
305        has been "hh" or "ll").
306     */
307     switch ( *(spec++) )
308     {
309         case 'h':
310             if ( *spec == 'h' )
311             {
312                 /* hh -> char */
313                 status->flags |= E_char;
314                 ++spec;
315             }
316             else
317             {
318                 /* h -> short */
319                 status->flags |= E_short;
320             }
321             break;
322         case 'l':
323             if ( *spec == 'l' )
324             {
325                 /* ll -> long long */
326                 status->flags |= E_llong;
327                 ++spec;
328             }
329             else
330             {
331                 /* k -> long */
332                 status->flags |= E_long;
333             }
334             break;
335         case 'j':
336             /* j -> intmax_t, which might or might not be long long */
337             status->flags |= E_intmax;
338             break;
339         case 'z':
340             /* z -> size_t, which might or might not be unsigned int */
341             status->flags |= E_size;
342             break;
343         case 't':
344             /* t -> ptrdiff_t, which might or might not be long */
345             status->flags |= E_ptrdiff;
346             break;
347         case 'L':
348             /* L -> long double */
349             status->flags |= E_ldouble;
350             break;
351         default:
352             --spec;
353             break;
354     }
355
356     /* Conversion specifier */
357     switch ( *spec )
358     {
359         case 'd':
360             /* FALLTHROUGH */
361         case 'i':
362             status->base = 10;
363             break;
364         case 'o':
365             status->base = 8;
366             status->flags |= E_unsigned;
367             break;
368         case 'u':
369             status->base = 10;
370             status->flags |= E_unsigned;
371             break;
372         case 'x':
373             status->base = 16;
374             status->flags |= ( E_lower | E_unsigned );
375             break;
376         case 'X':
377             status->base = 16;
378             status->flags |= E_unsigned;
379             break;
380         case 'f':
381         case 'F':
382         case 'e':
383         case 'E':
384         case 'g':
385         case 'G':
386             break;
387         case 'a':
388         case 'A':
389             break;
390         case 'c':
391             /* TODO: Flags, wide chars. */
392             DELIVER( va_arg( status->arg, int ) );
393             return ++spec;
394         case 's':
395             /* TODO: Flags, wide chars. */
396             {
397                 char * s = va_arg( status->arg, char * );
398                 while ( *s != '\0' )
399                 {
400                     DELIVER( *(s++) );
401                 }
402                 return ++spec;
403             }
404         case 'p':
405             /* TODO: E_long -> E_intptr */
406             status->base = 16;
407             status->flags |= ( E_lower | E_unsigned | E_alt | E_long );
408             break;
409         case 'n':
410            {
411                int * val = va_arg( status->arg, int * );
412                *val = status->i;
413                return ++spec;
414            }
415         default:
416             /* No conversion specifier. Bad conversion. */
417             return orig_spec;
418     }
419
420     /* Do the actual output based on our findings */
421     if ( status->base != 0 )
422     {
423         /* Integer conversions */
424         /* TODO: Check for invalid flag combinations. */
425         if ( status->flags & E_unsigned )
426         {
427             uintmax_t value;
428             switch ( status->flags & ( E_char | E_short | E_long | E_llong | E_size ) )
429             {
430                 case E_char:
431                     value = (uintmax_t)(unsigned char)va_arg( status->arg, int );
432                     break;
433                 case E_short:
434                     value = (uintmax_t)(unsigned short)va_arg( status->arg, int );
435                     break;
436                 case 0:
437                     value = (uintmax_t)va_arg( status->arg, unsigned int );
438                     break;
439                 case E_long:
440                     value = (uintmax_t)va_arg( status->arg, unsigned long );
441                     break;
442                 case E_llong:
443                     value = (uintmax_t)va_arg( status->arg, unsigned long long );
444                     break;
445                 case E_size:
446                     value = (uintmax_t)va_arg( status->arg, size_t );
447                     break;
448             }
449             ++(status->this);
450             if ( ( value / status->base ) != 0 )
451             {
452                 int2base( (intmax_t)(value / status->base), status );
453             }
454             int digit = value % status->base;
455             if ( digit < 0 )
456             {
457                 digit *= -1;
458             }
459             if ( status->flags & E_lower )
460             {
461                 DELIVER( _PDCLIB_digits[ digit ] );
462             }
463             else
464             {
465                 DELIVER( _PDCLIB_Xdigits[ digit ] );
466             }
467         }
468         else
469         {
470             switch ( status->flags & ( E_char | E_short | E_long | E_llong | E_intmax ) )
471             {
472                 case E_char:
473                     int2base( (intmax_t)(char)va_arg( status->arg, int ), status );
474                     break;
475                 case E_short:
476                     int2base( (intmax_t)(short)va_arg( status->arg, int ), status );
477                     break;
478                 case 0:
479                     int2base( (intmax_t)va_arg( status->arg, int ), status );
480                     break;
481                 case E_long:
482                     int2base( (intmax_t)va_arg( status->arg, long ), status );
483                     break;
484                 case E_llong:
485                     int2base( (intmax_t)va_arg( status->arg, long long ), status );
486                     break;
487                 case E_ptrdiff:
488                     int2base( (intmax_t)va_arg( status->arg, ptrdiff_t ), status );
489                     break;
490                 case E_intmax:
491                     int2base( va_arg( status->arg, intmax_t ), status );
492                     break;
493             }
494         }
495         if ( status->flags & E_minus )
496         {
497             while ( status->this < status->width )
498             {
499                 DELIVER( ' ' );
500                 ++(status->this);
501             }
502         }
503         if ( status->i >= status->n )
504         {
505             status->s[status->n - 1] = '\0';
506         }
507     }
508     return ++spec;
509 }
510
511 #ifdef TEST
512 #include <_PDCLIB_test.h>
513
514 #include <limits.h>
515 #include <string.h>
516
517 static int testprintf( char * buffer, size_t n, const char * format, ... )
518 {
519     /* Members: base, flags, n, i, this, s, width, prec, stream, arg         */
520     struct _PDCLIB_status_t status = { 0, 0, n, 0, 0, buffer, 0, 0, NULL, NULL };
521     memset( buffer, '\0', 100 );
522     va_start( status.arg, format );
523     if ( *(_PDCLIB_print( format, &status )) != '\0' )
524     {
525         printf( "_PDCLIB_print() did not return end-of-specifier on '%s'.\n", format );
526         ++rc;
527     }
528     va_end( status.arg );
529     return status.i;
530 }
531
532 int main( void )
533 {
534     char buffer[100];
535     TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MIN ) == 4 );
536     TESTCASE( strcmp( buffer, "-128" ) == 0 );
537     TESTCASE( testprintf( buffer, 100, "%hhd", CHAR_MAX ) == 3 );
538     TESTCASE( strcmp( buffer, "127" ) == 0 );
539     TESTCASE( testprintf( buffer, 100, "%hhd", 0 ) == 1 );
540     TESTCASE( strcmp( buffer, "0" ) == 0 );
541     TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MIN ) == 6 );
542     TESTCASE( strcmp( buffer, "-32768" ) == 0 );
543     TESTCASE( testprintf( buffer, 100, "%hd", SHRT_MAX ) == 5 );
544     TESTCASE( strcmp( buffer, "32767" ) == 0 );
545     TESTCASE( testprintf( buffer, 100, "%hd", 0 ) == 1 );
546     TESTCASE( strcmp( buffer, "0" ) == 0 );
547     TESTCASE( testprintf( buffer, 100, "%d", INT_MIN ) == 11 );
548     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
549     TESTCASE( testprintf( buffer, 100, "%d", INT_MAX ) == 10 );
550     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
551     TESTCASE( testprintf( buffer, 100, "%d", 0 ) == 1 );
552     TESTCASE( strcmp( buffer, "0" ) == 0 );
553     TESTCASE( testprintf( buffer, 100, "%ld", LONG_MIN ) == 11 );
554     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
555     TESTCASE( testprintf( buffer, 100, "%ld", LONG_MAX ) == 10 );
556     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
557     TESTCASE( testprintf( buffer, 100, "%ld", 0l ) == 1 );
558     TESTCASE( strcmp( buffer, "0" ) == 0 );
559     TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MIN ) == 20 );
560     TESTCASE( strcmp( buffer, "-9223372036854775808" ) == 0 );
561     TESTCASE( testprintf( buffer, 100, "%lld", LLONG_MAX ) == 19 );
562     TESTCASE( strcmp( buffer, "9223372036854775807" ) == 0 );
563     TESTCASE( testprintf( buffer, 100, "%lld", 0ll ) );
564     TESTCASE( strcmp( buffer, "0" ) == 0 );
565     TESTCASE( testprintf( buffer, 100, "%hhu", UCHAR_MAX ) == 3 );
566     TESTCASE( strcmp( buffer, "255" ) == 0 );
567     TESTCASE( testprintf( buffer, 100, "%hhu", (unsigned char)-1 ) == 3 );
568     TESTCASE( strcmp( buffer, "255" ) == 0 );
569     TESTCASE( testprintf( buffer, 100, "%hu", USHRT_MAX ) == 5 );
570     TESTCASE( strcmp( buffer, "65535" ) == 0 );
571     TESTCASE( testprintf( buffer, 100, "%hu", (unsigned short)-1 ) == 5 );
572     TESTCASE( strcmp( buffer, "65535" ) == 0 );
573     TESTCASE( testprintf( buffer, 100, "%u", UINT_MAX ) == 10 );
574     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
575     TESTCASE( testprintf( buffer, 100, "%u", -1u ) == 10 );
576     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
577     TESTCASE( testprintf( buffer, 100, "%lu", ULONG_MAX ) == 10 );
578     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
579     TESTCASE( testprintf( buffer, 100, "%lu", -1ul ) == 10 );
580     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
581     TESTCASE( testprintf( buffer, 100, "%llu", ULLONG_MAX ) == 20 );
582     TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
583     TESTCASE( testprintf( buffer, 100, "%llu", -1ull ) == 20 );
584     TESTCASE( strcmp( buffer, "18446744073709551615" ) == 0 );
585     TESTCASE( testprintf( buffer, 100, "%X", UINT_MAX ) == 8 );
586     TESTCASE( strcmp( buffer, "FFFFFFFF" ) == 0 );
587     TESTCASE( testprintf( buffer, 100, "%#X", -1u ) == 10 );
588     TESTCASE( strcmp( buffer, "0XFFFFFFFF" ) == 0 );
589     TESTCASE( testprintf( buffer, 100, "%x", UINT_MAX ) == 8 );
590     TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
591     TESTCASE( testprintf( buffer, 100, "%#x", -1u ) == 10 );
592     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
593     TESTCASE( testprintf( buffer, 100, "%o", UINT_MAX ) == 11 );
594     TESTCASE( strcmp( buffer, "37777777777" ) == 0 );
595     TESTCASE( testprintf( buffer, 100, "%#o", -1u ) == 12 );
596     TESTCASE( strcmp( buffer, "037777777777" ) == 0 );
597     TESTCASE( testprintf( buffer, 100, "%+d", INT_MIN ) == 11 );
598     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
599     TESTCASE( testprintf( buffer, 100, "%+d", INT_MAX ) == 11 );
600     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
601     TESTCASE( testprintf( buffer, 100, "%+d", 0 ) == 2 );
602     TESTCASE( strcmp( buffer, "+0" ) == 0 );
603     TESTCASE( testprintf( buffer, 100, "%+u", UINT_MAX ) == 10 );
604     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
605     TESTCASE( testprintf( buffer, 100, "%+u", -1u ) == 10 );
606     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
607     TESTCASE( testprintf( buffer, 100, "% d", INT_MIN ) == 11 );
608     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
609     TESTCASE( testprintf( buffer, 100, "% d", INT_MAX ) == 11 );
610     TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
611     TESTCASE( testprintf( buffer, 100, "% d", 0 ) == 2 );
612     TESTCASE( strcmp( buffer, " 0" ) == 0 );
613     TESTCASE( testprintf( buffer, 100, "% u", UINT_MAX ) == 10 );
614     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
615     TESTCASE( testprintf( buffer, 100, "% u", -1u ) == 10 );
616     TESTCASE( strcmp( buffer, "4294967295" ) == 0 );
617     TESTCASE( testprintf( buffer, 100, "%9d", INT_MIN ) == 11 );
618     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
619     TESTCASE( testprintf( buffer, 100, "%9d", INT_MAX ) == 10 );
620     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
621     TESTCASE( testprintf( buffer, 100, "%10d", INT_MIN ) == 11 );
622     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
623     TESTCASE( testprintf( buffer, 100, "%10d", INT_MAX ) == 10 );
624     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
625     TESTCASE( testprintf( buffer, 100, "%11d", INT_MIN ) == 11 );
626     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
627     TESTCASE( testprintf( buffer, 100, "%11d", INT_MAX ) == 11 );
628     TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
629     TESTCASE( testprintf( buffer, 100, "%12d", INT_MIN ) == 12 );
630     TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
631     TESTCASE( testprintf( buffer, 100, "%12d", INT_MAX ) == 12 );
632     TESTCASE( strcmp( buffer, "  2147483647" ) == 0 );
633     TESTCASE( testprintf( buffer, 100, "%-9d", INT_MIN ) == 11 );
634     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
635     TESTCASE( testprintf( buffer, 100, "%-9d", INT_MAX ) == 10 );
636     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
637     TESTCASE( testprintf( buffer, 100, "%-10d", INT_MIN ) == 11 );
638     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
639     TESTCASE( testprintf( buffer, 100, "%-10d", INT_MAX ) == 10 );
640     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
641     TESTCASE( testprintf( buffer, 100, "%-11d", INT_MIN ) == 11 );
642     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
643     TESTCASE( testprintf( buffer, 100, "%-11d", INT_MAX ) == 11 );
644     TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
645     TESTCASE( testprintf( buffer, 100, "%-12d", INT_MIN ) == 12 );
646     TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
647     TESTCASE( testprintf( buffer, 100, "%-12d", INT_MAX ) == 12 );
648     TESTCASE( strcmp( buffer, "2147483647  " ) == 0 );
649     TESTCASE( testprintf( buffer, 100, "%09d", INT_MIN ) == 11 );
650     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
651     TESTCASE( testprintf( buffer, 100, "%09d", INT_MAX ) == 10 );
652     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
653     TESTCASE( testprintf( buffer, 100, "%010d", INT_MIN ) == 11 );
654     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
655     TESTCASE( testprintf( buffer, 100, "%010d", INT_MAX ) == 10 );
656     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
657     TESTCASE( testprintf( buffer, 100, "%011d", INT_MIN ) == 11 );
658     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
659     TESTCASE( testprintf( buffer, 100, "%011d", INT_MAX ) == 11 );
660     TESTCASE( strcmp( buffer, "02147483647" ) == 0 );
661     TESTCASE( testprintf( buffer, 100, "%012d", INT_MIN ) == 12 );
662     TESTCASE( strcmp( buffer, "-02147483648" ) == 0 );
663     TESTCASE( testprintf( buffer, 100, "%012d", INT_MAX ) == 12 );
664     TESTCASE( strcmp( buffer, "002147483647" ) == 0 );
665     TESTCASE( testprintf( buffer, 100, "%-09d", INT_MIN ) == 11 );
666     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
667     TESTCASE( testprintf( buffer, 100, "%-09d", INT_MAX ) == 10 );
668     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
669     TESTCASE( testprintf( buffer, 100, "%-010d", INT_MIN ) == 11 );
670     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
671     TESTCASE( testprintf( buffer, 100, "%-010d", INT_MAX ) == 10 );
672     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
673     TESTCASE( testprintf( buffer, 100, "%-011d", INT_MIN ) == 11 );
674     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
675     TESTCASE( testprintf( buffer, 100, "%-011d", INT_MAX ) == 11 );
676     TESTCASE( strcmp( buffer, "2147483647 " ) == 0 );
677     TESTCASE( testprintf( buffer, 100, "%-012d", INT_MIN ) == 12 );
678     TESTCASE( strcmp( buffer, "-2147483648 " ) == 0 );
679     TESTCASE( testprintf( buffer, 100, "%-012d", INT_MAX ) == 12 );
680     TESTCASE( strcmp( buffer, "2147483647  " ) == 0 );
681     TESTCASE( testprintf( buffer, 8, "%9d", INT_MAX ) == 10 );
682     TESTCASE( strcmp( buffer, "2147483" ) == 0 );
683     TESTCASE( testprintf( buffer, 8, "%9d", INT_MIN ) == 11 );
684     TESTCASE( strcmp( buffer, "-214748" ) == 0 );
685     TESTCASE( testprintf( buffer, 9, "%9d", INT_MAX ) == 10 );
686     TESTCASE( strcmp( buffer, "21474836" ) == 0 );
687     TESTCASE( testprintf( buffer, 9, "%9d", INT_MIN ) == 11 );
688     TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
689     TESTCASE( testprintf( buffer, 10, "%9d", INT_MAX ) == 10 );
690     TESTCASE( strcmp( buffer, "214748364" ) == 0 );
691     TESTCASE( testprintf( buffer, 10, "%9d", INT_MIN ) == 11 );
692     TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
693     TESTCASE( testprintf( buffer, 9, "%10d", INT_MAX ) == 10 );
694     TESTCASE( strcmp( buffer, "21474836" ) == 0 );
695     TESTCASE( testprintf( buffer, 9, "%10d", INT_MIN ) == 11 );
696     TESTCASE( strcmp( buffer, "-2147483" ) == 0 );
697     TESTCASE( testprintf( buffer, 10, "%10d", INT_MAX ) == 10 );
698     TESTCASE( strcmp( buffer, "214748364" ) == 0 );
699     TESTCASE( testprintf( buffer, 10, "%10d", INT_MIN ) == 11 );
700     TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
701     TESTCASE( testprintf( buffer, 11, "%10d", INT_MAX ) == 10 );
702     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
703     TESTCASE( testprintf( buffer, 11, "%10d", INT_MIN ) == 11 );
704     TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
705     TESTCASE( testprintf( buffer, 10, "%11d", INT_MAX ) == 11 );
706     TESTCASE( strcmp( buffer, " 21474836" ) == 0 );
707     TESTCASE( testprintf( buffer, 10, "%11d", INT_MIN ) == 11 );
708     TESTCASE( strcmp( buffer, "-21474836" ) == 0 );
709     TESTCASE( testprintf( buffer, 11, "%11d", INT_MAX ) == 11 );
710     TESTCASE( strcmp( buffer, " 214748364" ) == 0 );
711     TESTCASE( testprintf( buffer, 11, "%11d", INT_MIN ) == 11 );
712     TESTCASE( strcmp( buffer, "-214748364" ) == 0 );
713     TESTCASE( testprintf( buffer, 12, "%11d", INT_MAX ) == 11 );
714     TESTCASE( strcmp( buffer, " 2147483647" ) == 0 );
715     TESTCASE( testprintf( buffer, 12, "%11d", INT_MIN ) == 11 );
716     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
717     TESTCASE( testprintf( buffer, 11, "%12d", INT_MAX ) == 12 );
718     TESTCASE( strcmp( buffer, "  21474836" ) == 0 );
719     TESTCASE( testprintf( buffer, 11, "%12d", INT_MIN ) == 12 );
720     TESTCASE( strcmp( buffer, " -21474836" ) == 0 );
721     TESTCASE( testprintf( buffer, 12, "%12d", INT_MAX ) == 12 );
722     TESTCASE( strcmp( buffer, "  214748364" ) == 0 );
723     TESTCASE( testprintf( buffer, 12, "%12d", INT_MIN ) == 12 );
724     TESTCASE( strcmp( buffer, " -214748364" ) == 0 );
725     TESTCASE( testprintf( buffer, 13, "%12d", INT_MAX ) == 12 );
726     TESTCASE( strcmp( buffer, "  2147483647" ) == 0 );
727     TESTCASE( testprintf( buffer, 13, "%12d", INT_MIN ) == 12 );
728     TESTCASE( strcmp( buffer, " -2147483648" ) == 0 );
729     TESTCASE( testprintf( buffer, 100, "%030.20d", INT_MAX ) == 30 );
730     TESTCASE( strcmp( buffer, "          00000000002147483647" ) == 0 );
731     TESTCASE( testprintf( buffer, 100, "%.6x", UINT_MAX ) == 8 );
732     TESTCASE( strcmp( buffer, "ffffffff" ) == 0 );
733     TESTCASE( testprintf( buffer, 100, "%#6.3x", UINT_MAX ) == 10 );
734     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
735     TESTCASE( testprintf( buffer, 100, "%#3.6x", UINT_MAX ) == 10 );
736     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
737     TESTCASE( testprintf( buffer, 100, "%.6d", INT_MIN ) == 11 );
738     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
739     TESTCASE( testprintf( buffer, 100, "%6.3d", INT_MIN ) == 11 );
740     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
741     TESTCASE( testprintf( buffer, 100, "%3.6d", INT_MIN ) == 11 );
742     TESTCASE( strcmp( buffer, "-2147483648" ) == 0 );
743     TESTCASE( testprintf( buffer, 100, "%#0.6x", UINT_MAX ) == 10 );
744     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
745     TESTCASE( testprintf( buffer, 100, "%#06.3x", UINT_MAX ) == 10 );
746     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
747     TESTCASE( testprintf( buffer, 100, "%#03.6x", UINT_MAX ) == 10 );
748     TESTCASE( strcmp( buffer, "0xffffffff" ) == 0 );
749     TESTCASE( testprintf( buffer, 100, "%#0.6d", INT_MAX ) == 10 );
750     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
751     TESTCASE( testprintf( buffer, 100, "%#06.3d", INT_MAX ) == 10 );
752     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
753     TESTCASE( testprintf( buffer, 100, "%#03.6d", INT_MAX ) == 10 );
754     TESTCASE( strcmp( buffer, "2147483647" ) == 0 );
755     TESTCASE( testprintf( buffer, 100, "%#+.6d", INT_MAX ) == 11 );
756     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
757     TESTCASE( testprintf( buffer, 100, "%#+6.3d", INT_MAX ) == 11 );
758     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
759     TESTCASE( testprintf( buffer, 100, "%#+3.6d", INT_MAX ) == 11 );
760     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
761     TESTCASE( testprintf( buffer, 100, "%+0.6d", INT_MAX ) == 11 );
762     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
763     TESTCASE( testprintf( buffer, 100, "%+06.3d", INT_MAX ) == 11 );
764     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
765     TESTCASE( testprintf( buffer, 100, "%+03.6d", INT_MAX ) == 11 );
766     TESTCASE( strcmp( buffer, "+2147483647" ) == 0 );
767     TESTCASE( testprintf( buffer, 100, "%c", 'x' ) == 1 );
768     TESTCASE( strcmp( buffer, "x" ) == 0 );
769     TESTCASE( testprintf( buffer, 100, "%s", "abcdef" ) == 6 );
770     TESTCASE( strcmp( buffer, "abcdef" ) == 0 );
771     TESTCASE( testprintf( buffer, 100, "%p", (void *)0xdeadbeef ) == 10 );
772     TESTCASE( strcmp( buffer, "0xdeadbeef" ) == 0 );
773     return TEST_RESULTS;
774 }
775
776 #endif