made a lot of changes

This commit is contained in:
eck 1989-12-18 15:04:14 +00:00
parent bb48507f58
commit 2f92b46a9d
40 changed files with 540 additions and 386 deletions

View file

@ -0,0 +1,55 @@
LIST
Makefile
clearerr.c
data.c
doprnt.c
doscan.c
ecvt.c
fclose.c
feof.c
ferror.c
fflush.c
fgetc.c
fgetpos.c
fgets.c
fileno.c
fillbuf.c
fltpr.c
flushbuf.c
fopen.c
fprintf.c
fputc.c
fputs.c
fread.c
freopen.c
fscanf.c
fseek.c
fsetpos.c
ftell.c
fwrite.c
gcvt.c
getc.c
getchar.c
gets.c
icompute.c
isatty.c
loc_incl.h
perror.c
printf.c
putc.c
putchar.c
puts.c
remove.c
rename.c
rewind.c
scanf.c
setbuf.c
setvbuf.c
sprintf.c
sscanf.c
tmpfile.c
tmpnam.c
ungetc.c
vfprintf.c
vprintf.c
vsprintf.c

View file

@ -1,4 +1,4 @@
llib
loc_incl.h
tmpfile.c
tmpnam.c
rename.c
@ -15,6 +15,7 @@ vfprintf.c
vprintf.c
vsprintf.c
doprnt.c
icompute.c
fscanf.c
scanf.c
sscanf.c
@ -40,6 +41,7 @@ ftell.c
clearerr.c
feof.c
ferror.c
fileno.c
fltpr.c
ecvt.c
gcvt.c
@ -47,4 +49,5 @@ fillbuf.c
fclose.c
flushbuf.c
fflush.c
isatty.c
data.c

View file

@ -0,0 +1,70 @@
CFLAGS=-L -LIB
.SUFFIXES: .o .e .c
.e.o:
$(CC) $(CFLAGS) -c -o $@ $*.e
clean:
rm -rf tmpfile.o tmpnam.o rename.o remove.o fopen.o freopen.o \
setbuf.o setvbuf.o perror.o fprintf.o printf.o sprintf.o \
vfprintf.o vprintf.o vsprintf.o doprnt.o icompute.o \
fscanf.o scanf.o sscanf.o doscan.o fgetc.o fgets.o getc.o \
getchar.o gets.o putc.o putchar.o fputc.o puts.o fputs.o \
ungetc.o fread.o fwrite.o fgetpos.o fsetpos.o rewind.o \
fseek.o ftell.o clearerr.o feof.o ferror.o fileno.o \
fltpr.o ecvt.o gcvt.o fillbuf.o fclose.o flushbuf.o \
fflush.o isatty.o data.o OLIST
tmpfile.o:
tmpnam.o:
rename.o:
remove.o:
fopen.o:
freopen.o:
setbuf.o:
setvbuf.o:
perror.o:
fprintf.o:
printf.o:
sprintf.o:
vfprintf.o:
vprintf.o:
vsprintf.o:
doprnt.o:
icompute.o:
fscanf.o:
scanf.o:
sscanf.o:
doscan.o:
fgetc.o:
fgets.o:
getc.o:
getchar.o:
gets.o:
putc.o:
putchar.o:
fputc.o:
puts.o:
fputs.o:
ungetc.o:
fread.o:
fwrite.o:
fgetpos.o:
fsetpos.o:
rewind.o:
fseek.o:
ftell.o:
clearerr.o:
feof.o:
ferror.o:
fileno.o:
fltpr.o:
ecvt.o:
gcvt.o:
fillbuf.o:
fclose.o:
flushbuf.o:
fflush.o:
isatty.o:
data.o:

View file

@ -5,24 +5,24 @@
#include <stdio.h>
struct _iobuf _stdin = {
struct __iobuf __stdin = {
0, 0, _IOREAD, 0,
(char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL,
};
struct _iobuf _stdout = {
1, 0, _IOWRITE, 0,
struct __iobuf __stdout = {
0, 1, _IOWRITE, 0,
(char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL,
};
struct _iobuf _stderr = {
2, 0, _IOWRITE | _IONBF, 0,
struct __iobuf __stderr = {
0, 2, _IOWRITE | _IOLBF, 0,
(char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL,
};
struct _iobuf *_iotable[FOPEN_MAX] = {
&_stdin,
&_stdout,
&_stderr,
FILE *__iotab[FOPEN_MAX] = {
&__stdin,
&__stdout,
&__stderr,
0
};

View file

@ -9,7 +9,8 @@
#include <string.h>
#include "loc_incl.h"
static char *
/* gnum() is used to get the width and precision fields of a format. */
static const char *
gnum(register const char *f, int *ip, va_list *app)
{
register int i, c;
@ -28,28 +29,15 @@ gnum(register const char *f, int *ip, va_list *app)
return f;
}
#if EM_WSIZE == EM_PSIZE
#if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */
#elif EM_LSIZE == EM_PSIZE
#elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG)
#else
#define set_pointer(flags) /* nothing */
#error garbage pointer size
#define set_pointer(flags) /* compilation might continue */
#endif
char *
_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++ = (c>9 ? c-10+'a' : c+'0');
return s;
}
/* print an ordinal number */
static char *
o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
@ -91,16 +79,14 @@ o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
else if (flags & FL_SPACE) *s++ = ' ';
unsigned_val = signed_val;
}
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0';
if (!unsigned_val) {
if (precision != 0)
*s++ = '0';
if (!precision)
return s;
}
if (((flags & FL_ALT) && (c == 'x' || c == 'X' || c == 'o'))
} else if (((flags & FL_ALT) && (c == 'x' || c == 'X'))
|| c == 'p') {
*s++ = '0';
if (c != 'o')
*s++ = (c == 'X') ? 'X' : 'x';
*s++ = (c == 'X' ? 'X' : 'x');
}
switch (c) {
@ -133,8 +119,7 @@ f_print(va_list *ap, int flags, char *s, char c, int precision)
long double ld_val;
if (flags & FL_LONGDOUBLE) ld_val = va_arg(*ap, long double);
else
ld_val = (long double) va_arg(*ap, double);
else ld_val = (long double) va_arg(*ap, double);
switch(c) {
case 'f':
@ -165,7 +150,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
register int j;
int i, c, width, precision, zfill, flags, between_fill;
int nrchars=0;
char *oldfmt, *s1, buf[1025];
const char *oldfmt;
char *s1, buf[1025];
while (c = *fmt++) {
if (c != '%') {
@ -210,15 +196,10 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
}
if (!(flags & FL_WIDTHSPEC)) width = 0;
if (flags & FL_SIGN)
flags &= ~FL_SPACE;
if (flags & FL_SIGN) flags &= ~FL_SPACE;
if (flags & (FL_LJUST | FL_PRECSPEC))
flags &= ~FL_ZEROFILL;
if (flags & FL_LJUST) flags &= ~FL_ZEROFILL;
zfill = ' ';
if (flags & FL_ZEROFILL)
zfill = '0';
s = s1 = buf;
@ -270,12 +251,14 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
case 'x':
case 'X':
if (!(flags & FL_PRECSPEC)) precision = 1;
else if (c != 'p') flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 0);
break;
case 'd':
case 'i':
flags |= FL_SIGNEDCONV;
if (!(flags & FL_PRECSPEC)) precision = 1;
else flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 1);
break;
case 'c':
@ -308,6 +291,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
fmt = va_arg(ap, char *);
continue;
}
zfill = ' ';
if (flags & FL_ZEROFILL) zfill = '0';
j = s - s1;
/* between_fill is true under the following conditions:

View file

@ -10,8 +10,9 @@
#include "loc_incl.h"
#define NUMLEN 512
#define NR_CHARS 256
static char Xtable[128];
static char Xtable[NR_CHARS];
static char inp_buf[NUMLEN];
/* Collect a number of characters which constitite an ordinal number.
@ -22,58 +23,60 @@ static char inp_buf[NUMLEN];
*/
static char *
o_collect(register int c, register FILE *stream, char type,
int width, int *base)
int width, int *basep)
{
register char *bufp = inp_buf;
register int base;
switch (type) {
case 'i': /* i means octal, decimal or hexadecimal */
case 'p':
case 'x':
case 'X': *base = 16; break;
case 'X': base = 16; break;
case 'd':
case 'u': *base = 10; break;
case 'o': *base = 8; break;
case 'b': *base = 2; break;
case 'u': base = 10; break;
case 'o': base = 8; break;
case 'b': base = 2; break;
}
if (c == '-' || c == '+') {
*bufp++ = c;
width--;
if (--width);
c = getc(stream);
}
if (width && c == '0' && *base == 16) {
if (width && c == '0' && base == 16) {
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
if (c != 'x' && c != 'X') {
if (type == 'i') *base = 8;
if (type == 'i') base = 8;
}
else if (width) {
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
}
else if (type == 'i') *base = 10;
else if (type == 'i') base = 10;
while (width) {
if (((*base == 10) && isdigit(c))
|| ((*base == 16) && isxdigit(c))
|| ((*base == 8) && isdigit(c) && (c < '8'))
|| ((*base == 2) && isdigit(c) && (c < '2'))) {
if (((base == 10) && isdigit(c))
|| ((base == 16) && isxdigit(c))
|| ((base == 8) && isdigit(c) && (c < '8'))
|| ((base == 2) && isdigit(c) && (c < '2'))) {
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
else break;
}
if (c != EOF) ungetc(c, stream);
if (type == 'i') *base = 0;
*bufp-- = '\0';
return bufp;
if (width && c != EOF) ungetc(c, stream);
if (type == 'i') base = 0;
*basep = base;
*bufp = '\0';
return bufp - 1;
}
#ifndef NOFLOAT
@ -82,68 +85,69 @@ o_collect(register int c, register FILE *stream, char type,
* is encountered, leaving the offending character in the input. This means
* that 1.el leaves the 'l' in the input queue. Since all detection of
* format errors is done here, _doscan() doesn't call strtod() when it's
* not necessary.
* not necessary, although the use of the width field can cause incomplete
* numbers to be passed to strtod(). (e.g. 1.3e+)
*/
static char *
f_collect(register int c, register FILE *stream, int width)
f_collect(register int c, register FILE *stream, register int width)
{
register char *bufp = inp_buf;
int digit_seen = 0;
if (c == '-' || c == '+') {
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
if (width && c == '.') {
*bufp++ = c;
width--;
if(--width)
c = getc(stream);
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
}
if (!digit_seen) {
if (c != EOF) ungetc(c, stream);
if (width && c != EOF) ungetc(c, stream);
return inp_buf - 1;
}
else digit_seen = 0;
if (width && (c == 'e' || c == 'E')) {
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
if (width && (c == '+' || c == '-')) {
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
width--;
if (--width)
c = getc(stream);
}
if (!digit_seen) {
if (c != EOF) ungetc(c,stream);
if (width && c != EOF) ungetc(c,stream);
return inp_buf - 1;
}
}
if (c != EOF) ungetc(c, stream);
*bufp-- = '\0';
return bufp;
if (width && c != EOF) ungetc(c, stream);
*bufp = '\0';
return bufp - 1;
}
#endif /* NOFLOAT */
@ -165,16 +169,11 @@ _doscan(register FILE *stream, const char *format, va_list ap)
int flags; /* some flags */
int reverse; /* reverse the checking in [...] */
int kind;
register int ic;
register int ic; /* the input character */
#ifndef NOFLOAT
long double ld_val;
#endif
ic = getc(stream);
if (ic == EOF)
return EOF;
ungetc(ic,stream);
while (1) {
if (isspace(*format)) {
while (isspace(*format))
@ -185,13 +184,14 @@ _doscan(register FILE *stream, const char *format, va_list ap)
ic = getc(stream);
nrchars++;
}
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
else {
if (!*format) break; /* end of format */
ic = getc(stream);
nrchars++;
}
if (!*format)
break; /* end of format */
if (ic == EOF)
return conv ? done : EOF;
if (*format != '%') {
@ -238,8 +238,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
if (kind == ic) continue;
break;
case 'n':
if (ic != EOF)
ungetc(ic, stream);
if (ic != EOF) ungetc(ic, stream);
nrchars--;
if (flags & FL_SHORT)
*va_arg(ap, short *) = (short) nrchars;
@ -258,7 +257,10 @@ _doscan(register FILE *stream, const char *format, va_list ap)
case 'X': /* ditto */
if (!(flags & FL_WIDTHSPEC))
width = NUMLEN;
if (!width) return done;
if (!width) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
str = o_collect(ic, stream, kind, width, &base);
if (str < inp_buf) return done;
nrchars += str - inp_buf + 1;
@ -281,20 +283,25 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 1;
if (!(flags & FL_NOASSIGN))
tmp_string = va_arg(ap, char *);
if (!width) return done;
if (width && !(flags & FL_NOASSIGN) && (ic != EOF))
if (!width) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
if (!(flags & FL_NOASSIGN) && (ic != EOF))
++done;
while (width && ic != EOF) {
if (!(flags & FL_NOASSIGN))
*tmp_string++ = (char) ic;
width--;
if (--width) {
ic = getc(stream);
nrchars++;
}
}
if (ic != EOF)
ungetc(ic,stream);
if (width) {
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
break;
case 's':
@ -302,36 +309,45 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 0xffff;
if (!(flags & FL_NOASSIGN))
tmp_string = va_arg(ap, char *);
if (!width) return done;
if (!width) {
if (ic != EOF) ungetc(ic,stream);
return done;
}
if (!(flags & FL_NOASSIGN))
++done;
while (width && ic != EOF && !isspace(ic)) {
if (!(flags & FL_NOASSIGN))
*tmp_string++ = (char) ic;
width--;
if (--width) {
ic = getc(stream);
nrchars++;
}
}
/* terminate the string */
if (!(flags & FL_NOASSIGN))
*tmp_string = '\0';
}
if (ic != EOF)
ungetc(ic,stream);
if (width) {
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
break;
case '[':
{ int old_nrchars;
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!width) return done;
if (!width) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
if ( *(++format) == '^' ) {
reverse = 1;
format++;
} else
reverse = 0;
for (tmp_string = Xtable; tmp_string < &Xtable[128]
for (tmp_string = Xtable; tmp_string < &Xtable[NR_CHARS]
; tmp_string++)
*tmp_string = 0;
@ -353,26 +369,37 @@ _doscan(register FILE *stream, const char *format, va_list ap)
else Xtable['-'] = 1;
}
}
if (!*format)
if (!*format) {
if (ic != EOF) ungetc(ic,stream);
return done;
}
old_nrchars = nrchars;
if (!(flags & FL_NOASSIGN))
tmp_string = va_arg(ap, char *);
if (ic == EOF || !(Xtable[ic] ^ reverse)) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
while (width && ic != EOF && (Xtable[ic] ^ reverse)) {
if (!(flags & FL_NOASSIGN))
*tmp_string++ = (char) ic;
width--;
if (--width) {
ic = getc(stream);
nrchars++;
}
if (ic != EOF)
ungetc(ic, stream);
}
if (width) {
if (ic != EOF) ungetc(ic, stream);
nrchars--;
}
if (!(flags & FL_NOASSIGN)) { /* terminate string */
*tmp_string = '\0';
++done;
}
break;
}
#ifndef NOFLOAT:
case 'e':
case 'E':
@ -402,7 +429,5 @@ _doscan(register FILE *stream, const char *format, va_list ap)
} /* end switch */
++format;
}
if (ic != EOF)
ungetc(ic, stream);
return conv ? done : EOF;
}

View file

@ -1,16 +1,24 @@
/*
* ecvt.c - conversion routines for printing floating point numbers
*/
/* $Header$ */
#ifndef NOFLOAT
static char *cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag);
#define NDIGITS 128
#include "../stdlib/ext_fmt.h"
void _dbl_ext_cvt(double value, struct EXTEND *e);
char *_ext_str_cvt(struct EXTEND *e, int ndigit, int *decpt, int * sign, int ecvtflag);
static char *
cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag)
{
struct EXTEND e;
_dbl_ext_cvt(value, &e);
return _ext_str_cvt(&e, ndigit, decpt, sign, ecvtflag);
}
char *
ecvt(long double value, int ndigit, int *decpt, int *sign)
{
return cvt(value, ndigit, decpt, sign, 1);
}
@ -20,88 +28,4 @@ fcvt(long double value, int ndigit, int *decpt, int *sign)
return cvt(value, ndigit, decpt, sign, 0);
}
static struct powers_of_10 {
long double pval;
long double rpval;
int exp;
} p10[] = {
1.0e32, 1.0e-32, 32,
1.0e16, 1.0e-16, 16,
1.0e8, 1.0e-8, 8,
1.0e4, 1.0e-4, 4,
1.0e2, 1.0e-2, 2,
1.0e1, 1.0e-1, 1,
1.0e0, 1.0e0, 0
};
static char *
cvt(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 /* NOFLOAT */

View file

@ -15,8 +15,8 @@ fclose(FILE *fp)
register int i, retval = 0;
for (i=0; i<FOPEN_MAX; i++)
if (fp == _iotable[i]) {
_iotable[i] = 0;
if (fp == __iotab[i]) {
__iotab[i] = 0;
break;
}
if (i >= FOPEN_MAX)

View file

@ -5,10 +5,8 @@
#include <stdio.h>
#undef feof
int
feof(FILE *stream)
(feof)(FILE *stream)
{
return (stream->_flags & _IOEOF) != 0;
return feof(stream);
}

View file

@ -5,10 +5,8 @@
#include <stdio.h>
#undef ferror
int
ferror(FILE *stream)
(ferror)(FILE *stream)
{
return (stream->_flags & _IOERR) != 0;
return ferror(stream);
}

View file

@ -15,7 +15,7 @@ fflush(FILE *stream)
if (!stream) {
for(i= 0; i < FOPEN_MAX; i++)
if (_iotable[i] && fflush(_iotable[i]))
if (__iotab[i] && fflush(__iotab[i]))
retval = EOF;
return retval;
}

View file

@ -4,7 +4,6 @@
/* $Header$ */
#include <stdio.h>
#include <sys/file.h>
int
fgetpos(FILE *stream, fpos_t *pos)

View file

@ -6,7 +6,7 @@
#include <stdio.h>
char *
fgets(char *s, int n, FILE *stream)
fgets(char *s, register int n, register FILE *stream)
{
register int ch;
register char *ptr;
@ -17,8 +17,11 @@ fgets(char *s, int n, FILE *stream)
if ( ch == '\n')
break;
}
if (ch == EOF && ptr == s)
return (char *)NULL;
if (ch == EOF) {
if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
}
*ptr = '\0';
return s;
}

View file

@ -0,0 +1,12 @@
/*
* fileno .c - map a stream to a file descriptor
*/
/* $Header$ */
#include <stdio.h>
int
(fileno)(FILE *stream)
{
return stream->_fd;
}

View file

@ -10,7 +10,7 @@
int read(int d, char *buf, int nbytes);
int
_fillbuf(register FILE *stream)
__fillbuf(register FILE *stream)
{
static unsigned char ch[FOPEN_MAX];
@ -33,16 +33,21 @@ _fillbuf(register FILE *stream)
stream->_bufsiz = BUFSIZ;
}
}
if (io_testflag(stream, _IONBF | _IOLBF)) {
register int i;
for (i = 0; i < FOPEN_MAX; i++) {
if (__iotab[i] && io_testflag(__iotab[i], _IOLBF))
if (io_testflag(__iotab[i], _IOWRITING))
(void) fflush(__iotab[i]);
}
}
if (!stream->_buf) {
stream->_buf = &ch[fileno(stream)];
stream->_bufsiz = 1;
}
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) {

View file

@ -56,7 +56,7 @@ _pscien(long double r, register char *s, int n, int flags)
*s++ = *s1++;
if ((n > 0) || (flags & FL_ALT))
*s++ = '.';
while (--n>0)
while (--n >= 0)
if (*s1) *s++ = *s1++;
else *s++ = '0';
*s++ = 'e';

View file

@ -7,12 +7,17 @@
#include <stdlib.h>
#include "loc_incl.h"
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
int write(int d, const char *buf, int nbytes);
int isatty(int d);
extern int (*_fflush)(FILE *stream);
int
_flushbuf(int c, FILE * stream)
__flushbuf(int c, FILE * stream)
{
_fflush = fflush;
if (fileno(stream) < 0) return EOF;
if (!io_testflag(stream, _IOWRITE)) return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
@ -25,21 +30,19 @@ _flushbuf(int c, FILE * stream)
if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) {
stream->_flags |= _IONBF;
}
else {
} else {
stream->_flags |= _IOLBF;
stream->_bufsiz = BUFSIZ;
stream->_count = -1;
}
}
else {
} else {
if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) {
stream->_flags |= _IONBF;
}
else {
} else {
stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ;
if (!io_testflag(stream, _IOLBF))
stream->_count = BUFSIZ - 1;
}
}
@ -51,33 +54,48 @@ _flushbuf(int c, FILE * stream)
char c1 = c;
stream->_count = 0;
if (io_testflag(stream, _IOAPPEND)) {
if (lseek(fileno(stream), 0L, 2) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
if (write(fileno(stream), &c1, 1) != 1) {
stream->_flags |= _IOERR;
return EOF;
}
return c;
}
else if (io_testflag(stream, _IOLBF)) {
} else if (io_testflag(stream, _IOLBF)) {
*stream->_ptr++ = c;
if (c == '\n' || stream->_count == -stream->_bufsiz) {
if (io_testflag(stream, _IOAPPEND)) {
if (lseek(fileno(stream), 0L, 2) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
if (write(fileno(stream), (char *)stream->_buf,
-stream->_count) != -stream->_count) {
stream->_flags |= _IOERR;
return EOF;
}
else {
} else {
stream->_ptr = stream->_buf;
stream->_count = 0;
}
}
}
else {
} else {
int count = stream->_ptr - stream->_buf;
stream->_count = stream->_bufsiz - 1;
stream->_ptr = stream->_buf + 1;
if (count > 0) {
if (io_testflag(stream, _IOAPPEND)) {
if (lseek(fileno(stream), 0L, 2) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
if (write(fileno(stream), (char *)stream->_buf, count)
!= count) {
*(stream->_buf) = c;
@ -89,13 +107,3 @@ _flushbuf(int c, FILE * stream)
}
return c;
}
void
_cleanup(void) /* fflush((FILE *)NULL) ??? */
{
register int i;
for( i = 0; i < FOPEN_MAX; i++ )
if (_iotable[i])
fclose(_iotable[i]);
}

View file

@ -5,12 +5,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include "loc_incl.h"
#define PMODE 0666
int open(const char *path, int flags, int mode);
/* The next 3 defines are true in all UNIX systems known to me.
*/
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
/* Since the O_CREAT flag is not available on all systems, we can't get it
* from the standard library. Furthermore, even if we know that <fcntl.h>
* contains such a flag, it's not sure whether it can be used, since we
* might be cross-compiling for another system, which may use an entirely
* different value for O_CREAT (or not support such a mode). The safest
* thing is to just use the Version 7 semantics for open, and use creat()
* whenever necessary.
*
* Another problem is O_APPEND, for which the same holds. When "a"
* open-mode is used, an lseek() to the end is done before every write()
* system-call.
*
* The O_CREAT, O_TRUNC and O_APPEND given here, are only for convenience.
* They are not passed to open(), so the values don't have to match a value
* from the real world. It is enough when they are unique.
*/
#define O_CREAT 0x010
#define O_TRUNC 0x020
#define O_APPEND 0x040
int open(const char *path, int flags);
int creat(const char *path, int mode);
int close(int d);
FILE *
@ -21,7 +48,7 @@ fopen(const char *name, const char *mode)
FILE *stream;
int fd, flags = 0;
for (i = 0; _iotable[i] != 0 ; i++)
for (i = 0; __iotab[i] != 0 ; i++)
if ( i >= FOPEN_MAX )
return (FILE *)NULL;
@ -36,7 +63,7 @@ fopen(const char *name, const char *mode)
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
flags |= _IOWRITE | _IOWRITING;
flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT;
break;
@ -57,7 +84,13 @@ fopen(const char *name, const char *mode)
}
}
fd = open(name, rwmode | rwflags, PMODE);
/* Perform a creat() when the file should be truncated or when
* the file is opened for writing and the open() failed.
*/
if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0)
&& (flags & _IOWRITE)))
fd = creat(name, PMODE);
if (fd < 0) return (FILE *)NULL;
@ -66,13 +99,13 @@ fopen(const char *name, const char *mode)
return (FILE *)NULL;
}
if ((flags & _IOREAD) && (flags & _IOWRITE))
if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0;
stream->_fd = fd;
stream->_flags = flags;
stream->_buf = NULL;
_iotable[i] = stream;
__iotab[i] = stream;
return stream;
}

View file

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

View file

@ -8,11 +8,11 @@
int
fputs(register const char *s, register FILE *stream)
{
register int retval = 0;
register int i = 0;
while (*s)
if (putc(*s++, stream) == EOF) return -1;
else retval++;
if (putc(*s++, stream) == EOF) return EOF;
else i++;
return retval;
return i;
}

View file

@ -6,17 +6,19 @@
#include <stdio.h>
size_t
fread(register void *ptr, size_t size, size_t nmemb, FILE *stream)
fread(void *ptr, size_t size, size_t nmemb, register FILE *stream)
{
int c;
size_t ndone = 0, s;
register char *cp = ptr;
register int c;
size_t ndone = 0;
register size_t s;
if (size)
while ( ndone < nmemb ) {
s = size;
do {
if ((c = getc(stream)) != EOF)
*ptr++ = c;
*cp++ = c;
else
return ndone;
} while (--s);

View file

@ -5,11 +5,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include "loc_incl.h"
#define PMODE 0666
int open(const char *path, int flags, int mode);
/* Do not "optimize" this file to use the open with O_CREAT if the file
* does not exist. The reason is given in fopen.c.
*/
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 0x010
#define O_TRUNC 0x020
#define O_APPEND 0x040
int open(const char *path, int flags);
int creat(const char *path, int mode);
int close(int d);
FILE *
@ -17,57 +29,52 @@ freopen(const char *name, const char *mode, FILE *stream)
{
register int i;
int rwmode = 0, rwflags = 0;
int fd, flags = stream->_flags & ~(_IOWRITE|_IOREAD|_IOERR|_IOEOF);
int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF);
(void) fflush(stream); /* ignore errors */
(void) close(fileno(stream));
switch(*mode++) {
case 'r':
flags |= _IOREAD;
rwmode = O_RDONLY;
break;
case 'w':
flags |= _IOWRITE;
rwmode = O_WRONLY;
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
flags |= _IOWRITE;
flags |= _IOWRITE | _IOAPPEND;
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;
}
}
fd = open(name, rwmode | rwflags, PMODE);
if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0)
&& (flags & _IOWRITE)))
fd = creat(name, PMODE);
if (fd < 0) {
for( i = 0; i < FOPEN_MAX; i++) {
if (stream == _iotable[i]) {
_iotable[i] = 0;
if (stream == __iotab[i]) {
__iotab[i] = 0;
break;
}
}

View file

@ -4,10 +4,16 @@
/* $Header$ */
#include <stdio.h>
#include <sys/file.h>
#if (SEEK_CUR != 1) || (SEEK_END != 2) || (SEEK_SET != 0)
#error SEEK_* values are wrong
#endif
#include "loc_incl.h"
int lseek(int d, int offset, int whence);
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
int
fseek(FILE *stream, long int offset, int whence)
@ -15,19 +21,6 @@ fseek(FILE *stream, long int offset, int whence)
int count, adjust = 0;
long pos;
#if (SEEK_CUR != L_INCR) || (SEEK_SET != L_SET) || (SEEK_END != L_XTND)
int swhence; /* the real whence for lseek */
switch(whence) { /* map to UNIX values */
case SEEK_CUR: swhence = L_INCR; break;
case SEEK_SET: swhence = L_SET; break;
case SEEK_END: swhence = L_XTND; break;
default: swhence = whence; break;
}
#else
#define swhence whence
#endif
stream->_flags &= ~(_IOEOF | _IOERR);
/* Clear both the end of file and error flags */
@ -36,15 +29,13 @@ fseek(FILE *stream, long int offset, int whence)
&& stream->_buf
&& !io_testflag(stream,_IONBF))
adjust = stream->_count;
pos = lseek(fileno(stream), offset - adjust, swhence);
pos = lseek(fileno(stream), offset - adjust, whence);
stream->_count = 0;
}
else if (io_testflag(stream,_IOWRITING)) {
} else if (io_testflag(stream,_IOWRITING)) {
fflush(stream);
pos = lseek(fileno(stream), offset, swhence);
}
else /* neither reading nor writing. The buffer must be empty */
pos = lseek(fileno(stream), offset, swhence);
pos = lseek(fileno(stream), offset, whence);
} else /* neither reading nor writing. The buffer must be empty */
pos = lseek(fileno(stream), offset, whence);
if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
stream->_flags &= ~(_IOREADING | _IOWRITING);

View file

@ -4,10 +4,16 @@
/* $Header$ */
#include <stdio.h>
#include <sys/file.h>
#if (SEEK_CUR != 1) || (SEEK_SET != 0) || (SEEK_END != 2)
#error SEEK_* values are wrong
#endif
#include "loc_incl.h"
int lseek(int d, int offset, int whence);
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
long ftell(FILE *stream)
{
@ -22,7 +28,7 @@ long ftell(FILE *stream)
adjust = stream->_ptr - stream->_buf;
else adjust = 0;
result = lseek(fileno(stream), 0, L_INCR);
result = lseek(fileno(stream), 0, SEEK_CUR);
if ( result == -1 )
return result;

View file

@ -6,20 +6,21 @@
#include <stdio.h>
size_t
fwrite(register const void *ptr, size_t size, size_t nmemb,
fwrite(const void *ptr, size_t size, size_t nmemb,
register FILE *stream)
{
unsigned s;
unsigned ndone = 0;
register const unsigned char *cp = ptr;
register size_t s;
size_t ndone = 0;
if (size)
while ( ndone < nmemb ) {
s = size;
do {
if (putc((int)*(unsigned char *)ptr, stream)
if (putc((int)*cp, stream)
== EOF)
return ndone;
ptr++;
cp++;
}
while (--s);
ndone++;

View file

@ -5,10 +5,8 @@
#include <stdio.h>
#undef getc
int
getc(FILE *stream)
(getc)(FILE *stream)
{
return fgetc(stream);
return getc(stream);
}

View file

@ -5,10 +5,8 @@
#include <stdio.h>
#undef getchar
int
getchar(void)
(getchar)(void)
{
return getc(stdin);
return getchar();
}

View file

@ -8,15 +8,19 @@
char *
gets(char *s)
{
register FILE *stream = stdin;
register int ch;
register char *ptr;
ptr = s;
while ((ch = getc(stdin)) != EOF && ch != '\n')
while ((ch = getc(stream)) != EOF && ch != '\n')
*ptr++ = ch;
if (ch == EOF && ptr==s)
return (char *)NULL;
if (ch == EOF) {
if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
}
*ptr = '\0';
return s;

View file

@ -0,0 +1,21 @@
/*
* icompute.c - compute an integer
*/
/* $Header$ */
#include "loc_incl.h"
/* This routine is used in doprnt.c as well as in tmpfile.c and tmpnam.c. */
char *
_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++ = (c>9 ? c-10+'a' : c+'0');
return s;
}

View file

@ -0,0 +1,17 @@
/*
* isatty - check if a file descriptor is associated with a terminal
*/
/* $Header$ */
int gtty(int d, char *buf);
int isatty(int d)
{
char buf[128];
/* not a sgttyb struct; it might not be large enough;
I know for a fact that it isn't large enough on PC/IX,
where gtty is an ioctl(..., TCGETA, ...)
*/
return gtty(d, buf) >= 0;
}

View file

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

View file

@ -5,10 +5,8 @@
#include <stdio.h>
#undef putc
int
putc(int c, FILE *stream)
(putc)(int c, FILE *stream)
{
return fputc(c, stream);
return putc(c, stream);
}

View file

@ -5,10 +5,8 @@
#include <stdio.h>
#undef putchar
int
putchar(int c)
(putchar)(int c)
{
return putc(c, stdout);
return putchar(c);
}

View file

@ -8,5 +8,13 @@
int
puts(register const char *s)
{
return fputs(s, stdout);
register FILE *file = stdout;
register int i = 0;
while (*s) {
if (putc(*s++, file) == EOF) return EOF;
else i++;
}
if (putc('\n', file) == EOF) return EOF;
return i + 1;
}

View file

@ -6,7 +6,6 @@
#include <stdio.h>
int link(const char *name1, const char *name2);
int unlink(const char *path);
int
rename(const char *old, const char *new) {

View file

@ -7,11 +7,14 @@
#include <stdlib.h>
#include "loc_incl.h"
extern int (*_fflush)(FILE *stream);
int
setvbuf(register FILE *stream, char *buf, int mode, size_t size)
{
int retval = 0;
_fflush = fflush;
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF;

View file

@ -17,7 +17,7 @@ sprintf(char * s, const char *format, ...)
va_start(ap, format);
tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) s;
tmp_stream._ptr = (unsigned char *) s;
tmp_stream._count = 32767;

View file

@ -5,9 +5,10 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
int sscanf(const char *s, const char *format, ...)
int sscanf(char *s, const char *format, ...)
{
va_list ap;
int retval;
@ -16,10 +17,10 @@ int sscanf(const char *s, const char *format, ...)
va_start(ap, format);
tmp_stream._fd = -1;
tmp_stream._flags = _IOREAD + _IONBF;
tmp_stream._flags = _IOREAD + _IONBF + _IOREADING;
tmp_stream._buf = (unsigned char *) s;
tmp_stream._ptr = (unsigned char *) s;
tmp_stream._count = 32767;
tmp_stream._count = strlen(s);
retval = _doscan(&tmp_stream, format, ap);

View file

@ -10,11 +10,5 @@
int
vfprintf(FILE *stream, const char *format, va_list arg)
{
int retval;
retval = _doprnt (format, arg, stream);
if (io_testflag(stream, _IOLBF))
fflush(stream);
return retval;
return _doprnt (format, arg, stream);
}

View file

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