The R Project SVN R

Rev

Rev 21789 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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