xref: /freebsd/lib/libthr/thread/thr_printf.c (revision f8bbbce458194ff4312c610d32a64ff4a3a71d45)
1bb535300SJeff Roberson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4bb535300SJeff Roberson  * Copyright (c) 2002 Jonathan Mini <mini@freebsd.org>
5bb535300SJeff Roberson  * All rights reserved.
6bb535300SJeff Roberson  *
7bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
8bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
9bb535300SJeff Roberson  * are met:
10bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
12bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
13bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
14bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
15bb535300SJeff Roberson  *
16bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bb535300SJeff Roberson  * SUCH DAMAGE.
27bb535300SJeff Roberson  */
28bb535300SJeff Roberson 
29bb535300SJeff Roberson #include <stdarg.h>
307d9d7ca2SMike Makonnen #include <string.h>
31bb535300SJeff Roberson #include <unistd.h>
32bb535300SJeff Roberson #include <pthread.h>
33bb535300SJeff Roberson 
348495e8b1SKonstantin Belousov #include "libc_private.h"
35bb535300SJeff Roberson #include "thr_private.h"
36bb535300SJeff Roberson 
37bb535300SJeff Roberson static void	pchar(int fd, char c);
38bb535300SJeff Roberson static void	pstr(int fd, const char *s);
39bb535300SJeff Roberson 
40bb535300SJeff Roberson /*
41bb535300SJeff Roberson  * Write formatted output to stdout, in a thread-safe manner.
42bb535300SJeff Roberson  *
43bb535300SJeff Roberson  * Recognises the following conversions:
44bb535300SJeff Roberson  *	%c	-> char
45bb535300SJeff Roberson  *	%d	-> signed int (base 10)
46bb535300SJeff Roberson  *	%s	-> string
47bb535300SJeff Roberson  *	%u	-> unsigned int (base 10)
48bb535300SJeff Roberson  *	%x	-> unsigned int (base 16)
49bb535300SJeff Roberson  *	%p	-> unsigned int (base 16)
50bb535300SJeff Roberson  */
51bb535300SJeff Roberson void
_thread_printf(int fd,const char * fmt,...)52bb535300SJeff Roberson _thread_printf(int fd, const char *fmt, ...)
53bb535300SJeff Roberson {
54bb535300SJeff Roberson 	va_list	ap;
553a7d122fSConrad Meyer 
563a7d122fSConrad Meyer 	va_start(ap, fmt);
573a7d122fSConrad Meyer 	_thread_vprintf(fd, fmt, ap);
583a7d122fSConrad Meyer 	va_end(ap);
593a7d122fSConrad Meyer }
603a7d122fSConrad Meyer 
613a7d122fSConrad Meyer void
_thread_vprintf(int fd,const char * fmt,va_list ap)623a7d122fSConrad Meyer _thread_vprintf(int fd, const char *fmt, va_list ap)
633a7d122fSConrad Meyer {
643a7d122fSConrad Meyer 	static const char digits[16] = "0123456789abcdef";
65a091d823SDavid Xu 	char buf[20];
66bb535300SJeff Roberson 	char *s;
67a091d823SDavid Xu 	unsigned long r, u;
68a091d823SDavid Xu 	int c;
69a091d823SDavid Xu 	long d;
70a3c00561SConrad Meyer 	int islong, isalt;
71bb535300SJeff Roberson 
72bb535300SJeff Roberson 	while ((c = *fmt++)) {
73a3c00561SConrad Meyer 		isalt = 0;
74a091d823SDavid Xu 		islong = 0;
75bb535300SJeff Roberson 		if (c == '%') {
76a091d823SDavid Xu next:			c = *fmt++;
77a091d823SDavid Xu 			if (c == '\0')
783a7d122fSConrad Meyer 				return;
79bb535300SJeff Roberson 			switch (c) {
80a3c00561SConrad Meyer 			case '#':
81a3c00561SConrad Meyer 				isalt = 1;
82a3c00561SConrad Meyer 				goto next;
83bb535300SJeff Roberson 			case 'c':
84bb535300SJeff Roberson 				pchar(fd, va_arg(ap, int));
85bb535300SJeff Roberson 				continue;
86bb535300SJeff Roberson 			case 's':
87bb535300SJeff Roberson 				pstr(fd, va_arg(ap, char *));
88bb535300SJeff Roberson 				continue;
89a091d823SDavid Xu 			case 'l':
90a091d823SDavid Xu 				islong = 1;
91a091d823SDavid Xu 				goto next;
92a091d823SDavid Xu 			case 'p':
93a3c00561SConrad Meyer 				pstr(fd, "0x");
94a091d823SDavid Xu 				islong = 1;
95b9b20abfSPedro F. Giffuni 				/* FALLTHROUGH */
96bb535300SJeff Roberson 			case 'd':
97bb535300SJeff Roberson 			case 'u':
98bb535300SJeff Roberson 			case 'x':
99a3c00561SConrad Meyer 				if (c == 'x' && isalt)
100a3c00561SConrad Meyer 					pstr(fd, "0x");
101bb535300SJeff Roberson 				r = ((c == 'u') || (c == 'd')) ? 10 : 16;
102bb535300SJeff Roberson 				if (c == 'd') {
103a091d823SDavid Xu 					if (islong)
104a091d823SDavid Xu 						d = va_arg(ap, unsigned long);
105a091d823SDavid Xu 					else
106bb535300SJeff Roberson 						d = va_arg(ap, unsigned);
107bb535300SJeff Roberson 					if (d < 0) {
108bb535300SJeff Roberson 						pchar(fd, '-');
109a091d823SDavid Xu 						u = (unsigned long)(d * -1);
110bb535300SJeff Roberson 					} else
111a091d823SDavid Xu 						u = (unsigned long)d;
112a091d823SDavid Xu 				} else {
113a091d823SDavid Xu 					if (islong)
114a091d823SDavid Xu 						u = va_arg(ap, unsigned long);
115a091d823SDavid Xu 					else
116bb535300SJeff Roberson 						u = va_arg(ap, unsigned);
117a091d823SDavid Xu 				}
118bb535300SJeff Roberson 				s = buf;
119bb535300SJeff Roberson 				do {
120bb535300SJeff Roberson 					*s++ = digits[u % r];
121bb535300SJeff Roberson 				} while (u /= r);
122bb535300SJeff Roberson 				while (--s >= buf)
123bb535300SJeff Roberson 					pchar(fd, *s);
124bb535300SJeff Roberson 				continue;
125bb535300SJeff Roberson 			}
126bb535300SJeff Roberson 		}
127bb535300SJeff Roberson 		pchar(fd, c);
128bb535300SJeff Roberson 	}
129bb535300SJeff Roberson }
130bb535300SJeff Roberson 
131bb535300SJeff Roberson /*
132bb535300SJeff Roberson  * Write a single character to stdout, in a thread-safe manner.
133bb535300SJeff Roberson  */
134bb535300SJeff Roberson static void
pchar(int fd,char c)135bb535300SJeff Roberson pchar(int fd, char c)
136bb535300SJeff Roberson {
137bb535300SJeff Roberson 
13805e948d9SMike Makonnen 	__sys_write(fd, &c, 1);
139bb535300SJeff Roberson }
140bb535300SJeff Roberson 
141bb535300SJeff Roberson /*
142bb535300SJeff Roberson  * Write a string to stdout, in a thread-safe manner.
143bb535300SJeff Roberson  */
144bb535300SJeff Roberson static void
pstr(int fd,const char * s)145bb535300SJeff Roberson pstr(int fd, const char *s)
146bb535300SJeff Roberson {
147bb535300SJeff Roberson 
14805e948d9SMike Makonnen 	__sys_write(fd, s, strlen(s));
149bb535300SJeff Roberson }
150bb535300SJeff Roberson 
151