xref: /openbsd-src/lib/libcurses/base/safe_sprintf.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: safe_sprintf.c,v 1.4 2001/01/22 18:01:48 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Thomas E. Dickey <dickey@clark.net> 1997                        *
33  ****************************************************************************/
34 
35 #include <curses.priv.h>
36 #include <ctype.h>
37 
38 MODULE_ID("$From: safe_sprintf.c,v 1.13 2000/12/10 02:43:28 tom Exp $")
39 
40 #if USE_SAFE_SPRINTF
41 
42 typedef enum {
43     Flags, Width, Prec, Type, Format
44 } PRINTF;
45 
46 #define VA_INTGR(type) ival = va_arg(ap, type)
47 #define VA_FLOAT(type) fval = va_arg(ap, type)
48 #define VA_POINT(type) pval = (void *)va_arg(ap, type)
49 
50 /*
51  * Scan a variable-argument list for printf to determine the number of
52  * characters that would be emitted.
53  */
54 static int
55 _nc_printf_length(const char *fmt, va_list ap)
56 {
57     size_t length = BUFSIZ;
58     char *buffer;
59     char *format;
60     int len = 0;
61 
62     if (fmt == 0 || *fmt == '\0')
63 	return -1;
64     if ((format = typeMalloc(char, strlen(fmt) + 1)) == 0)
65 	  return -1;
66     if ((buffer = typeMalloc(char, length)) == 0) {
67 	free(format);
68 	return -1;
69     }
70 
71     while (*fmt != '\0') {
72 	if (*fmt == '%') {
73 	    static char dummy[] = "";
74 	    PRINTF state = Flags;
75 	    char *pval = dummy;	/* avoid const-cast */
76 	    double fval = 0.0;
77 	    int done = FALSE;
78 	    int ival = 0;
79 	    int prec = -1;
80 	    int type = 0;
81 	    int used = 0;
82 	    int width = -1;
83 	    size_t f = 0;
84 
85 	    format[f++] = *fmt;
86 	    while (*++fmt != '\0' && len >= 0 && !done) {
87 		format[f++] = *fmt;
88 
89 		if (isdigit(*fmt)) {
90 		    int num = *fmt - '0';
91 		    if (state == Flags && num != 0)
92 			state = Width;
93 		    if (state == Width) {
94 			if (width < 0)
95 			    width = 0;
96 			width = (width * 10) + num;
97 		    } else if (state == Prec) {
98 			if (prec < 0)
99 			    prec = 0;
100 			prec = (prec * 10) + num;
101 		    }
102 		} else if (*fmt == '*') {
103 		    VA_INTGR(int);
104 		    if (state == Flags)
105 			state = Width;
106 		    if (state == Width) {
107 			width = ival;
108 		    } else if (state == Prec) {
109 			prec = ival;
110 		    }
111 		    sprintf(&format[--f], "%d", ival);
112 		    f = strlen(format);
113 		} else if (isalpha(*fmt)) {
114 		    done = TRUE;
115 		    switch (*fmt) {
116 		    case 'Z':	/* FALLTHRU */
117 		    case 'h':	/* FALLTHRU */
118 		    case 'l':	/* FALLTHRU */
119 			done = FALSE;
120 			type = *fmt;
121 			break;
122 		    case 'i':	/* FALLTHRU */
123 		    case 'd':	/* FALLTHRU */
124 		    case 'u':	/* FALLTHRU */
125 		    case 'x':	/* FALLTHRU */
126 		    case 'X':	/* FALLTHRU */
127 			if (type == 'l')
128 			    VA_INTGR(long);
129 			else if (type == 'Z')
130 			    VA_INTGR(size_t);
131 			else
132 			    VA_INTGR(int);
133 			used = 'i';
134 			break;
135 		    case 'f':	/* FALLTHRU */
136 		    case 'e':	/* FALLTHRU */
137 		    case 'E':	/* FALLTHRU */
138 		    case 'g':	/* FALLTHRU */
139 		    case 'G':	/* FALLTHRU */
140 			VA_FLOAT(double);
141 			used = 'f';
142 			break;
143 		    case 'c':
144 			VA_INTGR(int);
145 			used = 'i';
146 			break;
147 		    case 's':
148 			VA_POINT(char *);
149 			if (prec < 0)
150 			    prec = strlen(pval);
151 			if (prec > (int) length) {
152 			    length = length + prec;
153 			    buffer = typeRealloc(char, length, buffer);
154 			    if (buffer == 0) {
155 				free(format);
156 				return -1;
157 			    }
158 			}
159 			used = 'p';
160 			break;
161 		    case 'p':
162 			VA_POINT(void *);
163 			used = 'p';
164 			break;
165 		    case 'n':
166 			VA_POINT(int *);
167 			used = 0;
168 			break;
169 		    default:
170 			break;
171 		    }
172 		} else if (*fmt == '.') {
173 		    state = Prec;
174 		} else if (*fmt == '%') {
175 		    done = TRUE;
176 		    used = 'p';
177 		}
178 	    }
179 	    format[f] = '\0';
180 	    switch (used) {
181 	    case 'i':
182 		sprintf(buffer, format, ival);
183 		break;
184 	    case 'f':
185 		sprintf(buffer, format, fval);
186 		break;
187 	    default:
188 		sprintf(buffer, format, pval);
189 		break;
190 	    }
191 	    len += (int) strlen(buffer);
192 	} else {
193 	    fmt++;
194 	    len++;
195 	}
196     }
197 
198     free(buffer);
199     free(format);
200     return len;
201 }
202 #endif
203 
204 /*
205  * Wrapper for vsprintf that allocates a buffer big enough to hold the result.
206  */
207 NCURSES_EXPORT(char *)
208 _nc_printf_string
209 (const char *fmt, va_list ap)
210 {
211 #if USE_SAFE_SPRINTF
212     char *buf = 0;
213     int len = _nc_printf_length(fmt, ap);
214 
215     if (len > 0) {
216 	if ((buf = typeMalloc(char, len + 1)) == 0)
217 	      return (0);
218 	vsprintf(buf, fmt, ap);
219     }
220 #else
221     static int rows, cols;
222     static char *buf;
223     static size_t len;
224 
225     if (screen_lines > rows || screen_columns > cols) {
226 	if (screen_lines > rows)
227 	    rows = screen_lines;
228 	if (screen_columns > cols)
229 	    cols = screen_columns;
230 	len = (rows * (cols + 1)) + 1;
231 	buf = typeRealloc(char, len, buf);
232 	if (buf == 0) {
233 	    return (0);
234 	}
235     }
236 
237     if (buf != 0) {
238 # if HAVE_VSNPRINTF
239 	vsnprintf(buf, len, fmt, ap);	/* GNU extension */
240 # else
241 	vsprintf(buf, fmt, ap);	/* ANSI */
242 # endif
243     }
244 #endif
245     return buf;
246 }
247