~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to missing/vsnprintf.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * Copyright (c) 1990, 1993
3
 
 *      The Regents of the University of California.  All rights reserved.
4
 
 *
5
 
 * This code is derived from software contributed to Berkeley by
6
 
 * Chris Torek.
7
 
 *
8
 
 * Redistribution and use in source and binary forms, with or without
9
 
 * modification, are permitted provided that the following conditions
10
 
 * are met:
11
 
 * 1. Redistributions of source code must retain the above copyright
12
 
 *    notice, this list of conditions and the following disclaimer.
13
 
 * 2. Redistributions in binary form must reproduce the above copyright
14
 
 *    notice, this list of conditions and the following disclaimer in the
15
 
 *    documentation and/or other materials provided with the distribution.
16
 
 * 3. Neither the name of the University nor the names of its contributors
17
 
 *    may be used to endorse or promote products derived from this software
18
 
 *    without specific prior written permission.
19
 
 *
20
 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21
 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24
 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 
 * SUCH DAMAGE.
31
 
 */
32
 
 
33
 
/*
34
 
 * IMPORTANT NOTE:
35
 
 * --------------
36
 
 * From ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
37
 
 * paragraph 3 above is now null and void.
38
 
 */
39
 
 
40
 
/* SNPRINTF.C  
41
 
 * fjc 7-31-97 Modified by Mib Software to be a standalone snprintf.c module.
42
 
 *      http://www.mibsoftware.com
43
 
 * Mib Software does not warrant this software any differently than the
44
 
 * University of California, Berkeley as described above.  All warranties
45
 
 * are disclaimed.  Use this software at your own risk.
46
 
 *
47
 
 *      All code referencing FILE * functions was eliminated, since it could
48
 
 *      never be called.  All header files and necessary files are collapsed
49
 
 *      into one file, internal functions are declared static.  This should
50
 
 *      allow inclusion into libraries with less chance of namespace collisions.
51
 
 *
52
 
 *      snprintf should be the only externally visible item.
53
 
 *     
54
 
 *      As of 7-31-97 FLOATING_POINT is NOT provided.  The code is somewhat
55
 
 *        non-portable, so it is disabled.
56
 
 */
57
 
 
58
 
/* Define FLOATING_POINT to get floating point. */
59
 
/*
60
 
#define FLOATING_POINT
61
 
*/
62
 
 
63
 
#include <sys/types.h>
64
 
#define u_long unsigned long
65
 
#define u_short unsigned short
66
 
#define u_int unsigned int
67
 
 
68
 
#if !defined(HAVE_STDARG_PROTOTYPES)
69
 
#if defined(__STDC__)
70
 
#define HAVE_STDARG_PROTOTYPES 1
71
 
#endif
72
 
#endif
73
 
 
74
 
#undef __P
75
 
#if defined(HAVE_STDARG_PROTOTYPES)
76
 
# include <stdarg.h>
77
 
# if !defined(__P)
78
 
#  define __P(x) x
79
 
# endif
80
 
#else
81
 
# define __P(x) ()
82
 
# if !defined(const)
83
 
#  define const
84
 
# endif
85
 
# include <varargs.h>
86
 
#endif
87
 
#ifndef _BSD_VA_LIST_ 
88
 
#define _BSD_VA_LIST_ va_list
89
 
#endif
90
 
 
91
 
#ifdef __STDC__
92
 
# include <limits.h>
93
 
#else
94
 
# ifndef LONG_MAX
95
 
#  ifdef HAVE_LIMITS_H
96
 
#   include <limits.h>
97
 
#  else
98
 
    /* assuming 32bit(2's compliment) long */
99
 
#   define LONG_MAX 2147483647
100
 
#  endif
101
 
# endif
102
 
#endif
103
 
 
104
 
#if defined(__hpux) && !defined(__GNUC__) && !defined(__STDC__)
105
 
#define const
106
 
#endif
107
 
 
108
 
#if defined(sgi)
109
 
#undef __const
110
 
#define __const
111
 
#endif /* People who don't like const sys_error */
112
 
 
113
 
#include <stddef.h>
114
 
#if defined(__hpux) && !defined(__GNUC__) || defined(__DECC)
115
 
#include <string.h>
116
 
#endif
117
 
 
118
 
#if !defined(__CYGWIN32__) && defined(__hpux) && !defined(__GNUC__)
119
 
#include <stdlib.h>
120
 
#endif
121
 
 
122
 
#ifndef NULL
123
 
#define NULL    0
124
 
#endif
125
 
 
126
 
/*
127
 
 * NB: to fit things in six character monocase externals, the stdio
128
 
 * code uses the prefix `__s' for stdio objects, typically followed
129
 
 * by a three-character attempt at a mnemonic.
130
 
 */
131
 
 
132
 
/* stdio buffers */
133
 
struct __sbuf {
134
 
        unsigned char *_base;
135
 
        size_t  _size;
136
 
};
137
 
 
138
 
 
139
 
/*
140
 
 * stdio state variables.
141
 
 *
142
 
 * The following always hold:
143
 
 *
144
 
 *      if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
145
 
 *              _lbfsize is -_bf._size, else _lbfsize is 0
146
 
 *      if _flags&__SRD, _w is 0
147
 
 *      if _flags&__SWR, _r is 0
148
 
 *
149
 
 * This ensures that the getc and putc macros (or inline functions) never
150
 
 * try to write or read from a file that is in `read' or `write' mode.
151
 
 * (Moreover, they can, and do, automatically switch from read mode to
152
 
 * write mode, and back, on "r+" and "w+" files.)
153
 
 *
154
 
 * _lbfsize is used only to make the inline line-buffered output stream
155
 
 * code as compact as possible.
156
 
 *
157
 
 * _ub, _up, and _ur are used when ungetc() pushes back more characters
158
 
 * than fit in the current _bf, or when ungetc() pushes back a character
159
 
 * that does not match the previous one in _bf.  When this happens,
160
 
 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
161
 
 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
162
 
 *
163
 
 * NB: see WARNING above before changing the layout of this structure!
164
 
 */
165
 
typedef struct __sFILE {
166
 
        unsigned char *_p;      /* current position in (some) buffer */
167
 
#if 0
168
 
        size_t  _r;             /* read space left for getc() */
169
 
#endif
170
 
        size_t  _w;             /* write space left for putc() */
171
 
        short   _flags;         /* flags, below; this FILE is free if 0 */
172
 
        short   _file;          /* fileno, if Unix descriptor, else -1 */
173
 
        struct  __sbuf _bf;     /* the buffer (at least 1 byte, if !NULL) */
174
 
        size_t  _lbfsize;       /* 0 or -_bf._size, for inline putc */
175
 
        int     (*vwrite)(/* struct __sFILE*, struct __suio * */);
176
 
} FILE;
177
 
 
178
 
 
179
 
#define __SLBF  0x0001          /* line buffered */
180
 
#define __SNBF  0x0002          /* unbuffered */
181
 
#define __SRD   0x0004          /* OK to read */
182
 
#define __SWR   0x0008          /* OK to write */
183
 
        /* RD and WR are never simultaneously asserted */
184
 
#define __SRW   0x0010          /* open for reading & writing */
185
 
#define __SEOF  0x0020          /* found EOF */
186
 
#define __SERR  0x0040          /* found error */
187
 
#define __SMBF  0x0080          /* _buf is from malloc */
188
 
#define __SAPP  0x0100          /* fdopen()ed in append mode */
189
 
#define __SSTR  0x0200          /* this is an sprintf/snprintf string */
190
 
#define __SOPT  0x0400          /* do fseek() optimisation */
191
 
#define __SNPT  0x0800          /* do not do fseek() optimisation */
192
 
#define __SOFF  0x1000          /* set iff _offset is in fact correct */
193
 
#define __SMOD  0x2000          /* true => fgetln modified _p text */
194
 
 
195
 
 
196
 
#define EOF     (-1)
197
 
 
198
 
 
199
 
#define __sfeof(p)      (((p)->_flags & __SEOF) != 0)
200
 
#define __sferror(p)    (((p)->_flags & __SERR) != 0)
201
 
#define __sclearerr(p)  ((void)((p)->_flags &= ~(__SERR|__SEOF)))
202
 
#define __sfileno(p)    ((p)->_file)
203
 
 
204
 
#undef feof
205
 
#undef ferror
206
 
#undef clearerr
207
 
#define feof(p)         __sfeof(p)
208
 
#define ferror(p)       __sferror(p)
209
 
#define clearerr(p)     __sclearerr(p)
210
 
 
211
 
#ifndef _ANSI_SOURCE
212
 
#define fileno(p)       __sfileno(p)
213
 
#endif
214
 
 
215
 
 
216
 
/*
217
 
 * I/O descriptors for __sfvwrite().
218
 
 */
219
 
struct __siov {
220
 
        const void *iov_base;
221
 
        size_t  iov_len;
222
 
};
223
 
struct __suio {
224
 
        struct  __siov *uio_iov;
225
 
        int     uio_iovcnt;
226
 
        int     uio_resid;
227
 
};
228
 
 
229
 
#if !defined(HAVE_VSNPRINTF) || !defined(HAVE_SNPRINTF)
230
 
/*
231
 
 * Write some memory regions.  Return zero on success, EOF on error.
232
 
 *
233
 
 * This routine is large and unsightly, but most of the ugliness due
234
 
 * to the three different kinds of output buffering is handled here.
235
 
 */
236
 
static int BSD__sfvwrite(fp, uio)
237
 
        register FILE *fp;
238
 
        register struct __suio *uio;
239
 
{
240
 
        register size_t len;
241
 
        register const char *p;
242
 
        register struct __siov *iov;
243
 
        register int w;
244
 
 
245
 
        if ((len = uio->uio_resid) == 0)
246
 
                return (0);
247
 
#ifndef __hpux
248
 
#define MIN(a, b) ((a) < (b) ? (a) : (b))
249
 
#endif
250
 
#define COPY(n)   (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
251
 
 
252
 
        iov = uio->uio_iov;
253
 
        p = iov->iov_base;
254
 
        len = iov->iov_len;
255
 
        iov++;
256
 
#define GETIOV(extra_work) \
257
 
        while (len == 0) { \
258
 
                extra_work; \
259
 
                p = iov->iov_base; \
260
 
                len = iov->iov_len; \
261
 
                iov++; \
262
 
        }
263
 
        if (fp->_flags & __SNBF) {
264
 
        /* fjc 7-31-97 Will never happen.  We are working with
265
 
                                           strings only
266
 
        */
267
 
        } else if ((fp->_flags & __SLBF) == 0) {
268
 
        /*
269
 
                 * Fully buffered: fill partially full buffer, if any,
270
 
                 * and then flush.  If there is no partial buffer, write
271
 
                 * one _bf._size byte chunk directly (without copying).
272
 
                 *
273
 
                 * String output is a special case: write as many bytes
274
 
                 * as fit, but pretend we wrote everything.  This makes
275
 
                 * snprintf() return the number of bytes needed, rather
276
 
                 * than the number used, and avoids its write function
277
 
                 * (so that the write function can be invalid).
278
 
                 */
279
 
                do {
280
 
                        GETIOV(;);
281
 
                        w = fp->_w;
282
 
                        if (fp->_flags & __SSTR) {
283
 
                                if (len < w)
284
 
                                        w = len;
285
 
                                COPY(w);        /* copy MIN(fp->_w,len), */
286
 
                                fp->_w -= w;
287
 
                                fp->_p += w;
288
 
                                w = len;        /* but pretend copied all */
289
 
                        } else {
290
 
                                /* fjc 7-31-97 Will never happen.  We are working with
291
 
                                                                   strings only
292
 
                                */
293
 
                        }
294
 
                        p += w;
295
 
                        len -= w;
296
 
                } while ((uio->uio_resid -= w) != 0);
297
 
        } else {
298
 
                /* fjc 7-31-97 Will never happen.  We are working with
299
 
                                                   strings only
300
 
                */
301
 
        }
302
 
        return (0);
303
 
}
304
 
#endif
305
 
 
306
 
/*
307
 
 * Actual printf innards.
308
 
 *
309
 
 * This code is large and complicated...
310
 
 */
311
 
 
312
 
/*
313
 
 * Flush out all the vectors defined by the given uio,
314
 
 * then reset it so that it can be reused.
315
 
 */
316
 
static int
317
 
BSD__sprint(FILE *fp, register struct __suio *uio)
318
 
{
319
 
        register int err;
320
 
 
321
 
        if (uio->uio_resid == 0) {
322
 
                uio->uio_iovcnt = 0;
323
 
                return (0);
324
 
        }
325
 
        err = (*fp->vwrite)(fp, uio);
326
 
        uio->uio_resid = 0;
327
 
        uio->uio_iovcnt = 0;
328
 
        return (err);
329
 
}
330
 
 
331
 
 
332
 
/*
333
 
 * Helper function for `fprintf to unbuffered unix file': creates a
334
 
 * temporary buffer.  We only work on write-only files; this avoids
335
 
 * worries about ungetc buffers and so forth.
336
 
 */
337
 
static int
338
 
BSD__sbprintf(register FILE *fp, const char *fmt, va_list ap)
339
 
{
340
 
/* We don't support files. */
341
 
        return 0;
342
 
}
343
 
 
344
 
 
345
 
/*
346
 
 * Macros for converting digits to letters and vice versa
347
 
 */
348
 
#define to_digit(c)     ((c) - '0')
349
 
#define is_digit(c)     ((unsigned)to_digit(c) <= 9)
350
 
#define to_char(n)      ((n) + '0')
351
 
 
352
 
#ifdef _HAVE_SANE_QUAD_
353
 
/*
354
 
 * Convert an unsigned long long to ASCII for printf purposes, returning
355
 
 * a pointer to the first character of the string representation.
356
 
 * Octal numbers can be forced to have a leading zero; hex numbers
357
 
 * use the given digits.
358
 
 */
359
 
static char *
360
 
BSD__uqtoa(register u_quad_t val, char *endp, int base, int octzero, const char *xdigs)
361
 
{
362
 
        register char *cp = endp;
363
 
        register long sval;
364
 
 
365
 
        /*
366
 
         * Handle the three cases separately, in the hope of getting
367
 
         * better/faster code.
368
 
         */
369
 
        switch (base) {
370
 
        case 10:
371
 
                if (val < 10) { /* many numbers are 1 digit */
372
 
                        *--cp = to_char(val);
373
 
                        return (cp);
374
 
                }
375
 
                /*
376
 
                 * On many machines, unsigned arithmetic is harder than
377
 
                 * signed arithmetic, so we do at most one unsigned mod and
378
 
                 * divide; this is sufficient to reduce the range of
379
 
                 * the incoming value to where signed arithmetic works.
380
 
                 */
381
 
                if (val > LLONG_MAX) {
382
 
                        *--cp = to_char(val % 10);
383
 
                        sval = val / 10;
384
 
                } else
385
 
                        sval = val;
386
 
                do {
387
 
                        *--cp = to_char(sval % 10);
388
 
                        sval /= 10;
389
 
                } while (sval != 0);
390
 
                break;
391
 
 
392
 
        case 8:
393
 
                do {
394
 
                        *--cp = to_char(val & 7);
395
 
                        val >>= 3;
396
 
                } while (val);
397
 
                if (octzero && *cp != '0')
398
 
                        *--cp = '0';
399
 
                break;
400
 
 
401
 
        case 16:
402
 
                do {
403
 
                        *--cp = xdigs[val & 15];
404
 
                        val >>= 4;
405
 
                } while (val);
406
 
                break;
407
 
 
408
 
        default:                        /* oops */
409
 
                /* 
410
 
                abort();
411
 
                */
412
 
                break;  /* fjc 7-31-97.  Don't reference abort() here */
413
 
        }
414
 
        return (cp);
415
 
}
416
 
#endif /* _HAVE_SANE_QUAD_ */
417
 
 
418
 
/*
419
 
 * Convert an unsigned long to ASCII for printf purposes, returning
420
 
 * a pointer to the first character of the string representation.
421
 
 * Octal numbers can be forced to have a leading zero; hex numbers
422
 
 * use the given digits.
423
 
 */
424
 
static char *
425
 
BSD__ultoa(register u_long val, char *endp, int base, int octzero, const char *xdigs)
426
 
{
427
 
        register char *cp = endp;
428
 
        register long sval;
429
 
 
430
 
        /*
431
 
         * Handle the three cases separately, in the hope of getting
432
 
         * better/faster code.
433
 
         */
434
 
        switch (base) {
435
 
        case 10:
436
 
                if (val < 10) { /* many numbers are 1 digit */
437
 
                        *--cp = to_char(val);
438
 
                        return (cp);
439
 
                }
440
 
                /*
441
 
                 * On many machines, unsigned arithmetic is harder than
442
 
                 * signed arithmetic, so we do at most one unsigned mod and
443
 
                 * divide; this is sufficient to reduce the range of
444
 
                 * the incoming value to where signed arithmetic works.
445
 
                 */
446
 
                if (val > LONG_MAX) {
447
 
                        *--cp = to_char(val % 10);
448
 
                        sval = val / 10;
449
 
                } else
450
 
                        sval = val;
451
 
                do {
452
 
                        *--cp = to_char(sval % 10);
453
 
                        sval /= 10;
454
 
                } while (sval != 0);
455
 
                break;
456
 
 
457
 
        case 8:
458
 
                do {
459
 
                        *--cp = to_char(val & 7);
460
 
                        val >>= 3;
461
 
                } while (val);
462
 
                if (octzero && *cp != '0')
463
 
                        *--cp = '0';
464
 
                break;
465
 
 
466
 
        case 16:
467
 
                do {
468
 
                        *--cp = xdigs[val & 15];
469
 
                        val >>= 4;
470
 
                } while (val);
471
 
                break;
472
 
 
473
 
        default:                        /* oops */
474
 
                /* 
475
 
                abort();
476
 
                */
477
 
                break;  /* fjc 7-31-97.  Don't reference abort() here */
478
 
        }
479
 
        return (cp);
480
 
}
481
 
 
482
 
#ifdef FLOATING_POINT
483
 
#include <math.h>
484
 
/* #include "floatio.h" */
485
 
 
486
 
#ifndef MAXEXP
487
 
# define MAXEXP 1024
488
 
#endif
489
 
 
490
 
#ifndef MAXFRACT
491
 
# define MAXFRACT 64
492
 
#endif
493
 
 
494
 
#define BUF             (MAXEXP+MAXFRACT+1)     /* + decimal point */
495
 
#define DEFPREC         6
496
 
 
497
 
static char *cvt __P((double, int, int, char *, int *, int, int *, char *));
498
 
static int exponent __P((char *, int, int));
499
 
 
500
 
#else /* no FLOATING_POINT */
501
 
 
502
 
#define BUF             68
503
 
 
504
 
#endif /* FLOATING_POINT */
505
 
 
506
 
 
507
 
/*
508
 
 * Flags used during conversion.
509
 
 */
510
 
#define ALT             0x001           /* alternate form */
511
 
#define HEXPREFIX       0x002           /* add 0x or 0X prefix */
512
 
#define LADJUST         0x004           /* left adjustment */
513
 
#define LONGDBL         0x008           /* long double; unimplemented */
514
 
#define LONGINT         0x010           /* long integer */
515
 
 
516
 
#ifdef _HAVE_SANE_QUAD_
517
 
#define QUADINT         0x020           /* quad integer */
518
 
#endif /* _HAVE_SANE_QUAD_ */
519
 
 
520
 
#define SHORTINT        0x040           /* short integer */
521
 
#define ZEROPAD         0x080           /* zero (as opposed to blank) pad */
522
 
#define FPT             0x100           /* Floating point number */
523
 
static int
524
 
BSD_vfprintf(FILE *fp, const char *fmt0, va_list ap)
525
 
{
526
 
        register const char *fmt; /* format string */
527
 
        register int ch;        /* character from fmt */
528
 
        register int n;         /* handy integer (short term usage) */
529
 
        register const char *cp;/* handy char pointer (short term usage) */
530
 
        register struct __siov *iovp;/* for PRINT macro */
531
 
        register int flags;     /* flags as above */
532
 
        int ret;                /* return value accumulator */
533
 
        int width;              /* width from format (%8d), or 0 */
534
 
        int prec;               /* precision from format (%.3d), or -1 */
535
 
        char sign;              /* sign prefix (' ', '+', '-', or \0) */
536
 
#ifdef FLOATING_POINT
537
 
        char softsign;          /* temporary negative sign for floats */
538
 
        double _double = 0;     /* double precision arguments %[eEfgG] */
539
 
        int expt;               /* integer value of exponent */
540
 
        int expsize = 0;        /* character count for expstr */
541
 
        int ndig = 0;           /* actual number of digits returned by cvt */
542
 
        char expstr[7];         /* buffer for exponent string */
543
 
#endif
544
 
        u_long  ulval;          /* integer arguments %[diouxX] */
545
 
#ifdef _HAVE_SANE_QUAD_
546
 
        u_quad_t uqval;         /* %q integers */
547
 
#endif /* _HAVE_SANE_QUAD_ */
548
 
        int base;               /* base for [diouxX] conversion */
549
 
        int dprec;              /* a copy of prec if [diouxX], 0 otherwise */
550
 
        int fieldsz;            /* field size expanded by sign, etc */
551
 
        int realsz;             /* field size expanded by dprec */
552
 
        int size;               /* size of converted field or string */
553
 
        const char *xdigs = 0;  /* digits for [xX] conversion */
554
 
#define NIOV 8
555
 
        struct __suio uio;      /* output information: summary */
556
 
        struct __siov iov[NIOV];/* ... and individual io vectors */
557
 
        char buf[BUF];          /* space for %c, %[diouxX], %[eEfgG] */
558
 
        char ox[2];             /* space for 0x hex-prefix */
559
 
        char *const ebuf = buf + sizeof(buf);
560
 
 
561
 
        /*
562
 
         * Choose PADSIZE to trade efficiency vs. size.  If larger printf
563
 
         * fields occur frequently, increase PADSIZE and make the initializers
564
 
         * below longer.
565
 
         */
566
 
#define PADSIZE 16              /* pad chunk size */
567
 
        static const char blanks[PADSIZE] =
568
 
         {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
569
 
        static const char zeroes[PADSIZE] =
570
 
         {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
571
 
 
572
 
        /*
573
 
         * BEWARE, these `goto error' on error, and PAD uses `n'.
574
 
         */
575
 
#define PRINT(ptr, len) { \
576
 
        iovp->iov_base = (ptr); \
577
 
        iovp->iov_len = (len); \
578
 
        uio.uio_resid += (len); \
579
 
        iovp++; \
580
 
        if (++uio.uio_iovcnt >= NIOV) { \
581
 
                if (BSD__sprint(fp, &uio)) \
582
 
                        goto error; \
583
 
                iovp = iov; \
584
 
        } \
585
 
}
586
 
#define PAD(howmany, with) { \
587
 
        if ((n = (howmany)) > 0) { \
588
 
                while (n > PADSIZE) { \
589
 
                        PRINT(with, PADSIZE); \
590
 
                        n -= PADSIZE; \
591
 
                } \
592
 
                PRINT(with, n); \
593
 
        } \
594
 
}
595
 
#define FLUSH() { \
596
 
        if (uio.uio_resid && BSD__sprint(fp, &uio)) \
597
 
                goto error; \
598
 
        uio.uio_iovcnt = 0; \
599
 
        iovp = iov; \
600
 
}
601
 
 
602
 
        /*
603
 
         * To extend shorts properly, we need both signed and unsigned
604
 
         * argument extraction methods.
605
 
         */
606
 
#define SARG() \
607
 
        (flags&LONGINT ? va_arg(ap, long) : \
608
 
            flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
609
 
            (long)va_arg(ap, int))
610
 
#define UARG() \
611
 
        (flags&LONGINT ? va_arg(ap, u_long) : \
612
 
            flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
613
 
            (u_long)va_arg(ap, u_int))
614
 
 
615
 
        /* optimise fprintf(stderr) (and other unbuffered Unix files) */
616
 
        if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
617
 
            fp->_file >= 0)
618
 
                return (BSD__sbprintf(fp, fmt0, ap));
619
 
 
620
 
        fmt = fmt0;
621
 
        uio.uio_iov = iovp = iov;
622
 
        uio.uio_resid = 0;
623
 
        uio.uio_iovcnt = 0;
624
 
        ret = 0;
625
 
        xdigs = 0;
626
 
 
627
 
        /*
628
 
         * Scan the format for conversions (`%' character).
629
 
         */
630
 
        for (;;) {
631
 
                for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
632
 
                        /* void */;
633
 
                if ((n = fmt - cp) != 0) {
634
 
                        PRINT(cp, n);
635
 
                        ret += n;
636
 
                }
637
 
                if (ch == '\0')
638
 
                        goto done;
639
 
                fmt++;          /* skip over '%' */
640
 
 
641
 
                flags = 0;
642
 
                dprec = 0;
643
 
                width = 0;
644
 
                prec = -1;
645
 
                sign = '\0';
646
 
 
647
 
rflag:          ch = *fmt++;
648
 
reswitch:       switch (ch) {
649
 
                case ' ':
650
 
                        /*
651
 
                         * ``If the space and + flags both appear, the space
652
 
                         * flag will be ignored.''
653
 
                         *      -- ANSI X3J11
654
 
                         */
655
 
                        if (!sign)
656
 
                                sign = ' ';
657
 
                        goto rflag;
658
 
                case '#':
659
 
                        flags |= ALT;
660
 
                        goto rflag;
661
 
                case '*':
662
 
                        /*
663
 
                         * ``A negative field width argument is taken as a
664
 
                         * - flag followed by a positive field width.''
665
 
                         *      -- ANSI X3J11
666
 
                         * They don't exclude field widths read from args.
667
 
                         */
668
 
                        if ((width = va_arg(ap, int)) >= 0)
669
 
                                goto rflag;
670
 
                        width = -width;
671
 
                        /* FALLTHROUGH */
672
 
                case '-':
673
 
                        flags |= LADJUST;
674
 
                        goto rflag;
675
 
                case '+':
676
 
                        sign = '+';
677
 
                        goto rflag;
678
 
                case '.':
679
 
                        if ((ch = *fmt++) == '*') {
680
 
                                n = va_arg(ap, int);
681
 
                                prec = n < 0 ? -1 : n;
682
 
                                goto rflag;
683
 
                        }
684
 
                        n = 0;
685
 
                        while (is_digit(ch)) {
686
 
                                n = 10 * n + to_digit(ch);
687
 
                                ch = *fmt++;
688
 
                        }
689
 
                        prec = n < 0 ? -1 : n;
690
 
                        goto reswitch;
691
 
                case '0':
692
 
                        /*
693
 
                         * ``Note that 0 is taken as a flag, not as the
694
 
                         * beginning of a field width.''
695
 
                         *      -- ANSI X3J11
696
 
                         */
697
 
                        flags |= ZEROPAD;
698
 
                        goto rflag;
699
 
                case '1': case '2': case '3': case '4':
700
 
                case '5': case '6': case '7': case '8': case '9':
701
 
                        n = 0;
702
 
                        do {
703
 
                                n = 10 * n + to_digit(ch);
704
 
                                ch = *fmt++;
705
 
                        } while (is_digit(ch));
706
 
                        width = n;
707
 
                        goto reswitch;
708
 
#ifdef FLOATING_POINT
709
 
                case 'L':
710
 
                        flags |= LONGDBL;
711
 
                        goto rflag;
712
 
#endif
713
 
                case 'h':
714
 
                        flags |= SHORTINT;
715
 
                        goto rflag;
716
 
#if SIZEOF_PTRDIFF_T == SIZEOF_LONG
717
 
                case 't':
718
 
#endif
719
 
                case 'l':
720
 
                        flags |= LONGINT;
721
 
                        goto rflag;
722
 
#ifdef _HAVE_SANE_QUAD_
723
 
#if SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
724
 
                case 't':
725
 
#endif
726
 
                case 'q':
727
 
                        flags |= QUADINT;
728
 
                        goto rflag;
729
 
#endif /* _HAVE_SANE_QUAD_ */
730
 
                case 'c':
731
 
                        cp = buf;
732
 
                        *buf = (char)va_arg(ap, int);
733
 
                        size = 1;
734
 
                        sign = '\0';
735
 
                        break;
736
 
                case 'D':
737
 
                        flags |= LONGINT;
738
 
                        /*FALLTHROUGH*/
739
 
                case 'd':
740
 
                case 'i':
741
 
#ifdef _HAVE_SANE_QUAD_
742
 
                        if (flags & QUADINT) {
743
 
                                uqval = va_arg(ap, quad_t);
744
 
                                if ((quad_t)uqval < 0) {
745
 
                                        uqval = -uqval;
746
 
                                        sign = '-';
747
 
                                }
748
 
                        } else
749
 
#endif /* _HAVE_SANE_QUAD_ */
750
 
                        {
751
 
                                ulval = SARG();
752
 
                                if ((long)ulval < 0) {
753
 
                                        ulval = -ulval;
754
 
                                        sign = '-';
755
 
                                }
756
 
                        }
757
 
                        base = 10;
758
 
                        goto number;
759
 
#ifdef FLOATING_POINT
760
 
                case 'e':               /* anomalous precision */
761
 
                case 'E':
762
 
                        if (prec != 0)
763
 
                                flags |= ALT;
764
 
                        prec = (prec == -1) ?
765
 
                                DEFPREC + 1 : prec + 1;
766
 
                        /* FALLTHROUGH */
767
 
                        goto fp_begin;
768
 
                case 'f':               /* always print trailing zeroes */
769
 
                        if (prec != 0)
770
 
                                flags |= ALT;
771
 
                case 'g':
772
 
                case 'G':
773
 
                        if (prec == -1)
774
 
                                prec = DEFPREC;
775
 
fp_begin:               _double = va_arg(ap, double);
776
 
                        /* do this before tricky precision changes */
777
 
                        if (isinf(_double)) {
778
 
                                if (_double < 0)
779
 
                                        sign = '-';
780
 
                                cp = "Inf";
781
 
                                size = 3;
782
 
                                break;
783
 
                        }
784
 
                        if (isnan(_double)) {
785
 
                                cp = "NaN";
786
 
                                size = 3;
787
 
                                break;
788
 
                        }
789
 
                        flags |= FPT;
790
 
                        cp = cvt(_double, prec, flags, &softsign,
791
 
                                &expt, ch, &ndig, buf);
792
 
                        if (ch == 'g' || ch == 'G') {
793
 
                                if (expt <= -4 || (expt > prec && expt > 1))
794
 
                                        ch = (ch == 'g') ? 'e' : 'E';
795
 
                                else
796
 
                                        ch = 'g';
797
 
                        } 
798
 
                        if (ch <= 'e') {        /* 'e' or 'E' fmt */
799
 
                                --expt;
800
 
                                expsize = exponent(expstr, expt, ch);
801
 
                                size = expsize + ndig;
802
 
                                if (ndig > 1 || flags & ALT)
803
 
                                        ++size;
804
 
                        } else if (ch == 'f') {         /* f fmt */
805
 
                                if (expt > 0) {
806
 
                                        size = expt;
807
 
                                        if (prec || flags & ALT)
808
 
                                                size += prec + 1;
809
 
                                } else if (!prec) { /* "0" */
810
 
                                        size = 1;
811
 
                                } else  /* "0.X" */
812
 
                                        size = prec + 2;
813
 
                        } else if (expt >= ndig) {      /* fixed g fmt */
814
 
                                size = expt;
815
 
                                if (flags & ALT)
816
 
                                        ++size;
817
 
                        } else
818
 
                                size = ndig + (expt > 0 ?
819
 
                                        1 : 2 - expt);
820
 
 
821
 
                        if (softsign)
822
 
                                sign = '-';
823
 
                        break;
824
 
#endif /* FLOATING_POINT */
825
 
                case 'n':
826
 
#ifdef _HAVE_SANE_QUAD_
827
 
                        if (flags & QUADINT)
828
 
                                *va_arg(ap, quad_t *) = ret;
829
 
                        else if (flags & LONGINT)
830
 
#else /* _HAVE_SANE_QUAD_ */
831
 
                        if (flags & LONGINT)
832
 
#endif /* _HAVE_SANE_QUAD_ */
833
 
                                *va_arg(ap, long *) = ret;
834
 
                        else if (flags & SHORTINT)
835
 
                                *va_arg(ap, short *) = ret;
836
 
                        else
837
 
                                *va_arg(ap, int *) = ret;
838
 
                        continue;       /* no output */
839
 
                case 'O':
840
 
                        flags |= LONGINT;
841
 
                        /*FALLTHROUGH*/
842
 
                case 'o':
843
 
#ifdef _HAVE_SANE_QUAD_
844
 
                        if (flags & QUADINT)
845
 
                                uqval = va_arg(ap, u_quad_t);
846
 
                        else
847
 
#endif /* _HAVE_SANE_QUAD_ */
848
 
                                ulval = UARG();
849
 
                        base = 8;
850
 
                        goto nosign;
851
 
                case 'p':
852
 
                        /*
853
 
                         * ``The argument shall be a pointer to void.  The
854
 
                         * value of the pointer is converted to a sequence
855
 
                         * of printable characters, in an implementation-
856
 
                         * defined manner.''
857
 
                         *      -- ANSI X3J11
858
 
                         */
859
 
                        prec = sizeof(void*)*CHAR_BIT/4;
860
 
#ifdef _HAVE_LLP64_
861
 
                        uqval = (u_long)va_arg(ap, void *);
862
 
                        flags = (flags) | QUADINT | HEXPREFIX;
863
 
#else
864
 
                        ulval = (u_long)va_arg(ap, void *);
865
 
#ifdef _HAVE_SANE_QUAD_
866
 
                        flags = (flags & ~QUADINT) | HEXPREFIX;
867
 
#else /* _HAVE_SANE_QUAD_ */
868
 
                        flags = (flags) | HEXPREFIX;
869
 
#endif /* _HAVE_SANE_QUAD_ */
870
 
#endif
871
 
                        base = 16;
872
 
                        xdigs = "0123456789abcdef";
873
 
                        ch = 'x';
874
 
                        goto nosign;
875
 
                case 's':
876
 
                        if ((cp = va_arg(ap, char *)) == NULL)
877
 
                                cp = "(null)";
878
 
                        if (prec >= 0) {
879
 
                                /*
880
 
                                 * can't use strlen; can only look for the
881
 
                                 * NUL in the first `prec' characters, and
882
 
                                 * strlen() will go further.
883
 
                                 */
884
 
                                const char *p = (char *)memchr(cp, 0, prec);
885
 
 
886
 
                                if (p != NULL) {
887
 
                                        size = p - cp;
888
 
                                        if (size > prec)
889
 
                                                size = prec;
890
 
                                } else
891
 
                                        size = prec;
892
 
                        } else
893
 
                                size = strlen(cp);
894
 
                        sign = '\0';
895
 
                        break;
896
 
                case 'U':
897
 
                        flags |= LONGINT;
898
 
                        /*FALLTHROUGH*/
899
 
                case 'u':
900
 
#ifdef _HAVE_SANE_QUAD_
901
 
                        if (flags & QUADINT)
902
 
                                uqval = va_arg(ap, u_quad_t);
903
 
                        else
904
 
#endif /* _HAVE_SANE_QUAD_ */
905
 
                                ulval = UARG();
906
 
                        base = 10;
907
 
                        goto nosign;
908
 
                case 'X':
909
 
                        xdigs = "0123456789ABCDEF";
910
 
                        goto hex;
911
 
                case 'x':
912
 
                        xdigs = "0123456789abcdef";
913
 
hex:
914
 
#ifdef _HAVE_SANE_QUAD_
915
 
                        if (flags & QUADINT)
916
 
                                uqval = va_arg(ap, u_quad_t);
917
 
                        else
918
 
#endif /* _HAVE_SANE_QUAD_ */
919
 
                                ulval = UARG();
920
 
                        base = 16;
921
 
                        /* leading 0x/X only if non-zero */
922
 
                        if (flags & ALT &&
923
 
#ifdef _HAVE_SANE_QUAD_
924
 
                            (flags & QUADINT ? uqval != 0 : ulval != 0)
925
 
#else /* _HAVE_SANE_QUAD_ */
926
 
                            ulval != 0
927
 
#endif /* _HAVE_SANE_QUAD_ */
928
 
                            )
929
 
                                flags |= HEXPREFIX;
930
 
 
931
 
                        /* unsigned conversions */
932
 
nosign:                 sign = '\0';
933
 
                        /*
934
 
                         * ``... diouXx conversions ... if a precision is
935
 
                         * specified, the 0 flag will be ignored.''
936
 
                         *      -- ANSI X3J11
937
 
                         */
938
 
number:                 if ((dprec = prec) >= 0)
939
 
                                flags &= ~ZEROPAD;
940
 
 
941
 
                        /*
942
 
                         * ``The result of converting a zero value with an
943
 
                         * explicit precision of zero is no characters.''
944
 
                         *      -- ANSI X3J11
945
 
                         */
946
 
#ifdef _HAVE_SANE_QUAD_
947
 
                        if (flags & QUADINT) {
948
 
                                if (uqval != 0 || prec != 0)
949
 
                                        cp = BSD__uqtoa(uqval, ebuf, base,
950
 
                                            flags & ALT, xdigs);
951
 
                        } else
952
 
#else /* _HAVE_SANE_QUAD_ */
953
 
#endif /* _HAVE_SANE_QUAD_ */
954
 
                        {
955
 
                                if (ulval != 0 || prec != 0)
956
 
                                        cp = BSD__ultoa(ulval, ebuf, base,
957
 
                                            flags & ALT, xdigs);
958
 
                        }
959
 
                        size = ebuf - cp;
960
 
                        break;
961
 
                default:        /* "%?" prints ?, unless ? is NUL */
962
 
                        if (ch == '\0')
963
 
                                goto done;
964
 
                        /* pretend it was %c with argument ch */
965
 
                        cp = buf;
966
 
                        *buf = ch;
967
 
                        size = 1;
968
 
                        sign = '\0';
969
 
                        break;
970
 
                }
971
 
 
972
 
                /*
973
 
                 * All reasonable formats wind up here.  At this point, `cp'
974
 
                 * points to a string which (if not flags&LADJUST) should be
975
 
                 * padded out to `width' places.  If flags&ZEROPAD, it should
976
 
                 * first be prefixed by any sign or other prefix; otherwise,
977
 
                 * it should be blank padded before the prefix is emitted.
978
 
                 * After any left-hand padding and prefixing, emit zeroes
979
 
                 * required by a decimal [diouxX] precision, then print the
980
 
                 * string proper, then emit zeroes required by any leftover
981
 
                 * floating precision; finally, if LADJUST, pad with blanks.
982
 
                 *
983
 
                 * Compute actual size, so we know how much to pad.
984
 
                 * fieldsz excludes decimal prec; realsz includes it.
985
 
                 */
986
 
                fieldsz = size;
987
 
                if (sign)
988
 
                        fieldsz++;
989
 
                else if (flags & HEXPREFIX)
990
 
                        fieldsz += 2;
991
 
                realsz = dprec > fieldsz ? dprec : fieldsz;
992
 
 
993
 
                /* right-adjusting blank padding */
994
 
                if ((flags & (LADJUST|ZEROPAD)) == 0)
995
 
                        PAD(width - realsz, blanks);
996
 
 
997
 
                /* prefix */
998
 
                if (sign) {
999
 
                        PRINT(&sign, 1);
1000
 
                } else if (flags & HEXPREFIX) {
1001
 
                        ox[0] = '0';
1002
 
                        ox[1] = ch;
1003
 
                        PRINT(ox, 2);
1004
 
                }
1005
 
 
1006
 
                /* right-adjusting zero padding */
1007
 
                if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1008
 
                        PAD(width - realsz, zeroes);
1009
 
 
1010
 
                /* leading zeroes from decimal precision */
1011
 
                PAD(dprec - fieldsz, zeroes);
1012
 
 
1013
 
                /* the string or number proper */
1014
 
#ifdef FLOATING_POINT
1015
 
                if ((flags & FPT) == 0) {
1016
 
                        PRINT(cp, size);
1017
 
                } else {        /* glue together f_p fragments */
1018
 
                        if (ch >= 'f') {        /* 'f' or 'g' */
1019
 
                                if (_double == 0) {
1020
 
                                /* kludge for __dtoa irregularity */
1021
 
                                        if (ndig <= 1 &&
1022
 
                                            (flags & ALT) == 0) {
1023
 
                                                PRINT("0", 1);
1024
 
                                        } else {
1025
 
                                                PRINT("0.", 2);
1026
 
                                                PAD(ndig - 1, zeroes);
1027
 
                                        }
1028
 
                                } else if (expt == 0 && ndig == 0 && (flags & ALT) == 0) {
1029
 
                                        PRINT("0", 1);
1030
 
                                } else if (expt <= 0) {
1031
 
                                        PRINT("0.", 2);
1032
 
                                        PAD(-expt, zeroes);
1033
 
                                        PRINT(cp, ndig);
1034
 
                                } else if (expt >= ndig) {
1035
 
                                        PRINT(cp, ndig);
1036
 
                                        PAD(expt - ndig, zeroes);
1037
 
                                        if (flags & ALT)
1038
 
                                                PRINT(".", 1);
1039
 
                                } else {
1040
 
                                        PRINT(cp, expt);
1041
 
                                        cp += expt;
1042
 
                                        PRINT(".", 1);
1043
 
                                        PRINT(cp, ndig-expt);
1044
 
                                }
1045
 
                        } else {        /* 'e' or 'E' */
1046
 
                                if (ndig > 1 || flags & ALT) {
1047
 
                                        ox[0] = *cp++;
1048
 
                                        ox[1] = '.';
1049
 
                                        PRINT(ox, 2);
1050
 
                                        if (_double || flags & ALT == 0) {
1051
 
                                                PRINT(cp, ndig-1);
1052
 
                                        } else  /* 0.[0..] */
1053
 
                                                /* __dtoa irregularity */
1054
 
                                                PAD(ndig - 1, zeroes);
1055
 
                                } else  /* XeYYY */
1056
 
                                        PRINT(cp, 1);
1057
 
                                PRINT(expstr, expsize);
1058
 
                        }
1059
 
                }
1060
 
#else
1061
 
                PRINT(cp, size);
1062
 
#endif
1063
 
                /* left-adjusting padding (always blank) */
1064
 
                if (flags & LADJUST)
1065
 
                        PAD(width - realsz, blanks);
1066
 
 
1067
 
                /* finally, adjust ret */
1068
 
                ret += width > realsz ? width : realsz;
1069
 
 
1070
 
                FLUSH();        /* copy out the I/O vectors */
1071
 
        }
1072
 
done:
1073
 
        FLUSH();
1074
 
error:
1075
 
        return (__sferror(fp) ? EOF : ret);
1076
 
        /* NOTREACHED */
1077
 
}
1078
 
 
1079
 
#ifdef FLOATING_POINT
1080
 
 
1081
 
extern char *BSD__dtoa __P((double, int, int, int *, int *, char **));
1082
 
 
1083
 
static char *
1084
 
cvt(value, ndigits, flags, sign, decpt, ch, length, buf)
1085
 
        double value;
1086
 
        int ndigits, flags, *decpt, ch, *length;
1087
 
        char *sign, *buf;
1088
 
{
1089
 
        int mode, dsgn;
1090
 
        char *digits, *bp, *rve;
1091
 
 
1092
 
        if (ch == 'f')
1093
 
                mode = 3;
1094
 
        else {
1095
 
                mode = 2;
1096
 
        }
1097
 
        if (value < 0) {
1098
 
                value = -value;
1099
 
                *sign = '-';
1100
 
        } else if (value == 0.0 && 1.0/value < 0) {
1101
 
            *sign = '-';
1102
 
        } else {
1103
 
            *sign = '\000';
1104
 
        }
1105
 
        digits = BSD__dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
1106
 
        memcpy(buf, digits, rve - digits);
1107
 
        xfree(digits);
1108
 
        rve = buf + (rve - digits);
1109
 
        digits = buf;
1110
 
        if (flags & ALT) {      /* Print trailing zeros */
1111
 
                bp = digits + ndigits;
1112
 
                if (ch == 'f') {
1113
 
                        if (*digits == '0' && value)
1114
 
                                *decpt = -ndigits + 1;
1115
 
                        bp += *decpt;
1116
 
                }
1117
 
                if (value == 0) /* kludge for __dtoa irregularity */
1118
 
                        rve = bp;
1119
 
                while (rve < bp)
1120
 
                        *rve++ = '0';
1121
 
        }
1122
 
        *length = rve - digits;
1123
 
        return (digits);
1124
 
}
1125
 
 
1126
 
static int
1127
 
exponent(p0, exp, fmtch)
1128
 
        char *p0;
1129
 
        int exp, fmtch;
1130
 
{
1131
 
        register char *p, *t;
1132
 
        char expbuf[MAXEXP];
1133
 
 
1134
 
        p = p0;
1135
 
        *p++ = fmtch;
1136
 
        if (exp < 0) {
1137
 
                exp = -exp;
1138
 
                *p++ = '-';
1139
 
        }
1140
 
        else
1141
 
                *p++ = '+';
1142
 
        t = expbuf + MAXEXP;
1143
 
        if (exp > 9) {
1144
 
                do {
1145
 
                        *--t = to_char(exp % 10);
1146
 
                } while ((exp /= 10) > 9);
1147
 
                *--t = to_char(exp);
1148
 
                for (; t < expbuf + MAXEXP; *p++ = *t++);
1149
 
        }
1150
 
        else {
1151
 
                *p++ = '0';
1152
 
                *p++ = to_char(exp);
1153
 
        }
1154
 
        return (p - p0);
1155
 
}
1156
 
#endif /* FLOATING_POINT */
1157
 
 
1158
 
#ifndef HAVE_VSNPRINTF
1159
 
int
1160
 
vsnprintf(str, n, fmt, ap)
1161
 
        char *str;
1162
 
        size_t n;
1163
 
        const char *fmt;
1164
 
        _BSD_VA_LIST_ ap;
1165
 
{
1166
 
        int ret;
1167
 
        FILE f;
1168
 
 
1169
 
        if ((int)n < 1)
1170
 
                return (EOF);
1171
 
        f._flags = __SWR | __SSTR;
1172
 
        f._bf._base = f._p = (unsigned char *)str;
1173
 
        f._bf._size = f._w = n - 1;
1174
 
        f.vwrite = BSD__sfvwrite;
1175
 
        ret = BSD_vfprintf(&f, fmt, ap);
1176
 
        *f._p = 0;
1177
 
        return (ret);
1178
 
}
1179
 
#endif
1180
 
 
1181
 
#if defined(LIBC_SCCS) && !defined(lint)
1182
 
static char sccsid[] = "@(#)snprintf.c  8.1 (Berkeley) 6/4/93";
1183
 
#endif /* LIBC_SCCS and not lint */
1184
 
 
1185
 
#ifndef HAVE_SNPRINTF
1186
 
int
1187
 
#if defined(HAVE_STDARG_PROTOTYPES)
1188
 
snprintf(char *str, size_t n, char const *fmt, ...)
1189
 
#else
1190
 
snprintf(str, n, fmt, va_alist)
1191
 
char *str, *fmt;
1192
 
size_t n;
1193
 
va_dcl
1194
 
#endif
1195
 
{
1196
 
        int ret;
1197
 
        va_list ap;
1198
 
        FILE f;
1199
 
 
1200
 
        if ((int)n < 1)
1201
 
                return (EOF);
1202
 
 
1203
 
#if defined(HAVE_STDARG_PROTOTYPES)
1204
 
        va_start(ap, fmt);
1205
 
#else
1206
 
        va_start(ap);
1207
 
#endif
1208
 
        f._flags = __SWR | __SSTR;
1209
 
        f._bf._base = f._p = (unsigned char *)str;
1210
 
        f._bf._size = f._w = n - 1;
1211
 
        f.vwrite = BSD__sfvwrite;
1212
 
        ret = BSD_vfprintf(&f, fmt, ap);
1213
 
        *f._p = 0;
1214
 
        va_end(ap);
1215
 
        return (ret);
1216
 
}
1217
 
#endif