1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* doprnt.c - print formatted output
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "loc_incl.h"
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
/* gnum() is used to get the width and precision fields of a format. */
|
|
|
|
static const char *
|
1989-05-30 13:34:25 +00:00
|
|
|
gnum(register const char *f, int *ip, va_list *app)
|
|
|
|
{
|
1989-12-18 15:04:14 +00:00
|
|
|
register int i, c;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
if (*f == '*') {
|
|
|
|
*ip = va_arg((*app), int);
|
|
|
|
f++;
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
while ((c = *f - '0') >= 0 && c <= 9) {
|
|
|
|
i = i*10 + c;
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
*ip = i;
|
|
|
|
}
|
1989-06-26 10:37:05 +00:00
|
|
|
return f;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
#if _EM_WSIZE == _EM_PSIZE
|
1989-05-30 13:34:25 +00:00
|
|
|
#define set_pointer(flags) /* nothing */
|
1989-12-18 15:04:14 +00:00
|
|
|
#elif _EM_LSIZE == _EM_PSIZE
|
1989-05-30 13:34:25 +00:00
|
|
|
#define set_pointer(flags) (flags |= FL_LONG)
|
|
|
|
#else
|
|
|
|
#error garbage pointer size
|
1989-12-18 15:04:14 +00:00
|
|
|
#define set_pointer(flags) /* compilation might continue */
|
1989-05-30 13:34:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* print an ordinal number */
|
|
|
|
static char *
|
|
|
|
o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
|
|
|
|
{
|
|
|
|
long signed_val;
|
|
|
|
unsigned long unsigned_val;
|
|
|
|
char *old_s = s;
|
1991-02-20 09:48:00 +00:00
|
|
|
int i, base;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
switch (flags & (FL_SHORT | FL_LONG)) {
|
|
|
|
case FL_SHORT:
|
|
|
|
if (is_signed) {
|
|
|
|
signed_val = (short) va_arg(*ap, int);
|
|
|
|
} else {
|
|
|
|
unsigned_val = (unsigned short) va_arg(*ap, unsigned);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FL_LONG:
|
|
|
|
if (is_signed) {
|
|
|
|
signed_val = va_arg(*ap, long);
|
|
|
|
} else {
|
|
|
|
unsigned_val = va_arg(*ap, unsigned long);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (is_signed) {
|
|
|
|
signed_val = va_arg(*ap, int);
|
|
|
|
} else {
|
|
|
|
unsigned_val = va_arg(*ap, unsigned int);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_signed) {
|
|
|
|
if (signed_val < 0) {
|
|
|
|
*s++ = '-';
|
|
|
|
signed_val = -signed_val;
|
|
|
|
} else if (flags & FL_SIGN) *s++ = '+';
|
|
|
|
else if (flags & FL_SPACE) *s++ = ' ';
|
|
|
|
unsigned_val = signed_val;
|
|
|
|
}
|
1989-12-18 15:04:14 +00:00
|
|
|
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0';
|
1989-05-30 13:34:25 +00:00
|
|
|
if (!unsigned_val) {
|
1989-12-18 15:04:14 +00:00
|
|
|
if (!precision)
|
|
|
|
return s;
|
|
|
|
} else if (((flags & FL_ALT) && (c == 'x' || c == 'X'))
|
|
|
|
|| c == 'p') {
|
1989-05-30 13:34:25 +00:00
|
|
|
*s++ = '0';
|
1989-12-18 15:04:14 +00:00
|
|
|
*s++ = (c == 'X' ? 'X' : 'x');
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'b': base = 2; break;
|
|
|
|
case 'o': base = 8; break;
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
case 'u': base = 10; break;
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
case 'p': base = 16; break;
|
|
|
|
}
|
|
|
|
|
1989-06-26 10:37:05 +00:00
|
|
|
s = _i_compute(unsigned_val, base, s, precision - 1);
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
if (c == 'X')
|
|
|
|
while (old_s != s) {
|
|
|
|
*old_s = toupper(*old_s);
|
|
|
|
old_s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NOFLOAT
|
|
|
|
static char *
|
|
|
|
f_print(va_list *ap, int flags, char *s, char c, int precision)
|
|
|
|
{
|
|
|
|
register char *old_s = s;
|
|
|
|
long double ld_val;
|
|
|
|
|
|
|
|
if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double);
|
1989-12-18 15:04:14 +00:00
|
|
|
else ld_val = (long double) va_arg(*ap, double);
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case 'f':
|
1989-06-26 10:37:05 +00:00
|
|
|
s = _pfloat(ld_val, s, precision, flags);
|
1989-05-30 13:34:25 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
1989-12-18 15:04:14 +00:00
|
|
|
s = _pscien(ld_val, s, precision , flags);
|
1989-05-30 13:34:25 +00:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
1990-02-27 16:48:01 +00:00
|
|
|
s = _gcvt(ld_val, precision, s, flags) + strlen(s);
|
1989-05-30 13:34:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( c == 'E' || c == 'G') {
|
|
|
|
while (*old_s && *old_s != 'e') old_s++;
|
|
|
|
if (*old_s == 'e') *old_s = 'E';
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif /* NOFLOAT */
|
|
|
|
|
|
|
|
int
|
|
|
|
_doprnt(register const char *fmt, va_list ap, FILE *stream)
|
|
|
|
{
|
1989-12-18 15:04:14 +00:00
|
|
|
register char *s;
|
|
|
|
unsigned int uint;
|
|
|
|
register int j;
|
|
|
|
int i, c, width, precision, zfill, flags, between_fill;
|
1989-05-30 13:34:25 +00:00
|
|
|
int nrchars=0;
|
1989-12-18 15:04:14 +00:00
|
|
|
const char *oldfmt;
|
|
|
|
char *s1, buf[1025];
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
while (c = *fmt++) {
|
|
|
|
if (c != '%') {
|
1989-12-18 15:04:14 +00:00
|
|
|
#ifdef CPM
|
1989-05-30 13:34:25 +00:00
|
|
|
if (c == '\n') {
|
1989-06-26 10:37:05 +00:00
|
|
|
if (putc('\r', stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
1989-05-30 13:34:25 +00:00
|
|
|
nrchars++;
|
|
|
|
}
|
|
|
|
#endif
|
1989-06-26 10:37:05 +00:00
|
|
|
if (putc(c, stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
1989-05-30 13:34:25 +00:00
|
|
|
nrchars++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
flags = 0;
|
|
|
|
do {
|
|
|
|
switch(*fmt) {
|
|
|
|
case '-': flags |= FL_LJUST; break;
|
|
|
|
case '+': flags |= FL_SIGN; break;
|
|
|
|
case ' ': flags |= FL_SPACE; break;
|
|
|
|
case '#': flags |= FL_ALT; break;
|
|
|
|
case '0': flags |= FL_ZEROFILL; break;
|
|
|
|
default: flags |= FL_NOMORE; continue;
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
} while(!(flags & FL_NOMORE));
|
|
|
|
|
|
|
|
oldfmt = fmt;
|
|
|
|
fmt = gnum(fmt, &width, &ap);
|
|
|
|
if (fmt != oldfmt) flags |= FL_WIDTHSPEC;
|
|
|
|
|
|
|
|
if (*fmt == '.') {
|
|
|
|
fmt++; oldfmt = fmt;
|
|
|
|
fmt = gnum(fmt, &precision, &ap);
|
|
|
|
if (precision >= 0) flags |= FL_PRECSPEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & FL_WIDTHSPEC) && width < 0) {
|
|
|
|
width = -width;
|
|
|
|
flags |= FL_LJUST;
|
|
|
|
}
|
1989-06-26 10:37:05 +00:00
|
|
|
if (!(flags & FL_WIDTHSPEC)) width = 0;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
if (flags & FL_SIGN) flags &= ~FL_SPACE;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
1989-12-18 15:04:14 +00:00
|
|
|
if (flags & FL_LJUST) flags &= ~FL_ZEROFILL;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
s = s1 = buf;
|
|
|
|
|
|
|
|
switch (*fmt) {
|
|
|
|
case 'h': flags |= FL_SHORT; fmt++; break;
|
|
|
|
case 'l': flags |= FL_LONG; fmt++; break;
|
|
|
|
case 'L': flags |= FL_LONGDOUBLE; fmt++; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c = *fmt++) {
|
|
|
|
default:
|
1989-12-18 15:04:14 +00:00
|
|
|
#ifdef CPM
|
1989-05-30 13:34:25 +00:00
|
|
|
if (c == '\n') {
|
1989-06-26 10:37:05 +00:00
|
|
|
if (putc('\r', stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
1989-05-30 13:34:25 +00:00
|
|
|
nrchars++;
|
|
|
|
}
|
|
|
|
#endif
|
1989-06-26 10:37:05 +00:00
|
|
|
if (putc(c, stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
1989-05-30 13:34:25 +00:00
|
|
|
nrchars++;
|
|
|
|
continue;
|
|
|
|
case 'n':
|
|
|
|
if (flags & FL_SHORT)
|
|
|
|
*va_arg(ap, short *) = (short) nrchars;
|
|
|
|
else if (flags & FL_LONG)
|
|
|
|
*va_arg(ap, long *) = (long) nrchars;
|
|
|
|
else
|
|
|
|
*va_arg(ap, int *) = (int) nrchars;
|
|
|
|
continue;
|
|
|
|
case 's':
|
|
|
|
s1 = va_arg(ap, char *);
|
|
|
|
if (s1 == NULL)
|
|
|
|
s1 = "(null)";
|
|
|
|
s = s1;
|
1989-06-26 10:37:05 +00:00
|
|
|
while (precision || !(flags & FL_PRECSPEC)) {
|
1989-12-18 15:04:14 +00:00
|
|
|
if (*s == '\0')
|
1989-05-30 13:34:25 +00:00
|
|
|
break;
|
|
|
|
s++;
|
1989-06-26 10:37:05 +00:00
|
|
|
precision--;
|
|
|
|
}
|
1989-05-30 13:34:25 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
set_pointer(flags);
|
|
|
|
/* fallthrough */
|
|
|
|
case 'b':
|
|
|
|
case 'o':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
if (!(flags & FL_PRECSPEC)) precision = 1;
|
1989-12-18 15:04:14 +00:00
|
|
|
else if (c != 'p') flags &= ~FL_ZEROFILL;
|
1989-05-30 13:34:25 +00:00
|
|
|
s = o_print(&ap, flags, s, c, precision, 0);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
flags |= FL_SIGNEDCONV;
|
|
|
|
if (!(flags & FL_PRECSPEC)) precision = 1;
|
1989-12-18 15:04:14 +00:00
|
|
|
else flags &= ~FL_ZEROFILL;
|
1989-05-30 13:34:25 +00:00
|
|
|
s = o_print(&ap, flags, s, c, precision, 1);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
uint = va_arg(ap, unsigned int);
|
1989-12-18 15:04:14 +00:00
|
|
|
for ( i= sizeof(uint) -1 ; i>=0 ; i-- ) {
|
1989-05-30 13:34:25 +00:00
|
|
|
if ( *s = uint%256 ) s++;
|
|
|
|
uint/= 256 ;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifndef NOFLOAT
|
|
|
|
case 'G':
|
|
|
|
case 'g':
|
|
|
|
if ((flags & FL_PRECSPEC) && (precision == 0))
|
|
|
|
precision = 1;
|
|
|
|
case 'f':
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
if (!(flags & FL_PRECSPEC))
|
|
|
|
precision = 6;
|
|
|
|
|
|
|
|
if (precision >= sizeof(buf))
|
|
|
|
precision = sizeof(buf) - 1;
|
|
|
|
|
|
|
|
flags |= FL_SIGNEDCONV;
|
|
|
|
s = f_print(&ap, flags, s, c, precision);
|
|
|
|
break;
|
|
|
|
#endif /* NOFLOAT */
|
|
|
|
case 'r':
|
|
|
|
ap = va_arg(ap, va_list);
|
|
|
|
fmt = va_arg(ap, char *);
|
|
|
|
continue;
|
|
|
|
}
|
1989-12-18 15:04:14 +00:00
|
|
|
zfill = ' ';
|
|
|
|
if (flags & FL_ZEROFILL) zfill = '0';
|
1989-05-30 13:34:25 +00:00
|
|
|
j = s - s1;
|
|
|
|
|
|
|
|
/* between_fill is true under the following conditions:
|
|
|
|
* 1- the fill character is '0'
|
|
|
|
* and
|
|
|
|
* 2a- the number is of the form 0x... or 0X...
|
|
|
|
* or
|
|
|
|
* 2b- the number contains a sign or space
|
|
|
|
*/
|
|
|
|
between_fill = 0;
|
|
|
|
if ((flags & FL_ZEROFILL)
|
|
|
|
&& (((c == 'x' || c == 'X') && (flags & FL_ALT))
|
|
|
|
|| (c == 'p')
|
|
|
|
|| ((flags & FL_SIGNEDCONV)
|
|
|
|
&& ( *s1 == '+' || *s1 == '-' || *s1 == ' '))))
|
|
|
|
between_fill++;
|
|
|
|
|
|
|
|
if ((i = width - j) > 0)
|
|
|
|
if (!(flags & FL_LJUST)) { /* right justify */
|
|
|
|
nrchars += i;
|
|
|
|
if (between_fill) {
|
1989-06-26 10:37:05 +00:00
|
|
|
if (flags & FL_SIGNEDCONV) {
|
|
|
|
j--; nrchars++;
|
|
|
|
if (putc(*s1++, stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
|
|
|
} else {
|
|
|
|
j -= 2; nrchars += 2;
|
|
|
|
if ((putc(*s1++, stream) == EOF)
|
|
|
|
|| (putc(*s1++, stream) == EOF))
|
|
|
|
return nrchars ? -nrchars : -1;
|
|
|
|
}
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
1989-06-26 10:37:05 +00:00
|
|
|
do {
|
|
|
|
if (putc(zfill, stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
|
|
|
} while (--i);
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nrchars += j;
|
1989-06-26 10:37:05 +00:00
|
|
|
while (--j >= 0) {
|
|
|
|
if (putc(*s1++, stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
|
|
|
}
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
if (i > 0) nrchars += i;
|
|
|
|
while (--i >= 0)
|
1989-06-26 10:37:05 +00:00
|
|
|
if (putc(zfill, stream) == EOF)
|
|
|
|
return nrchars ? -nrchars : -1;
|
1989-05-30 13:34:25 +00:00
|
|
|
}
|
|
|
|
return nrchars;
|
|
|
|
}
|