1 /****************************************************************************
2 * Copyright (c) 1998-2010,2012 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.26 2012/02/22 22:40:24 tom Exp $")
41 Flags, Width, Prec, Type, Format
44 #define VA_INTGR(type) ival = va_arg(ap, type)
45 #define VA_FLOAT(type) fval = va_arg(ap, type)
46 #define VA_POINT(type) pval = (void *)va_arg(ap, type)
49 * Scan a variable-argument list for printf to determine the number of
50 * characters that would be emitted.
53 _nc_printf_length(const char *fmt, va_list ap)
55 size_t length = BUFSIZ;
62 if (fmt == 0 || *fmt == '\0')
64 fmt_len = strlen(fmt) + 1;
65 if ((format = typeMalloc(char, fmt_len)) == 0)
67 if ((buffer = typeMalloc(char, length)) == 0) {
72 while (*fmt != '\0') {
74 static char dummy[] = "";
76 char *pval = dummy; /* avoid const-cast */
87 while (*++fmt != '\0' && len >= 0 && !done) {
90 if (isdigit(UChar(*fmt))) {
92 if (state == Flags && num != 0)
97 width = (width * 10) + num;
98 } else if (state == Prec) {
101 prec = (prec * 10) + num;
103 } else if (*fmt == '*') {
107 if (state == Width) {
109 } else if (state == Prec) {
113 _nc_SLIMIT(sizeof(fmt_arg))
115 fmt_len += strlen(fmt_arg);
116 if ((format = _nc_doalloc(format, fmt_len)) == 0) {
120 _nc_STRCPY(&format[f], fmt_arg, fmt_len - f);
122 } else if (isalpha(UChar(*fmt))) {
125 case 'Z': /* FALLTHRU */
126 case 'h': /* FALLTHRU */
127 case 'l': /* FALLTHRU */
131 case 'i': /* FALLTHRU */
132 case 'd': /* FALLTHRU */
133 case 'u': /* FALLTHRU */
134 case 'x': /* FALLTHRU */
135 case 'X': /* FALLTHRU */
138 else if (type == 'Z')
144 case 'f': /* FALLTHRU */
145 case 'e': /* FALLTHRU */
146 case 'E': /* FALLTHRU */
147 case 'g': /* FALLTHRU */
148 case 'G': /* FALLTHRU */
160 if (prec > (int) length) {
161 length = length + prec;
162 buffer = typeRealloc(char, length, buffer);
181 } else if (*fmt == '.') {
183 } else if (*fmt == '%') {
191 _nc_SPRINTF(buffer, _nc_SLIMIT(length) format, ival);
194 _nc_SPRINTF(buffer, _nc_SLIMIT(length) format, fval);
197 _nc_SPRINTF(buffer, _nc_SLIMIT(length) format, pval);
200 len += (int) strlen(buffer);
213 #define my_buffer _nc_globals.safeprint_buf
214 #define my_length _nc_globals.safeprint_used
217 * Wrapper for vsprintf that allocates a buffer big enough to hold the result.
219 NCURSES_EXPORT(char *)
220 NCURSES_SP_NAME(_nc_printf_string) (NCURSES_SP_DCLx
231 begin_va_copy(ap2, ap);
232 len = _nc_printf_length(fmt, ap2);
235 if ((int) my_length < len + 1) {
236 my_length = 2 * (len + 1);
237 my_buffer = typeRealloc(char, my_length, my_buffer);
239 if (my_buffer != 0) {
242 vsprintf(my_buffer, fmt, ap);
247 #define MyCols _nc_globals.safeprint_cols
248 #define MyRows _nc_globals.safeprint_rows
250 if (screen_lines(SP_PARM) > MyRows || screen_columns(SP_PARM) > MyCols) {
251 if (screen_lines(SP_PARM) > MyRows)
252 MyRows = screen_lines(SP_PARM);
253 if (screen_columns(SP_PARM) > MyCols)
254 MyCols = screen_columns(SP_PARM);
255 my_length = (size_t) (MyRows * (MyCols + 1)) + 1;
256 my_buffer = typeRealloc(char, my_length, my_buffer);
259 if (my_buffer != 0) {
261 vsnprintf(my_buffer, my_length, fmt, ap); /* GNU extension */
263 vsprintf(my_buffer, fmt, ap); /* ANSI */
268 } else if (my_buffer != 0) { /* see _nc_freeall() */
277 NCURSES_EXPORT(char *)
278 _nc_printf_string(const char *fmt, va_list ap)
280 return NCURSES_SP_NAME(_nc_printf_string) (CURRENT_SCREEN, fmt, ap);