xref: /netbsd-src/libexec/ld.elf_so/xprintf.c (revision d42a77727be99633563b3ae4d52fbeb25623d1c3)
1 /*	$NetBSD: xprintf.c,v 1.23 2021/03/06 20:09:39 christos Exp $	 */
2 
3 /*
4  * Copyright 1996 Matt Thomas <matt@3am-software.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: xprintf.c,v 1.23 2021/03/06 20:09:39 christos Exp $");
33 #endif /* not lint */
34 
35 #include <string.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 
41 #include "rtldenv.h"
42 
43 #ifdef RTLD_LOADER
44 #define	SZ_LONG		0x01
45 #define	SZ_UNSIGNED	0x02
46 #define SZ_SIZE_T	0x04
47 
48 /*
49  * Non-mallocing printf, for use by malloc and rtld itself.
50  * This avoids putting in most of stdio.
51  *
52  * deals with formats %x, %p, %s, and %d.
53  */
54 size_t
xvsnprintf(char * buf,size_t buflen,const char * fmt,va_list ap)55 xvsnprintf(char *buf, size_t buflen, const char *fmt, va_list ap)
56 {
57 	char *bp = buf;
58 	char *const ep = buf + buflen - 4;
59 	int size, prec;
60 
61 	while (*fmt != '\0' && bp < ep) {
62 		switch (*fmt) {
63 		case '\\':{
64 			if (fmt[1] != '\0')
65 				*bp++ = *++fmt;
66 			continue;
67 		}
68 		case '%':{
69 			size = 0;
70 			prec = -1;
71 	rflag:		switch (fmt[1]) {
72 			case '*':
73 				prec = va_arg(ap, int);
74 				/* FALLTHROUGH */
75 			case '.':
76 			case '#':
77 				fmt++;
78 				goto rflag;
79 			case 'l':
80 			case 'j':
81 				size |= SZ_LONG;
82 				fmt++;
83 				goto rflag;
84 			case 'z':
85 				size |= SZ_SIZE_T;
86 				fmt++;
87 				goto rflag;
88 			case 'u':
89 				size |= SZ_UNSIGNED;
90 				/* FALLTHROUGH */
91 			case 'd':{
92 				long sval;
93 				unsigned long uval;
94 				char digits[sizeof(int) * 3], *dp = digits;
95 #define	SARG() \
96 (size & SZ_LONG ? va_arg(ap, long) : \
97 ((size & SZ_SIZE_T ? (long)va_arg(ap, size_t) : \
98 va_arg(ap, int))))
99 #define	UARG() \
100 (size & SZ_LONG ? va_arg(ap, unsigned long) : \
101 ((size & SZ_SIZE_T ? va_arg(ap, size_t) : \
102 va_arg(ap, unsigned int))))
103 
104 				if (fmt[1] == 'd') {
105 					if (size & SZ_UNSIGNED)
106 						sval = UARG();
107 					else
108 						sval = SARG();
109 					if (sval < 0) {
110 						if ((sval << 1) == 0) {
111 							/*
112 							 * We can't flip the
113 							 * sign of this since
114 							 * it can't be
115 							 * represented as a
116 							 * positive number in
117 							 * two complement,
118 							 * handle the first
119 							 * digit. After that,
120 							 * it can be flipped
121 							 * since it is now not
122 							 * 2^(n-1).
123 							 */
124 							*dp++ = '0'-(sval % 10);
125 							sval /= 10;
126 						}
127 						*bp++ = '-';
128 						uval = -sval;
129 					} else {
130 						uval = sval;
131 					}
132 				} else {
133 					if (size & SZ_UNSIGNED)
134 						uval = UARG();
135 					else
136 						uval = SARG();
137 				}
138 				do {
139 					*dp++ = '0' + (uval % 10);
140 					uval /= 10;
141 				} while (uval != 0);
142 				do {
143 					*bp++ = *--dp;
144 				} while (dp != digits && bp < ep);
145 				fmt += 2;
146 				break;
147 			}
148 			case 'x':
149 			case 'p':{
150 				unsigned long val = va_arg(ap, unsigned long);
151 				unsigned long mask = ~(~0UL >> 4);
152 				int bits = sizeof(val) * 8 - 4;
153 				const char hexdigits[] = "0123456789abcdef";
154 				if (fmt[1] == 'p') {
155 					*bp++ = '0';
156 					*bp++ = 'x';
157 				}
158 				/* handle the border case */
159 				if (val == 0) {
160 					*bp++ = '0';
161 					fmt += 2;
162 					break;
163 				}
164 				/* suppress 0s */
165 				while ((val & mask) == 0)
166 					bits -= 4, mask >>= 4;
167 
168 				/* emit the hex digits */
169 				while (bits >= 0 && bp < ep) {
170 					*bp++ = hexdigits[(val & mask) >> bits];
171 					bits -= 4, mask >>= 4;
172 				}
173 				fmt += 2;
174 				break;
175 			}
176 			case 's':{
177 				const char *str = va_arg(ap, const char *);
178 				int len;
179 
180 				if (str == NULL)
181 					str = "(null)";
182 
183 				if (prec < 0)
184 					len = strlen(str);
185 				else
186 					len = prec;
187 				if (ep - bp < len)
188 					len = ep - bp;
189 				memcpy(bp, str, len);
190 				bp += len;
191 				fmt += 2;
192 				break;
193 			}
194 			case 'c':{
195 				int c = va_arg(ap, int);
196 				*bp++ = (char)c;
197 				fmt += 2;
198 				break;
199 			}
200 			default:
201 				*bp++ = *fmt;
202 				break;
203 			}
204 			break;
205 		}
206 		default:
207 			*bp++ = *fmt++;
208 			break;
209 		}
210 	}
211 
212 	*bp = '\0';
213 	return bp - buf;
214 }
215 
216 void
xvprintf(const char * fmt,va_list ap)217 xvprintf(const char *fmt, va_list ap)
218 {
219 	char buf[256];
220 
221 	(void) write(2, buf, xvsnprintf(buf, sizeof(buf), fmt, ap));
222 }
223 
224 void
xprintf(const char * fmt,...)225 xprintf(const char *fmt, ...)
226 {
227 	va_list ap;
228 
229 	va_start(ap, fmt);
230 
231 	xvprintf(fmt, ap);
232 
233 	va_end(ap);
234 }
235 
236 void
xsnprintf(char * buf,size_t buflen,const char * fmt,...)237 xsnprintf(char *buf, size_t buflen, const char *fmt, ...)
238 {
239 	va_list ap;
240 
241 	va_start(ap, fmt);
242 
243 	xvsnprintf(buf, buflen, fmt, ap);
244 
245 	va_end(ap);
246 }
247 
248 #include "errlist_concat.h"
249 
250 const char *
xstrerror(int error)251 xstrerror(int error)
252 {
253 
254 	if (error >= concat_nerr || error < 0) {
255 		static char buf[128];
256 		xsnprintf(buf, sizeof(buf), "Unknown error: %d", error);
257 		return buf;
258 	}
259 	return concat_errlist + concat_offset[error];
260 }
261 
262 void
xerrx(int eval,const char * fmt,...)263 xerrx(int eval, const char *fmt, ...)
264 {
265 	va_list ap;
266 
267 	va_start(ap, fmt);
268 	xvprintf(fmt, ap);
269 	va_end(ap);
270 	(void) write(2, "\n", 1);
271 
272 	exit(eval);
273 }
274 
275 void
xerr(int eval,const char * fmt,...)276 xerr(int eval, const char *fmt, ...)
277 {
278 	int saved_errno = errno;
279 	va_list ap;
280 
281 	va_start(ap, fmt);
282 	xvprintf(fmt, ap);
283 	va_end(ap);
284 
285 	xprintf(": %s\n", xstrerror(saved_errno));
286 	exit(eval);
287 }
288 
289 void
xwarn(const char * fmt,...)290 xwarn(const char *fmt, ...)
291 {
292 	int saved_errno = errno;
293 	va_list ap;
294 
295 	va_start(ap, fmt);
296 	xvprintf(fmt, ap);
297 	va_end(ap);
298 
299 	xprintf(": %s\n", xstrerror(saved_errno));
300 	errno = saved_errno;
301 }
302 
303 void
xwarnx(const char * fmt,...)304 xwarnx(const char *fmt, ...)
305 {
306 	va_list ap;
307 
308 	va_start(ap, fmt);
309 	xvprintf(fmt, ap);
310 	va_end(ap);
311 	(void) write(2, "\n", 1);
312 }
313 
314 #ifdef DEBUG
315 void
xassert(const char * file,int line,const char * failedexpr)316 xassert(const char *file, int line, const char *failedexpr)
317 {
318 
319 	xprintf("assertion \"%s\" failed: file \"%s\", line %d\n",
320 		failedexpr, file, line);
321 	abort();
322 	/* NOTREACHED */
323 }
324 #endif
325 #endif
326