Import gmp-4.3.1
[dragonfly.git] / contrib / gmp / printf / snprntffuns.c
1 /* __gmp_snprintf_funs -- support for gmp_snprintf and gmp_vsnprintf.
2
3    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
4    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5    FUTURE GNU MP RELEASES.
6
7 Copyright 2001, 2002 Free Software Foundation, Inc.
8
9 This file is part of the GNU MP Library.
10
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23
24 #include "config.h"
25
26 #if HAVE_STDARG
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "gmp.h"
36 #include "gmp-impl.h"
37
38
39 #if ! HAVE_VSNPRINTF
40 #define vsnprintf  __gmp_replacement_vsnprintf
41 #endif
42
43
44 /* glibc 2.0.x vsnprintf returns either -1 or size-1 for an overflow, with
45    no indication how big the output would have been.  It's necessary to
46    re-run to determine that size.
47
48    "size-1" would mean sucess from a C99 vsnprintf, and the re-run is
49    unnecessary in this case, but we don't bother to try to detect what sort
50    of vsnprintf we've got.  size-1 should occur rarely in normal
51    circumstances.
52
53    vsnprintf might trash it's given ap (it does for instance in glibc 2.1.3
54    on powerpc), so copy it in case we need to use it to probe for the size
55    output that would have been produced.  Note there's no need to preserve
56    it for our callers, just for ourselves.  */
57
58 static int
59 gmp_snprintf_format (struct gmp_snprintf_t *d, const char *fmt,
60                      va_list orig_ap)
61 {
62   int      ret, step, alloc, avail;
63   va_list  ap;
64   char     *p;
65
66   ASSERT (d->size >= 0);
67
68   avail = d->size;
69   if (avail > 1)
70     {
71       va_copy (ap, orig_ap);
72       ret = vsnprintf (d->buf, avail, fmt, ap);
73       if (ret == -1)
74         {
75           ASSERT (strlen (d->buf) == avail-1);
76           ret = avail-1;
77         }
78
79       step = MIN (ret, avail-1);
80       d->size -= step;
81       d->buf += step;
82
83       if (ret != avail-1)
84         return ret;
85
86       /* probably glibc 2.0.x truncated output, probe for actual size */
87       alloc = MAX (128, ret);
88     }
89   else
90     {
91       /* no space to write anything, just probe for size */
92       alloc = 128;
93     }
94
95   do
96     {
97       alloc *= 2;
98       p = __GMP_ALLOCATE_FUNC_TYPE (alloc, char);
99       va_copy (ap, orig_ap);
100       ret = vsnprintf (p, alloc, fmt, ap);
101       (*__gmp_free_func) (p, alloc);
102     }
103   while (ret == alloc-1 || ret == -1);
104
105   return ret;
106 }
107
108 static int
109 gmp_snprintf_memory (struct gmp_snprintf_t *d, const char *str, size_t len)
110 {
111   size_t n;
112
113   ASSERT (d->size >= 0);
114
115   if (d->size > 1)
116     {
117       n = MIN (d->size-1, len);
118       memcpy (d->buf, str, n);
119       d->buf += n;
120       d->size -= n;
121     }
122   return len;
123 }
124
125 static int
126 gmp_snprintf_reps (struct gmp_snprintf_t *d, int c, int reps)
127 {
128   size_t n;
129
130   ASSERT (reps >= 0);
131   ASSERT (d->size >= 0);
132
133   if (d->size > 1)
134     {
135       n = MIN (d->size-1, reps);
136       memset (d->buf, c, n);
137       d->buf += n;
138       d->size -= n;
139     }
140   return reps;
141 }
142
143 static int
144 gmp_snprintf_final (struct gmp_snprintf_t *d)
145 {
146   if (d->size >= 1)
147     d->buf[0] = '\0';
148   return 0;
149 }
150
151 const struct doprnt_funs_t  __gmp_snprintf_funs = {
152   (doprnt_format_t) gmp_snprintf_format,
153   (doprnt_memory_t) gmp_snprintf_memory,
154   (doprnt_reps_t)   gmp_snprintf_reps,
155   (doprnt_final_t)  gmp_snprintf_final
156 };