ack/lang/cem/libcc/stdio/doprnt.c

243 lines
4.1 KiB
C
Raw Permalink Normal View History

1994-06-24 14:02:31 +00:00
/* $Id$ */
1987-01-27 15:57:55 +00:00
#include <stdio.h>
1988-04-15 14:43:19 +00:00
#include <varargs.h>
1987-01-27 15:57:55 +00:00
#ifndef NOFLOAT
extern char *_pfloat();
extern char *_pscien();
1988-08-03 16:29:30 +00:00
extern char *gcvt();
1987-01-27 15:57:55 +00:00
#endif
# define wsize(par) ( (sizeof par) / sizeof (int) )
1988-04-15 14:43:19 +00:00
static char *gnum(f,ip,app) register char *f; int *ip; va_list *app; {
1987-01-27 15:57:55 +00:00
register int i,c;
if (*f == '*') {
1988-04-15 14:43:19 +00:00
*ip = va_arg((*app), int);
1987-01-27 15:57:55 +00:00
f++;
} else {
i = 0;
while ((c = *f - '0') >= 0 && c <= 9) {
i = i*10 + c;
f++;
}
*ip = i;
}
return(f);
}
#define signbit(par) (1L<<(sizeof par*8 -1))
static char *i_compute(val,base,s) unsigned val; char *s; {
int c;
c= val % base ;
val/= base ;
if (val)
s = i_compute(val,base,s);
*s++ = (c>9 ? c-10+'a' : c+'0');
return(s);
}
#ifndef NOLONG
static char *l_compute(l1,d,s) long l1; char *s; {
int c;
long l2;
if ( l1<0 ) {
/* assumption: d is a multiple of 2 */
c= l1&1 ;
l2= ( (l1>>1) & ~signbit(l1) );
l1= l2/(d>>1) ;
c += (l2%(d>>1))<<1 ;
} else {
c= l1 % d ;
l1= l1 / d ;
}
if (l1)
s = l_compute(l1,d,s);
*s++ = (c>9 ? c-10+'a' : c+'0');
return(s);
}
#endif
1988-04-15 14:43:19 +00:00
_doprnt(fmt,ap,stream)
register char *fmt; va_list ap ; FILE *stream;
1987-01-27 15:57:55 +00:00
{
register char *s;
#ifndef NOLONG
long l;
int lflag ;
#else
#define lflag 0
#endif
#ifndef NOFLOAT
double dbl ;
1988-08-03 16:29:30 +00:00
int capitalE = 0;
1987-01-27 15:57:55 +00:00
#endif
int inte ;
unsigned int uint ;
register int j ;
int i,c,rjust,width,ndigit,ndfnd,zfill;
1987-10-22 14:07:54 +00:00
char *oldfmt,*s1,buf[1025];
1987-01-27 15:57:55 +00:00
while (c = *fmt++) {
if (c != '%') {
#ifdef CPM
if (c == '\n') putc('\r',stream);
#endif
putc(c,stream);
continue;
}
#ifndef NOLONG
lflag = 0 ;
#endif
j = 10 ;
rjust = 0;
if (*fmt == '-') {
fmt++;
rjust++;
}
zfill = ' ';
if (*fmt == '0') {
fmt++;
zfill = '0';
}
1988-04-15 14:43:19 +00:00
fmt = gnum(fmt,&width,&ap);
1987-01-27 15:57:55 +00:00
ndigit = 0; ndfnd = 0;
if (*fmt == '.') {
fmt++; oldfmt = fmt;
1988-04-15 14:43:19 +00:00
fmt = gnum(fmt,&ndigit,&ap);
1987-01-27 15:57:55 +00:00
ndfnd = (fmt != oldfmt);
}
s = s1 = buf;
#ifndef NOLONG
if ( *fmt == 'l' || *fmt == 'L' ) {
fmt++ ; lflag++ ;
}
#endif
switch (c = *fmt++) {
default:
#ifdef CPM
1990-01-31 15:50:30 +00:00
if (c == '\n') putc('\r',stream);
1987-01-27 15:57:55 +00:00
#endif
putc(c,stream);
continue;
case 's':
1988-04-15 14:43:19 +00:00
s1 = va_arg(ap, char *);
if (s1 == 0)
s1 = "(null)";
1987-01-27 15:57:55 +00:00
s = s1;
do {
if (*s == 0)
break;
s++;
} while (--ndigit);
break;
case 'b':
j = 2;
case 'u':
getu:
if ( !lflag ) {
1988-04-15 14:43:19 +00:00
inte = va_arg(ap, int);
1987-01-27 15:57:55 +00:00
goto i_unsignd ;
}
#ifndef NOLONG
case 'U':
getlu:
1988-04-15 14:43:19 +00:00
l = va_arg(ap, long);
1987-01-27 15:57:55 +00:00
goto l_unsignd ;
case 'B':
j = 2 ;
goto getlu ;
case 'X':
j = 16;
goto getlu ;
case 'O':
j = 8;
goto getlu ;
case 'D':
l_signed:
1988-04-15 14:43:19 +00:00
l = va_arg(ap, long);
1987-01-27 15:57:55 +00:00
if (l < 0) {
*s++ = '-';
l = -l;
}
l_unsignd:
s = l_compute(l,j,s);
break;
#endif
case 'x':
j = 16;
goto getu ;
case 'o':
j = 8;
goto getu ;
case 'd':
if ( lflag ) goto l_signed; ;
1988-04-15 14:43:19 +00:00
inte = va_arg(ap, int);
1987-01-27 15:57:55 +00:00
if ( inte<0 ) {
*s++ = '-';
inte= -inte ;
}
i_unsignd:
s = i_compute(inte,j,s);
break;
case 'c':
1988-04-15 14:43:19 +00:00
uint = va_arg(ap, unsigned int);
1987-01-27 15:57:55 +00:00
for ( i= sizeof uint -1 ; i>=0 ; i-- ) {
if ( *s = uint%256 ) s++;
uint/= 256 ;
}
break;
#ifndef NOFLOAT
1988-08-03 16:29:30 +00:00
case 'E':
capitalE = 1;
/* fall through */
1987-01-27 15:57:55 +00:00
case 'e':
1987-10-22 13:35:05 +00:00
if (ndigit >= sizeof(buf)) ndigit = sizeof(buf) - 1;
1988-04-15 14:43:19 +00:00
dbl = va_arg(ap, double);
1987-01-27 15:57:55 +00:00
s = _pscien(dbl,s,ndigit,ndfnd);
break;
case 'f':
1987-10-22 13:35:05 +00:00
if (ndigit >= sizeof(buf)) ndigit = sizeof(buf) - 1;
1988-04-15 14:43:19 +00:00
dbl = va_arg(ap, double);
1987-01-27 15:57:55 +00:00
s = _pfloat(dbl,s,ndigit,ndfnd);
break;
1988-08-03 16:29:30 +00:00
case 'G':
capitalE = 1;
/* fall through */
case 'g':
if (ndigit >= sizeof(buf)) ndigit = sizeof(buf) - 1;
dbl = va_arg(ap, double);
1988-08-04 15:48:20 +00:00
s = gcvt(dbl, ndigit ? ndigit : 6, s) + strlen(s);
1988-08-03 16:29:30 +00:00
break;
1987-01-27 15:57:55 +00:00
#endif
case 'r':
1989-06-19 09:56:16 +00:00
ap = va_arg(ap, va_list);
1988-04-15 14:43:19 +00:00
fmt = va_arg(ap, char *);
1987-01-27 15:57:55 +00:00
continue;
}
1988-08-03 16:29:30 +00:00
#ifndef NOFLOAT
if (capitalE) {
register char *p = buf;
capitalE=0;
while (*p && *p != 'e') p++;
if (*p == 'e') *p = 'E';
}
#endif
1987-01-27 15:57:55 +00:00
j = s - s1;
if ((c = width - j) > 0)
if (rjust == 0)
do putc(zfill,stream);
while (--c);
while (--j >= 0)
putc(*s1++,stream);
while (--c >= 0)
putc(zfill,stream);
}
}