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