1 /****************************************************************************
2 * Copyright (c) 1998-2001,2003 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.18 2003/08/09 21:52:04 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
) {
112 sprintf(fmt_arg
, "%d", ival
);
113 fmt_len
+= strlen(fmt_arg
);
114 if ((format
= realloc(format
, fmt_len
)) == 0) {
117 strcpy(&format
[--f
], fmt_arg
);
119 } else if (isalpha(UChar(*fmt
))) {
122 case 'Z': /* FALLTHRU */
123 case 'h': /* FALLTHRU */
124 case 'l': /* FALLTHRU */
128 case 'i': /* FALLTHRU */
129 case 'd': /* FALLTHRU */
130 case 'u': /* FALLTHRU */
131 case 'x': /* FALLTHRU */
132 case 'X': /* FALLTHRU */
135 else if (type
== 'Z')
141 case 'f': /* FALLTHRU */
142 case 'e': /* FALLTHRU */
143 case 'E': /* FALLTHRU */
144 case 'g': /* FALLTHRU */
145 case 'G': /* FALLTHRU */
157 if (prec
> (int) length
) {
158 length
= length
+ prec
;
159 buffer
= typeRealloc(char, length
, buffer
);
178 } else if (*fmt
== '.') {
180 } else if (*fmt
== '%') {
188 sprintf(buffer
, format
, ival
);
191 sprintf(buffer
, format
, fval
);
194 sprintf(buffer
, format
, pval
);
197 len
+= (int) strlen(buffer
);
211 * Wrapper for vsprintf that allocates a buffer big enough to hold the result.
213 NCURSES_EXPORT(char *)
214 _nc_printf_string(const char *fmt
, va_list ap
)
222 int len
= _nc_printf_length(fmt
, ap
);
224 if ((int) used
< len
+ 1) {
225 used
= 2 * (len
+ 1);
226 buf
= typeRealloc(char, used
, buf
);
231 vsprintf(buf
, fmt
, ap
);
236 static int rows
, cols
;
238 if (screen_lines
> rows
|| screen_columns
> cols
) {
239 if (screen_lines
> rows
)
241 if (screen_columns
> cols
)
242 cols
= screen_columns
;
243 used
= (rows
* (cols
+ 1)) + 1;
244 buf
= typeRealloc(char, used
, buf
);
249 vsnprintf(buf
, used
, fmt
, ap
); /* GNU extension */
251 vsprintf(buf
, fmt
, ap
); /* ANSI */
256 } else if (buf
!= 0) { /* see _nc_freeall() */