-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnprintf.c
2334 lines (1966 loc) · 84.9 KB
/
snprintf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <[email protected]>
*/
/*
* Copyright Patrick Powell 1995
* This code is based on code written by Patrick Powell ([email protected])
* It may be used for any purpose as long as this notice remains intact
* on all source code distributions
*/
/**************************************************************
* Original:
* Patrick Powell Tue Apr 11 09:48:21 PDT 1995
* A bombproof version of doprnt (dopr) included.
* Sigh. This sort of thing is always nasty do deal with. Note that
* the version here does not include floating point...
*
* snprintf() is used instead of sprintf() as it does limit checks
* for string length. This covers a nasty loophole.
*
* The other functions are there to prevent NULL pointers from
* causing nasty effects.
*
* More Recently:
* Brandon Long <[email protected]> 9/15/96 for mutt 0.43
* This was ugly. It is still ugly. I opted out of floating point
* numbers, but the formatter understands just about everything
* from the normal C string format, at least as far as I can tell from
* the Solaris 2.5 printf(3S) man page.
*
* Brandon Long <[email protected]> 10/22/97 for mutt 0.87.1
* Ok, added some minimal floating point support, which means this
* probably requires libm on most operating systems. Don't yet
* support the exponent (e,E) and sigfig (g,G). Also, fmtint()
* was pretty badly broken, it just wasn't being exercised in ways
* which showed it, so that's been fixed. Also, formated the code
* to mutt conventions, and removed dead code left over from the
* original. Also, there is now a builtin-test, just compile with:
* gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
* and run snprintf for results.
*
* Thomas Roessler <[email protected]> 01/27/98 for mutt 0.89i
* The PGP code was using unsigned hexadecimal formats.
* Unfortunately, unsigned formats simply didn't work.
*
* Michael Elkins <[email protected]> 03/05/98 for mutt 0.90.8
* The original code assumed that both snprintf() and vsnprintf() were
* missing. Some systems only have snprintf() but not vsnprintf(), so
* the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
*
* Andrew Tridgell ([email protected]) Oct 1998
* fixed handling of %.0f
* added test for HAVE_LONG_DOUBLE
*
* raf ([email protected]) Sep 2001
* improved speed of %[diouxXp]
* fixed handling of %#
* fixed handling of %[feEgG]
* fixed return value (required length, not truncated length)
* fixed error reporting (return -1 on format error)
* fixed snprintf(NULL, 0, "")
* fixed handling of %9.[diouxXp]
* fixed handling of %p
* fixed handling of flags [+- #0]
* fixed handling of precision (%[diouxXps])
* fixed handling of field width (%c)
* fixed handling of %h[diouxX] (bigendian)
* added ability to mimic glibc or solaris behaviour
* now ISO C 89 compliant formatting except format is not an mbstr
* added manpage in pod format (perldoc -F snprintf.c)
* added rigorous testing (145893 tests)
*
* raf ([email protected]) May 2002
* added solaris 8 bug compatibility
*
* raf ([email protected]) Dec 2003
* added examples to the manpage
*
* raf ([email protected]) Jun 2010
* fixed for 64-bit systems
*
**************************************************************/
/*
=head1 NAME
I<snprintf(3)> - safe sprintf
=head1 SYNOPSIS
#include <slack/std.h>
#ifndef HAVE_SNPRINTF
#include <slack/snprintf.h>
#endif
int snprintf(char *str, size_t size, const char *format, ...);
int vsnprintf(char *str, size_t size, const char *format, va_list args);
=head1 DESCRIPTION
Safe version of I<sprintf(3)> that doesn't suffer from buffer overruns.
=over 4
=cut
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <sys/types.h>
#include "snprintf.h"
#ifdef HAVE_LONG_DOUBLE
#define LDOUBLE long double
#else
#define LDOUBLE double
#endif
#ifndef TEST
#if defined __STDC__ || defined __cplusplus
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#if defined __STDC__ || defined __cplusplus
int sprintf(char *str, const char *format, ...);
#else
int sprintf();
#endif
/* Format read states */
#define PARSE_DEFAULT 0
#define PARSE_FLAGS 1
#define PARSE_WIDTH 2
#define PARSE_DOT 3
#define PARSE_PRECISION 4
#define PARSE_SIZE 5
#define PARSE_CONVERSION 6
#define PARSE_DONE 7
/* Format flags */
#define FLAG_MINUS (1 << 0)
#define FLAG_PLUS (1 << 1)
#define FLAG_SPACE (1 << 2)
#define FLAG_ALT (1 << 3)
#define FLAG_ZERO (1 << 4)
#define FLAG_UP (1 << 5)
#define FLAG_UNSIGNED (1 << 6)
#define FLAG_F (1 << 7)
#define FLAG_E (1 << 8)
#define FLAG_G (1 << 9)
#define FLAG_PTR_SIGNED (1 << 10)
#define FLAG_PTR_NIL (1 << 11)
#define FLAG_PTR_NOALT (1 << 12)
/* Conversion flags */
#define SIZE_SHORT 1
#define SIZE_LONG 2
#define SIZE_LDOUBLE 3
#ifndef max
#define max(a, b) ((a >= b) ? a : b)
#endif
/*
=item C<int snprintf(char *str, size_t size, const char *format, ...)>
Writes output to the buffer, C<str>, under control of the format string
C<format>, that specifies how subsequent arguments are converted for output.
It is similar to I<sprintf(3)>, except that C<size> specifies the maximum
number of bytes to produce. The trailing C<nul> byte is counted towards this
limit, so you must allocate at least C<size> bytes for C<str>.
If C<size> is zero, nothing is written, and C<str> may be C<null>.
Otherwise, output bytes beyond the C<n-1>st are discarded rather than being
written to C<str>, and a C<nul> byte is written at the end of the bytes
actually written to C<str>. If copying takes place between objects that
overlap, the behaviour is undefined.
On success, returns the number of bytes that would have been written had
C<size> been sufficiently large, not counting the terminating C<nul> byte.
Thus, the C<nul>-terminated output has been completely written if and only
if the return value is non-negative and less than C<size>. On error, returns
C<-1> (i.e. encoding error).
Note that if your system already has I<snprintf(3)>, but this implementation
was installed anyway, it's because the system implementation has a broken
return value. Some older implementations (e.g. I<glibc-2.0>) return C<-1>
when the string is truncated rather than returning the number of bytes that
would have been written had C<size> been sufficiently large (not counting
the terminating C<nul> byte) as required by I<ISO/IEC 9899:1999(E)>.
=cut
*/
#ifndef HAVE_SNPRINTF
#ifdef __STDC__
int snprintf(char *str, size_t size, const char *format, ...)
#else
int snprintf(str, size, format, va_alist)
char *str;
size_t size;
const char *format;
va_dcl
#endif
{
int ret;
va_list args;
#ifdef __STDC__
va_start(args, format);
#else
va_start(args);
#endif
ret = vsnprintf(str, size, format, args);
va_end(args);
return ret;
}
#endif
/*
=item C<int vsnprintf(char *str, size_t size, const char *format, va_list args)>
Equivalent to I<snprintf(3)> with the variable argument list specified
directly as for I<vsprintf(3)>.
=cut
*/
#ifndef HAVE_VSNPRINTF
static void outch(char *buffer, size_t *currlen, size_t *reqlen, size_t size, int c)
{
if (*currlen + 1 < size)
buffer[(*currlen)++] = (char)c;
++*reqlen;
}
static void fmtch(char *buffer, size_t *currlen, size_t *reqlen, size_t size, int value, int flags, int width)
{
int padlen;
/*
** A negative field width argument is taken as a - flag followed by a
** positive field width argument
*/
if (width < 0)
{
flags |= FLAG_MINUS;
width = -width;
}
padlen = width - 1;
if (padlen < 0)
padlen = 0;
if (flags & FLAG_MINUS)
padlen = -padlen;
while (padlen > 0)
{
outch(buffer, currlen, reqlen, size, ' ');
--padlen;
}
outch(buffer, currlen, reqlen, size, (unsigned int)value);
while (padlen < 0)
{
outch(buffer, currlen, reqlen, size, ' ');
++padlen;
}
}
static void fmtstr(char *buffer, size_t *currlen, size_t *reqlen, size_t size, char *value, int flags, int width, int precision)
{
int padlen, bytes;
int cnt = 0;
/*
** A negative field width argument is taken as a - flag followed by a
** positive field width argument
*/
if (width < 0)
{
flags |= FLAG_MINUS;
width = -width;
}
/* A negative precision argument is taken as if the precision were omitted */
if (precision < 0)
precision = -1;
#ifdef HAVE_PRINTF_STR_FMT_NULL
/* Print NULL as "(null)" if possible, otherwise as "" (like glibc) */
if (value == NULL)
value = (precision == -1 || precision >= 6) ? "(null)" : "";
#endif
for (bytes = 0; (precision == -1 || bytes < precision) && value[bytes]; ++bytes)
{}
padlen = width - bytes;
if (padlen < 0)
padlen = 0;
if (flags & FLAG_MINUS)
padlen = -padlen;
while (padlen > 0)
{
outch(buffer, currlen, reqlen, size, ' ');
--padlen;
}
while (*value && (precision == -1 || (cnt < precision)))
{
outch(buffer, currlen, reqlen, size, *value++);
++cnt;
}
while (padlen < 0)
{
outch(buffer, currlen, reqlen, size, ' ');
++padlen;
}
}
static void fmtint(char *buffer, size_t *currlen, size_t *reqlen, size_t size, long value, int base, int width, int precision, int flags)
{
int signvalue = 0;
unsigned long uvalue;
unsigned char convert[22];
int place = 0;
int spadlen = 0;
int zpadlen = 0;
char *digits;
int zextra = 0;
int sextra = 0;
/*
** A negative field width argument is taken as a - flag followed by a
** positive field width argument
*/
if (width < 0)
{
#ifdef HAVE_PRINTF_WITH_SOLARIS_NEGATIVE_WIDTH_BEHAVIOUR
if (!(flags & FLAG_ZERO) || precision >= 0)
#endif
flags |= FLAG_MINUS;
width = -width;
}
/* A negative precision argument is taken as if the precision were omitted */
if (precision < 0)
precision = -1;
/* If the space and + flags both appear, the space flag will be ignored */
if (flags & FLAG_PLUS)
flags &= ~FLAG_SPACE;
/* If the 0 and - flags both appear, the 0 flag will be ignored */
if (flags & FLAG_MINUS)
flags &= ~FLAG_ZERO;
/* If a precision is specified, the 0 flag will be ignored */
/* The d, i, o, u, x and X conversions have a default precision of 1 */
if (precision == -1)
precision = 1;
else
flags &= ~FLAG_ZERO;
/*
** The + flag: The result of the conversion will always begin with a plus
** or minus sign. (It will begin with a sign only when a negative value is
** converted if this flag is not specified.)
**
** The space flag: If the first character of a signed conversion is not a
** sign, or if a signed conversion results in no characters, a space will
** be prefixed to the result.
*/
uvalue = (unsigned long)value;
if (!(flags & FLAG_UNSIGNED))
{
if (value < 0)
{
signvalue = '-';
uvalue = (unsigned long)-value;
}
else if (flags & FLAG_PLUS)
signvalue = '+';
else if (flags & FLAG_SPACE)
signvalue = ' ';
}
#ifdef HAVE_PRINTF_PTR_FMT_SIGNED
/* Behave like glibc (treat %p as signed (almost)) */
if (flags & FLAG_PTR_SIGNED && value)
{
if (flags & FLAG_PLUS)
signvalue = '+';
else if (flags & FLAG_SPACE)
signvalue = ' ';
}
#endif
#ifdef HAVE_PRINTF_PTR_FMT_NOALT
/* Behave like Solaris - %p never produces 0x */
if (flags & FLAG_PTR_NOALT)
flags &= ~FLAG_ALT;
#endif
#ifdef HAVE_PRINTF_PTR_FMT_NIL
if (flags & FLAG_PTR_NIL && !value)
{
spadlen = width - 5;
if (spadlen < 0)
spadlen = 0;
if (flags & FLAG_MINUS)
spadlen = -spadlen;
while (spadlen > 0)
{
outch(buffer, currlen, reqlen, size, ' ');
--spadlen;
}
outch(buffer, currlen, reqlen, size, '(');
outch(buffer, currlen, reqlen, size, 'n');
outch(buffer, currlen, reqlen, size, 'i');
outch(buffer, currlen, reqlen, size, 'l');
outch(buffer, currlen, reqlen, size, ')');
while (spadlen < 0)
{
outch(buffer, currlen, reqlen, size, ' ');
++spadlen;
}
return;
}
#endif
/* %[XEG] produce uppercase alpha characters */
digits = (flags & FLAG_UP) ? "0123456789ABCDEF" : "0123456789abcdef";
/*
** The result of converting a zero value with a precision of zero is no
** characters
*/
if (precision || uvalue)
{
do
{
convert[place++] = digits[uvalue % base];
uvalue /= base;
}
while (uvalue && (place < 21));
}
convert[place] = 0;
/*
** The # flag: For o conversion, it increases the precision to force the
** first digit of the result to be a zero. For x (or X) conversion, a
** non-zero result will have 0x (or 0X) prefixed to it.
*/
if (flags & FLAG_ALT)
{
if (base == 16 && value)
sextra = 2;
#ifdef HAVE_PRINTF_WITH_SOLARIS8_ZERO_PRECISION_ALT_OCTAL_BEHAVIOUR
else if (base == 8 && precision <= place && (place != 0 && convert[place - 1] != '0'))
zextra = 1;
#else
else if (base == 8 && precision <= place && (place == 0 || convert[place - 1] != '0'))
zextra = 1;
#endif
}
/*
** The 0 flag: Leading zeroes (following any indication of sign or base)
** are used to pad to the field width; no space padding is performed.
*/
zpadlen = precision - place;
if (zpadlen < 0)
zpadlen = 0;
zpadlen += zextra;
spadlen = width - (place + zpadlen) - (signvalue ? 1 : 0) - sextra;
if (spadlen < 0)
spadlen = 0;
if (flags & FLAG_ZERO)
{
zpadlen = max(zpadlen, spadlen);
spadlen = 0;
}
if (flags & FLAG_MINUS)
spadlen = -spadlen;
while (spadlen > 0)
{
outch(buffer, currlen, reqlen, size, ' ');
--spadlen;
}
if (signvalue)
outch(buffer, currlen, reqlen, size, signvalue);
if (flags & FLAG_ALT && base == 16 && sextra == 2)
{
outch(buffer, currlen, reqlen, size, '0');
outch(buffer, currlen, reqlen, size, (flags & FLAG_UP) ? 'X' : 'x');
}
while (zpadlen > 0)
{
outch(buffer, currlen, reqlen, size, '0');
--zpadlen;
}
while (place > 0)
outch(buffer, currlen, reqlen, size, convert[--place]);
while (spadlen < 0)
{
outch(buffer, currlen, reqlen, size, ' ');
++spadlen;
}
}
#ifndef DBL_MAX_10_EXP
#define DBL_MAX_10_EXP 308
#endif
#ifndef LDBL_MAX_10_EXP
#define LDBL_MAX_10_EXP 4932
#endif
#define DBL_MAX_EXP_DIGITS 3
#define LDBL_MAX_EXP_DIGITS 4
#define MAX_DIGITS(ldbl) \
((ldbl) ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP)
#define MAX_EXP_DIGITS(ldbl) \
((ldbl) ? LDBL_MAX_EXP_DIGITS : DBL_MAX_EXP_DIGITS)
#define F_MAX(width, precision, ldbl) \
max((width), 1 + MAX_DIGITS(ldbl) + 1 + (precision))
#define E_MAX(width, precision, ldbl) \
max((width), 3 + (precision) + 2 + MAX_EXP_DIGITS(ldbl))
#define G_MAX(width, precision, ldbl) \
max(F_MAX((width), precision, ldbl), E_MAX((width), precision, ldbl))
static void fmtfp(char *buffer, size_t *currlen, size_t *reqlen, size_t size, LDOUBLE fvalue, int width, int precision, int flags, int cflags)
{
/* Calculate maximum space required and delegate to sprintf() */
char buf[512], *convert = buf, *p;
char format[1 + 5 + 20 + 1 + 20 + 1 + 1 + 1];
char widstr[21];
int fpmax;
/*
** A negative field width argument is taken as a - flag followed by a
** positive field width argument
*/
if (width < 0)
{
#ifdef HAVE_PRINTF_WITH_SOLARIS_NEGATIVE_WIDTH_BEHAVIOUR
if ((flags & FLAG_ZERO) == 0)
#endif
flags |= FLAG_MINUS;
width = -width;
}
/* A negative precision argument is taken as if the precision were omitted */
if (precision < 0)
precision = -1;
/*
** The ISO C standard (Section 7.9.6.2, The fscanf function), says that
** if the precision is missing, the default precision for f, e and E is
** taken to be 6. The default precision for g and G is taken to be 1.
** However, many systems use 6, so we'll provide an option.
*/
if (precision == -1)
precision =
#ifdef HAVE_PRINTF_FLT_FMT_G_STD
(flags & FLAG_G) ? 1 :
#endif
6;
/* Calculate maximum possible length */
if (flags & FLAG_F)
fpmax = F_MAX(width, precision, (cflags & SIZE_LDOUBLE)) + 1;
else if (flags & FLAG_E)
fpmax = E_MAX(width, precision, (cflags & SIZE_LDOUBLE)) + 1;
else
fpmax = G_MAX(width, precision, (cflags & SIZE_LDOUBLE)) + 1;
/* Ensure enough space or bail out */
if (fpmax > 512 && !(convert = malloc(fpmax)))
return;
/* Build the format string (probably should have just copied it) */
sprintf(widstr, "%d", width);
sprintf(format, "%%%s%s%s%s%s%s.%d%s%s",
(flags & FLAG_MINUS) ? "-" : "",
(flags & FLAG_PLUS) ? "+" : "",
(flags & FLAG_SPACE) ? " " : "",
(flags & FLAG_ALT) ? "#" : "",
(flags & FLAG_ZERO) ? "0" : "",
(width) ? widstr : "",
precision,
(cflags & SIZE_LDOUBLE) ? "L" : "",
(flags & FLAG_E) ? (flags & FLAG_UP) ? "E" : "e" :
(flags & FLAG_G) ? (flags & FLAG_UP) ? "G" : "g" : "f"
);
/* Delegate to sprintf() */
if (cflags & SIZE_LDOUBLE)
sprintf(convert, format, fvalue);
else
sprintf(convert, format, (double)fvalue);
/* Copy to buffer */
for (p = convert; *p; ++p)
outch(buffer, currlen, reqlen, size, *p);
/* Deallocate if necessary */
if (convert != buf)
free(convert);
}
int vsnprintf(char *str, size_t size, const char *format, va_list args)
{
char ch;
long value;
LDOUBLE fvalue;
char *strvalue;
int width;
int precision;
int state;
int flags;
int cflags;
size_t currlen;
size_t reqlen;
state = PARSE_DEFAULT;
currlen = reqlen = flags = cflags = width = 0;
precision = -1;
ch = *format++;
while (state != PARSE_DONE)
{
if (ch == '\0')
{
if (state == PARSE_DEFAULT)
state = PARSE_DONE;
else
return -1;
}
switch (state)
{
case PARSE_DEFAULT:
if (ch == '%')
state = PARSE_FLAGS;
else
outch(str, &currlen, &reqlen, size, ch);
ch = *format++;
break;
case PARSE_FLAGS:
switch (ch)
{
case '-':
flags |= FLAG_MINUS;
ch = *format++;
break;
case '+':
flags |= FLAG_PLUS;
ch = *format++;
break;
case ' ':
flags |= FLAG_SPACE;
ch = *format++;
break;
case '#':
flags |= FLAG_ALT;
ch = *format++;
break;
case '0':
flags |= FLAG_ZERO;
ch = *format++;
break;
default:
state = PARSE_WIDTH;
break;
}
break;
case PARSE_WIDTH:
if (isdigit((int)(unsigned char)ch))
{
width = 10 * width + ch - '0';
ch = *format++;
}
else if (ch == '*')
{
width = va_arg(args, int);
ch = *format++;
state = PARSE_DOT;
}
else
state = PARSE_DOT;
break;
case PARSE_DOT:
if (ch == '.')
{
precision = 0;
state = PARSE_PRECISION;
ch = *format++;
}
else
state = PARSE_SIZE;
break;
case PARSE_PRECISION:
if (isdigit((int)(unsigned char)ch))
{
precision = 10 * precision + ch - '0';
ch = *format++;
}
else if (ch == '*')
{
precision = va_arg(args, int);
ch = *format++;
state = PARSE_SIZE;
}
else
state = PARSE_SIZE;
break;
case PARSE_SIZE:
switch (ch)
{
case 'h':
cflags = SIZE_SHORT;
ch = *format++;
break;
case 'l':
cflags = SIZE_LONG;
ch = *format++;
break;
case 'L':
cflags = SIZE_LDOUBLE;
ch = *format++;
break;
}
state = PARSE_CONVERSION;
break;
case PARSE_CONVERSION:
switch (ch)
{
case 'd':
case 'i':
if (cflags == SIZE_SHORT)
value = (short)va_arg(args, int);
else if (cflags == SIZE_LONG)
value = (long)va_arg(args, long);
else
value = (int)va_arg(args, int);
fmtint(str, &currlen, &reqlen, size, value, 10, width, precision, flags);
break;
case 'o':
flags |= FLAG_UNSIGNED;
if (cflags == SIZE_SHORT)
value = (unsigned short)va_arg(args, int);
else if (cflags == SIZE_LONG)
value = (unsigned long)va_arg(args, long);
else
value = (unsigned int)va_arg(args, int);
fmtint(str, &currlen, &reqlen, size, value, 8, width, precision, flags);
break;
case 'u':
flags |= FLAG_UNSIGNED;
if (cflags == SIZE_SHORT)
value = (unsigned short)va_arg(args, int);
else if (cflags == SIZE_LONG)
value = (unsigned long)va_arg(args, long);
else
value = (unsigned int)va_arg(args, int);
fmtint(str, &currlen, &reqlen, size, value, 10, width, precision, flags);
break;
case 'X':
flags |= FLAG_UP;
case 'x':
flags |= FLAG_UNSIGNED;
if (cflags == SIZE_SHORT)
value = (unsigned short)va_arg(args, int);
else if (cflags == SIZE_LONG)
value = (unsigned long)va_arg(args, long);
else
value = (unsigned int)va_arg(args, int);
fmtint(str, &currlen, &reqlen, size, value, 16, width, precision, flags);
break;
case 'f':
flags |= FLAG_F;
if (cflags == SIZE_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = (LDOUBLE)va_arg(args, double);
fmtfp(str, &currlen, &reqlen, size, fvalue, width, precision, flags, cflags);
break;
case 'E':
flags |= FLAG_UP;
case 'e':
flags |= FLAG_E;
if (cflags == SIZE_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = (LDOUBLE)va_arg(args, double);
fmtfp(str, &currlen, &reqlen, size, fvalue, width, precision, flags, cflags);
break;
case 'G':
flags |= FLAG_UP;
case 'g':
flags |= FLAG_G;
if (cflags == SIZE_LDOUBLE)
fvalue = va_arg(args, LDOUBLE);
else
fvalue = (LDOUBLE)va_arg(args, double);
fmtfp(str, &currlen, &reqlen, size, fvalue, width, precision, flags, cflags);
break;
case 'c':
fmtch(str, &currlen, &reqlen, size, va_arg(args, int), flags, width);
break;
case 's':
strvalue = va_arg(args, char *);
fmtstr(str, &currlen, &reqlen, size, strvalue, flags, width, precision);
break;
case 'p':
flags |= FLAG_UNSIGNED;
#ifdef HAVE_PRINTF_PTR_FMT_ALTERNATE
flags |= FLAG_ALT;
#endif
#ifdef HAVE_PRINTF_PTR_FMT_SIGNED
flags |= FLAG_PTR_SIGNED;
#endif
#ifdef HAVE_PRINTF_PTR_FMT_NIL
flags |= FLAG_PTR_NIL;
#endif
#ifdef HAVE_PRINTF_PTR_FMT_NOALT
flags |= FLAG_PTR_NOALT;
#endif
cflags = SIZE_LONG;
strvalue = va_arg(args, void *);
fmtint(str, &currlen, &reqlen, size, (long)strvalue, 16, width, precision, flags);
break;
case 'n':
if (cflags == SIZE_SHORT)
{
short *num = va_arg(args, short *);
*num = (short)currlen;
}
else if (cflags == SIZE_LONG)
{
long *num = va_arg(args, long *);
*num = (long)currlen;
}
else
{
int *num = va_arg(args, int *);
*num = currlen;
}
break;
case '%':
if (flags != 0 || cflags != 0 || width != 0 || precision != 0)
return -1;
outch(str, &currlen, &reqlen, size, ch);
break;
default:
return -1;
}
ch = *format++;
state = PARSE_DEFAULT;
flags = cflags = width = 0;
precision = -1;
break;
case PARSE_DONE:
break;
}
}
if (size)
str[currlen] = '\0';
return reqlen;
}
#endif
/*
=back
=head1 MT-Level