The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

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