2000-04-03 03:04:32 +04:00
|
|
|
/* $NetBSD: xprintf.c,v 1.6 2000/04/02 23:04:32 soren Exp $ */
|
1996-12-16 23:37:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1996 Matt Thomas <matt@3am-software.com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
#include <sys/cdefs.h>
|
1996-12-16 23:37:55 +03:00
|
|
|
#include "rtldenv.h"
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
|
|
|
#include <varargs.h>
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-04 00:18:00 +03:00
|
|
|
#ifdef RTLD_LOADER
|
1999-02-07 20:23:40 +03:00
|
|
|
#define SZ_SHORT 0x01
|
|
|
|
#define SZ_INT 0x02
|
|
|
|
#define SZ_LONG 0x04
|
|
|
|
#define SZ_QUAD 0x08
|
|
|
|
#define SZ_UNSIGNED 0x10
|
|
|
|
#define SZ_MASK 0x0f
|
|
|
|
|
1996-12-16 23:37:55 +03:00
|
|
|
/*
|
|
|
|
* Non-mallocing printf, for use by malloc and rtld itself.
|
|
|
|
* This avoids putting in most of stdio.
|
|
|
|
*
|
|
|
|
* deals withs formats %x, %p, %s, and %d.
|
|
|
|
*/
|
|
|
|
size_t
|
1999-03-01 19:40:07 +03:00
|
|
|
xvsnprintf(buf, buflen, fmt, ap)
|
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
|
|
|
const char *fmt;
|
|
|
|
va_list ap;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
char *bp = buf;
|
|
|
|
char *const ep = buf + buflen - 4;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
while (*fmt != NULL && bp < ep) {
|
|
|
|
switch (*fmt) {
|
|
|
|
case '\\':{
|
|
|
|
if (fmt[1] != '\0')
|
|
|
|
*bp++ = *++fmt;
|
|
|
|
continue;
|
1999-02-07 20:23:40 +03:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
case '%':{
|
|
|
|
size = SZ_INT;
|
|
|
|
rflag: switch (fmt[1]) {
|
|
|
|
case 'h':
|
|
|
|
size = (size&SZ_MASK)|SZ_SHORT;
|
|
|
|
fmt++;
|
|
|
|
goto rflag;
|
|
|
|
case 'l':
|
|
|
|
size = (size&SZ_MASK)|SZ_LONG;
|
|
|
|
fmt++;
|
|
|
|
if (fmt[1] == 'l') {
|
|
|
|
case 'q':
|
|
|
|
size = (size&SZ_MASK)|SZ_QUAD;
|
|
|
|
fmt++;
|
|
|
|
}
|
|
|
|
goto rflag;
|
|
|
|
case 'u':
|
|
|
|
size |= SZ_UNSIGNED;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'd':{
|
|
|
|
long long sval;
|
|
|
|
unsigned long long uval;
|
|
|
|
char digits[sizeof(int) * 3], *dp = digits;
|
1999-02-28 00:52:23 +03:00
|
|
|
#define SARG() \
|
1999-03-01 19:40:07 +03:00
|
|
|
(size & SZ_SHORT ? va_arg(ap, short) : \
|
|
|
|
size & SZ_LONG ? va_arg(ap, long) : \
|
|
|
|
size & SZ_QUAD ? va_arg(ap, long long) : \
|
|
|
|
va_arg(ap, int))
|
1999-02-28 00:52:23 +03:00
|
|
|
#define UARG() \
|
1999-03-01 19:40:07 +03:00
|
|
|
(size & SZ_SHORT ? va_arg(ap, unsigned short) : \
|
|
|
|
size & SZ_LONG ? va_arg(ap, unsigned long) : \
|
|
|
|
size & SZ_QUAD ? va_arg(ap, unsigned long long) : \
|
|
|
|
va_arg(ap, unsigned int))
|
1999-02-28 00:52:23 +03:00
|
|
|
#define ARG() (size & SZ_UNSIGNED ? UARG() : SARG())
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
if (fmt[1] == 'd') {
|
|
|
|
sval = ARG();
|
|
|
|
if (sval < 0) {
|
|
|
|
if ((sval << 1) == 0) {
|
|
|
|
/*
|
|
|
|
* We can't flip the
|
|
|
|
* sign of this since
|
|
|
|
* it can't be
|
|
|
|
* represented as a
|
|
|
|
* positive number in
|
|
|
|
* two complement,
|
|
|
|
* handle the first
|
|
|
|
* digit. After that,
|
|
|
|
* it can be flipped
|
|
|
|
* since it is now not
|
|
|
|
* 2^(n-1).
|
|
|
|
*/
|
|
|
|
*dp++ = '0'-(sval % 10);
|
|
|
|
sval /= 10;
|
|
|
|
}
|
|
|
|
*bp++ = '-';
|
|
|
|
uval = -sval;
|
|
|
|
} else {
|
|
|
|
uval = sval;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uval = ARG();
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
*dp++ = '0' + (uval % 10);
|
|
|
|
uval /= 10;
|
|
|
|
} while (uval != 0);
|
|
|
|
do {
|
|
|
|
*bp++ = *--dp;
|
|
|
|
} while (dp != digits && bp < ep);
|
|
|
|
fmt += 2;
|
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
case 'x':
|
|
|
|
case 'p':{
|
|
|
|
unsigned long val = va_arg(ap, unsigned long);
|
|
|
|
unsigned long mask = ~(~0UL >> 4);
|
|
|
|
int bits = sizeof(val) * 8 - 4;
|
|
|
|
const char hexdigits[] = "0123456789abcdef";
|
|
|
|
if (fmt[1] == 'p') {
|
|
|
|
*bp++ = '0';
|
|
|
|
*bp++ = 'x';
|
|
|
|
}
|
|
|
|
/* handle the border case */
|
|
|
|
if (val == 0) {
|
|
|
|
*bp++ = '0';
|
|
|
|
fmt += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* suppress 0s */
|
|
|
|
while ((val & mask) == 0)
|
|
|
|
bits -= 4, mask >>= 4;
|
|
|
|
|
|
|
|
/* emit the hex digits */
|
|
|
|
while (bits >= 0 && bp < ep) {
|
|
|
|
*bp++ = hexdigits[(val & mask) >> bits];
|
|
|
|
bits -= 4, mask >>= 4;
|
|
|
|
}
|
|
|
|
fmt += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's':{
|
|
|
|
const char *str = va_arg(ap, const char *);
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
str = "(null)";
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
if (ep - bp < len)
|
|
|
|
len = ep - bp;
|
|
|
|
memcpy(bp, str, len);
|
|
|
|
bp += len;
|
|
|
|
fmt += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
*bp++ = *fmt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-03-01 19:40:07 +03:00
|
|
|
default:
|
|
|
|
*bp++ = *fmt++;
|
|
|
|
break;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
*bp = '\0';
|
|
|
|
return bp - buf;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
xvprintf(fmt, ap)
|
|
|
|
const char *fmt;
|
|
|
|
va_list ap;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
char buf[256];
|
|
|
|
(void) write(2, buf, xvsnprintf(buf, sizeof(buf), fmt, ap));
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
xprintf(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xprintf(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
va_start(ap);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
xvprintf(fmt, ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
va_end(ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
xsnprintf(char *buf, size_t buflen, const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xsnprintf(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
va_list ap;
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
|
|
|
const char *fmt;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
va_start(ap);
|
|
|
|
buf = va_arg(ap, char *);
|
|
|
|
buflen = va_arg(ap, size_t);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
|
|
|
xvsnprintf(buf, buflen, fmt, ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
va_end(ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
1999-03-01 19:40:07 +03:00
|
|
|
xstrerror(error)
|
|
|
|
int error;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
if (error >= sys_nerr || error < 0) {
|
|
|
|
static char buf[128];
|
|
|
|
xsnprintf(buf, sizeof(buf), "Unknown error: %d", error);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
return sys_errlist[error];
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
xerrx(int eval, const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xerrx(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
va_list ap;
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
int eval;
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
va_start(ap);
|
|
|
|
eval = va_arg(ap, int);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
xvprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
exit(eval);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
xerr(int eval, const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xerr(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
int saved_errno = errno;
|
|
|
|
va_list ap;
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
int eval;
|
|
|
|
const char *fmt;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
va_start(ap);
|
|
|
|
eval = va_arg(ap, int);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
|
|
|
xvprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
xprintf(": %s\n", xstrerror(saved_errno));
|
|
|
|
exit(eval);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
xwarn(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xwarn(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
int saved_errno = errno;
|
|
|
|
va_list ap;
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
const char *fmt;
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
va_start(ap);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
|
|
|
xvprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
|
1999-03-01 19:40:07 +03:00
|
|
|
xprintf(": %s\n", xstrerror(saved_errno));
|
|
|
|
errno = saved_errno;
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
#ifdef __STDC__
|
|
|
|
xwarnx(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
xwarnx(va_alist)
|
|
|
|
va_dcl
|
|
|
|
#endif
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
va_list ap;
|
|
|
|
#ifdef __STDC__
|
|
|
|
va_start(ap, fmt);
|
|
|
|
#else
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
va_start(ap);
|
|
|
|
fmt = va_arg(ap, const char *);
|
|
|
|
#endif
|
|
|
|
xvprintf(fmt, ap);
|
2000-04-03 03:04:32 +04:00
|
|
|
(void) write(2, "\n", 1);
|
1999-03-01 19:40:07 +03:00
|
|
|
va_end(ap);
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-01 19:40:07 +03:00
|
|
|
xassert(file, line, failedexpr)
|
|
|
|
const char *file;
|
|
|
|
int line;
|
|
|
|
const char *failedexpr;
|
1996-12-16 23:37:55 +03:00
|
|
|
{
|
1999-03-01 19:40:07 +03:00
|
|
|
xprintf("assertion \"%s\" failed: file \"%s\", line %d\n",
|
|
|
|
failedexpr, file, line);
|
|
|
|
abort();
|
|
|
|
/* NOTREACHED */
|
1996-12-16 23:37:55 +03:00
|
|
|
}
|
1999-03-04 00:18:00 +03:00
|
|
|
#endif
|