1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey <dickey@clark.net> 1997 *
31 ****************************************************************************/
33 #include <curses.priv.h>
36 MODULE_ID("$Id: safe_sprintf.c,v 1.5 1998/02/11 12:13:57 tom Exp $")
40 typedef enum { Flags, Width, Prec, Type, Format } PRINTF;
42 #define VA_INTGR(type) ival = va_arg(ap, type)
43 #define VA_FLOAT(type) fval = va_arg(ap, type)
44 #define VA_POINT(type) pval = (void *)va_arg(ap, type)
47 * Scan a variable-argument list for printf to determine the number of
48 * characters that would be emitted.
51 _nc_printf_length(const char *fmt, va_list ap)
53 size_t length = BUFSIZ;
58 if (fmt == 0 || *fmt == '\0')
60 if ((format = malloc(strlen(fmt)+1)) == 0)
62 if ((buffer = malloc(length)) == 0) {
67 while (*fmt != '\0') {
81 while (*++fmt != '\0' && len >= 0 && !done) {
86 if (state == Flags && num != 0)
91 width = (width * 10) + num;
92 } else if (state == Prec) {
95 prec = (prec * 10) + num;
97 } else if (*fmt == '*') {
101 if (state == Width) {
103 } else if (state == Prec) {
106 sprintf(&format[--f], "%d", ival);
108 } else if (isalpha(*fmt)) {
111 case 'Z': /* FALLTHRU */
112 case 'h': /* FALLTHRU */
113 case 'l': /* FALLTHRU */
114 case 'L': /* FALLTHRU */
118 case 'i': /* FALLTHRU */
119 case 'd': /* FALLTHRU */
120 case 'u': /* FALLTHRU */
121 case 'x': /* FALLTHRU */
122 case 'X': /* FALLTHRU */
125 else if (type == 'Z')
131 case 'f': /* FALLTHRU */
132 case 'e': /* FALLTHRU */
133 case 'E': /* FALLTHRU */
134 case 'g': /* FALLTHRU */
135 case 'G': /* FALLTHRU */
137 VA_FLOAT(long double);
150 if (prec > (int)length) {
151 length = length + prec;
152 buffer = realloc(buffer, length);
171 } else if (*fmt == '.') {
173 } else if (*fmt == '%') {
181 sprintf(buffer, format, ival);
184 sprintf(buffer, format, fval);
187 sprintf(buffer, format, pval);
190 len += (int)strlen(buffer);
204 * Wrapper for vsprintf that allocates a buffer big enough to hold the result.
207 _nc_printf_string(const char *fmt, va_list ap)
211 int len = _nc_printf_length(fmt, ap);
215 vsprintf(buf, fmt, ap);
218 static int rows, cols;
222 if (screen_lines > rows || screen_columns > cols) {
223 if (screen_lines > rows) rows = screen_lines;
224 if (screen_columns > cols) cols = screen_columns;
225 len = (rows * (cols + 1)) + 1;
229 buf = realloc(buf, len);
234 vsnprintf(buf, len, fmt, ap); /* GNU extension */
236 vsprintf(buf, fmt, ap); /* ANSI */