xref: /freebsd/lib/libthr/thread/thr_printf.c (revision bb535300dd871731b2542594a917bc2892479ca0)
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>
37bb535300SJeff Roberson #include <unistd.h>
38bb535300SJeff Roberson #include <pthread.h>
39bb535300SJeff Roberson 
40bb535300SJeff Roberson #include "thr_private.h"
41bb535300SJeff Roberson 
42bb535300SJeff Roberson static void	pchar(int fd, char c);
43bb535300SJeff Roberson static void	pstr(int fd, const char *s);
44bb535300SJeff Roberson 
45bb535300SJeff Roberson /*
46bb535300SJeff Roberson  * Write formatted output to stdout, in a thread-safe manner.
47bb535300SJeff Roberson  *
48bb535300SJeff Roberson  * Recognises the following conversions:
49bb535300SJeff Roberson  *	%c	-> char
50bb535300SJeff Roberson  *	%d	-> signed int (base 10)
51bb535300SJeff Roberson  *	%s	-> string
52bb535300SJeff Roberson  *	%u	-> unsigned int (base 10)
53bb535300SJeff Roberson  *	%x	-> unsigned int (base 16)
54bb535300SJeff Roberson  *	%p	-> unsigned int (base 16)
55bb535300SJeff Roberson  */
56bb535300SJeff Roberson void
57bb535300SJeff Roberson _thread_printf(int fd, const char *fmt, ...)
58bb535300SJeff Roberson {
59bb535300SJeff Roberson 	static const char digits[16] = "0123456789abcdef";
60bb535300SJeff Roberson 	va_list	 ap;
61bb535300SJeff Roberson 	char buf[10];
62bb535300SJeff Roberson 	char *s;
63bb535300SJeff Roberson 	unsigned r, u;
64bb535300SJeff Roberson 	int c, d;
65bb535300SJeff Roberson 
66bb535300SJeff Roberson 	va_start(ap, fmt);
67bb535300SJeff Roberson 	while ((c = *fmt++)) {
68bb535300SJeff Roberson 		if (c == '%') {
69bb535300SJeff Roberson 			c = *fmt++;
70bb535300SJeff Roberson 			switch (c) {
71bb535300SJeff Roberson 			case 'c':
72bb535300SJeff Roberson 				pchar(fd, va_arg(ap, int));
73bb535300SJeff Roberson 				continue;
74bb535300SJeff Roberson 			case 's':
75bb535300SJeff Roberson 				pstr(fd, va_arg(ap, char *));
76bb535300SJeff Roberson 				continue;
77bb535300SJeff Roberson 			case 'd':
78bb535300SJeff Roberson 			case 'u':
79bb535300SJeff Roberson 			case 'p':
80bb535300SJeff Roberson 			case 'x':
81bb535300SJeff Roberson 				r = ((c == 'u') || (c == 'd')) ? 10 : 16;
82bb535300SJeff Roberson 				if (c == 'd') {
83bb535300SJeff Roberson 					d = va_arg(ap, unsigned);
84bb535300SJeff Roberson 					if (d < 0) {
85bb535300SJeff Roberson 						pchar(fd, '-');
86bb535300SJeff Roberson 						u = (unsigned)(d * -1);
87bb535300SJeff Roberson 					} else
88bb535300SJeff Roberson 						u = (unsigned)d;
89bb535300SJeff Roberson 				} else
90bb535300SJeff Roberson 					u = va_arg(ap, unsigned);
91bb535300SJeff Roberson 				s = buf;
92bb535300SJeff Roberson 				do {
93bb535300SJeff Roberson 					*s++ = digits[u % r];
94bb535300SJeff Roberson 				} while (u /= r);
95bb535300SJeff Roberson 				while (--s >= buf)
96bb535300SJeff Roberson 					pchar(fd, *s);
97bb535300SJeff Roberson 				continue;
98bb535300SJeff Roberson 			}
99bb535300SJeff Roberson 		}
100bb535300SJeff Roberson 		pchar(fd, c);
101bb535300SJeff Roberson 	}
102bb535300SJeff Roberson 	va_end(ap);
103bb535300SJeff Roberson }
104bb535300SJeff Roberson 
105bb535300SJeff Roberson /*
106bb535300SJeff Roberson  * Write a single character to stdout, in a thread-safe manner.
107bb535300SJeff Roberson  */
108bb535300SJeff Roberson static void
109bb535300SJeff Roberson pchar(int fd, char c)
110bb535300SJeff Roberson {
111bb535300SJeff Roberson 
112bb535300SJeff Roberson 	write(fd, &c, 1);
113bb535300SJeff Roberson }
114bb535300SJeff Roberson 
115bb535300SJeff Roberson /*
116bb535300SJeff Roberson  * Write a string to stdout, in a thread-safe manner.
117bb535300SJeff Roberson  */
118bb535300SJeff Roberson static void
119bb535300SJeff Roberson pstr(int fd, const char *s)
120bb535300SJeff Roberson {
121bb535300SJeff Roberson 
122bb535300SJeff Roberson 	write(fd, s, strlen(s));
123bb535300SJeff Roberson }
124bb535300SJeff Roberson 
125