xref: /netbsd-src/sys/lib/libsa/subr_prf.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: subr_prf.c,v 1.30 2023/05/29 03:56:52 rin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)printf.c	8.1 (Berkeley) 6/11/93
32  */
33 
34 /*
35  * Scaled down version of printf(3).
36  */
37 
38 #include <sys/cdefs.h>
39 #include <sys/types.h>
40 #include <sys/stdint.h>		/* XXX: for intptr_t */
41 
42 #include "stand.h"
43 
44 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
45 #define INTMAX_T	longlong_t
46 #define UINTMAX_T	u_longlong_t
47 #else
48 #define INTMAX_T	long
49 #define UINTMAX_T	u_long
50 #endif
51 
52 #if 0 /* XXX: abuse intptr_t until the situation with ptrdiff_t is clear */
53 #define PTRDIFF_T	ptrdiff_t
54 #else
55 #define PTRDIFF_T	intptr_t
56 #endif
57 
58 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
59 static void kprintn(void (*)(int), UINTMAX_T, int, int, int);
60 #else
61 static void kprintn(void (*)(int), UINTMAX_T, int);
62 #endif
63 static void sputchar(int);
64 static void kdoprnt(void (*)(int), const char *, va_list);
65 
66 static char *sbuf, *ebuf;
67 
68 const char hexdigits[16] = "0123456789abcdef";
69 
70 #define LONG		0x01
71 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
72 #define LLONG		0x02
73 #endif
74 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
75 #define ALT		0x04
76 #define SPACE		0x08
77 #define LADJUST		0x10
78 #define SIGN		0x20
79 #define ZEROPAD		0x40
80 #define NEGATIVE	0x80
81 #define KPRINTN(base)	kprintn(put, ul, base, lflag, width)
82 #define RADJUSTZEROPAD()					\
83 do {								\
84 	if ((lflag & (ZEROPAD|LADJUST)) == ZEROPAD) {		\
85 		while (width-- > 0)				\
86 			put('0');				\
87 	}							\
88 } while (/*CONSTCOND*/0)
89 #define LADJUSTPAD()						\
90 do {								\
91 	if (lflag & LADJUST) {					\
92 		while (width-- > 0)				\
93 			put(' ');				\
94 	}							\
95 } while (/*CONSTCOND*/0)
96 #define RADJUSTPAD()						\
97 do {								\
98 	if ((lflag & (ZEROPAD|LADJUST)) == 0) {			\
99 		while (width-- > 0)				\
100 			put(' ');				\
101 	}							\
102 } while (/*CONSTCOND*/0)
103 #else	/* LIBSA_PRINTF_WIDTH_SUPPORT */
104 #define KPRINTN(base)	kprintn(put, ul, base)
105 #define RADJUSTZEROPAD()	/**/
106 #define LADJUSTPAD()		/**/
107 #define RADJUSTPAD()		/**/
108 #endif	/* LIBSA_PRINTF_WIDTH_SUPPORT */
109 
110 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
111 #define KPRINT(base)						\
112 do {								\
113 	ul = (lflag & LLONG)					\
114 	    ? va_arg(ap, u_longlong_t)				\
115 	    : (lflag & LONG)					\
116 		? va_arg(ap, u_long)				\
117 		: va_arg(ap, u_int);				\
118 	KPRINTN(base);						\
119 } while (/*CONSTCOND*/0)
120 #else	/* LIBSA_PRINTF_LONGLONG_SUPPORT */
121 #define KPRINT(base)						\
122 do {								\
123 	ul = (lflag & LONG)					\
124 	    ? va_arg(ap, u_long) : va_arg(ap, u_int);		\
125 	KPRINTN(base);						\
126 } while (/*CONSTCOND*/0)
127 #endif	/* LIBSA_PRINTF_LONGLONG_SUPPORT */
128 
129 static void
130 null_sputchar(int c)
131 {
132 	sbuf++;
133 }
134 
135 static void
136 sputchar(int c)
137 {
138 
139 	if (sbuf < ebuf)
140 		*sbuf++ = c;
141 }
142 
143 void
144 vprintf(const char *fmt, va_list ap)
145 {
146 
147 	kdoprnt(putchar, fmt, ap);
148 }
149 
150 int
151 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
152 {
153 
154 	sbuf = buf;
155 	ebuf = buf + size - 1;
156 	kdoprnt(buf == NULL ? null_sputchar : sputchar, fmt, ap);
157 	if (buf != NULL)
158 		*sbuf = '\0';
159 	return sbuf - buf;
160 }
161 
162 static void
163 kdoprnt(void (*put)(int), const char *fmt, va_list ap)
164 {
165 	char *p;
166 	int ch;
167 	UINTMAX_T ul;
168 	int lflag;
169 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
170 	int width;
171 	char *q;
172 #endif
173 
174 	for (;;) {
175 		while ((ch = *fmt++) != '%') {
176 			if (ch == '\0')
177 				return;
178 			put(ch);
179 		}
180 		lflag = 0;
181 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
182 		width = 0;
183 #endif
184 reswitch:
185 		switch (ch = *fmt++) {
186 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
187 		case '#':
188 			lflag |= ALT;
189 			goto reswitch;
190 		case ' ':
191 			lflag |= SPACE;
192 			goto reswitch;
193 		case '-':
194 			lflag |= LADJUST;
195 			goto reswitch;
196 		case '+':
197 			lflag |= SIGN;
198 			goto reswitch;
199 		case '0':
200 			lflag |= ZEROPAD;
201 			goto reswitch;
202 		case '1': case '2': case '3': case '4': case '5':
203 		case '6': case '7': case '8': case '9':
204 			for (;;) {
205 				width *= 10;
206 				width += ch - '0';
207 				ch = *fmt;
208 				if ((unsigned)ch - '0' > 9)
209 					break;
210 				++fmt;
211 			}
212 			goto reswitch;
213 #endif
214 		case 'l':
215 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
216 			if (*fmt == 'l') {
217 				++fmt;
218 				lflag |= LLONG;
219 			} else
220 #endif
221 				lflag |= LONG;
222 			goto reswitch;
223 		case 'j':
224 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
225 			if (sizeof(intmax_t) == sizeof(long long))
226 				lflag |= LLONG;
227 			else
228 #endif
229 			if (sizeof(intmax_t) == sizeof(long))
230 				lflag |= LONG;
231 			goto reswitch;
232 		case 't':
233 			if (sizeof(PTRDIFF_T) == sizeof(long))
234 				lflag |= LONG;
235 			goto reswitch;
236 		case 'z':
237 			if (sizeof(ssize_t) == sizeof(long))
238 				lflag |= LONG;
239 			goto reswitch;
240 		case 'c':
241 			ch = va_arg(ap, int);
242 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
243 			--width;
244 #endif
245 			RADJUSTPAD();
246 			put(ch & 0xFF);
247 			LADJUSTPAD();
248 			break;
249 		case 's':
250 			p = va_arg(ap, char *);
251 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
252 			for (q = p; *q != '\0'; ++q)
253 				continue;
254 			width -= q - p;
255 #endif
256 			RADJUSTPAD();
257 			while ((ch = (unsigned char)*p++))
258 				put(ch);
259 			LADJUSTPAD();
260 			break;
261 		case 'd':
262 			ul =
263 #ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
264 			(lflag & LLONG) ? va_arg(ap, longlong_t) :
265 #endif
266 			(lflag & LONG) ? va_arg(ap, long) : va_arg(ap, int);
267 			if ((INTMAX_T)ul < 0) {
268 				ul = -(INTMAX_T)ul;
269 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
270 				lflag |= NEGATIVE;
271 #else
272 				put('-');
273 #endif
274 			}
275 			KPRINTN(10);
276 			break;
277 		case 'o':
278 			KPRINT(8);
279 			break;
280 		case 'u':
281 			KPRINT(10);
282 			break;
283 		case 'p':
284 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
285 			lflag |= (LONG|ALT);
286 #else
287 			put('0');
288 			put('x');
289 #endif
290 			/* FALLTHROUGH */
291 		case 'x':
292 			KPRINT(16);
293 			break;
294 		default:
295 			if (ch == '\0')
296 				return;
297 			put(ch);
298 			break;
299 		}
300 	}
301 }
302 
303 static void
304 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
305 kprintn(void (*put)(int), UINTMAX_T ul, int base, int lflag, int width)
306 #else
307 kprintn(void (*put)(int), UINTMAX_T ul, int base)
308 #endif
309 {
310 					/* hold a INTMAX_T in base 8 */
311 	char *p, buf[(sizeof(INTMAX_T) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
312 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
313 	char *q;
314 #endif
315 
316 	p = buf;
317 	do {
318 		*p++ = hexdigits[ul % base];
319 	} while (ul /= base);
320 #ifdef LIBSA_PRINTF_WIDTH_SUPPORT
321 	q = p;
322 	if (lflag & ALT && *(p - 1) != '0') {
323 		if (base == 8) {
324 			*p++ = '0';
325 		} else if (base == 16) {
326 			*p++ = 'x';
327 			*p++ = '0';
328 		}
329 	}
330 	if (lflag & NEGATIVE)
331 		*p++ = '-';
332 	else if (lflag & SIGN)
333 		*p++ = '+';
334 	else if (lflag & SPACE)
335 		*p++ = ' ';
336 	width -= p - buf;
337 	if (lflag & ZEROPAD) {
338 		while (p > q)
339 			put(*--p);
340 	}
341 #endif
342 	RADJUSTPAD();
343 	RADJUSTZEROPAD();
344 	do {
345 		put(*--p);
346 	} while (p > buf);
347 	LADJUSTPAD();
348 }
349