beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / printf / snprntffuns.c
blob41a7670c6da21d965501f13564c367850dff3f39
1 /* __gmp_snprintf_funs -- support for gmp_snprintf and gmp_vsnprintf.
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.
7 Copyright 2001, 2002 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
22 later version.
24 or both in parallel, as here.
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 for more details.
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library. If not,
33 see https://www.gnu.org/licenses/. */
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <string.h>
39 #include "gmp.h"
40 #include "gmp-impl.h"
43 #if ! HAVE_VSNPRINTF
44 #define vsnprintf __gmp_replacement_vsnprintf
45 #endif
48 /* glibc 2.0.x vsnprintf returns either -1 or size-1 for an overflow, with
49 no indication how big the output would have been. It's necessary to
50 re-run to determine that size.
52 "size-1" would mean success from a C99 vsnprintf, and the re-run is
53 unnecessary in this case, but we don't bother to try to detect what sort
54 of vsnprintf we've got. size-1 should occur rarely in normal
55 circumstances.
57 vsnprintf might trash it's given ap (it does for instance in glibc 2.1.3
58 on powerpc), so copy it in case we need to use it to probe for the size
59 output that would have been produced. Note there's no need to preserve
60 it for our callers, just for ourselves. */
62 static int
63 gmp_snprintf_format (struct gmp_snprintf_t *d, const char *fmt,
64 va_list orig_ap)
66 int ret, step, alloc, avail;
67 va_list ap;
68 char *p;
70 ASSERT (d->size >= 0);
72 avail = d->size;
73 if (avail > 1)
75 va_copy (ap, orig_ap);
76 ret = vsnprintf (d->buf, avail, fmt, ap);
77 if (ret == -1)
79 ASSERT (strlen (d->buf) == avail-1);
80 ret = avail-1;
83 step = MIN (ret, avail-1);
84 d->size -= step;
85 d->buf += step;
87 if (ret != avail-1)
88 return ret;
90 /* probably glibc 2.0.x truncated output, probe for actual size */
91 alloc = MAX (128, ret);
93 else
95 /* no space to write anything, just probe for size */
96 alloc = 128;
101 alloc *= 2;
102 p = __GMP_ALLOCATE_FUNC_TYPE (alloc, char);
103 va_copy (ap, orig_ap);
104 ret = vsnprintf (p, alloc, fmt, ap);
105 (*__gmp_free_func) (p, alloc);
107 while (ret == alloc-1 || ret == -1);
109 return ret;
112 static int
113 gmp_snprintf_memory (struct gmp_snprintf_t *d, const char *str, size_t len)
115 size_t n;
117 ASSERT (d->size >= 0);
119 if (d->size > 1)
121 n = MIN (d->size-1, len);
122 memcpy (d->buf, str, n);
123 d->buf += n;
124 d->size -= n;
126 return len;
129 static int
130 gmp_snprintf_reps (struct gmp_snprintf_t *d, int c, int reps)
132 size_t n;
134 ASSERT (reps >= 0);
135 ASSERT (d->size >= 0);
137 if (d->size > 1)
139 n = MIN (d->size-1, reps);
140 memset (d->buf, c, n);
141 d->buf += n;
142 d->size -= n;
144 return reps;
147 static int
148 gmp_snprintf_final (struct gmp_snprintf_t *d)
150 if (d->size >= 1)
151 d->buf[0] = '\0';
152 return 0;
155 const struct doprnt_funs_t __gmp_snprintf_funs = {
156 (doprnt_format_t) gmp_snprintf_format,
157 (doprnt_memory_t) gmp_snprintf_memory,
158 (doprnt_reps_t) gmp_snprintf_reps,
159 (doprnt_final_t) gmp_snprintf_final