1 /****************************************************************************
2 * Copyright 2018,2020 Thomas E. Dickey *
3 * Copyright 1998-2012,2013 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Thomas E. Dickey 1997-on *
32 ****************************************************************************/
34 #include <curses.priv.h>
37 MODULE_ID("$Id: safe_sprintf.c,v 1.33 2020/02/02 23:34:34 tom Exp $")
42 Flags, Width, Prec, Type, Format
45 #define VA_INTGR(type) ival = (int) va_arg(ap, type)
46 #define VA_FLOAT(type) fval = va_arg(ap, type)
47 #define VA_POINT(type) pval = (void *)va_arg(ap, type)
50 * Scan a variable-argument list for printf to determine the number of
51 * characters that would be emitted.
54 _nc_printf_length(const char *fmt, va_list ap)
56 size_t length = BUFSIZ;
63 if (fmt == 0 || *fmt == '\0')
65 fmt_len = strlen(fmt) + 1;
66 if ((format = typeMalloc(char, fmt_len)) == 0)
68 if ((buffer = typeMalloc(char, length)) == 0) {
73 while (*fmt != '\0') {
75 static char dummy[] = "";
77 char *pval = dummy; /* avoid const-cast */
88 while (*++fmt != '\0' && len >= 0 && !done) {
91 if (isdigit(UChar(*fmt))) {
93 if (state == Flags && num != 0)
98 width = (width * 10) + num;
99 } else if (state == Prec) {
102 prec = (prec * 10) + num;
104 } else if (*fmt == '*') {
108 if (state == Width) {
110 } else if (state == Prec) {
114 _nc_SLIMIT(sizeof(fmt_arg))
116 fmt_len += strlen(fmt_arg);
117 if ((format = _nc_doalloc(format, fmt_len)) == 0) {
122 _nc_STRCPY(&format[f], fmt_arg, fmt_len - f);
124 } else if (isalpha(UChar(*fmt))) {
127 case 'Z': /* FALLTHRU */
128 case 'h': /* FALLTHRU */
129 case 'l': /* FALLTHRU */
133 case 'i': /* FALLTHRU */
134 case 'd': /* FALLTHRU */
135 case 'u': /* FALLTHRU */
136 case 'x': /* FALLTHRU */
137 case 'X': /* FALLTHRU */
140 else if (type == 'Z')
146 case 'f': /* FALLTHRU */
147 case 'e': /* FALLTHRU */
148 case 'E': /* FALLTHRU */
149 case 'g': /* FALLTHRU */
150 case 'G': /* FALLTHRU */
161 prec = (int) strlen(pval);
162 if (prec > (int) length) {
163 length = length + (size_t) prec;
164 buffer = typeRealloc(char, length, buffer);
183 } else if (*fmt == '.') {
185 } else if (*fmt == '%') {
193 _nc_SPRINTF(buffer, _nc_SLIMIT(length) format, ival);
196 _nc_SPRINTF(buffer, _nc_SLIMIT(length) format, fval);
199 _nc_SPRINTF(buffer, _nc_SLIMIT(length) format, pval);
202 len += (int) strlen(buffer);
215 #define my_buffer _nc_globals.safeprint_buf
216 #define my_length _nc_globals.safeprint_used
219 * Wrapper for vsprintf that allocates a buffer big enough to hold the result.
221 NCURSES_EXPORT(char *)
222 NCURSES_SP_NAME(_nc_printf_string) (NCURSES_SP_DCLx
228 if (SP_PARM != 0 && fmt != 0) {
233 begin_va_copy(ap2, ap);
234 len = _nc_printf_length(fmt, ap2);
237 if ((int) my_length < len + 1) {
238 my_length = (size_t) (2 * (len + 1));
239 my_buffer = typeRealloc(char, my_length, my_buffer);
241 if (my_buffer != 0) {
244 vsprintf(my_buffer, fmt, ap);
249 #define MyCols _nc_globals.safeprint_cols
250 #define MyRows _nc_globals.safeprint_rows
252 if (screen_lines(SP_PARM) > MyRows || screen_columns(SP_PARM) > MyCols) {
253 if (screen_lines(SP_PARM) > MyRows)
254 MyRows = screen_lines(SP_PARM);
255 if (screen_columns(SP_PARM) > MyCols)
256 MyCols = screen_columns(SP_PARM);
257 my_length = (size_t) (MyRows * (MyCols + 1)) + 1;
258 my_buffer = typeRealloc(char, my_length, my_buffer);
261 if (my_buffer != 0) {
263 vsnprintf(my_buffer, my_length, fmt, ap); /* SUSv2, 1997 */
265 vsprintf(my_buffer, fmt, ap); /* ISO/ANSI C, 1989 */
270 } else if (my_buffer != 0) { /* see _nc_freeall() */
279 NCURSES_EXPORT(char *)
280 _nc_printf_string(const char *fmt, va_list ap)
282 return NCURSES_SP_NAME(_nc_printf_string) (CURRENT_SCREEN, fmt, ap);