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