various fixes and improvements
This commit is contained in:
parent
11349c78cd
commit
27d7d5ed68
24 changed files with 173 additions and 417 deletions
|
@ -25,7 +25,7 @@ gnum(register const char *f, int *ip, va_list *app)
|
|||
}
|
||||
*ip = i;
|
||||
}
|
||||
return(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
#if EM_WSIZE == EM_PSIZE
|
||||
|
@ -38,16 +38,16 @@ gnum(register const char *f, int *ip, va_list *app)
|
|||
#endif
|
||||
|
||||
char *
|
||||
i_compute(unsigned long val, int base, char *s, int nrdigits)
|
||||
_i_compute(unsigned long val, int base, char *s, int nrdigits)
|
||||
{
|
||||
int c;
|
||||
|
||||
c= val % base ;
|
||||
val /= base ;
|
||||
if (val || nrdigits > 0)
|
||||
s = i_compute(val, base, s, nrdigits - 1);
|
||||
s = _i_compute(val, base, s, nrdigits - 1);
|
||||
*s++ = (c>9 ? c-10+'a' : c+'0');
|
||||
return(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
/* print an ordinal number */
|
||||
|
@ -114,7 +114,7 @@ o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
|
|||
case 'p': base = 16; break;
|
||||
}
|
||||
|
||||
s = i_compute(unsigned_val, base, s, precision - 1);
|
||||
s = _i_compute(unsigned_val, base, s, precision - 1);
|
||||
|
||||
if (c == 'X')
|
||||
while (old_s != s) {
|
||||
|
@ -130,42 +130,23 @@ static char *
|
|||
f_print(va_list *ap, int flags, char *s, char c, int precision)
|
||||
{
|
||||
register char *old_s = s;
|
||||
double d_val;
|
||||
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
long double ld_val;
|
||||
|
||||
if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double);
|
||||
else
|
||||
#endif
|
||||
d_val = va_arg(*ap, double);
|
||||
ld_val = (long double) va_arg(*ap, double);
|
||||
|
||||
switch(c) {
|
||||
case 'f':
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
if (flags & FL_LONGDOUBLE)
|
||||
s = _pfloat_ldbl(ld_val, s, precision, flags);
|
||||
else
|
||||
#endif
|
||||
s = _pfloat(d_val, s, precision, flags);
|
||||
s = _pfloat(ld_val, s, precision, flags);
|
||||
break;
|
||||
case 'e':
|
||||
case 'E':
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
if (flags & FL_LONGDOUBLE)
|
||||
s = _pscien_ldbl(ld_val, s, precision, flags);
|
||||
else
|
||||
#endif
|
||||
s = _pscien(d_val, s, precision, flags);
|
||||
s = _pscien(ld_val, s, precision, flags);
|
||||
break;
|
||||
case 'g':
|
||||
case 'G':
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
if (flags & FL_LONGDOUBLE)
|
||||
s = gcvt_ldbl(ld_val, precision, s, flags) + strlen(s);
|
||||
else
|
||||
#endif
|
||||
s = gcvt(d_val, precision, s, flags) + strlen(s);
|
||||
s = gcvt(ld_val, precision, s, flags) + strlen(s);
|
||||
break;
|
||||
}
|
||||
if ( c == 'E' || c == 'G') {
|
||||
|
@ -190,11 +171,13 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
|
|||
if (c != '%') {
|
||||
#ifdef CPM
|
||||
if (c == '\n') {
|
||||
putc('\r', stream);
|
||||
if (putc('\r', stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
nrchars++;
|
||||
}
|
||||
#endif
|
||||
putc(c, stream);
|
||||
if (putc(c, stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
nrchars++;
|
||||
continue;
|
||||
}
|
||||
|
@ -225,7 +208,7 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
|
|||
width = -width;
|
||||
flags |= FL_LJUST;
|
||||
}
|
||||
if (!(flags & FL_WIDTHSPEC)) width = 1;
|
||||
if (!(flags & FL_WIDTHSPEC)) width = 0;
|
||||
|
||||
if (flags & FL_SIGN)
|
||||
flags &= ~FL_SPACE;
|
||||
|
@ -249,11 +232,13 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
|
|||
default:
|
||||
#ifdef CPM
|
||||
if (c == '\n') {
|
||||
putc('\r', stream);
|
||||
if (putc('\r', stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
nrchars++;
|
||||
}
|
||||
#endif
|
||||
putc(c, stream);
|
||||
if (putc(c, stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
nrchars++;
|
||||
continue;
|
||||
case 'n':
|
||||
|
@ -269,11 +254,12 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
|
|||
if (s1 == NULL)
|
||||
s1 = "(null)";
|
||||
s = s1;
|
||||
do {
|
||||
while (precision || !(flags & FL_PRECSPEC)) {
|
||||
if (*s == '\0')
|
||||
break;
|
||||
s++;
|
||||
} while (!(flags & FL_PRECSPEC) || --precision);
|
||||
precision--;
|
||||
}
|
||||
break;
|
||||
case 'p':
|
||||
set_pointer(flags);
|
||||
|
@ -343,26 +329,33 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
|
|||
if (!(flags & FL_LJUST)) { /* right justify */
|
||||
nrchars += i;
|
||||
if (between_fill) {
|
||||
if (flags & FL_SIGNEDCONV) {
|
||||
j--; nrchars++;
|
||||
putc(*s1++, stream);
|
||||
} else {
|
||||
j -= 2; nrchars += 2;
|
||||
putc(*s1++, stream);
|
||||
putc(*s1++, stream);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
do putc(zfill, stream);
|
||||
while (--i);
|
||||
do {
|
||||
if (putc(zfill, stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
} while (--i);
|
||||
}
|
||||
|
||||
nrchars += j;
|
||||
while (--j >= 0)
|
||||
putc(*s1++, stream);
|
||||
while (--j >= 0) {
|
||||
if (putc(*s1++, stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
}
|
||||
|
||||
if (i > 0) nrchars += i;
|
||||
while (--i >= 0)
|
||||
putc(zfill, stream);
|
||||
if (putc(zfill, stream) == EOF)
|
||||
return nrchars ? -nrchars : -1;
|
||||
}
|
||||
return nrchars;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include <stdarg.h>
|
||||
#include "loc_incl.h"
|
||||
|
||||
#define NUMLEN 128
|
||||
#define NUMLEN 512
|
||||
|
||||
static char Xtable[128];
|
||||
static char inp_buf[NUMLEN];
|
||||
|
@ -157,6 +157,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
|
|||
{
|
||||
int done = 0; /* number of items done */
|
||||
int nrchars = 0; /* number of characters read */
|
||||
int conv = 0; /* # of conversions */
|
||||
int base; /* conversion base */
|
||||
unsigned long val; /* an integer value */
|
||||
char *str, *tmp_string; /* temporary pointers */
|
||||
|
@ -166,7 +167,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
|
|||
int kind;
|
||||
register int ic;
|
||||
#ifndef NOFLOAT
|
||||
double d_val;
|
||||
long double ld_val;
|
||||
#endif
|
||||
|
||||
ic = getc(stream);
|
||||
|
@ -192,7 +193,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
|
|||
if (!*format)
|
||||
break; /* end of format */
|
||||
if (ic == EOF)
|
||||
return done; /* seen an error */
|
||||
return conv ? done : EOF;
|
||||
if (*format != '%') {
|
||||
if (ic != *format)
|
||||
break; /* matching error */
|
||||
|
@ -229,7 +230,9 @@ _doscan(register FILE *stream, const char *format, va_list ap)
|
|||
ic = getc(stream);
|
||||
nrchars++;
|
||||
}
|
||||
if (ic == EOF) return conv ? done : EOF;
|
||||
}
|
||||
conv++;
|
||||
switch (kind) {
|
||||
default:
|
||||
if (kind == ic) continue;
|
||||
|
@ -384,16 +387,14 @@ _doscan(register FILE *stream, const char *format, va_list ap)
|
|||
if (str < inp_buf) return done;
|
||||
nrchars += str - inp_buf + 1;
|
||||
if (!(flags & FL_NOASSIGN)) {
|
||||
d_val = strtod(inp_buf, &tmp_string);
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
ld_val = strtod(inp_buf, &tmp_string);
|
||||
if (flags & FL_LONGDOUBLE)
|
||||
*va_arg(ap, long double *) = (long double) d_val;
|
||||
*va_arg(ap, long double *) = (long double) ld_val;
|
||||
else
|
||||
#endif /* EM_DSIZE != EM_LDSIZE */
|
||||
if (flags & FL_LONG)
|
||||
*va_arg(ap, double *) = (double) d_val;
|
||||
*va_arg(ap, double *) = (double) ld_val;
|
||||
else
|
||||
*va_arg(ap, float *) = (float) d_val;
|
||||
*va_arg(ap, float *) = (float) ld_val;
|
||||
done++;
|
||||
}
|
||||
break;
|
||||
|
@ -401,10 +402,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
|
|||
} /* end switch */
|
||||
++format;
|
||||
}
|
||||
all_done:
|
||||
if (ic != EOF)
|
||||
ungetc(ic, stream);
|
||||
/* nrchars--; just to keep it clean */
|
||||
quit:
|
||||
return done;
|
||||
return conv ? done : EOF;
|
||||
}
|
||||
|
|
|
@ -5,24 +5,24 @@
|
|||
|
||||
#ifndef NOFLOAT
|
||||
|
||||
static char *cvt(double value, int ndigit, int *decpt, int *sign, int ecvtflag);
|
||||
static char *cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag);
|
||||
#define NDIGITS 128
|
||||
|
||||
char *
|
||||
ecvt(double value, int ndigit, int *decpt, int *sign)
|
||||
ecvt(long double value, int ndigit, int *decpt, int *sign)
|
||||
{
|
||||
return cvt(value, ndigit, decpt, sign, 1);
|
||||
}
|
||||
|
||||
char *
|
||||
fcvt(double value, int ndigit, int *decpt, int *sign)
|
||||
fcvt(long double value, int ndigit, int *decpt, int *sign)
|
||||
{
|
||||
return cvt(value, ndigit, decpt, sign, 0);
|
||||
}
|
||||
|
||||
static struct powers_of_10 {
|
||||
double pval;
|
||||
double rpval;
|
||||
long double pval;
|
||||
long double rpval;
|
||||
int exp;
|
||||
} p10[] = {
|
||||
1.0e32, 1.0e-32, 32,
|
||||
|
@ -35,7 +35,7 @@ static struct powers_of_10 {
|
|||
};
|
||||
|
||||
static char *
|
||||
cvt(double value, int ndigit, int *decpt, int *sign, int ecvtflag)
|
||||
cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag)
|
||||
{
|
||||
static char buf[NDIGITS+1];
|
||||
register char *p = buf;
|
||||
|
@ -104,93 +104,4 @@ cvt(double value, int ndigit, int *decpt, int *sign, int ecvtflag)
|
|||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
|
||||
static char *cvt_ldbl(long double value, int ndigit, int *decpt,
|
||||
int *sign, int ecvtflag);
|
||||
|
||||
char *
|
||||
ecvt_ldbl(long double value, int ndigit, int *decpt, int *sign)
|
||||
{
|
||||
return cvt_ldbl(value, ndigit, decpt, sign, 1);
|
||||
}
|
||||
|
||||
char *
|
||||
fcvt_ldbl(long double value, int ndigit, int *decpt, int *sign)
|
||||
{
|
||||
return cvt_ldbl(value, ndigit, decpt, sign, 0);
|
||||
}
|
||||
|
||||
static char *
|
||||
cvt_ldbl(long double value, int ndigit, int *decpt, int *sign, int ecvtflag)
|
||||
{
|
||||
static char buf[NDIGITS+1];
|
||||
register char *p = buf;
|
||||
register char *pe;
|
||||
|
||||
if (ndigit < 0) ndigit = 0;
|
||||
if (ndigit > NDIGITS) ndigit = NDIGITS;
|
||||
pe = &buf[ndigit];
|
||||
buf[0] = '\0';
|
||||
|
||||
*sign = 0;
|
||||
if (value < 0) {
|
||||
*sign = 1;
|
||||
value = -value;
|
||||
}
|
||||
|
||||
*decpt = 0;
|
||||
if (value != 0.0) {
|
||||
register struct powers_of_10 *pp = &p10[0];
|
||||
|
||||
if (value >= 10.0) do {
|
||||
while (value >= pp->pval) {
|
||||
value *= pp->rpval;
|
||||
*decpt += pp->exp;
|
||||
}
|
||||
} while ((++pp)->exp > 0);
|
||||
|
||||
pp = &p10[0];
|
||||
if (value < 1.0) do {
|
||||
while (value * pp->pval < 10.0) {
|
||||
value *= pp->pval;
|
||||
*decpt -= pp->exp;
|
||||
}
|
||||
} while ((++pp)->exp > 0);
|
||||
|
||||
(*decpt)++; /* because now value in [1.0, 10.0) */
|
||||
}
|
||||
if (!ecvtflag) {
|
||||
/* for fcvt() we need ndigit digits behind the dot */
|
||||
pe += *decpt;
|
||||
if (pe > &buf[NDIGITS]) pe = &buf[NDIGITS];
|
||||
}
|
||||
while (p <= pe) {
|
||||
*p++ = (int)value + '0';
|
||||
value = 10.0 * (value - (int)value);
|
||||
}
|
||||
if (pe >= buf) {
|
||||
p = pe;
|
||||
*p += 5; /* round of at the end */
|
||||
while (*p > '9') {
|
||||
*p = '0';
|
||||
if (p > buf) ++*--p;
|
||||
else {
|
||||
*p = '1';
|
||||
++*decpt;
|
||||
if (!ecvtflag) {
|
||||
/* maybe add another digit at the end,
|
||||
because the point was shifted right
|
||||
*/
|
||||
if (pe > buf) *pe = '0';
|
||||
pe++;
|
||||
}
|
||||
}
|
||||
}
|
||||
*pe = '\0';
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
#endif /* EM_DSIZE != EM_LDSIZE */
|
||||
#endif /* NOFLOAT */
|
||||
|
|
|
@ -20,12 +20,12 @@ fclose(FILE *fp)
|
|||
break;
|
||||
}
|
||||
if (i >= FOPEN_MAX)
|
||||
return(EOF);
|
||||
if (fflush(fp)) retval = EOF; /* ??? */
|
||||
if (close(fileno(fp))) retval = EOF; /* ??? */
|
||||
return EOF;
|
||||
if (fflush(fp)) retval = EOF;
|
||||
if (close(fileno(fp))) retval = EOF;
|
||||
if ( io_testflag(fp,_IOMYBUF) && fp->_buf )
|
||||
free((void *)fp->_buf);
|
||||
if (fp != &_stdin && fp != &_stdout && fp != &_stderr)
|
||||
if (fp != stdin && fp != stdout && fp != stderr)
|
||||
free((void *)fp);
|
||||
return(retval);
|
||||
return retval;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include <stdio.h>
|
||||
#include "loc_incl.h"
|
||||
|
||||
int write(int d, static char *buf, int nbytes);
|
||||
int write(int d, const char *buf, int nbytes);
|
||||
|
||||
int
|
||||
fflush(FILE *stream)
|
||||
|
@ -20,28 +20,39 @@ fflush(FILE *stream)
|
|||
return retval;
|
||||
}
|
||||
|
||||
if (!stream->_buf ||
|
||||
io_testflag(stream,_IONBF) ||
|
||||
!io_testflag(stream,_IOWRITE) )
|
||||
return(0);
|
||||
if (!stream->_buf
|
||||
|| io_testflag(stream, _IONBF)
|
||||
|| !io_testflag(stream, _IOWRITE)
|
||||
|| !io_testflag(stream, _IOWRITING))
|
||||
return 0;
|
||||
|
||||
if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
|
||||
stream->_flags &= ~_IOWRITING;
|
||||
|
||||
/*
|
||||
if (io_testflag(stream, _IOLBF))
|
||||
count = -stream->_count;
|
||||
else count = stream->_bufsiz - stream->_count;
|
||||
*/
|
||||
count = stream->_ptr - stream->_buf;
|
||||
stream->_ptr = stream->_buf;
|
||||
|
||||
if ( count <= 0 )
|
||||
return(0);
|
||||
return 0;
|
||||
|
||||
c1 = write(stream->_fd, (char *)stream->_buf, count);
|
||||
|
||||
if ( count == c1 ) {
|
||||
if (io_testflag(stream, _IOLBF))
|
||||
stream->_count = 0;
|
||||
else stream->_count = stream->_bufsiz;
|
||||
stream->_ptr = stream->_buf;
|
||||
return(count);
|
||||
}
|
||||
stream->_count = 0;
|
||||
|
||||
/*
|
||||
if(stream != stderr)
|
||||
fprintf(stderr,"written %d bytes :\"%.*s\"\n"
|
||||
, c1, c1, stream->_buf);
|
||||
*/
|
||||
|
||||
if ( count == c1 )
|
||||
return 0;
|
||||
|
||||
stream->_flags |= _IOERR;
|
||||
return(EOF);
|
||||
return EOF;
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ fgets(char *s, int n, FILE *stream)
|
|||
if ( ch == '\n')
|
||||
break;
|
||||
}
|
||||
if (ch == EOF && ptr==s)
|
||||
return(NULL);
|
||||
if (ch == EOF && ptr == s)
|
||||
return (char *)NULL;
|
||||
*ptr = '\0';
|
||||
return(s);
|
||||
return s;
|
||||
}
|
||||
|
|
|
@ -16,12 +16,13 @@ _fillbuf(register FILE *stream)
|
|||
|
||||
stream->_count = 0;
|
||||
if (fileno(stream) < 0) return EOF;
|
||||
if (io_testflag(stream, (_IOEOF | _IOERR )))
|
||||
return (EOF);
|
||||
|
||||
if (!io_testflag(stream, _IOREAD) )
|
||||
return (EOF);
|
||||
if (io_testflag(stream, (_IOEOF | _IOERR ))) return EOF;
|
||||
if (!io_testflag(stream, _IOREAD)) return EOF;
|
||||
if (io_testflag(stream, _IOWRITING)) return EOF;
|
||||
|
||||
if (!io_testflag(stream, _IOREADING))
|
||||
stream->_flags |= _IOREADING;
|
||||
|
||||
if (!io_testflag(stream, _IONBF) && !stream->_buf) {
|
||||
stream->_buf = (unsigned char *) malloc(BUFSIZ);
|
||||
if (!stream->_buf) {
|
||||
|
@ -38,6 +39,10 @@ _fillbuf(register FILE *stream)
|
|||
}
|
||||
stream->_ptr = stream->_buf;
|
||||
stream->_count = read(stream->_fd, (char *)stream->_buf, stream->_bufsiz);
|
||||
/*
|
||||
fprintf(stderr,"read %d bytes, \"%.*s\"\n"
|
||||
, stream->_count, stream->_count, stream->_buf);
|
||||
*/
|
||||
|
||||
if (stream->_count <= 0){
|
||||
if (stream->_count == 0) {
|
||||
|
@ -46,7 +51,7 @@ _fillbuf(register FILE *stream)
|
|||
else
|
||||
stream->_flags |= _IOERR;
|
||||
|
||||
return (EOF);
|
||||
return EOF;
|
||||
}
|
||||
stream->_count--;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "loc_incl.h"
|
||||
|
||||
char *
|
||||
_pfloat(double r, register char *s, int n, int flags)
|
||||
_pfloat(long double r, register char *s, int n, int flags)
|
||||
{
|
||||
register char *s1;
|
||||
int sign, dp;
|
||||
|
@ -36,11 +36,11 @@ _pfloat(double r, register char *s, int n, int flags)
|
|||
while (--i >= 0)
|
||||
if (*s1) *s++ = *s1++;
|
||||
else *s++ = '0';
|
||||
return(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
char *
|
||||
_pscien(double r, register char *s, int n, int flags)
|
||||
_pscien(long double r, register char *s, int n, int flags)
|
||||
{
|
||||
int sign, dp;
|
||||
register char *s1;
|
||||
|
@ -72,77 +72,6 @@ _pscien(double r, register char *s, int n, int flags)
|
|||
}
|
||||
*s++ = '0' + (dp/10);
|
||||
*s++ = '0' + (dp%10);
|
||||
return(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
char *
|
||||
_pfloat_ldbl(long double r, register char *s, int n, int flags)
|
||||
{
|
||||
register char *s1;
|
||||
int sign, dp;
|
||||
register int i;
|
||||
|
||||
s1 = fcvt(r, n, &dp, &sign);
|
||||
if (sign)
|
||||
*s++ = '-';
|
||||
else if (flags & FL_SIGN)
|
||||
*s++ = '+';
|
||||
else if (flags & FL_SPACE)
|
||||
*s++ = ' ';
|
||||
|
||||
if (dp<=0)
|
||||
*s++ = '0';
|
||||
for (i=dp; i>0; i--)
|
||||
if (*s1) *s++ = *s1++;
|
||||
else *s++ = '0';
|
||||
if (((i=n) > 0) || (flags & FL_ALT))
|
||||
*s++ = '.';
|
||||
while (++dp <= 0) {
|
||||
if (--i<0)
|
||||
break;
|
||||
*s++ = '0';
|
||||
}
|
||||
while (--i >= 0)
|
||||
if (*s1) *s++ = *s1++;
|
||||
else *s++ = '0';
|
||||
return(s);
|
||||
}
|
||||
|
||||
char *
|
||||
_pscien_ldbl(long double r, register char *s, int n, int flags)
|
||||
{
|
||||
int sign, dp;
|
||||
register char *s1;
|
||||
|
||||
s1 = ecvt(r, n + 1, &dp, &sign);
|
||||
if (sign)
|
||||
*s++ = '-';
|
||||
else if (flags & FL_SIGN)
|
||||
*s++ = '+';
|
||||
else if (flags & FL_SPACE)
|
||||
*s++ = ' ';
|
||||
|
||||
*s++ = *s1++;
|
||||
if ((n > 0) || (flags & FL_ALT))
|
||||
*s++ = '.';
|
||||
while (--n>0)
|
||||
if (*s1) *s++ = *s1++;
|
||||
else *s++ = '0';
|
||||
*s++ = 'e';
|
||||
if ( r != 0 ) --dp ;
|
||||
if ( dp<0 ) {
|
||||
*s++ = '-' ; dp= -dp ;
|
||||
} else {
|
||||
*s++ = '+' ;
|
||||
}
|
||||
if (dp >= 100) {
|
||||
*s++ = '0' + (dp / 100);
|
||||
dp %= 100;
|
||||
}
|
||||
*s++ = '0' + (dp/10);
|
||||
*s++ = '0' + (dp%10);
|
||||
return(s);
|
||||
}
|
||||
#endif /* EM_DSIZE != EM_LDSIZE */
|
||||
#endif /* NOFLOAT */
|
||||
|
|
|
@ -14,6 +14,11 @@ int
|
|||
_flushbuf(int c, FILE * stream)
|
||||
{
|
||||
if (fileno(stream) < 0) return EOF;
|
||||
if (!io_testflag(stream, _IOWRITE)) return EOF;
|
||||
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
|
||||
|
||||
stream->_flags &= ~_IOREADING;
|
||||
stream->_flags |= _IOWRITING;
|
||||
if (!io_testflag(stream, _IONBF)) {
|
||||
if (!stream->_buf) {
|
||||
if (stream == stdout && isatty(fileno(stdout))) {
|
||||
|
|
|
@ -26,42 +26,34 @@ fopen(const char *name, const char *mode)
|
|||
return (FILE *)NULL;
|
||||
|
||||
switch(*mode++) {
|
||||
|
||||
case 'r':
|
||||
flags |= _IOREAD;
|
||||
flags |= _IOREAD | _IOREADING;
|
||||
rwmode = O_RDONLY;
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
flags |= _IOWRITE;
|
||||
flags |= _IOWRITE | _IOWRITING;
|
||||
rwmode = O_WRONLY;
|
||||
rwflags = O_CREAT | O_TRUNC;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
flags |= _IOWRITE;
|
||||
flags |= _IOWRITE | _IOWRITING;
|
||||
rwmode = O_WRONLY;
|
||||
rwflags |= O_APPEND | O_CREAT;
|
||||
break;
|
||||
|
||||
default:
|
||||
return (FILE *)NULL;
|
||||
}
|
||||
|
||||
while (*mode) {
|
||||
switch(*mode++) {
|
||||
|
||||
case 'b':
|
||||
break;
|
||||
|
||||
case '+':
|
||||
rwmode = O_RDWR;
|
||||
flags |= _IOREAD | _IOWRITE;
|
||||
break;
|
||||
|
||||
default:
|
||||
return (FILE *)NULL;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,10 +66,13 @@ fopen(const char *name, const char *mode)
|
|||
return (FILE *)NULL;
|
||||
}
|
||||
|
||||
if ((flags & _IOREAD) && (flags & _IOWRITE))
|
||||
flags &= ~(_IOREADING | _IOWRITING);
|
||||
|
||||
stream->_count = 0;
|
||||
stream->_fd = fd;
|
||||
stream->_flags = flags;
|
||||
stream->_buf = 0;
|
||||
stream->_buf = NULL;
|
||||
_iotable[i] = stream;
|
||||
return stream;
|
||||
}
|
||||
|
|
|
@ -16,8 +16,8 @@ fprintf(FILE *stream, const char *format, ...)
|
|||
va_start(ap, format);
|
||||
|
||||
retval = _doprnt (format, ap, stream);
|
||||
if ( io_testflag(stream,_IOLBF) )
|
||||
fflush(stream);
|
||||
if ( retval >= 0 && io_testflag(stream,_IOLBF) )
|
||||
if (fflush(stream)) return EOF;
|
||||
|
||||
va_end(ap);
|
||||
|
||||
|
|
|
@ -84,5 +84,5 @@ freopen(const char *name, const char *mode, FILE *stream)
|
|||
|
||||
stream->_fd = fd;
|
||||
stream->_flags = flags;
|
||||
return(stream);
|
||||
return stream;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ int lseek(int d, int offset, int whence);
|
|||
int
|
||||
fseek(FILE *stream, long int offset, int whence)
|
||||
{
|
||||
int count;
|
||||
int count, adjust = 0;
|
||||
long pos;
|
||||
|
||||
#if (SEEK_CUR != L_INCR) || (SEEK_SET != L_SET) || (SEEK_END != L_XTND)
|
||||
|
@ -31,30 +31,24 @@ fseek(FILE *stream, long int offset, int whence)
|
|||
stream->_flags &= ~(_IOEOF | _IOERR);
|
||||
/* Clear both the end of file and error flags */
|
||||
|
||||
if ( io_testflag(stream,_IOREAD) ) {
|
||||
if ( whence < 2 && stream->_buf
|
||||
&& !io_testflag(stream,_IONBF) ) {
|
||||
count = stream->_count;
|
||||
pos = offset;
|
||||
|
||||
if ( whence == SEEK_SET )
|
||||
pos +=
|
||||
count - lseek(fileno(stream), 0L, swhence);
|
||||
else
|
||||
offset -= count;
|
||||
|
||||
if ( count > 0 && pos <= count
|
||||
&& pos >= stream->_buf - stream->_ptr ) {
|
||||
stream->_ptr += (int) pos;
|
||||
stream->_count -= (int) pos;
|
||||
return(0);
|
||||
}
|
||||
}
|
||||
pos = lseek(fileno(stream), offset, swhence);
|
||||
if (io_testflag(stream, _IOREADING)) {
|
||||
if (whence == SEEK_CUR
|
||||
&& stream->_buf
|
||||
&& !io_testflag(stream,_IONBF))
|
||||
adjust = stream->_count;
|
||||
pos = lseek(fileno(stream), offset - adjust, swhence);
|
||||
stream->_count = 0;
|
||||
} else if ( io_testflag(stream,_IOWRITE) ) {
|
||||
}
|
||||
else if (io_testflag(stream,_IOWRITING)) {
|
||||
fflush(stream);
|
||||
pos = lseek(fileno(stream), offset, swhence);
|
||||
}
|
||||
return((pos == -1) ? -1 : 0 );
|
||||
else /* neither reading nor writing. The buffer must be empty */
|
||||
pos = lseek(fileno(stream), offset, swhence);
|
||||
|
||||
if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
|
||||
stream->_flags &= ~(_IOREADING | _IOWRITING);
|
||||
|
||||
stream->_ptr = stream->_buf;
|
||||
return ((pos == -1) ? -1 : 0);
|
||||
}
|
||||
|
|
|
@ -9,19 +9,18 @@
|
|||
|
||||
int lseek(int d, int offset, int whence);
|
||||
|
||||
long ftell(FILE * stream)
|
||||
long ftell(FILE *stream)
|
||||
{
|
||||
long result;
|
||||
int adjust;
|
||||
int adjust = 0;
|
||||
|
||||
if ( io_testflag(stream,_IOREAD) )
|
||||
if (io_testflag(stream,_IOREADING))
|
||||
adjust = -stream->_count;
|
||||
else if (io_testflag(stream,_IOWRITE)
|
||||
else if (io_testflag(stream,_IOWRITING)
|
||||
&& stream->_buf
|
||||
&& !io_testflag(stream,_IONBF))
|
||||
adjust = stream->_ptr - stream->_buf;
|
||||
else
|
||||
return -1L;
|
||||
else adjust = 0;
|
||||
|
||||
result = lseek(fileno(stream), 0, L_INCR);
|
||||
|
||||
|
|
|
@ -18,11 +18,11 @@ fwrite(register const void *ptr, size_t size, size_t nmemb,
|
|||
do {
|
||||
if (putc((int)*(unsigned char *)ptr, stream)
|
||||
== EOF)
|
||||
return(ndone);
|
||||
return ndone;
|
||||
ptr++;
|
||||
}
|
||||
while (--s);
|
||||
ndone++;
|
||||
}
|
||||
return(ndone);
|
||||
return ndone;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#define USE_EXP(exp, ndigits) (((exp) < LOW_EXP + 1) || (exp >= ndigits + 1))
|
||||
|
||||
char *
|
||||
gcvt(double value, int ndigit, char *s, int flags)
|
||||
gcvt(long double value, int ndigit, char *s, int flags)
|
||||
{
|
||||
int sign, dp;
|
||||
register char *s1, *s2;
|
||||
|
@ -74,71 +74,4 @@ gcvt(double value, int ndigit, char *s, int flags)
|
|||
*s2 = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
char *
|
||||
gcvt_ldbl(long double value, int ndigit, char *s, int flags)
|
||||
{
|
||||
int sign, dp;
|
||||
register char *s1, *s2;
|
||||
register int i;
|
||||
register int nndigit = ndigit;
|
||||
|
||||
s1 = ecvt_ldbl(value, ndigit, &dp, &sign);
|
||||
s2 = s;
|
||||
if (sign) *s2++ = '-';
|
||||
else if (flags & FL_SIGN)
|
||||
*s2++ = '+';
|
||||
else if (flags & FL_SPACE)
|
||||
*s2++ = ' ';
|
||||
|
||||
if (!(flags & FL_ALT))
|
||||
for (i = nndigit - 1; i > 0 && s1[i] == '0'; i--)
|
||||
nndigit--;
|
||||
|
||||
if (USE_EXP(dp,ndigit)) {
|
||||
/* Use E format */
|
||||
dp--;
|
||||
*s2++ = *s1++;
|
||||
if ((nndigit > 1) || (flags & FL_ALT)) *s2++ = '.';
|
||||
while (--nndigit > 0) *s2++ = *s1++;
|
||||
*s2++ = 'e';
|
||||
if (dp < 0) {
|
||||
*s2++ = '-';
|
||||
dp = -dp;
|
||||
}
|
||||
else *s2++ = '+';
|
||||
s2 += NDIGINEXP(dp);
|
||||
*s2 = 0;
|
||||
for (i = NDIGINEXP(dp); i > 0; i--) {
|
||||
*--s2 = dp % 10 + '0';
|
||||
dp /= 10;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
/* Use f format */
|
||||
if (dp <= 0) {
|
||||
if (*s1 != '0') {
|
||||
/* otherwise the whole number is 0 */
|
||||
*s2++ = '0';
|
||||
*s2++ = '.';
|
||||
}
|
||||
while (dp < 0) {
|
||||
dp++;
|
||||
*s2++ = '0';
|
||||
}
|
||||
}
|
||||
for (i = 1; i <= nndigit; i++) {
|
||||
*s2++ = *s1++;
|
||||
if (i == dp) *s2++ = '.';
|
||||
}
|
||||
if (i <= dp) {
|
||||
while (i++ <= dp) *s2++ = '0';
|
||||
*s2++ = '.';
|
||||
}
|
||||
if ((s2[-1]=='.') && !(flags & FL_ALT)) s2--;
|
||||
*s2 = '\0';
|
||||
return s;
|
||||
}
|
||||
#endif /* EM_DSIZE != EM_LDSIZE */
|
||||
#endif /* NOFLOAT */
|
||||
|
|
|
@ -11,28 +11,17 @@
|
|||
#include <stdarg.h>
|
||||
int _doprnt(const char *format, va_list ap, FILE *stream);
|
||||
int _doscan(FILE * stream, const char *format, va_list ap);
|
||||
char *i_compute(unsigned long val, int base, char *s, int nrdigits);
|
||||
char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
|
||||
|
||||
FILE *popen(const char *command, const char *type);
|
||||
FILE *fdopen(int fd, const char *mode);
|
||||
|
||||
#ifndef NOFLOAT
|
||||
char *_pfloat(double r, register char *s, int n, int flags);
|
||||
char *_pscien(double r, register char *s, int n, int flags);
|
||||
char *ecvt(double value, int ndigit, int *decpt, int *sign);
|
||||
char *fcvt(double value, int ndigit, int *decpt, int *sign);
|
||||
char *gcvt(double value, int ndigit, char *buf, int flags);
|
||||
|
||||
/*
|
||||
* When the sizes of doubles and long doubles are equal, the formats must
|
||||
* be equal (since a backend only knows the size of a floating-point
|
||||
* number). This means that the special routines for long doubles are not
|
||||
* necessary.
|
||||
*/
|
||||
#if EM_DSIZE != EM_LDSIZE
|
||||
char *_pfloat_ldbl(long double r, register char *s, int n, int flags);
|
||||
char *_pscien_ldbl(long double r, register char *s, int n, int flags);
|
||||
char *ecvt_ldbl(long double value, int ndigit, int *decpt, int *sign);
|
||||
char *fcvt_ldbl(long double value, int ndigit, int *decpt, int *sign);
|
||||
char *gcvt_ldbl(long double value, int ndigit, char *s, int flags);
|
||||
#endif /* EM_DSIZE != EM_LDSIZE */
|
||||
char *_pfloat(long double r, register char *s, int n, int flags);
|
||||
char *_pscien(long double r, register char *s, int n, int flags);
|
||||
char *ecvt(long double value, int ndigit, int *decpt, int *sign);
|
||||
char *fcvt(long double value, int ndigit, int *decpt, int *sign);
|
||||
char *gcvt(long double value, int ndigit, char *s, int flags);
|
||||
#endif /* NOFLOAT */
|
||||
|
||||
#define FL_LJUST 0x0001 /* left-justify field */
|
||||
|
|
|
@ -16,8 +16,8 @@ printf(const char *format, ...)
|
|||
va_start(ap, format);
|
||||
|
||||
retval = _doprnt(format, ap, stdout);
|
||||
if (io_testflag(stdout,_IOLBF))
|
||||
fflush(stdout);
|
||||
if (retval >= 0 && io_testflag(stdout,_IOLBF))
|
||||
if (fflush(stdout)) return EOF;
|
||||
|
||||
va_end(ap);
|
||||
|
||||
|
|
|
@ -9,10 +9,5 @@
|
|||
void
|
||||
setbuf(register FILE *stream, char *buf)
|
||||
{
|
||||
int mode;
|
||||
|
||||
if (buf) mode = _IOFBF;
|
||||
else mode = _IONBF;
|
||||
|
||||
(void) setvbuf(stream, buf, mode, (size_t) BUFSIZ);
|
||||
(void) setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), (size_t) BUFSIZ);
|
||||
}
|
||||
|
|
|
@ -15,13 +15,16 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
|
|||
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
|
||||
return EOF;
|
||||
|
||||
if ( stream->_buf && io_testflag(stream,_IOMYBUF) )
|
||||
if (stream->_buf && io_testflag(stream,_IOMYBUF) )
|
||||
free((void *)stream->_buf);
|
||||
|
||||
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
|
||||
|
||||
if (!buf && (mode != _IONBF))
|
||||
if ((buf = (char *) malloc(size)) == NULL) retval = -1;
|
||||
if ((buf = (char *) malloc(size)) == NULL) retval = EOF;
|
||||
|
||||
if (io_testflag(stream, _IOREADING) || io_testflag(stream, _IOWRITING))
|
||||
retval = EOF;
|
||||
|
||||
stream->_buf = (unsigned char *) buf;
|
||||
|
||||
|
@ -32,9 +35,6 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
|
|||
if (!buf) {
|
||||
stream->_bufsiz = 1;
|
||||
} else {
|
||||
if (io_testflag(stream, _IOWRITE)
|
||||
&& !io_testflag(stream, _IOLBF))
|
||||
stream->_count = size;
|
||||
stream->_bufsiz = size;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,13 +17,12 @@ tmpfile(void) {
|
|||
|
||||
if (!name) {
|
||||
name = name_buffer + strlen(name_buffer);
|
||||
i_compute(getpid(), 10, name, 5);
|
||||
name += strlen(name);
|
||||
*name++ = '\0';
|
||||
name = _i_compute(getpid(), 10, name, 5);
|
||||
*name = '\0';
|
||||
}
|
||||
|
||||
file = fopen(name_buffer,"wb+");
|
||||
if (!file) return (FILE *)NULL;
|
||||
if (remove(name_buffer)) return (FILE *)NULL;
|
||||
(void) remove(name_buffer);
|
||||
return file;
|
||||
}
|
||||
|
|
|
@ -17,12 +17,12 @@ tmpnam(char *s) {
|
|||
|
||||
if (!name) {
|
||||
name = name_buffer + strlen(name_buffer);
|
||||
name = i_compute(getpid(), 10, name, 5);
|
||||
name = _i_compute(getpid(), 10, name, 5);
|
||||
*name++ = '.';
|
||||
*name++ = '\0';
|
||||
}
|
||||
if (++count > TMP_MAX) count = 1; /* wrap-around */
|
||||
*i_compute(count, 10, name, 3) = '\0';
|
||||
*_i_compute(count, 10, name, 3) = '\0';
|
||||
if (s) return strcpy(s, name_buffer);
|
||||
else return name_buffer;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* ungetc.c - push a character back onto an imput stream
|
||||
* ungetc.c - push a character back onto an input stream
|
||||
*/
|
||||
/* $Header$ */
|
||||
|
||||
|
@ -11,14 +11,14 @@ ungetc(int ch, FILE *stream)
|
|||
{
|
||||
unsigned char *p;
|
||||
|
||||
if (ch == EOF || !io_testflag(stream,_IOREAD))
|
||||
if (ch == EOF || !io_testflag(stream,_IOREADING))
|
||||
return EOF;
|
||||
if (stream->_ptr == stream->_buf) {
|
||||
if (stream->_count != 0) return EOF;
|
||||
stream->_ptr++;
|
||||
}
|
||||
stream->_count++;
|
||||
p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */
|
||||
p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */
|
||||
*p = (unsigned char) ch;
|
||||
return ch;
|
||||
}
|
||||
|
|
|
@ -13,8 +13,8 @@ vprintf(const char *format, va_list arg)
|
|||
int retval;
|
||||
|
||||
retval = _doprnt(format, arg, stdout);
|
||||
if (io_testflag(stdout, _IOLBF))
|
||||
fflush(stdout);
|
||||
if (retval >= 0 && io_testflag(stdout, _IOLBF))
|
||||
if (fflush(stdout)) return EOF;
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue