Debian Codegraph @

common/compat.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1998 Todd C. Miller <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * $Id: compat.c,v 1.13 2015/07/24 08:26:05 ragge Exp $
28 */
29
30 /*
31 * Copyright (c) 1987, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * $NetBSD: gettemp.c,v 1.13 2003/12/05 00:57:36 uebayasi Exp $
59 */
60
61 #include <string.h>
62
63 #include "config.h"
64 #include "compat.h"
65
66 #ifndef HAVE_STRLCAT
67 /*
68 * Appends src to string dst of size siz (unlike strncat, siz is the
69 * full size of dst, not space left). At most siz-1 characters
70 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
71 * Returns strlen(initial dst) + strlen(src); if retval >= siz,
72 * truncation occurred.
73 */
74 size_t
75 strlcat(char *dst, const char *src, size_t siz)
76 {
77 char *d = dst;
78 const char *s = src;
79 size_t n = siz;
80 size_t dlen;
81
82 /* Find the end of dst and adjust bytes left but don't go past end */
83 while (n-- != 0 && *d != '\0')
84 d++;
85 dlen = d - dst;
86 n = siz - dlen;
87
88 if (n == 0)
89 return(dlen + strlen(s));
90 while (*s != '\0') {
91 if (n != 1) {
92 *d++ = *s;
93 n--;
94 }
95 s++;
96 }
97 *d = '\0';
98
99 return(dlen + (s - src)); /* count does not include NUL */
100 }
101 #endif
102
103
104 #ifndef HAVE_STRLCPY
105 /*
106 * Copy src to string dst of size siz. At most siz-1 characters
107 * will be copied. Always NUL terminates (unless siz == 0).
108 * Returns strlen(src); if retval >= siz, truncation occurred.
109 */
110 size_t
111 strlcpy(char *dst, const char *src, size_t siz)
112 {
113 char *d = dst;
114 const char *s = src;
115 size_t n = siz;
116
117 /* Copy as many bytes as will fit */
118 if (n != 0 && --n != 0) {
119 do {
120 if ((*d++ = *s++) == 0)
121 break;
122 } while (--n != 0);
123 }
124
125 /* Not enough room in dst, add NUL and traverse rest of src */
126 if (n == 0) {
127 if (siz != 0)
128 *d = '\0'; /* NUL-terminate dst */
129 while (*s++)
130 ;
131 }
132
133 return(s - src - 1); /* count does not include NUL */
134 }
135 #endif
136
137 #ifndef HAVE_GETOPT
138 char *optarg;
139 int optind = 1;
140 int
141 getopt(int argc, char * const argv[], const char *args)
142 {
143 int n;
144 int nlen = strlen(args);
145 char cmd;
146 char rv;
147
148 if (argv[optind] && *argv[optind] == '-') {
149 cmd = *(argv[optind] + 1);
150
151 for (n = 0; n < nlen; n++) {
152 if (args[n] == ':')
153 continue;
154 if (args[n] == cmd) {
155 rv = *(argv[optind] + 1);
156 if (args[n+1] == ':') {
157 if (*(argv[optind] + 2) != '\0') {
158 optarg = argv[optind] + 2;
159 optind += 1;
160 } else {
161 optarg = argv[optind + 1];
162 optind += 2;
163 }
164 if (!optarg)
165 optarg="";
166 return rv;
167 } else {
168 optarg = NULL;
169 optind += 1;
170 return rv;
171 }
172 }
173 }
174 }
175
176 return -1;
177 }
178 #endif
179
180 #if !defined(HAVE_MKSTEMP) && !defined(_WIN32)
181 #include <fcntl.h> /* open() */
182 #include <unistd.h> /* getpid() */
183
184 int
185 mkstemp(char *path)
186 {
187 char *trv;
188 unsigned int pid;
189
190 /* To guarantee multiple calls generate unique names even if
191 the file is not created. 676 different possibilities with 7
192 or more X's, 26 with 6 or less. */
193 static char xtra[2] = "aa";
194 int xcnt = 0;
195
196 pid = getpid();
197
198 /* Move to end of path and count trailing X's. */
199 for (trv = path; *trv; ++trv)
200 if (*trv == 'X')
201 xcnt++;
202 else
203 xcnt = 0;
204
205 /* Use at least one from xtra. Use 2 if more than 6 X's. */
206 if (*(trv - 1) == 'X')
207 *--trv = xtra[0];
208 if (xcnt > 6 && *(trv - 1) == 'X')
209 *--trv = xtra[1];
210
211 /* Set remaining X's to pid digits with 0's to the left. */
212 while (*--trv == 'X') {
213 *trv = (pid % 10) + '0';
214 pid /= 10;
215 }
216
217 /* update xtra for next call. */
218 if (xtra[0] != 'z')
219 xtra[0]++;
220 else {
221 xtra[0] = 'a';
222 if (xtra[1] != 'z')
223 xtra[1]++;
224 else
225 xtra[1] = 'a';
226 }
227
228 return open(path, O_CREAT | O_EXCL | O_RDWR, 0600);
229 }
230 #endif
231
232 #ifndef HAVE_FFS
233 int
234 ffs(int x)
235 {
236 int r = 1;
237 if (!x) return 0;
238 if (!(x & 0xffff)) { x >>= 16; r += 16; }
239 if (!(x & 0xff)) { x >>= 8; r += 8; }
240 if (!(x & 0xf)) { x >>= 4; r += 4; }
241 if (!(x & 3)) { x >>= 2; r += 2; }
242 if (!(x & 1)) { x >>= 1; r += 1; }
243
244 return r;
245 }
246 #endif
247
248 /*
249 * Copyright Patrick Powell 1995
250 * This code is based on code written by Patrick Powell ([email protected])
251 * It may be used for any purpose as long as this notice remains intact
252 * on all source code distributions
253 */
254
255 #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
256 #include <ctype.h> /* isdigit() */
257
258 static void
259 dopr(char *buffer, size_t maxlen, const char *format, va_list args);
260
261 static void
262 fmtstr(char *buffer, size_t *currlen, size_t maxlen, char *value, int flags,
263 int min, int max);
264
265 static void
266 fmtint(char *buffer, size_t *currlen, size_t maxlen, long value, int base,
267 int min, int max, int flags);
268
269 static void
270 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
271 int min, int max, int flags);
272
273 static void
274 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c);
275
276 /*
277 * dopr(): poor man's version of doprintf
278 */
279
280 /* format read states */
281 #define DP_S_DEFAULT 0
282 #define DP_S_FLAGS 1
283 #define DP_S_MIN 2
284 #define DP_S_DOT 3
285 #define DP_S_MAX 4
286 #define DP_S_MOD 5
287 #define DP_S_CONV 6
288 #define DP_S_DONE 7
289
290 /* format flags - Bits */
291 #define DP_F_MINUS (1 << 0)
292 #define DP_F_PLUS (1 << 1)
293 #define DP_F_SPACE (1 << 2)
294 #define DP_F_NUM (1 << 3)
295 #define DP_F_ZERO (1 << 4)
296 #define DP_F_UP (1 << 5)
297 #define DP_F_UNSIGNED (1 << 6)
298
299 /* Conversion Flags */
300 #define DP_C_SHORT 1
301 #define DP_C_LONG 2
302 #define DP_C_LDOUBLE 3
303 #define DP_C_LONG_LONG 4
304
305 #define char_to_int(p) (p - '0')
306 #define abs_val(p) (p < 0 ? -p : p)
307
308
309 static void
310 dopr(char *buffer, size_t maxlen, const char *format, va_list args)
311 {
312 char *strvalue, ch;
313 long value;
314 long double fvalue;
315 int min = 0, max = -1, state = DP_S_DEFAULT, flags = 0, cflags = 0;
316 size_t currlen = 0;
317
318 ch = *format++;
319
320 while (state != DP_S_DONE) {
321 if ((ch == '\0') || (currlen >= maxlen))
322 state = DP_S_DONE;
323
324 switch(state) {
325 case DP_S_DEFAULT:
326 if (ch == '%')
327 state = DP_S_FLAGS;
328 else
329 dopr_outch(buffer, &currlen, maxlen, ch);
330 ch = *format++;
331 break;
332 case DP_S_FLAGS:
333 switch (ch) {
334 case '-':
335 flags |= DP_F_MINUS;
336 ch = *format++;
337 break;
338 case '+':
339 flags |= DP_F_PLUS;
340 ch = *format++;
341 break;
342 case ' ':
343 flags |= DP_F_SPACE;
344 ch = *format++;
345 break;
346 case '#':
347 flags |= DP_F_NUM;
348 ch = *format++;
349 break;
350 case '0':
351 flags |= DP_F_ZERO;
352 ch = *format++;
353 break;
354 default:
355 state = DP_S_MIN;
356 break;
357 }
358 break;
359 case DP_S_MIN:
360 if (isdigit((unsigned char)ch)) {
361 min = 10 * min + char_to_int (ch);
362 ch = *format++;
363 } else if (ch == '*') {
364 min = va_arg (args, int);
365 ch = *format++;
366 state = DP_S_DOT;
367 } else
368 state = DP_S_DOT;
369 break;
370 case DP_S_DOT:
371 if (ch == '.') {
372 state = DP_S_MAX;
373 ch = *format++;
374 } else
375 state = DP_S_MOD;
376 break;
377 case DP_S_MAX:
378 if (isdigit((unsigned char)ch)) {
379 if (max < 0)
380 max = 0;
381 max = 10 * max + char_to_int(ch);
382 ch = *format++;
383 } else if (ch == '*') {
384 max = va_arg (args, int);
385 ch = *format++;
386 state = DP_S_MOD;
387 } else
388 state = DP_S_MOD;
389 break;
390 case DP_S_MOD:
391 switch (ch) {
392 case 'h':
393 cflags = DP_C_SHORT;
394 ch = *format++;
395 break;
396 case 'l':
397 cflags = DP_C_LONG;
398 ch = *format++;
399 if (ch == 'l') {
400 cflags = DP_C_LONG_LONG;
401 ch = *format++;
402 }
403 break;
404 case 'q':
405 cflags = DP_C_LONG_LONG;
406 ch = *format++;
407 break;
408 case 'L':
409 cflags = DP_C_LDOUBLE;
410 ch = *format++;
411 break;
412 default:
413 break;
414 }
415 state = DP_S_CONV;
416 break;
417 case DP_S_CONV:
418 switch (ch) {
419 case 'd':
420 case 'i':
421 if (cflags == DP_C_SHORT)
422 value = va_arg(args, int);
423 else if (cflags == DP_C_LONG)
424 value = va_arg(args, long int);
425 else if (cflags == DP_C_LONG_LONG)
426 value = va_arg (args, long long);
427 else
428 value = va_arg (args, int);
429 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
430 break;
431 case 'o':
432 flags |= DP_F_UNSIGNED;
433 if (cflags == DP_C_SHORT)
434 value = va_arg(args, unsigned int);
435 else if (cflags == DP_C_LONG)
436 value = va_arg(args, unsigned long int);
437 else if (cflags == DP_C_LONG_LONG)
438 value = va_arg(args, unsigned long long);
439 else
440 value = va_arg(args, unsigned int);
441 fmtint(buffer, &currlen, maxlen, value, 8, min, max, flags);
442 break;
443 case 'u':
444 flags |= DP_F_UNSIGNED;
445 if (cflags == DP_C_SHORT)
446 value = va_arg(args, unsigned int);
447 else if (cflags == DP_C_LONG)
448 value = va_arg(args, unsigned long int);
449 else if (cflags == DP_C_LONG_LONG)
450 value = va_arg(args, unsigned long long);
451 else
452 value = va_arg(args, unsigned int);
453 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
454 break;
455 case 'X':
456 flags |= DP_F_UP;
457 case 'x':
458 flags |= DP_F_UNSIGNED;
459 if (cflags == DP_C_SHORT)
460 value = va_arg(args, unsigned int);
461 else if (cflags == DP_C_LONG)
462 value = va_arg(args, unsigned long int);
463 else if (cflags == DP_C_LONG_LONG)
464 value = va_arg(args, unsigned long long);
465 else
466 value = va_arg(args, unsigned int);
467 fmtint(buffer, &currlen, maxlen, value, 16, min, max, flags);
468 break;
469 case 'f':
470 if (cflags == DP_C_LDOUBLE)
471 fvalue = va_arg(args, long double);
472 else
473 fvalue = va_arg(args, double);
474 /* um, floating point? */
475 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
476 break;
477 case 'E':
478 flags |= DP_F_UP;
479 case 'e':
480 if (cflags == DP_C_LDOUBLE)
481 fvalue = va_arg(args, long double);
482 else
483 fvalue = va_arg(args, double);
484 break;
485 case 'G':
486 flags |= DP_F_UP;
487 case 'g':
488 if (cflags == DP_C_LDOUBLE)
489 fvalue = va_arg(args, long double);
490 else
491 fvalue = va_arg(args, double);
492 break;
493 case 'c':
494 dopr_outch(buffer, &currlen, maxlen, va_arg(args, int));
495 break;
496 case 's':
497 strvalue = va_arg(args, char *);
498 if (max < 0)
499 max = maxlen; /* ie, no max */
500 fmtstr(buffer, &currlen, maxlen, strvalue, flags, min, max);
501 break;
502 case 'p':
503 strvalue = va_arg(args, void *);
504 fmtint(buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
505 break;
506 case 'n':
507 if (cflags == DP_C_SHORT) {
508 short int *num;
509 num = va_arg(args, short int *);
510 *num = currlen;
511 } else if (cflags == DP_C_LONG) {
512 long int *num;
513 num = va_arg(args, long int *);
514 *num = currlen;
515 } else if (cflags == DP_C_LONG_LONG) {
516 long long *num;
517 num = va_arg(args, long long *);
518 *num = currlen;
519 } else {
520 int *num;
521 num = va_arg(args, int *);
522 *num = currlen;
523 }
524 break;
525 case '%':
526 dopr_outch(buffer, &currlen, maxlen, ch);
527 break;
528 case 'w': /* not supported yet, treat as next char */
529 ch = *format++;
530 break;
531 default: /* Unknown, skip */
532 break;
533 }
534 ch = *format++;
535 state = DP_S_DEFAULT;
536 flags = cflags = min = 0;
537 max = -1;
538 break;
539 case DP_S_DONE:
540 break;
541 default: /* hmm? */
542 break; /* some picky compilers need this */
543 }
544 }
545 if (currlen < maxlen - 1)
546 buffer[currlen] = '\0';
547 else
548 buffer[maxlen - 1] = '\0';
549 }
550
551 static void
552 fmtstr(char *buffer, size_t *currlen, size_t maxlen,
553 char *value, int flags, int min, int max)
554 {
555 int cnt = 0, padlen, strln; /* amount to pad */
556
557 if (value == 0)
558 value = "<NULL>";
559
560 for (strln = 0; value[strln]; ++strln); /* strlen */
561 padlen = min - strln;
562 if (padlen < 0)
563 padlen = 0;
564 if (flags & DP_F_MINUS)
565 padlen = -padlen; /* Left Justify */
566
567 while ((padlen > 0) && (cnt < max)) {
568 dopr_outch(buffer, currlen, maxlen, ' ');
569 --padlen;
570 ++cnt;
571 }
572 while (*value && (cnt < max)) {
573 dopr_outch(buffer, currlen, maxlen, *value++);
574 ++cnt;
575 }
576 while ((padlen < 0) && (cnt < max)) {
577 dopr_outch(buffer, currlen, maxlen, ' ');
578 ++padlen;
579 ++cnt;
580 }
581 }
582
583 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
584
585 static void
586 fmtint(char *buffer, size_t *currlen, size_t maxlen,
587 long value, int base, int min, int max, int flags)
588 {
589 unsigned long uvalue;
590 char convert[20];
591 int signvalue = 0, place = 0, caps = 0;
592 int spadlen = 0; /* amount to space pad */
593 int zpadlen = 0; /* amount to zero pad */
594
595 #define PADMAX(x,y) ((x) > (y) ? (x) : (y))
596
597 if (max < 0)
598 max = 0;
599
600 uvalue = value;
601
602 if (!(flags & DP_F_UNSIGNED)) {
603 if (value < 0) {
604 signvalue = '-';
605 uvalue = -value;
606 } else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
607 signvalue = '+';
608 else if (flags & DP_F_SPACE)
609 signvalue = ' ';
610 }
611
612 if (flags & DP_F_UP)
613 caps = 1; /* Should characters be upper case? */
614 do {
615 convert[place++] =
616 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
617 [uvalue % (unsigned)base];
618 uvalue = (uvalue / (unsigned)base );
619 } while (uvalue && (place < 20));
620 if (place == 20)
621 place--;
622 convert[place] = 0;
623
624 zpadlen = max - place;
625 spadlen = min - PADMAX(max, place) - (signvalue ? 1 : 0);
626 if (zpadlen < 0)
627 zpadlen = 0;
628 if (spadlen < 0)
629 spadlen = 0;
630 if (flags & DP_F_ZERO) {
631 zpadlen = PADMAX(zpadlen, spadlen);
632 spadlen = 0;
633 }
634 if (flags & DP_F_MINUS)
635 spadlen = -spadlen; /* Left Justifty */
636
637 /* Spaces */
638 while (spadlen > 0) {
639 dopr_outch(buffer, currlen, maxlen, ' ');
640 --spadlen;
641 }
642
643 /* Sign */
644 if (signvalue)
645 dopr_outch(buffer, currlen, maxlen, signvalue);
646
647 /* Zeros */
648 if (zpadlen > 0) {
649 while (zpadlen > 0) {
650 dopr_outch(buffer, currlen, maxlen, '0');
651 --zpadlen;
652 }
653 }
654
655 /* Digits */
656 while (place > 0)
657 dopr_outch(buffer, currlen, maxlen, convert[--place]);
658
659 /* Left Justified spaces */
660 while (spadlen < 0) {
661 dopr_outch (buffer, currlen, maxlen, ' ');
662 ++spadlen;
663 }
664 }
665
666 static long double
667 ldpow10(int exp)
668 {
669 long double result = 1;
670
671 while (exp) {
672 result *= 10;
673 exp--;
674 }
675
676 return result;
677 }
678
679 static long
680 lroundl(long double value)
681 {
682 long intpart = value;
683
684 value -= intpart;
685 if (value >= 0.5)
686 intpart++;
687
688 return intpart;
689 }
690
691 static void
692 fmtfp(char *buffer, size_t *currlen, size_t maxlen, long double fvalue,
693 int min, int max, int flags)
694 {
695 char iconvert[20], fconvert[20];
696 int signvalue = 0, iplace = 0, fplace = 0;
697 int padlen = 0; /* amount to pad */
698 int zpadlen = 0, caps = 0;
699 long intpart, fracpart;
700 long double ufvalue;
701
702 /*
703 * AIX manpage says the default is 0, but Solaris says the default
704 * is 6, and sprintf on AIX defaults to 6
705 */
706 if (max < 0)
707 max = 6;
708
709 ufvalue = abs_val(fvalue);
710
711 if (fvalue < 0)
712 signvalue = '-';
713 else if (flags & DP_F_PLUS) /* Do a sign (+/i) */
714 signvalue = '+';
715 else if (flags & DP_F_SPACE)
716 signvalue = ' ';
717
718 intpart = ufvalue;
719
720 /*
721 * Sorry, we only support 9 digits past the decimal because of our
722 * conversion method
723 */
724 if (max > 9)
725 max = 9;
726
727 /* We "cheat" by converting the fractional part to integer by
728 * multiplying by a factor of 10
729 */
730 fracpart = lroundl((ldpow10 (max)) * (ufvalue - intpart));
731
732 if (fracpart >= ldpow10 (max)) {
733 intpart++;
734 fracpart -= ldpow10 (max);
735 }
736
737 /* Convert integer part */
738 do {
739 iconvert[iplace++] =
740 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
741 [intpart % 10];
742 intpart = (intpart / 10);
743 } while(intpart && (iplace < 20));
744 if (iplace == 20)
745 iplace--;
746 iconvert[iplace] = 0;
747
748 /* Convert fractional part */
749 do {
750 fconvert[fplace++] =
751 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
752 [fracpart % 10];
753 fracpart = (fracpart / 10);
754 } while(fracpart && (fplace < 20));
755 if (fplace == 20)
756 fplace--;
757 fconvert[fplace] = 0;
758
759 /* -1 for decimal point, another -1 if we are printing a sign */
760 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
761 zpadlen = max - fplace;
762 if (zpadlen < 0)
763 zpadlen = 0;
764 if (padlen < 0)
765 padlen = 0;
766 if (flags & DP_F_MINUS)
767 padlen = -padlen; /* Left Justifty */
768
769 if ((flags & DP_F_ZERO) && (padlen > 0)) {
770 if (signvalue) {
771 dopr_outch(buffer, currlen, maxlen, signvalue);
772 --padlen;
773 signvalue = 0;
774 }
775 while (padlen > 0) {
776 dopr_outch(buffer, currlen, maxlen, '0');
777 --padlen;
778 }
779 }
780 while (padlen > 0) {
781 dopr_outch(buffer, currlen, maxlen, ' ');
782 --padlen;
783 }
784 if (signvalue)
785 dopr_outch(buffer, currlen, maxlen, signvalue);
786
787 while (iplace > 0)
788 dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
789
790 /*
791 * Decimal point. This should probably use locale to find the
792 * correct char to print out.
793 */
794 dopr_outch(buffer, currlen, maxlen, '.');
795
796 while (fplace > 0)
797 dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
798
799 while (zpadlen > 0) {
800 dopr_outch(buffer, currlen, maxlen, '0');
801 --zpadlen;
802 }
803
804 while (padlen < 0) {
805 dopr_outch(buffer, currlen, maxlen, ' ');
806 ++padlen;
807 }
808 }
809
810 static void
811 dopr_outch(char *buffer, size_t *currlen, size_t maxlen, char c)
812 {
813 if (*currlen < maxlen)
814 buffer[(*currlen)++] = c;
815 }
816 #endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */
817
818 #ifndef HAVE_VSNPRINTF
819 int
820 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
821 {
822 str[0] = 0;
823 dopr(str, count, fmt, args);
824
825 return(strlen(str));
826 }
827 #endif /* !HAVE_VSNPRINTF */
828
829 #ifndef HAVE_SNPRINTF
830 int
831 snprintf(char *str,size_t count,const char *fmt,...)
832 {
833 va_list ap;
834
835 va_start(ap, fmt);
836 (void) vsnprintf(str, count, fmt, ap);
837 va_end(ap);
838
839 return(strlen(str));
840 }
841
842 #endif /* !HAVE_SNPRINTF */