various fixes and improvements

This commit is contained in:
eck 1989-06-26 10:37:05 +00:00
parent 11349c78cd
commit 27d7d5ed68
24 changed files with 173 additions and 417 deletions

View file

@ -25,7 +25,7 @@ gnum(register const char *f, int *ip, va_list *app)
} }
*ip = i; *ip = i;
} }
return(f); return f;
} }
#if EM_WSIZE == EM_PSIZE #if EM_WSIZE == EM_PSIZE
@ -38,16 +38,16 @@ gnum(register const char *f, int *ip, va_list *app)
#endif #endif
char * 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; int c;
c= val % base ; c= val % base ;
val /= base ; val /= base ;
if (val || nrdigits > 0) 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'); *s++ = (c>9 ? c-10+'a' : c+'0');
return(s); return s;
} }
/* print an ordinal number */ /* 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; 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') if (c == 'X')
while (old_s != s) { while (old_s != s) {
@ -130,42 +130,23 @@ static char *
f_print(va_list *ap, int flags, char *s, char c, int precision) f_print(va_list *ap, int flags, char *s, char c, int precision)
{ {
register char *old_s = s; register char *old_s = s;
double d_val;
#if EM_DSIZE != EM_LDSIZE
long double ld_val; long double ld_val;
if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double); if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double);
else else
#endif ld_val = (long double) va_arg(*ap, double);
d_val = va_arg(*ap, double);
switch(c) { switch(c) {
case 'f': case 'f':
#if EM_DSIZE != EM_LDSIZE s = _pfloat(ld_val, s, precision, flags);
if (flags & FL_LONGDOUBLE)
s = _pfloat_ldbl(ld_val, s, precision, flags);
else
#endif
s = _pfloat(d_val, s, precision, flags);
break; break;
case 'e': case 'e':
case 'E': case 'E':
#if EM_DSIZE != EM_LDSIZE s = _pscien(ld_val, s, precision, flags);
if (flags & FL_LONGDOUBLE)
s = _pscien_ldbl(ld_val, s, precision, flags);
else
#endif
s = _pscien(d_val, s, precision, flags);
break; break;
case 'g': case 'g':
case 'G': case 'G':
#if EM_DSIZE != EM_LDSIZE s = gcvt(ld_val, precision, s, flags) + strlen(s);
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);
break; break;
} }
if ( c == 'E' || c == 'G') { if ( c == 'E' || c == 'G') {
@ -190,11 +171,13 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
if (c != '%') { if (c != '%') {
#ifdef CPM #ifdef CPM
if (c == '\n') { if (c == '\n') {
putc('\r', stream); if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++; nrchars++;
} }
#endif #endif
putc(c, stream); if (putc(c, stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++; nrchars++;
continue; continue;
} }
@ -225,7 +208,7 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
width = -width; width = -width;
flags |= FL_LJUST; flags |= FL_LJUST;
} }
if (!(flags & FL_WIDTHSPEC)) width = 1; if (!(flags & FL_WIDTHSPEC)) width = 0;
if (flags & FL_SIGN) if (flags & FL_SIGN)
flags &= ~FL_SPACE; flags &= ~FL_SPACE;
@ -249,11 +232,13 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
default: default:
#ifdef CPM #ifdef CPM
if (c == '\n') { if (c == '\n') {
putc('\r', stream); if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++; nrchars++;
} }
#endif #endif
putc(c, stream); if (putc(c, stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++; nrchars++;
continue; continue;
case 'n': case 'n':
@ -269,11 +254,12 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
if (s1 == NULL) if (s1 == NULL)
s1 = "(null)"; s1 = "(null)";
s = s1; s = s1;
do { while (precision || !(flags & FL_PRECSPEC)) {
if (*s == '\0') if (*s == '\0')
break; break;
s++; s++;
} while (!(flags & FL_PRECSPEC) || --precision); precision--;
}
break; break;
case 'p': case 'p':
set_pointer(flags); set_pointer(flags);
@ -343,26 +329,33 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
if (!(flags & FL_LJUST)) { /* right justify */ if (!(flags & FL_LJUST)) { /* right justify */
nrchars += i; nrchars += i;
if (between_fill) { if (between_fill) {
if (flags & FL_SIGNEDCONV) { if (flags & FL_SIGNEDCONV) {
j--; nrchars++; j--; nrchars++;
putc(*s1++, stream); if (putc(*s1++, stream) == EOF)
} else { return nrchars ? -nrchars : -1;
j -= 2; nrchars += 2; } else {
putc(*s1++, stream); j -= 2; nrchars += 2;
putc(*s1++, stream); if ((putc(*s1++, stream) == EOF)
} || (putc(*s1++, stream) == EOF))
return nrchars ? -nrchars : -1;
}
} }
do putc(zfill, stream); do {
while (--i); if (putc(zfill, stream) == EOF)
return nrchars ? -nrchars : -1;
} while (--i);
} }
nrchars += j; nrchars += j;
while (--j >= 0) while (--j >= 0) {
putc(*s1++, stream); if (putc(*s1++, stream) == EOF)
return nrchars ? -nrchars : -1;
}
if (i > 0) nrchars += i; if (i > 0) nrchars += i;
while (--i >= 0) while (--i >= 0)
putc(zfill, stream); if (putc(zfill, stream) == EOF)
return nrchars ? -nrchars : -1;
} }
return nrchars; return nrchars;
} }

View file

@ -9,7 +9,7 @@
#include <stdarg.h> #include <stdarg.h>
#include "loc_incl.h" #include "loc_incl.h"
#define NUMLEN 128 #define NUMLEN 512
static char Xtable[128]; static char Xtable[128];
static char inp_buf[NUMLEN]; 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 done = 0; /* number of items done */
int nrchars = 0; /* number of characters read */ int nrchars = 0; /* number of characters read */
int conv = 0; /* # of conversions */
int base; /* conversion base */ int base; /* conversion base */
unsigned long val; /* an integer value */ unsigned long val; /* an integer value */
char *str, *tmp_string; /* temporary pointers */ char *str, *tmp_string; /* temporary pointers */
@ -166,7 +167,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
int kind; int kind;
register int ic; register int ic;
#ifndef NOFLOAT #ifndef NOFLOAT
double d_val; long double ld_val;
#endif #endif
ic = getc(stream); ic = getc(stream);
@ -192,7 +193,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
if (!*format) if (!*format)
break; /* end of format */ break; /* end of format */
if (ic == EOF) if (ic == EOF)
return done; /* seen an error */ return conv ? done : EOF;
if (*format != '%') { if (*format != '%') {
if (ic != *format) if (ic != *format)
break; /* matching error */ break; /* matching error */
@ -229,7 +230,9 @@ _doscan(register FILE *stream, const char *format, va_list ap)
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} }
if (ic == EOF) return conv ? done : EOF;
} }
conv++;
switch (kind) { switch (kind) {
default: default:
if (kind == ic) continue; if (kind == ic) continue;
@ -384,16 +387,14 @@ _doscan(register FILE *stream, const char *format, va_list ap)
if (str < inp_buf) return done; if (str < inp_buf) return done;
nrchars += str - inp_buf + 1; nrchars += str - inp_buf + 1;
if (!(flags & FL_NOASSIGN)) { if (!(flags & FL_NOASSIGN)) {
d_val = strtod(inp_buf, &tmp_string); ld_val = strtod(inp_buf, &tmp_string);
#if EM_DSIZE != EM_LDSIZE
if (flags & FL_LONGDOUBLE) if (flags & FL_LONGDOUBLE)
*va_arg(ap, long double *) = (long double) d_val; *va_arg(ap, long double *) = (long double) ld_val;
else else
#endif /* EM_DSIZE != EM_LDSIZE */
if (flags & FL_LONG) if (flags & FL_LONG)
*va_arg(ap, double *) = (double) d_val; *va_arg(ap, double *) = (double) ld_val;
else else
*va_arg(ap, float *) = (float) d_val; *va_arg(ap, float *) = (float) ld_val;
done++; done++;
} }
break; break;
@ -401,10 +402,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
} /* end switch */ } /* end switch */
++format; ++format;
} }
all_done:
if (ic != EOF) if (ic != EOF)
ungetc(ic, stream); ungetc(ic, stream);
/* nrchars--; just to keep it clean */ return conv ? done : EOF;
quit:
return done;
} }

View file

@ -5,24 +5,24 @@
#ifndef NOFLOAT #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 #define NDIGITS 128
char * 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); return cvt(value, ndigit, decpt, sign, 1);
} }
char * 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); return cvt(value, ndigit, decpt, sign, 0);
} }
static struct powers_of_10 { static struct powers_of_10 {
double pval; long double pval;
double rpval; long double rpval;
int exp; int exp;
} p10[] = { } p10[] = {
1.0e32, 1.0e-32, 32, 1.0e32, 1.0e-32, 32,
@ -35,7 +35,7 @@ static struct powers_of_10 {
}; };
static char * 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]; static char buf[NDIGITS+1];
register char *p = buf; register char *p = buf;
@ -104,93 +104,4 @@ cvt(double value, int ndigit, int *decpt, int *sign, int ecvtflag)
} }
return buf; 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 */ #endif /* NOFLOAT */

View file

@ -20,12 +20,12 @@ fclose(FILE *fp)
break; break;
} }
if (i >= FOPEN_MAX) if (i >= FOPEN_MAX)
return(EOF); return EOF;
if (fflush(fp)) retval = EOF; /* ??? */ if (fflush(fp)) retval = EOF;
if (close(fileno(fp))) retval = EOF; /* ??? */ if (close(fileno(fp))) retval = EOF;
if ( io_testflag(fp,_IOMYBUF) && fp->_buf ) if ( io_testflag(fp,_IOMYBUF) && fp->_buf )
free((void *)fp->_buf); free((void *)fp->_buf);
if (fp != &_stdin && fp != &_stdout && fp != &_stderr) if (fp != stdin && fp != stdout && fp != stderr)
free((void *)fp); free((void *)fp);
return(retval); return retval;
} }

View file

@ -6,7 +6,7 @@
#include <stdio.h> #include <stdio.h>
#include "loc_incl.h" #include "loc_incl.h"
int write(int d, static char *buf, int nbytes); int write(int d, const char *buf, int nbytes);
int int
fflush(FILE *stream) fflush(FILE *stream)
@ -20,28 +20,39 @@ fflush(FILE *stream)
return retval; return retval;
} }
if (!stream->_buf || if (!stream->_buf
io_testflag(stream,_IONBF) || || io_testflag(stream, _IONBF)
!io_testflag(stream,_IOWRITE) ) || !io_testflag(stream, _IOWRITE)
return(0); || !io_testflag(stream, _IOWRITING))
return 0;
if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
stream->_flags &= ~_IOWRITING;
/*
if (io_testflag(stream, _IOLBF)) if (io_testflag(stream, _IOLBF))
count = -stream->_count; count = -stream->_count;
else count = stream->_bufsiz - stream->_count; else count = stream->_bufsiz - stream->_count;
*/
count = stream->_ptr - stream->_buf;
stream->_ptr = stream->_buf;
if ( count <= 0 ) if ( count <= 0 )
return(0); return 0;
c1 = write(stream->_fd, (char *)stream->_buf, count); c1 = write(stream->_fd, (char *)stream->_buf, count);
if ( count == c1 ) { stream->_count = 0;
if (io_testflag(stream, _IOLBF))
stream->_count = 0; /*
else stream->_count = stream->_bufsiz; if(stream != stderr)
stream->_ptr = stream->_buf; fprintf(stderr,"written %d bytes :\"%.*s\"\n"
return(count); , c1, c1, stream->_buf);
} */
if ( count == c1 )
return 0;
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return(EOF); return EOF;
} }

View file

@ -17,8 +17,8 @@ fgets(char *s, int n, FILE *stream)
if ( ch == '\n') if ( ch == '\n')
break; break;
} }
if (ch == EOF && ptr==s) if (ch == EOF && ptr == s)
return(NULL); return (char *)NULL;
*ptr = '\0'; *ptr = '\0';
return(s); return s;
} }

View file

@ -16,12 +16,13 @@ _fillbuf(register FILE *stream)
stream->_count = 0; stream->_count = 0;
if (fileno(stream) < 0) return EOF; if (fileno(stream) < 0) return EOF;
if (io_testflag(stream, (_IOEOF | _IOERR ))) if (io_testflag(stream, (_IOEOF | _IOERR ))) return EOF;
return (EOF); if (!io_testflag(stream, _IOREAD)) return EOF;
if (io_testflag(stream, _IOWRITING)) return EOF;
if (!io_testflag(stream, _IOREAD) )
return (EOF);
if (!io_testflag(stream, _IOREADING))
stream->_flags |= _IOREADING;
if (!io_testflag(stream, _IONBF) && !stream->_buf) { if (!io_testflag(stream, _IONBF) && !stream->_buf) {
stream->_buf = (unsigned char *) malloc(BUFSIZ); stream->_buf = (unsigned char *) malloc(BUFSIZ);
if (!stream->_buf) { if (!stream->_buf) {
@ -38,6 +39,10 @@ _fillbuf(register FILE *stream)
} }
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
stream->_count = read(stream->_fd, (char *)stream->_buf, stream->_bufsiz); 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){
if (stream->_count == 0) { if (stream->_count == 0) {
@ -46,7 +51,7 @@ _fillbuf(register FILE *stream)
else else
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return (EOF); return EOF;
} }
stream->_count--; stream->_count--;

View file

@ -7,7 +7,7 @@
#include "loc_incl.h" #include "loc_incl.h"
char * 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; register char *s1;
int sign, dp; int sign, dp;
@ -36,11 +36,11 @@ _pfloat(double r, register char *s, int n, int flags)
while (--i >= 0) while (--i >= 0)
if (*s1) *s++ = *s1++; if (*s1) *s++ = *s1++;
else *s++ = '0'; else *s++ = '0';
return(s); return s;
} }
char * 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; int sign, dp;
register char *s1; 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);
*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 */ #endif /* NOFLOAT */

View file

@ -14,6 +14,11 @@ int
_flushbuf(int c, FILE * stream) _flushbuf(int c, FILE * stream)
{ {
if (fileno(stream) < 0) return EOF; 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 (!io_testflag(stream, _IONBF)) {
if (!stream->_buf) { if (!stream->_buf) {
if (stream == stdout && isatty(fileno(stdout))) { if (stream == stdout && isatty(fileno(stdout))) {

View file

@ -26,42 +26,34 @@ fopen(const char *name, const char *mode)
return (FILE *)NULL; return (FILE *)NULL;
switch(*mode++) { switch(*mode++) {
case 'r': case 'r':
flags |= _IOREAD; flags |= _IOREAD | _IOREADING;
rwmode = O_RDONLY; rwmode = O_RDONLY;
break; break;
case 'w': case 'w':
flags |= _IOWRITE; flags |= _IOWRITE | _IOWRITING;
rwmode = O_WRONLY; rwmode = O_WRONLY;
rwflags = O_CREAT | O_TRUNC; rwflags = O_CREAT | O_TRUNC;
break; break;
case 'a': case 'a':
flags |= _IOWRITE; flags |= _IOWRITE | _IOWRITING;
rwmode = O_WRONLY; rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT; rwflags |= O_APPEND | O_CREAT;
break; break;
default: default:
return (FILE *)NULL; return (FILE *)NULL;
} }
while (*mode) { while (*mode) {
switch(*mode++) { switch(*mode++) {
case 'b': case 'b':
break; break;
case '+': case '+':
rwmode = O_RDWR; rwmode = O_RDWR;
flags |= _IOREAD | _IOWRITE; flags |= _IOREAD | _IOWRITE;
break; break;
default: default:
return (FILE *)NULL; return (FILE *)NULL;
} }
} }
@ -74,10 +66,13 @@ fopen(const char *name, const char *mode)
return (FILE *)NULL; return (FILE *)NULL;
} }
if ((flags & _IOREAD) && (flags & _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0; stream->_count = 0;
stream->_fd = fd; stream->_fd = fd;
stream->_flags = flags; stream->_flags = flags;
stream->_buf = 0; stream->_buf = NULL;
_iotable[i] = stream; _iotable[i] = stream;
return stream; return stream;
} }

View file

@ -16,8 +16,8 @@ fprintf(FILE *stream, const char *format, ...)
va_start(ap, format); va_start(ap, format);
retval = _doprnt (format, ap, stream); retval = _doprnt (format, ap, stream);
if ( io_testflag(stream,_IOLBF) ) if ( retval >= 0 && io_testflag(stream,_IOLBF) )
fflush(stream); if (fflush(stream)) return EOF;
va_end(ap); va_end(ap);

View file

@ -84,5 +84,5 @@ freopen(const char *name, const char *mode, FILE *stream)
stream->_fd = fd; stream->_fd = fd;
stream->_flags = flags; stream->_flags = flags;
return(stream); return stream;
} }

View file

@ -12,7 +12,7 @@ int lseek(int d, int offset, int whence);
int int
fseek(FILE *stream, long int offset, int whence) fseek(FILE *stream, long int offset, int whence)
{ {
int count; int count, adjust = 0;
long pos; long pos;
#if (SEEK_CUR != L_INCR) || (SEEK_SET != L_SET) || (SEEK_END != L_XTND) #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); stream->_flags &= ~(_IOEOF | _IOERR);
/* Clear both the end of file and error flags */ /* Clear both the end of file and error flags */
if ( io_testflag(stream,_IOREAD) ) { if (io_testflag(stream, _IOREADING)) {
if ( whence < 2 && stream->_buf if (whence == SEEK_CUR
&& !io_testflag(stream,_IONBF) ) { && stream->_buf
count = stream->_count; && !io_testflag(stream,_IONBF))
pos = offset; adjust = stream->_count;
pos = lseek(fileno(stream), offset - adjust, swhence);
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);
stream->_count = 0; stream->_count = 0;
} else if ( io_testflag(stream,_IOWRITE) ) { }
else if (io_testflag(stream,_IOWRITING)) {
fflush(stream); fflush(stream);
pos = lseek(fileno(stream), offset, swhence); 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);
} }

View file

@ -9,19 +9,18 @@
int lseek(int d, int offset, int whence); int lseek(int d, int offset, int whence);
long ftell(FILE * stream) long ftell(FILE *stream)
{ {
long result; long result;
int adjust; int adjust = 0;
if ( io_testflag(stream,_IOREAD) ) if (io_testflag(stream,_IOREADING))
adjust = -stream->_count; adjust = -stream->_count;
else if (io_testflag(stream,_IOWRITE) else if (io_testflag(stream,_IOWRITING)
&& stream->_buf && stream->_buf
&& !io_testflag(stream,_IONBF)) && !io_testflag(stream,_IONBF))
adjust = stream->_ptr - stream->_buf; adjust = stream->_ptr - stream->_buf;
else else adjust = 0;
return -1L;
result = lseek(fileno(stream), 0, L_INCR); result = lseek(fileno(stream), 0, L_INCR);

View file

@ -18,11 +18,11 @@ fwrite(register const void *ptr, size_t size, size_t nmemb,
do { do {
if (putc((int)*(unsigned char *)ptr, stream) if (putc((int)*(unsigned char *)ptr, stream)
== EOF) == EOF)
return(ndone); return ndone;
ptr++; ptr++;
} }
while (--s); while (--s);
ndone++; ndone++;
} }
return(ndone); return ndone;
} }

View file

@ -11,7 +11,7 @@
#define USE_EXP(exp, ndigits) (((exp) < LOW_EXP + 1) || (exp >= ndigits + 1)) #define USE_EXP(exp, ndigits) (((exp) < LOW_EXP + 1) || (exp >= ndigits + 1))
char * char *
gcvt(double value, int ndigit, char *s, int flags) gcvt(long double value, int ndigit, char *s, int flags)
{ {
int sign, dp; int sign, dp;
register char *s1, *s2; register char *s1, *s2;
@ -74,71 +74,4 @@ gcvt(double value, int ndigit, char *s, int flags)
*s2 = '\0'; *s2 = '\0';
return s; 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 */ #endif /* NOFLOAT */

View file

@ -11,28 +11,17 @@
#include <stdarg.h> #include <stdarg.h>
int _doprnt(const char *format, va_list ap, FILE *stream); int _doprnt(const char *format, va_list ap, FILE *stream);
int _doscan(FILE * stream, const char *format, va_list ap); 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 #ifndef NOFLOAT
char *_pfloat(double r, register char *s, int n, int flags); char *_pfloat(long double r, register char *s, int n, int flags);
char *_pscien(double r, register char *s, int n, int flags); char *_pscien(long double r, register char *s, int n, int flags);
char *ecvt(double value, int ndigit, int *decpt, int *sign); char *ecvt(long double value, int ndigit, int *decpt, int *sign);
char *fcvt(double value, int ndigit, int *decpt, int *sign); char *fcvt(long double value, int ndigit, int *decpt, int *sign);
char *gcvt(double value, int ndigit, char *buf, int flags); char *gcvt(long double value, int ndigit, char *s, 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 */
#endif /* NOFLOAT */ #endif /* NOFLOAT */
#define FL_LJUST 0x0001 /* left-justify field */ #define FL_LJUST 0x0001 /* left-justify field */

View file

@ -16,8 +16,8 @@ printf(const char *format, ...)
va_start(ap, format); va_start(ap, format);
retval = _doprnt(format, ap, stdout); retval = _doprnt(format, ap, stdout);
if (io_testflag(stdout,_IOLBF)) if (retval >= 0 && io_testflag(stdout,_IOLBF))
fflush(stdout); if (fflush(stdout)) return EOF;
va_end(ap); va_end(ap);

View file

@ -9,10 +9,5 @@
void void
setbuf(register FILE *stream, char *buf) setbuf(register FILE *stream, char *buf)
{ {
int mode; (void) setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), (size_t) BUFSIZ);
if (buf) mode = _IOFBF;
else mode = _IONBF;
(void) setvbuf(stream, buf, mode, (size_t) BUFSIZ);
} }

View file

@ -15,13 +15,16 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF) if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF; return EOF;
if ( stream->_buf && io_testflag(stream,_IOMYBUF) ) if (stream->_buf && io_testflag(stream,_IOMYBUF) )
free((void *)stream->_buf); free((void *)stream->_buf);
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF); stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
if (!buf && (mode != _IONBF)) 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; stream->_buf = (unsigned char *) buf;
@ -32,9 +35,6 @@ setvbuf(register FILE *stream, char *buf, int mode, size_t size)
if (!buf) { if (!buf) {
stream->_bufsiz = 1; stream->_bufsiz = 1;
} else { } else {
if (io_testflag(stream, _IOWRITE)
&& !io_testflag(stream, _IOLBF))
stream->_count = size;
stream->_bufsiz = size; stream->_bufsiz = size;
} }

View file

@ -17,13 +17,12 @@ tmpfile(void) {
if (!name) { if (!name) {
name = name_buffer + strlen(name_buffer); name = name_buffer + strlen(name_buffer);
i_compute(getpid(), 10, name, 5); name = _i_compute(getpid(), 10, name, 5);
name += strlen(name); *name = '\0';
*name++ = '\0';
} }
file = fopen(name_buffer,"wb+"); file = fopen(name_buffer,"wb+");
if (!file) return (FILE *)NULL; if (!file) return (FILE *)NULL;
if (remove(name_buffer)) return (FILE *)NULL; (void) remove(name_buffer);
return file; return file;
} }

View file

@ -17,12 +17,12 @@ tmpnam(char *s) {
if (!name) { if (!name) {
name = name_buffer + strlen(name_buffer); name = name_buffer + strlen(name_buffer);
name = i_compute(getpid(), 10, name, 5); name = _i_compute(getpid(), 10, name, 5);
*name++ = '.'; *name++ = '.';
*name++ = '\0'; *name++ = '\0';
} }
if (++count > TMP_MAX) count = 1; /* wrap-around */ 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); if (s) return strcpy(s, name_buffer);
else return name_buffer; else return name_buffer;
} }

View file

@ -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$ */ /* $Header$ */
@ -11,14 +11,14 @@ ungetc(int ch, FILE *stream)
{ {
unsigned char *p; unsigned char *p;
if (ch == EOF || !io_testflag(stream,_IOREAD)) if (ch == EOF || !io_testflag(stream,_IOREADING))
return EOF; return EOF;
if (stream->_ptr == stream->_buf) { if (stream->_ptr == stream->_buf) {
if (stream->_count != 0) return EOF; if (stream->_count != 0) return EOF;
stream->_ptr++; stream->_ptr++;
} }
stream->_count++; stream->_count++;
p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */ p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */
*p = (unsigned char) ch; *p = (unsigned char) ch;
return ch; return ch;
} }

View file

@ -13,8 +13,8 @@ vprintf(const char *format, va_list arg)
int retval; int retval;
retval = _doprnt(format, arg, stdout); retval = _doprnt(format, arg, stdout);
if (io_testflag(stdout, _IOLBF)) if (retval >= 0 && io_testflag(stdout, _IOLBF))
fflush(stdout); if (fflush(stdout)) return EOF;
return retval; return retval;
} }