2011-02-25 03:17:35 +03:00
|
|
|
/* $NetBSD: subr_prf.c,v 1.18 2011/02/25 00:17:36 joerg Exp $ */
|
1999-02-13 23:56:57 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. 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.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-02-13 23:56:57 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
* @(#)printf.c 8.1 (Berkeley) 6/11/93
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scaled down version of printf(3).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
2006-01-27 04:53:13 +03:00
|
|
|
#include <sys/stdint.h> /* XXX: for intptr_t */
|
1999-02-13 23:56:57 +03:00
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
|
|
|
#include "stand.h"
|
|
|
|
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
|
|
|
|
#define INTMAX_T longlong_t
|
|
|
|
#define UINTMAX_T u_longlong_t
|
|
|
|
#else
|
|
|
|
#define INTMAX_T long
|
|
|
|
#define UINTMAX_T u_long
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0 /* XXX: abuse intptr_t until the situation with ptrdiff_t is clear */
|
|
|
|
#define PTRDIFF_T ptrdiff_t
|
|
|
|
#else
|
|
|
|
#define PTRDIFF_T intptr_t
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
static void kprintn(void (*)(int), UINTMAX_T, int, int, int);
|
|
|
|
#else
|
|
|
|
static void kprintn(void (*)(int), UINTMAX_T, int);
|
|
|
|
#endif
|
2003-04-20 23:31:29 +04:00
|
|
|
static void sputchar(int);
|
|
|
|
static void kdoprnt(void (*)(int), const char *, va_list);
|
1999-02-13 23:56:57 +03:00
|
|
|
|
|
|
|
static char *sbuf, *ebuf;
|
|
|
|
|
2005-05-17 08:14:57 +04:00
|
|
|
const char hexdigits[] = "0123456789abcdef";
|
|
|
|
|
2010-01-19 18:26:45 +03:00
|
|
|
#define LONG 0x01
|
|
|
|
#ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
|
|
|
|
#define LLONG 0x02
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
#define ALT 0x04
|
|
|
|
#define SPACE 0x08
|
|
|
|
#define LADJUST 0x10
|
|
|
|
#define SIGN 0x20
|
|
|
|
#define ZEROPAD 0x40
|
|
|
|
#define NEGATIVE 0x80
|
|
|
|
#define KPRINTN(base) kprintn(put, ul, base, lflag, width)
|
|
|
|
#define RZERO() \
|
|
|
|
do { \
|
|
|
|
if ((lflag & (ZEROPAD|LADJUST)) == ZEROPAD) { \
|
|
|
|
while (width-- > 0) \
|
|
|
|
put('0'); \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#define RPAD() \
|
|
|
|
do { \
|
|
|
|
if (lflag & LADJUST) { \
|
|
|
|
while (width-- > 0) \
|
|
|
|
put(' '); \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#define LPAD() \
|
|
|
|
do { \
|
|
|
|
if ((lflag & (ZEROPAD|LADJUST)) == 0) { \
|
|
|
|
while (width-- > 0) \
|
|
|
|
put(' '); \
|
|
|
|
} \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#else /* LIBSA_PRINTF_WIDTH_SUPPORT */
|
|
|
|
#define KPRINTN(base) kprintn(put, ul, base)
|
|
|
|
#define RZERO() /**/
|
|
|
|
#define RPAD() /**/
|
|
|
|
#define LPAD() /**/
|
|
|
|
#endif /* LIBSA_PRINTF_WIDTH_SUPPORT */
|
|
|
|
|
|
|
|
#ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
|
|
|
|
#define KPRINT(base) \
|
|
|
|
do { \
|
|
|
|
ul = (lflag & LLONG) \
|
|
|
|
? va_arg(ap, u_longlong_t) \
|
|
|
|
: (lflag & LONG) \
|
|
|
|
? va_arg(ap, u_long) \
|
|
|
|
: va_arg(ap, u_int); \
|
|
|
|
KPRINTN(base); \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#else /* LIBSA_PRINTF_LONGLONG_SUPPORT */
|
|
|
|
#define KPRINT(base) \
|
|
|
|
do { \
|
|
|
|
ul = (lflag & LONG) \
|
|
|
|
? va_arg(ap, u_long) : va_arg(ap, u_int); \
|
|
|
|
KPRINTN(base); \
|
|
|
|
} while (/*CONSTCOND*/0)
|
|
|
|
#endif /* LIBSA_PRINTF_LONGLONG_SUPPORT */
|
|
|
|
|
1999-02-13 23:56:57 +03:00
|
|
|
static void
|
2003-04-20 23:31:29 +04:00
|
|
|
sputchar(int c)
|
1999-02-13 23:56:57 +03:00
|
|
|
{
|
2003-04-20 23:31:29 +04:00
|
|
|
|
1999-02-13 23:56:57 +03:00
|
|
|
if (sbuf < ebuf)
|
|
|
|
*sbuf++ = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vprintf(const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
|
|
|
|
kdoprnt(putchar, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
|
|
|
|
sbuf = buf;
|
|
|
|
ebuf = buf + size - 1;
|
|
|
|
kdoprnt(sputchar, fmt, ap);
|
|
|
|
*sbuf = '\0';
|
2007-11-24 16:20:53 +03:00
|
|
|
return sbuf - buf;
|
1999-02-13 23:56:57 +03:00
|
|
|
}
|
|
|
|
|
2002-02-18 02:19:00 +03:00
|
|
|
static void
|
2003-04-20 23:31:29 +04:00
|
|
|
kdoprnt(void (*put)(int), const char *fmt, va_list ap)
|
1999-02-13 23:56:57 +03:00
|
|
|
{
|
2000-03-30 16:19:46 +04:00
|
|
|
char *p;
|
2003-04-27 15:12:14 +04:00
|
|
|
int ch;
|
2010-01-19 18:26:45 +03:00
|
|
|
UINTMAX_T ul;
|
2003-04-27 15:12:14 +04:00
|
|
|
int lflag;
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
int width;
|
|
|
|
char *q;
|
|
|
|
#endif
|
1999-02-13 23:56:57 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
while ((ch = *fmt++) != '%') {
|
|
|
|
if (ch == '\0')
|
|
|
|
return;
|
|
|
|
put(ch);
|
|
|
|
}
|
|
|
|
lflag = 0;
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
width = 0;
|
|
|
|
#endif
|
2007-11-24 16:20:53 +03:00
|
|
|
reswitch:
|
|
|
|
switch (ch = *fmt++) {
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
case '#':
|
|
|
|
lflag |= ALT;
|
|
|
|
goto reswitch;
|
|
|
|
case ' ':
|
|
|
|
lflag |= SPACE;
|
|
|
|
goto reswitch;
|
|
|
|
case '-':
|
|
|
|
lflag |= LADJUST;
|
|
|
|
goto reswitch;
|
|
|
|
case '+':
|
|
|
|
lflag |= SIGN;
|
|
|
|
goto reswitch;
|
|
|
|
case '0':
|
|
|
|
lflag |= ZEROPAD;
|
|
|
|
goto reswitch;
|
|
|
|
case '1': case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9':
|
|
|
|
for (;;) {
|
|
|
|
width *= 10;
|
|
|
|
width += ch - '0';
|
|
|
|
ch = *fmt;
|
|
|
|
if ((unsigned)ch - '0' > 9)
|
|
|
|
break;
|
|
|
|
++fmt;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
goto reswitch;
|
1999-02-13 23:56:57 +03:00
|
|
|
case 'l':
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
|
|
|
|
if (*fmt == 'l') {
|
|
|
|
++fmt;
|
|
|
|
lflag |= LLONG;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
lflag |= LONG;
|
1999-02-13 23:56:57 +03:00
|
|
|
goto reswitch;
|
2006-01-27 04:53:13 +03:00
|
|
|
case 't':
|
2010-01-19 18:26:45 +03:00
|
|
|
if (sizeof(PTRDIFF_T) == sizeof(long))
|
|
|
|
lflag |= LONG;
|
2006-01-27 04:53:13 +03:00
|
|
|
goto reswitch;
|
|
|
|
case 'z':
|
2010-01-19 18:26:45 +03:00
|
|
|
if (sizeof(ssize_t) == sizeof(long))
|
|
|
|
lflag |= LONG;
|
2006-01-27 04:53:13 +03:00
|
|
|
goto reswitch;
|
1999-02-13 23:56:57 +03:00
|
|
|
case 'c':
|
|
|
|
ch = va_arg(ap, int);
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
--width;
|
|
|
|
#endif
|
|
|
|
RPAD();
|
|
|
|
put(ch & 0xFF);
|
|
|
|
LPAD();
|
1999-02-13 23:56:57 +03:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
p = va_arg(ap, char *);
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
for (q = p; *q; ++q);
|
|
|
|
width -= q - p;
|
|
|
|
#endif
|
|
|
|
RPAD();
|
|
|
|
while ((ch = (unsigned char)*p++))
|
1999-02-13 23:56:57 +03:00
|
|
|
put(ch);
|
2010-01-19 18:26:45 +03:00
|
|
|
LPAD();
|
1999-02-13 23:56:57 +03:00
|
|
|
break;
|
|
|
|
case 'd':
|
2010-01-19 18:26:45 +03:00
|
|
|
ul =
|
|
|
|
#ifdef LIBSA_PRINTF_LONGLONG_SUPPORT
|
|
|
|
(lflag & LLONG) ? va_arg(ap, longlong_t) :
|
|
|
|
#endif
|
|
|
|
(lflag & LONG) ? va_arg(ap, long) : va_arg(ap, int);
|
|
|
|
if ((INTMAX_T)ul < 0) {
|
|
|
|
ul = -(INTMAX_T)ul;
|
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
lflag |= NEGATIVE;
|
|
|
|
#else
|
1999-02-13 23:56:57 +03:00
|
|
|
put('-');
|
2010-01-19 18:26:45 +03:00
|
|
|
#endif
|
1999-02-13 23:56:57 +03:00
|
|
|
}
|
2010-01-19 18:26:45 +03:00
|
|
|
KPRINTN(10);
|
1999-02-13 23:56:57 +03:00
|
|
|
break;
|
|
|
|
case 'o':
|
2010-01-19 18:26:45 +03:00
|
|
|
KPRINT(8);
|
1999-02-13 23:56:57 +03:00
|
|
|
break;
|
|
|
|
case 'u':
|
2010-01-19 18:26:45 +03:00
|
|
|
KPRINT(10);
|
1999-02-13 23:56:57 +03:00
|
|
|
break;
|
|
|
|
case 'p':
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
lflag |= (LONG|ALT);
|
|
|
|
#else
|
1999-02-13 23:56:57 +03:00
|
|
|
put('0');
|
|
|
|
put('x');
|
2010-01-19 18:26:45 +03:00
|
|
|
#endif
|
2006-01-27 05:28:36 +03:00
|
|
|
/* FALLTHROUGH */
|
1999-02-13 23:56:57 +03:00
|
|
|
case 'x':
|
2010-01-19 18:26:45 +03:00
|
|
|
KPRINT(16);
|
1999-02-13 23:56:57 +03:00
|
|
|
break;
|
|
|
|
default:
|
2003-04-21 02:23:59 +04:00
|
|
|
if (ch == '\0')
|
|
|
|
return;
|
1999-02-13 23:56:57 +03:00
|
|
|
put(ch);
|
2007-11-24 16:20:53 +03:00
|
|
|
break;
|
1999-02-13 23:56:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
kprintn(void (*put)(int), UINTMAX_T ul, int base, int lflag, int width)
|
|
|
|
#else
|
|
|
|
kprintn(void (*put)(int), UINTMAX_T ul, int base)
|
|
|
|
#endif
|
1999-02-13 23:56:57 +03:00
|
|
|
{
|
2010-01-19 18:26:45 +03:00
|
|
|
/* hold a INTMAX_T in base 8 */
|
|
|
|
char *p, buf[(sizeof(INTMAX_T) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
|
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
char *q;
|
|
|
|
#endif
|
1999-02-13 23:56:57 +03:00
|
|
|
|
|
|
|
p = buf;
|
|
|
|
do {
|
2005-05-17 08:14:57 +04:00
|
|
|
*p++ = hexdigits[ul % base];
|
1999-02-13 23:56:57 +03:00
|
|
|
} while (ul /= base);
|
2010-01-19 18:26:45 +03:00
|
|
|
#ifdef LIBSA_PRINTF_WIDTH_SUPPORT
|
|
|
|
q = p;
|
|
|
|
if (lflag & ALT && *(p - 1) != '0') {
|
|
|
|
if (base == 8) {
|
|
|
|
*p++ = '0';
|
|
|
|
} else if (base == 16) {
|
|
|
|
*p++ = 'x';
|
|
|
|
*p++ = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lflag & NEGATIVE)
|
|
|
|
*p++ = '-';
|
|
|
|
else if (lflag & SIGN)
|
|
|
|
*p++ = '+';
|
|
|
|
else if (lflag & SPACE)
|
|
|
|
*p++ = ' ';
|
|
|
|
width -= p - buf;
|
|
|
|
if ((lflag & LADJUST) == 0) {
|
|
|
|
while (p > q)
|
|
|
|
put(*--p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RPAD();
|
|
|
|
RZERO();
|
1999-02-13 23:56:57 +03:00
|
|
|
do {
|
|
|
|
put(*--p);
|
|
|
|
} while (p > buf);
|
2010-01-19 18:26:45 +03:00
|
|
|
LPAD();
|
1999-02-13 23:56:57 +03:00
|
|
|
}
|