xref: /freebsd/lib/libthr/thread/thr_printf.c (revision 7d9d7ca2ed3e3d630e63482c41f2d26194129c2c)
1bb535300SJeff Roberson /*-
2bb535300SJeff Roberson  * Copyright (c) 2002 Jonathan Mini <mini@freebsd.org>
3bb535300SJeff Roberson  * All rights reserved.
4bb535300SJeff Roberson  *
5bb535300SJeff Roberson  * Redistribution and use in source and binary forms, with or without
6bb535300SJeff Roberson  * modification, are permitted provided that the following conditions
7bb535300SJeff Roberson  * are met:
8bb535300SJeff Roberson  * 1. Redistributions of source code must retain the above copyright
9bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer.
10bb535300SJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
11bb535300SJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
12bb535300SJeff Roberson  *    documentation and/or other materials provided with the distribution.
13bb535300SJeff Roberson  *
14bb535300SJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15bb535300SJeff Roberson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16bb535300SJeff Roberson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17bb535300SJeff Roberson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18bb535300SJeff Roberson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19bb535300SJeff Roberson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20bb535300SJeff Roberson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21bb535300SJeff Roberson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22bb535300SJeff Roberson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23bb535300SJeff Roberson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24bb535300SJeff Roberson  * SUCH DAMAGE.
25bb535300SJeff Roberson  *
26bb535300SJeff Roberson  * $FreeBSD$
27bb535300SJeff Roberson  */
28bb535300SJeff Roberson 
29bb535300SJeff Roberson #include <sys/cdefs.h>
30bb535300SJeff Roberson __FBSDID("$FreeBSD$");
31bb535300SJeff Roberson 
32bb535300SJeff Roberson #include <sys/types.h>
33bb535300SJeff Roberson #include <sys/fcntl.h>
34bb535300SJeff Roberson #include <sys/uio.h>
35bb535300SJeff Roberson #include <errno.h>
36bb535300SJeff Roberson #include <stdarg.h>
377d9d7ca2SMike Makonnen #include <string.h>
38bb535300SJeff Roberson #include <unistd.h>
39bb535300SJeff Roberson #include <pthread.h>
40bb535300SJeff Roberson 
41bb535300SJeff Roberson #include "thr_private.h"
42bb535300SJeff Roberson 
43bb535300SJeff Roberson static void	pchar(int fd, char c);
44bb535300SJeff Roberson static void	pstr(int fd, const char *s);
45bb535300SJeff Roberson 
46bb535300SJeff Roberson /*
47bb535300SJeff Roberson  * Write formatted output to stdout, in a thread-safe manner.
48bb535300SJeff Roberson  *
49bb535300SJeff Roberson  * Recognises the following conversions:
50bb535300SJeff Roberson  *	%c	-> char
51bb535300SJeff Roberson  *	%d	-> signed int (base 10)
52bb535300SJeff Roberson  *	%s	-> string
53bb535300SJeff Roberson  *	%u	-> unsigned int (base 10)
54bb535300SJeff Roberson  *	%x	-> unsigned int (base 16)
55bb535300SJeff Roberson  *	%p	-> unsigned int (base 16)
56bb535300SJeff Roberson  */
57bb535300SJeff Roberson void
58bb535300SJeff Roberson _thread_printf(int fd, const char *fmt, ...)
59bb535300SJeff Roberson {
60bb535300SJeff Roberson 	static const char digits[16] = "0123456789abcdef";
61bb535300SJeff Roberson 	va_list	 ap;
62bb535300SJeff Roberson 	char buf[10];
63bb535300SJeff Roberson 	char *s;
64bb535300SJeff Roberson 	unsigned r, u;
65bb535300SJeff Roberson 	int c, d;
66bb535300SJeff Roberson 
67bb535300SJeff Roberson 	va_start(ap, fmt);
68bb535300SJeff Roberson 	while ((c = *fmt++)) {
69bb535300SJeff Roberson 		if (c == '%') {
70bb535300SJeff Roberson 			c = *fmt++;
71bb535300SJeff Roberson 			switch (c) {
72bb535300SJeff Roberson 			case 'c':
73bb535300SJeff Roberson 				pchar(fd, va_arg(ap, int));
74bb535300SJeff Roberson 				continue;
75bb535300SJeff Roberson 			case 's':
76bb535300SJeff Roberson 				pstr(fd, va_arg(ap, char *));
77bb535300SJeff Roberson 				continue;
78bb535300SJeff Roberson 			case 'd':
79bb535300SJeff Roberson 			case 'u':
80bb535300SJeff Roberson 			case 'p':
81bb535300SJeff Roberson 			case 'x':
82bb535300SJeff Roberson 				r = ((c == 'u') || (c == 'd')) ? 10 : 16;
83bb535300SJeff Roberson 				if (c == 'd') {
84bb535300SJeff Roberson 					d = va_arg(ap, unsigned);
85bb535300SJeff Roberson 					if (d < 0) {
86bb535300SJeff Roberson 						pchar(fd, '-');
87bb535300SJeff Roberson 						u = (unsigned)(d * -1);
88bb535300SJeff Roberson 					} else
89bb535300SJeff Roberson 						u = (unsigned)d;
90bb535300SJeff Roberson 				} else
91bb535300SJeff Roberson 					u = va_arg(ap, unsigned);
92bb535300SJeff Roberson 				s = buf;
93bb535300SJeff Roberson 				do {
94bb535300SJeff Roberson 					*s++ = digits[u % r];
95bb535300SJeff Roberson 				} while (u /= r);
96bb535300SJeff Roberson 				while (--s >= buf)
97bb535300SJeff Roberson 					pchar(fd, *s);
98bb535300SJeff Roberson 				continue;
99bb535300SJeff Roberson 			}
100bb535300SJeff Roberson 		}
101bb535300SJeff Roberson 		pchar(fd, c);
102bb535300SJeff Roberson 	}
103bb535300SJeff Roberson 	va_end(ap);
104bb535300SJeff Roberson }
105bb535300SJeff Roberson 
106bb535300SJeff Roberson /*
107bb535300SJeff Roberson  * Write a single character to stdout, in a thread-safe manner.
108bb535300SJeff Roberson  */
109bb535300SJeff Roberson static void
110bb535300SJeff Roberson pchar(int fd, char c)
111bb535300SJeff Roberson {
112bb535300SJeff Roberson 
113bb535300SJeff Roberson 	write(fd, &c, 1);
114bb535300SJeff Roberson }
115bb535300SJeff Roberson 
116bb535300SJeff Roberson /*
117bb535300SJeff Roberson  * Write a string to stdout, in a thread-safe manner.
118bb535300SJeff Roberson  */
119bb535300SJeff Roberson static void
120bb535300SJeff Roberson pstr(int fd, const char *s)
121bb535300SJeff Roberson {
122bb535300SJeff Roberson 
123bb535300SJeff Roberson 	write(fd, s, strlen(s));
124bb535300SJeff Roberson }
125bb535300SJeff Roberson 
126