amd64 - add kvtop and add back ed(4) to AMD64_GENERIC
[dragonfly.git] / contrib / mpfr / printf.c
blobf7dc3c64401909cc837406677d894731db5f4ef6
1 /* mpfr_printf -- printf function and friends.
3 Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 /* The mpfr_printf-like functions are defined only if stdarg.h exist */
28 #ifdef HAVE_STDARG
30 #include <stdarg.h>
31 #include <errno.h>
32 #include "mpfr-impl.h"
34 #ifdef _MPFR_H_HAVE_FILE
36 /* Each printf-like function calls mpfr_vasprintf which
37 - returns the number of characters in the returned string excluding the
38 terminating null
39 - returns -1 and sets the erange flag if the number of produced characters
40 exceeds INT_MAX (in that case, also sets errno to EOVERFLOW in POSIX
41 systems) */
43 #define GET_STR_VA(sz, str, fmt, ap) \
44 do \
45 { \
46 sz = mpfr_vasprintf (&(str), fmt, ap); \
47 if (sz < 0) \
48 { \
49 if (str) \
50 mpfr_free_str (str); \
51 return -1; \
52 } \
53 } while (0)
55 #define GET_STR(sz, str, fmt) \
56 do \
57 { \
58 va_list ap; \
59 va_start(ap, fmt); \
60 sz = mpfr_vasprintf (&(str), fmt, ap); \
61 va_end (ap); \
62 if (sz < 0) \
63 { \
64 if (str) \
65 mpfr_free_str (str); \
66 return -1; \
67 } \
68 } while (0)
70 int
71 mpfr_printf (const char *fmt, ...)
73 char *str;
74 int ret;
76 GET_STR (ret, str, fmt);
77 ret = printf ("%s", str);
79 mpfr_free_str (str);
80 return ret;
83 int
84 mpfr_vprintf (const char *fmt, va_list ap)
86 char *str;
87 int ret;
89 GET_STR_VA (ret, str, fmt, ap);
90 ret = printf ("%s", str);
92 mpfr_free_str (str);
93 return ret;
97 int
98 mpfr_fprintf (FILE *fp, const char *fmt, ...)
100 char *str;
101 int ret;
103 GET_STR (ret, str, fmt);
104 ret = fprintf (fp, "%s", str);
106 mpfr_free_str (str);
107 return ret;
111 mpfr_vfprintf (FILE *fp, const char *fmt, va_list ap)
113 char *str;
114 int ret;
116 GET_STR_VA (ret, str, fmt, ap);
117 ret = fprintf (fp, "%s", str);
119 mpfr_free_str (str);
120 return ret;
122 #endif /* _MPFR_H_HAVE_FILE */
125 mpfr_sprintf (char *buf, const char *fmt, ...)
127 char *str;
128 int ret;
130 GET_STR (ret, str, fmt);
131 ret = sprintf (buf, "%s", str);
133 mpfr_free_str (str);
134 return ret;
138 mpfr_vsprintf (char *buf, const char *fmt, va_list ap)
140 char *str;
141 int ret;
143 GET_STR_VA (ret, str, fmt, ap);
144 ret = sprintf (buf, "%s", str);
146 mpfr_free_str (str);
147 return ret;
151 mpfr_snprintf (char *buf, size_t size, const char *fmt, ...)
153 char *str;
154 int ret;
155 size_t min_size;
157 GET_STR (ret, str, fmt);
159 /* C99 allows SIZE to be zero */
160 if (size != 0)
162 MPFR_ASSERTN (buf != NULL);
163 min_size = (size_t)ret < size ? (size_t)ret : size - 1;
164 strncpy (buf, str, min_size);
165 buf[min_size] = '\0';
168 mpfr_free_str (str);
169 return ret;
173 mpfr_vsnprintf (char *buf, size_t size, const char *fmt, va_list ap)
175 char *str;
176 int ret;
177 int min_size;
179 GET_STR_VA (ret, str, fmt, ap);
181 /* C99 allows SIZE to be zero */
182 if (size != 0)
184 MPFR_ASSERTN (buf != NULL);
185 min_size = (size_t)ret < size ? (size_t)ret : size - 1;
186 strncpy (buf, str, min_size);
187 buf[min_size] = '\0';
190 mpfr_free_str (str);
191 return ret;
195 mpfr_asprintf (char **pp, const char *fmt, ...)
197 int ret;
199 GET_STR (ret, *pp, fmt);
201 return ret;
203 #endif /* HAVE_STDARG */