]> andersk Git - openssh.git/blame - openbsd-compat/bsd-snprintf.c
- (bal) Replace 'unsigned long long' to 'u_int64_t' since not every
[openssh.git] / openbsd-compat / bsd-snprintf.c
CommitLineData
2d9a148e 1/**************************************************************
2 * Original:
3 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
4 * A bombproof version of doprnt (dopr) included.
5 * Sigh. This sort of thing is always nasty do deal with. Note that
6 * the version here does not include floating point...
dad3b556 7 *
2d9a148e 8 * snprintf() is used instead of sprintf() as it does limit checks
9 * for string length. This covers a nasty loophole.
dad3b556 10 *
2d9a148e 11 * The other functions are there to prevent NULL pointers from
12 * causing nast effects.
dad3b556 13 *
2d9a148e 14 * More Recently:
15 * Brandon Long <blong@fiction.net> 9/15/96 for mutt 0.43
16 * This was ugly. It is still ugly. I opted out of floating point
17 * numbers, but the formatter understands just about everything
18 * from the normal C string format, at least as far as I can tell from
19 * the Solaris 2.5 printf(3S) man page.
20 *
21 * Brandon Long <blong@fiction.net> 10/22/97 for mutt 0.87.1
22 * Ok, added some minimal floating point support, which means this
23 * probably requires libm on most operating systems. Don't yet
24 * support the exponent (e,E) and sigfig (g,G). Also, fmtint()
25 * was pretty badly broken, it just wasn't being exercised in ways
26 * which showed it, so that's been fixed. Also, formated the code
27 * to mutt conventions, and removed dead code left over from the
28 * original. Also, there is now a builtin-test, just compile with:
29 * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm
30 * and run snprintf for results.
31 *
32 * Thomas Roessler <roessler@guug.de> 01/27/98 for mutt 0.89i
33 * The PGP code was using unsigned hexadecimal formats.
34 * Unfortunately, unsigned formats simply didn't work.
35 *
36 * Michael Elkins <me@cs.hmc.edu> 03/05/98 for mutt 0.90.8
37 * The original code assumed that both snprintf() and vsnprintf() were
38 * missing. Some systems only have snprintf() but not vsnprintf(), so
39 * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF.
40 *
f1312c76 41 * Ben Lindstrom <mouring@eviladmin.org> 09/27/00 for OpenSSH
c73b8e3a 42 * Welcome to the world of %lld and %qd support. With other
43 * long long support. This is needed for sftp-server to work
44 * right.
f1312c76 45 *
46 * Ben Lindstrom <mouring@eviladmin.org> 02/12/01 for OpenSSH
47 * Removed all hint of VARARGS stuff and banished it to the void,
48 * and did a bit of KNF style work to make things a bit more
49 * acceptable. Consider stealing from mutt or enlightenment.
2d9a148e 50 **************************************************************/
dad3b556 51
0b202697 52#include "includes.h"
53
54RCSID("$Id$");
dad3b556 55
2d9a148e 56#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
dad3b556 57
f1312c76 58static void
59dopr(char *buffer, size_t maxlen, const char *format, va_list args);
60
61static void
62fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
63 int min, int max);
64
65static void
66fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base,
67 int min, int max, int flags);
68
69static void
70fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
71 int min, int max, int flags);
2d9a148e 72
958e5ae4 73static void
74dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
75
2d9a148e 76/*
77 * dopr(): poor man's version of doprintf
78 */
79
80/* format read states */
81#define DP_S_DEFAULT 0
82#define DP_S_FLAGS 1
83#define DP_S_MIN 2
84#define DP_S_DOT 3
85#define DP_S_MAX 4
86#define DP_S_MOD 5
87#define DP_S_CONV 6
88#define DP_S_DONE 7
89
90/* format flags - Bits */
91#define DP_F_MINUS (1 << 0)
92#define DP_F_PLUS (1 << 1)
93#define DP_F_SPACE (1 << 2)
94#define DP_F_NUM (1 << 3)
95#define DP_F_ZERO (1 << 4)
96#define DP_F_UP (1 << 5)
97#define DP_F_UNSIGNED (1 << 6)
98
99/* Conversion Flags */
c73b8e3a 100#define DP_C_SHORT 1
101#define DP_C_LONG 2
102#define DP_C_LDOUBLE 3
103#define DP_C_LONG_LONG 4
2d9a148e 104
105#define char_to_int(p) (p - '0')
f1312c76 106#define abs_val(p) (p < 0 ? -p : p)
2d9a148e 107
f1312c76 108
109static void
110dopr(char *buffer, size_t maxlen, const char *format, va_list args)
2d9a148e 111{
f1312c76 112 char *strvalue;
113 char ch;
114 long value;
115 long double fvalue;
116 int min = 0;
117 int max = -1;
118 int state = DP_S_DEFAULT;
119 int flags = 0;
120 int cflags = 0;
121 size_t currlen = 0;
2d9a148e 122
2d9a148e 123 ch = *format++;
f1312c76 124
125 while (state != DP_S_DONE) {
126 if ((ch == '\0') || (currlen >= maxlen))
127 state = DP_S_DONE;
128
129 switch(state) {
130 case DP_S_DEFAULT:
131 if (ch == '%')
132 state = DP_S_FLAGS;
133 else
134 dopr_outch(buffer, &currlen, maxlen, ch);
135 ch = *format++;
136 break;
137 case DP_S_FLAGS:
138 switch (ch) {
139 case '-':
140 flags |= DP_F_MINUS;
141 ch = *format++;
142 break;
143 case '+':
144 flags |= DP_F_PLUS;
145 ch = *format++;
146 break;
147 case ' ':
148 flags |= DP_F_SPACE;
149 ch = *format++;
150 break;
151 case '#':
152 flags |= DP_F_NUM;
153 ch = *format++;
154 break;
155 case '0':
156 flags |= DP_F_ZERO;
157 ch = *format++;
158 break;
159 default:
160 state = DP_S_MIN;
161 break;
162 }
163 break;
164 case DP_S_MIN:
165 if (isdigit((unsigned char)ch)) {
166 min = 10*min + char_to_int (ch);
167 ch = *format++;
168 } else if (ch == '*') {
169 min = va_arg (args, int);
170 ch = *format++;
171 state = DP_S_DOT;
172 } else
173 state = DP_S_DOT;
174 break;
175 case DP_S_DOT:
176 if (ch == '.') {
177 state = DP_S_MAX;
178 ch = *format++;
179 } else
180 state = DP_S_MOD;
181 break;
182 case DP_S_MAX:
183 if (isdigit((unsigned char)ch)) {
184 if (max < 0)
185 max = 0;
186 max = 10*max + char_to_int(ch);
187 ch = *format++;
188 } else if (ch == '*') {
189 max = va_arg (args, int);
190 ch = *format++;
191 state = DP_S_MOD;
192 } else
193 state = DP_S_MOD;
194 break;
195 case DP_S_MOD:
196 switch (ch) {
197 case 'h':
198 cflags = DP_C_SHORT;
199 ch = *format++;
200 break;
201 case 'l':
202 cflags = DP_C_LONG;
203 ch = *format++;
204 if (ch == 'l') {
205 cflags = DP_C_LONG_LONG;
206 ch = *format++;
207 }
208 break;
209 case 'q':
210 cflags = DP_C_LONG_LONG;
211 ch = *format++;
212 break;
213 case 'L':
214 cflags = DP_C_LDOUBLE;
215 ch = *format++;
216 break;
217 default:
218 break;
219 }
220 state = DP_S_CONV;
221 break;
222 case DP_S_CONV:
223 switch (ch) {
224 case 'd':
225 case 'i':
226 if (cflags == DP_C_SHORT)
227 value = va_arg(args, int);
228 else if (cflags == DP_C_LONG)
229 value = va_arg(args, long int);
230 else if (cflags == DP_C_LONG_LONG)
231 value = va_arg (args, long long);
232 else
233 value = va_arg (args, int);
234 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
235 break;
236 case 'o':
237 flags |= DP_F_UNSIGNED;
238 if (cflags == DP_C_SHORT)
239 value = va_arg(args, unsigned int);
240 else if (cflags == DP_C_LONG)
241 value = va_arg(args, unsigned long int);
242 else if (cflags == DP_C_LONG_LONG)
243 value = va_arg(args, unsigned long long);
244 else
245 value = va_arg(args, unsigned int);
246 fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
247 break;
248 case 'u':
249 flags |= DP_F_UNSIGNED;
250 if (cflags == DP_C_SHORT)
251 value = va_arg(args, unsigned int);
252 else if (cflags == DP_C_LONG)
253 value = va_arg(args, unsigned long int);
254 else if (cflags == DP_C_LONG_LONG)
255 value = va_arg(args, unsigned long long);
256 else
257 value = va_arg(args, unsigned int);
258 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
259 break;
260 case 'X':
261 flags |= DP_F_UP;
262 case 'x':
263 flags |= DP_F_UNSIGNED;
264 if (cflags == DP_C_SHORT)
265 value = va_arg(args, unsigned int);
266 else if (cflags == DP_C_LONG)
267 value = va_arg(args, unsigned long int);
268 else if (cflags == DP_C_LONG_LONG)
269 value = va_arg(args, unsigned long long);
270 else
271 value = va_arg(args, unsigned int);
272 fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
273 break;
274 case 'f':
275 if (cflags == DP_C_LDOUBLE)
276 fvalue = va_arg(args, long double);
277 else
278 fvalue = va_arg(args, double);
279 /* um, floating point? */
280 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
281 break;
282 case 'E':
283 flags |= DP_F_UP;
284 case 'e':
285 if (cflags == DP_C_LDOUBLE)
286 fvalue = va_arg(args, long double);
287 else
288 fvalue = va_arg(args, double);
289 break;
290 case 'G':
291 flags |= DP_F_UP;
292 case 'g':
293 if (cflags == DP_C_LDOUBLE)
294 fvalue = va_arg(args, long double);
295 else
296 fvalue = va_arg(args, double);
297 break;
298 case 'c':
299 dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
300 break;
301 case 's':
302 strvalue = va_arg(args, char *);
303 if (max < 0)
304 max = maxlen; /* ie, no max */
305 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
306 break;
307 case 'p':
308 strvalue = va_arg(args, void *);
309 fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
310 break;
311 case 'n':
312 if (cflags == DP_C_SHORT) {
313 short int *num;
314 num = va_arg(args, short int *);
315 *num = currlen;
316 } else if (cflags == DP_C_LONG) {
317 long int *num;
318 num = va_arg(args, long int *);
319 *num = currlen;
320 } else if (cflags == DP_C_LONG_LONG) {
321 long long *num;
322 num = va_arg(args, long long *);
323 *num = currlen;
324 } else {
325 int *num;
326 num = va_arg(args, int *);
327 *num = currlen;
328 }
329 break;
330 case '%':
331 dopr_outch(buffer, &currlen, maxlen, ch);
332 break;
333 case 'w': /* not supported yet, treat as next char */
334 ch = *format++;
335 break;
336 default: /* Unknown, skip */
337 break;
338 }
339 ch = *format++;
340 state = DP_S_DEFAULT;
341 flags = cflags = min = 0;
342 max = -1;
343 break;
344 case DP_S_DONE:
345 break;
346 default: /* hmm? */
347 break; /* some picky compilers need this */
348 }
349 }
350 if (currlen < maxlen - 1)
351 buffer[currlen] = '\0';
2d9a148e 352 else
f1312c76 353 buffer[maxlen - 1] = '\0';
2d9a148e 354}
355
f1312c76 356static void
357fmtstr(char *buffer, size_t *currlen, size_t maxlen,
358 char *value, int flags, int min, int max)
2d9a148e 359{
f1312c76 360 int padlen, strln; /* amount to pad */
361 int cnt = 0;
2d9a148e 362
f1312c76 363 if (value == 0)
364 value = "<NULL>";
365
366 for (strln = 0; value[strln]; ++strln); /* strlen */
367 padlen = min - strln;
368 if (padlen < 0)
369 padlen = 0;
370 if (flags & DP_F_MINUS)
371 padlen = -padlen; /* Left Justify */
372
373 while ((padlen > 0) && (cnt < max)) {
374 dopr_outch(buffer, currlen, maxlen, ' ');
375 --padlen;
376 ++cnt;
377 }
378 while (*value && (cnt < max)) {
379 dopr_outch(buffer, currlen, maxlen, *value++);
380 ++cnt;
381 }
382 while ((padlen < 0) && (cnt < max)) {
383 dopr_outch(buffer, currlen, maxlen, ' ');
384 ++padlen;
385 ++cnt;
386 }
2d9a148e 387}
388
389/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
390
f1312c76 391static void
392fmtint(char *buffer, size_t *currlen, size_t maxlen,
393 long value, int base, int min, int max, int flags)
2d9a148e 394{
f1312c76 395 unsigned long uvalue;
396 char convert[20];
397 int signvalue = 0;
398 int place = 0;
399 int spadlen = 0; /* amount to space pad */
400 int zpadlen = 0; /* amount to zero pad */
401 int caps = 0;
2d9a148e 402
f1312c76 403 if (max < 0)
404 max = 0;
405
406 uvalue = value;
407
408 if (!(flags & DP_F_UNSIGNED)) {
409 if (value < 0) {
410 signvalue = '-';
411 uvalue = -value;
412 } else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
413 signvalue = '+';
414 else if (flags & DP_F_SPACE)
415 signvalue = ' ';
416 }
2d9a148e 417
f1312c76 418 if (flags & DP_F_UP)
419 caps = 1; /* Should characters be upper case? */
420
421 do {
422 convert[place++] =
423 (caps? "0123456789ABCDEF":"0123456789abcdef")
424 [uvalue % (unsigned)base];
425 uvalue = (uvalue / (unsigned)base );
426 } while (uvalue && (place < 20));
427 if (place == 20)
428 place--;
429 convert[place] = 0;
430
431 zpadlen = max - place;
432 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
433 if (zpadlen < 0)
434 zpadlen = 0;
435 if (spadlen < 0)
436 spadlen = 0;
437 if (flags & DP_F_ZERO) {
438 zpadlen = MAX(zpadlen, spadlen);
439 spadlen = 0;
440 }
441 if (flags & DP_F_MINUS)
442 spadlen = -spadlen; /* Left Justifty */
443
444
445 /* Spaces */
446 while (spadlen > 0) {
447 dopr_outch(buffer, currlen, maxlen, ' ');
448 --spadlen;
449 }
450
451 /* Sign */
452 if (signvalue)
453 dopr_outch(buffer, currlen, maxlen, signvalue);
454
455 /* Zeros */
456 if (zpadlen > 0) {
457 while (zpadlen > 0) {
458 dopr_outch(buffer, currlen, maxlen, '0');
459 --zpadlen;
460 }
461 }
462
463 /* Digits */
464 while (place > 0)
465 dopr_outch(buffer, currlen, maxlen, convert[--place]);
2d9a148e 466
f1312c76 467 /* Left Justified spaces */
468 while (spadlen < 0) {
469 dopr_outch (buffer, currlen, maxlen, ' ');
470 ++spadlen;
471 }
b6019d68 472}
b6019d68 473
f1312c76 474static long double
475pow10(int exp)
dad3b556 476{
f1312c76 477 long double result = 1;
2d9a148e 478
f1312c76 479 while (exp) {
480 result *= 10;
481 exp--;
482 }
2d9a148e 483
f1312c76 484 return result;
dad3b556 485}
486
f1312c76 487static long
488round(long double value)
dad3b556 489{
f1312c76 490 long intpart = value;
2d9a148e 491
f1312c76 492 value -= intpart;
493 if (value >= 0.5)
494 intpart++;
2d9a148e 495
f1312c76 496 return intpart;
dad3b556 497}
498
f1312c76 499static void
500fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
501 int min, int max, int flags)
dad3b556 502{
f1312c76 503 char iconvert[20];
504 char fconvert[20];
505 int signvalue = 0;
506 int iplace = 0;
507 int fplace = 0;
508 int padlen = 0; /* amount to pad */
509 int zpadlen = 0;
510 int caps = 0;
511 long intpart;
512 long fracpart;
513 long double ufvalue;
2d9a148e 514
f1312c76 515 /*
516 * AIX manpage says the default is 0, but Solaris says the default
517 * is 6, and sprintf on AIX defaults to 6
518 */
519 if (max < 0)
520 max = 6;
521
522 ufvalue = abs_val(fvalue);
523
524 if (fvalue < 0)
525 signvalue = '-';
526 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
527 signvalue = '+';
528 else if (flags & DP_F_SPACE)
529 signvalue = ' ';
530
531 intpart = ufvalue;
532
533 /*
534 * Sorry, we only support 9 digits past the decimal because of our
535 * conversion method
536 */
537 if (max > 9)
538 max = 9;
539
540 /* We "cheat" by converting the fractional part to integer by
541 * multiplying by a factor of 10
542 */
543 fracpart = round((pow10 (max)) * (ufvalue - intpart));
544
545 if (fracpart >= pow10 (max)) {
546 intpart++;
547 fracpart -= pow10 (max);
548 }
549
550 /* Convert integer part */
551 do {
552 iconvert[iplace++] =
553 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
554 intpart = (intpart / 10);
555 } while(intpart && (iplace < 20));
556 if (iplace == 20)
557 iplace--;
558 iconvert[iplace] = 0;
559
560 /* Convert fractional part */
561 do {
562 fconvert[fplace++] =
563 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
564 fracpart = (fracpart / 10);
565 } while(fracpart && (fplace < 20));
566 if (fplace == 20)
567 fplace--;
568 fconvert[fplace] = 0;
569
570 /* -1 for decimal point, another -1 if we are printing a sign */
571 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
572 zpadlen = max - fplace;
573 if (zpadlen < 0)
574 zpadlen = 0;
575 if (padlen < 0)
576 padlen = 0;
577 if (flags & DP_F_MINUS)
578 padlen = -padlen; /* Left Justifty */
579
580 if ((flags & DP_F_ZERO) && (padlen > 0)) {
581 if (signvalue) {
582 dopr_outch(buffer, currlen, maxlen, signvalue);
583 --padlen;
584 signvalue = 0;
585 }
586 while (padlen > 0) {
587 dopr_outch(buffer, currlen, maxlen, '0');
588 --padlen;
589 }
590 }
591 while (padlen > 0) {
592 dopr_outch(buffer, currlen, maxlen, ' ');
593 --padlen;
594 }
595 if (signvalue)
596 dopr_outch(buffer, currlen, maxlen, signvalue);
597
598 while (iplace > 0)
599 dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
600
601 /*
602 * Decimal point. This should probably use locale to find the correct
603 * char to print out.
604 */
605 dopr_outch(buffer, currlen, maxlen, '.');
606
607 while (fplace > 0)
608 dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
609
610 while (zpadlen > 0) {
611 dopr_outch(buffer, currlen, maxlen, '0');
612 --zpadlen;
613 }
614
615 while (padlen < 0) {
616 dopr_outch(buffer, currlen, maxlen, ' ');
617 ++padlen;
618 }
2d9a148e 619}
620
f1312c76 621static void
622dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
2d9a148e 623{
f1312c76 624 if (*currlen < maxlen)
625 buffer[(*currlen)++] = c;
dad3b556 626}
2d9a148e 627#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
dad3b556 628
2d9a148e 629#ifndef HAVE_VSNPRINTF
f1312c76 630int
631vsnprintf(char *str, size_t count, const char *fmt, va_list args)
dad3b556 632{
f1312c76 633 str[0] = 0;
634 dopr(str, count, fmt, args);
635
636 return(strlen(str));
dad3b556 637}
2d9a148e 638#endif /* !HAVE_VSNPRINTF */
dad3b556 639
2d9a148e 640#ifndef HAVE_SNPRINTF
f1312c76 641int
642snprintf(char *str,size_t count,const char *fmt,...)
847e8865 643{
f1312c76 644 va_list ap;
645
646 va_start(ap, fmt);
647 (void) vsnprintf(str, count, fmt, ap);
648 va_end(ap);
649
650 return(strlen(str));
2d9a148e 651}
652
653#ifdef TEST_SNPRINTF
f1312c76 654int
655main(void)
2d9a148e 656{
f1312c76 657#define LONG_STRING 1024
658 char buf1[LONG_STRING];
659 char buf2[LONG_STRING];
660 char *fp_fmt[] = {
661 "%-1.5f",
662 "%1.5f",
663 "%123.9f",
664 "%10.5f",
665 "% 10.5f",
666 "%+22.9f",
667 "%+4.9f",
668 "%01.3f",
669 "%4f",
670 "%3.1f",
671 "%3.2f",
672 NULL
673 };
674 double fp_nums[] = {
675 -1.5,
676 134.21,
677 91340.2,
678 341.1234,
679 0203.9,
680 0.96,
681 0.996,
682 0.9996,
683 1.996,
684 4.136,
685 0
686 };
687 char *int_fmt[] = {
688 "%-1.5d",
689 "%1.5d",
690 "%123.9d",
691 "%5.5d",
692 "%10.5d",
693 "% 10.5d",
694 "%+22.33d",
695 "%01.3d",
696 "%4d",
697 "%lld",
698 "%qd",
699 NULL
700 };
701 long long int_nums[] = { -1, 134, 91340, 341, 0203, 0, 9999999 };
702 int x, y;
703 int fail = 0;
704 int num = 0;
705
706 printf("Testing snprintf format codes against system sprintf...\n");
707
708 for (x = 0; fp_fmt[x] != NULL ; x++) {
709 for (y = 0; fp_nums[y] != 0 ; y++) {
710 snprintf(buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]);
711 sprintf (buf2, fp_fmt[x], fp_nums[y]);
712 if (strcmp (buf1, buf2)) {
713 printf("snprintf doesn't match Format: %s\n\t"
714 "snprintf = %s\n\tsprintf = %s\n",
715 fp_fmt[x], buf1, buf2);
716 fail++;
717 }
718 num++;
719 }
720 }
721 for (x = 0; int_fmt[x] != NULL ; x++) {
722 for (y = 0; int_nums[y] != 0 ; y++) {
723 snprintf(buf1, sizeof (buf1), int_fmt[x], int_nums[y]);
724 sprintf(buf2, int_fmt[x], int_nums[y]);
725 if (strcmp (buf1, buf2)) {
726 printf("snprintf doesn't match Format: %s\n\t"
727 "snprintf = %s\n\tsprintf = %s\n",
728 int_fmt[x], buf1, buf2);
729 fail++;
730 }
731 num++;
732 }
733 }
734 printf("%d tests failed out of %d.\n", fail, num);
735 return(0);
847e8865 736}
2d9a148e 737#endif /* SNPRINTF_TEST */
847e8865 738
2d9a148e 739#endif /* !HAVE_SNPRINTF */
This page took 0.538495 seconds and 5 git commands to generate.