Home | History | Annotate | Line # | Download | only in ld.elf_so
xprintf.c revision 1.12
      1  1.12   mycroft /*	$NetBSD: xprintf.c,v 1.12 2002/09/24 14:05:33 mycroft Exp $	 */
      2   1.1       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      5   1.1       cgd  * All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. The name of the author may not be used to endorse or promote products
     16   1.1       cgd  *    derived from this software without specific prior written permission.
     17   1.1       cgd  *
     18   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1       cgd  */
     29   1.1       cgd 
     30   1.4  christos #include <sys/cdefs.h>
     31   1.1       cgd #include <string.h>
     32   1.7      matt #include <stdlib.h>
     33   1.1       cgd #include <unistd.h>
     34   1.1       cgd #include <errno.h>
     35   1.4  christos #include <stdarg.h>
     36   1.1       cgd 
     37  1.12   mycroft #include "rtldenv.h"
     38  1.12   mycroft 
     39   1.5  christos #ifdef RTLD_LOADER
     40   1.2  christos #define SZ_SHORT	0x01
     41   1.2  christos #define	SZ_INT		0x02
     42   1.2  christos #define	SZ_LONG		0x04
     43   1.2  christos #define	SZ_UNSIGNED	0x10
     44   1.2  christos #define	SZ_MASK		0x0f
     45   1.2  christos 
     46   1.1       cgd /*
     47   1.1       cgd  * Non-mallocing printf, for use by malloc and rtld itself.
     48   1.1       cgd  * This avoids putting in most of stdio.
     49   1.1       cgd  *
     50   1.1       cgd  * deals withs formats %x, %p, %s, and %d.
     51   1.1       cgd  */
     52   1.1       cgd size_t
     53   1.4  christos xvsnprintf(buf, buflen, fmt, ap)
     54   1.4  christos 	char *buf;
     55   1.4  christos 	size_t buflen;
     56   1.4  christos 	const char *fmt;
     57   1.4  christos 	va_list ap;
     58   1.1       cgd {
     59   1.4  christos 	char *bp = buf;
     60   1.4  christos 	char *const ep = buf + buflen - 4;
     61   1.4  christos 	int size;
     62   1.4  christos 
     63   1.8       eeh 	while (*fmt != '\0' && bp < ep) {
     64   1.4  christos 		switch (*fmt) {
     65   1.4  christos 		case '\\':{
     66   1.4  christos 			if (fmt[1] != '\0')
     67   1.4  christos 				*bp++ = *++fmt;
     68   1.4  christos 			continue;
     69   1.2  christos 		}
     70   1.4  christos 		case '%':{
     71   1.4  christos 			size = SZ_INT;
     72   1.4  christos 	rflag:		switch (fmt[1]) {
     73   1.4  christos 			case 'h':
     74   1.4  christos 				size = (size&SZ_MASK)|SZ_SHORT;
     75   1.4  christos 				fmt++;
     76   1.4  christos 				goto rflag;
     77   1.4  christos 			case 'l':
     78   1.4  christos 				size = (size&SZ_MASK)|SZ_LONG;
     79   1.4  christos 				fmt++;
     80   1.4  christos 				goto rflag;
     81   1.4  christos 			case 'u':
     82   1.4  christos 				size |= SZ_UNSIGNED;
     83   1.4  christos 				/* FALLTHROUGH */
     84   1.4  christos 			case 'd':{
     85  1.12   mycroft 				long sval;
     86  1.12   mycroft 				unsigned long uval;
     87   1.4  christos 				char digits[sizeof(int) * 3], *dp = digits;
     88   1.3    scottr #define	SARG() \
     89   1.7      matt (size & SZ_SHORT ? (short) va_arg(ap, int) : \
     90   1.4  christos size & SZ_LONG ? va_arg(ap, long) : \
     91   1.4  christos va_arg(ap, int))
     92   1.3    scottr #define	UARG() \
     93   1.7      matt (size & SZ_SHORT ? (unsigned short) va_arg(ap, unsigned int) : \
     94   1.4  christos size & SZ_LONG ? va_arg(ap, unsigned long) : \
     95   1.4  christos va_arg(ap, unsigned int))
     96   1.3    scottr #define	ARG()	(size & SZ_UNSIGNED ? UARG() : SARG())
     97   1.1       cgd 
     98   1.4  christos 				if (fmt[1] == 'd') {
     99   1.4  christos 					sval = ARG();
    100   1.4  christos 					if (sval < 0) {
    101   1.4  christos 						if ((sval << 1) == 0) {
    102   1.4  christos 							/*
    103   1.4  christos 							 * We can't flip the
    104   1.4  christos 							 * sign of this since
    105   1.4  christos 							 * it can't be
    106   1.4  christos 							 * represented as a
    107   1.4  christos 							 * positive number in
    108   1.4  christos 							 * two complement,
    109   1.4  christos 							 * handle the first
    110   1.4  christos 							 * digit. After that,
    111   1.4  christos 							 * it can be flipped
    112   1.4  christos 							 * since it is now not
    113   1.4  christos 							 * 2^(n-1).
    114   1.4  christos 							 */
    115   1.4  christos 							*dp++ = '0'-(sval % 10);
    116   1.4  christos 							sval /= 10;
    117   1.4  christos 						}
    118   1.4  christos 						*bp++ = '-';
    119   1.4  christos 						uval = -sval;
    120   1.4  christos 					} else {
    121   1.4  christos 						uval = sval;
    122   1.4  christos 					}
    123   1.4  christos 				} else {
    124   1.4  christos 					uval = ARG();
    125   1.4  christos 				}
    126   1.4  christos 				do {
    127   1.4  christos 					*dp++ = '0' + (uval % 10);
    128   1.4  christos 					uval /= 10;
    129   1.4  christos 				} while (uval != 0);
    130   1.4  christos 				do {
    131   1.4  christos 					*bp++ = *--dp;
    132   1.4  christos 				} while (dp != digits && bp < ep);
    133   1.4  christos 				fmt += 2;
    134   1.4  christos 				break;
    135   1.4  christos 			}
    136   1.4  christos 			case 'x':
    137   1.4  christos 			case 'p':{
    138   1.4  christos 				unsigned long val = va_arg(ap, unsigned long);
    139   1.4  christos 				unsigned long mask = ~(~0UL >> 4);
    140   1.4  christos 				int bits = sizeof(val) * 8 - 4;
    141   1.4  christos 				const char hexdigits[] = "0123456789abcdef";
    142   1.4  christos 				if (fmt[1] == 'p') {
    143   1.4  christos 					*bp++ = '0';
    144   1.4  christos 					*bp++ = 'x';
    145   1.4  christos 				}
    146   1.4  christos 				/* handle the border case */
    147   1.4  christos 				if (val == 0) {
    148   1.4  christos 					*bp++ = '0';
    149   1.4  christos 					fmt += 2;
    150   1.4  christos 					break;
    151   1.4  christos 				}
    152   1.4  christos 				/* suppress 0s */
    153   1.4  christos 				while ((val & mask) == 0)
    154   1.4  christos 					bits -= 4, mask >>= 4;
    155   1.4  christos 
    156   1.4  christos 				/* emit the hex digits */
    157   1.4  christos 				while (bits >= 0 && bp < ep) {
    158   1.4  christos 					*bp++ = hexdigits[(val & mask) >> bits];
    159   1.4  christos 					bits -= 4, mask >>= 4;
    160   1.4  christos 				}
    161   1.4  christos 				fmt += 2;
    162   1.4  christos 				break;
    163   1.4  christos 			}
    164   1.4  christos 			case 's':{
    165   1.4  christos 				const char *str = va_arg(ap, const char *);
    166   1.4  christos 				int len;
    167   1.4  christos 
    168   1.4  christos 				if (str == NULL)
    169   1.4  christos 					str = "(null)";
    170   1.4  christos 
    171   1.4  christos 				len = strlen(str);
    172   1.4  christos 				if (ep - bp < len)
    173   1.4  christos 					len = ep - bp;
    174   1.4  christos 				memcpy(bp, str, len);
    175   1.4  christos 				bp += len;
    176   1.4  christos 				fmt += 2;
    177   1.4  christos 				break;
    178   1.4  christos 			}
    179   1.4  christos 			default:
    180   1.4  christos 				*bp++ = *fmt;
    181   1.4  christos 				break;
    182   1.1       cgd 			}
    183   1.4  christos 			break;
    184   1.1       cgd 		}
    185   1.4  christos 		default:
    186   1.4  christos 			*bp++ = *fmt++;
    187   1.4  christos 			break;
    188   1.1       cgd 		}
    189   1.1       cgd 	}
    190   1.1       cgd 
    191   1.4  christos 	*bp = '\0';
    192   1.4  christos 	return bp - buf;
    193   1.1       cgd }
    194   1.1       cgd 
    195   1.1       cgd void
    196   1.4  christos xvprintf(fmt, ap)
    197   1.4  christos 	const char *fmt;
    198   1.4  christos 	va_list ap;
    199   1.1       cgd {
    200   1.4  christos 	char buf[256];
    201   1.4  christos 	(void) write(2, buf, xvsnprintf(buf, sizeof(buf), fmt, ap));
    202   1.1       cgd }
    203   1.1       cgd 
    204   1.1       cgd void
    205   1.4  christos xprintf(const char *fmt, ...)
    206   1.1       cgd {
    207   1.4  christos 	va_list ap;
    208   1.1       cgd 
    209   1.4  christos 	va_start(ap, fmt);
    210   1.4  christos 
    211   1.4  christos 	xvprintf(fmt, ap);
    212   1.4  christos 
    213   1.4  christos 	va_end(ap);
    214   1.1       cgd }
    215   1.1       cgd 
    216   1.1       cgd void
    217   1.4  christos xsnprintf(char *buf, size_t buflen, const char *fmt, ...)
    218   1.1       cgd {
    219   1.4  christos 	va_list ap;
    220   1.9       wiz 
    221   1.4  christos 	va_start(ap, fmt);
    222   1.4  christos 
    223   1.4  christos 	xvsnprintf(buf, buflen, fmt, ap);
    224   1.1       cgd 
    225   1.4  christos 	va_end(ap);
    226   1.1       cgd }
    227   1.1       cgd 
    228   1.1       cgd const char *
    229   1.4  christos xstrerror(error)
    230   1.4  christos 	int error;
    231   1.1       cgd {
    232   1.4  christos 	if (error >= sys_nerr || error < 0) {
    233   1.4  christos 		static char buf[128];
    234   1.4  christos 		xsnprintf(buf, sizeof(buf), "Unknown error: %d", error);
    235   1.4  christos 		return buf;
    236   1.4  christos 	}
    237   1.4  christos 	return sys_errlist[error];
    238   1.1       cgd }
    239   1.1       cgd 
    240   1.1       cgd void
    241   1.4  christos xerrx(int eval, const char *fmt, ...)
    242   1.1       cgd {
    243   1.4  christos 	va_list ap;
    244   1.9       wiz 
    245   1.4  christos 	va_start(ap, fmt);
    246   1.4  christos 	xvprintf(fmt, ap);
    247   1.4  christos 	va_end(ap);
    248  1.10     lukem 	(void) write(2, "\n", 1);
    249   1.4  christos 
    250   1.4  christos 	exit(eval);
    251   1.1       cgd }
    252   1.1       cgd 
    253   1.1       cgd void
    254   1.4  christos xerr(int eval, const char *fmt, ...)
    255   1.1       cgd {
    256   1.4  christos 	int saved_errno = errno;
    257   1.4  christos 	va_list ap;
    258   1.9       wiz 
    259   1.4  christos 	va_start(ap, fmt);
    260   1.4  christos 	xvprintf(fmt, ap);
    261   1.4  christos 	va_end(ap);
    262   1.1       cgd 
    263   1.4  christos 	xprintf(": %s\n", xstrerror(saved_errno));
    264   1.4  christos 	exit(eval);
    265   1.1       cgd }
    266   1.1       cgd 
    267   1.1       cgd void
    268   1.4  christos xwarn(const char *fmt, ...)
    269   1.1       cgd {
    270   1.4  christos 	int saved_errno = errno;
    271   1.4  christos 	va_list ap;
    272   1.9       wiz 
    273   1.4  christos 	va_start(ap, fmt);
    274   1.4  christos 	xvprintf(fmt, ap);
    275   1.4  christos 	va_end(ap);
    276   1.1       cgd 
    277   1.4  christos 	xprintf(": %s\n", xstrerror(saved_errno));
    278   1.4  christos 	errno = saved_errno;
    279   1.1       cgd }
    280   1.1       cgd 
    281   1.1       cgd void
    282   1.4  christos xwarnx(const char *fmt, ...)
    283   1.1       cgd {
    284   1.4  christos 	va_list ap;
    285   1.9       wiz 
    286   1.4  christos 	va_start(ap, fmt);
    287   1.4  christos 	xvprintf(fmt, ap);
    288  1.10     lukem 	va_end(ap);
    289   1.6     soren 	(void) write(2, "\n", 1);
    290   1.1       cgd }
    291   1.1       cgd 
    292  1.11   mycroft #ifdef DEBUG
    293   1.1       cgd void
    294   1.4  christos xassert(file, line, failedexpr)
    295   1.4  christos 	const char *file;
    296   1.4  christos 	int line;
    297   1.4  christos 	const char *failedexpr;
    298   1.1       cgd {
    299   1.4  christos 	xprintf("assertion \"%s\" failed: file \"%s\", line %d\n",
    300   1.4  christos 		failedexpr, file, line);
    301   1.4  christos 	abort();
    302   1.4  christos 	/* NOTREACHED */
    303   1.1       cgd }
    304  1.11   mycroft #endif
    305   1.5  christos #endif
    306