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 tmpfile.c
tmpnam.c tmpnam.c
rename.c rename.c
@ -15,6 +15,7 @@ vfprintf.c
vprintf.c vprintf.c
vsprintf.c vsprintf.c
doprnt.c doprnt.c
icompute.c
fscanf.c fscanf.c
scanf.c scanf.c
sscanf.c sscanf.c
@ -40,6 +41,7 @@ ftell.c
clearerr.c clearerr.c
feof.c feof.c
ferror.c ferror.c
fileno.c
fltpr.c fltpr.c
ecvt.c ecvt.c
gcvt.c gcvt.c
@ -47,4 +49,5 @@ fillbuf.c
fclose.c fclose.c
flushbuf.c flushbuf.c
fflush.c fflush.c
isatty.c
data.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> #include <stdio.h>
struct _iobuf _stdin = { struct __iobuf __stdin = {
0, 0, _IOREAD, 0, 0, 0, _IOREAD, 0,
(char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL, (char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL,
}; };
struct _iobuf _stdout = { struct __iobuf __stdout = {
1, 0, _IOWRITE, 0, 0, 1, _IOWRITE, 0,
(char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL, (char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL,
}; };
struct _iobuf _stderr = { struct __iobuf __stderr = {
2, 0, _IOWRITE | _IONBF, 0, 0, 2, _IOWRITE | _IOLBF, 0,
(char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL, (char *)NULL, (unsigned char *)NULL, (unsigned char *)NULL,
}; };
struct _iobuf *_iotable[FOPEN_MAX] = { FILE *__iotab[FOPEN_MAX] = {
&_stdin, &__stdin,
&_stdout, &__stdout,
&_stderr, &__stderr,
0 0
}; };

View file

@ -9,10 +9,11 @@
#include <string.h> #include <string.h>
#include "loc_incl.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) gnum(register const char *f, int *ip, va_list *app)
{ {
register int i, c; register int i, c;
if (*f == '*') { if (*f == '*') {
*ip = va_arg((*app), int); *ip = va_arg((*app), int);
@ -28,28 +29,15 @@ gnum(register const char *f, int *ip, va_list *app)
return f; return f;
} }
#if EM_WSIZE == EM_PSIZE #if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */ #define set_pointer(flags) /* nothing */
#elif EM_LSIZE == EM_PSIZE #elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG) #define set_pointer(flags) (flags |= FL_LONG)
#else #else
#define set_pointer(flags) /* nothing */
#error garbage pointer size #error garbage pointer size
#define set_pointer(flags) /* compilation might continue */
#endif #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 */ /* print an ordinal number */
static char * static char *
o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed) 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++ = ' '; else if (flags & FL_SPACE) *s++ = ' ';
unsigned_val = signed_val; unsigned_val = signed_val;
} }
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0';
if (!unsigned_val) { if (!unsigned_val) {
if (precision != 0) if (!precision)
*s++ = '0'; return s;
return s; } else if (((flags & FL_ALT) && (c == 'x' || c == 'X'))
} || c == 'p') {
if (((flags & FL_ALT) && (c == 'x' || c == 'X' || c == 'o'))
|| c == 'p') {
*s++ = '0'; *s++ = '0';
if (c != 'o') *s++ = (c == 'X' ? 'X' : 'x');
*s++ = (c == 'X') ? 'X' : 'x';
} }
switch (c) { switch (c) {
@ -133,8 +119,7 @@ f_print(va_list *ap, int flags, char *s, char c, int precision)
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 ld_val = (long double) va_arg(*ap, double);
ld_val = (long double) va_arg(*ap, double);
switch(c) { switch(c) {
case 'f': case 'f':
@ -142,7 +127,7 @@ f_print(va_list *ap, int flags, char *s, char c, int precision)
break; break;
case 'e': case 'e':
case 'E': case 'E':
s = _pscien(ld_val, s, precision, flags); s = _pscien(ld_val, s, precision , flags);
break; break;
case 'g': case 'g':
case 'G': case 'G':
@ -160,16 +145,17 @@ f_print(va_list *ap, int flags, char *s, char c, int precision)
int int
_doprnt(register const char *fmt, va_list ap, FILE *stream) _doprnt(register const char *fmt, va_list ap, FILE *stream)
{ {
register char *s; register char *s;
unsigned int uint; unsigned int uint;
register int j; register int j;
int i, c, width, precision, zfill, flags, between_fill; int i, c, width, precision, zfill, flags, between_fill;
int nrchars=0; int nrchars=0;
char *oldfmt, *s1, buf[1025]; const char *oldfmt;
char *s1, buf[1025];
while (c = *fmt++) { while (c = *fmt++) {
if (c != '%') { if (c != '%') {
#ifdef CPM #ifdef CPM
if (c == '\n') { if (c == '\n') {
if (putc('\r', stream) == EOF) if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1; return nrchars ? -nrchars : -1;
@ -210,15 +196,10 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
} }
if (!(flags & FL_WIDTHSPEC)) width = 0; if (!(flags & FL_WIDTHSPEC)) width = 0;
if (flags & FL_SIGN) if (flags & FL_SIGN) flags &= ~FL_SPACE;
flags &= ~FL_SPACE;
if (flags & (FL_LJUST | FL_PRECSPEC)) if (flags & FL_LJUST) flags &= ~FL_ZEROFILL;
flags &= ~FL_ZEROFILL;
zfill = ' ';
if (flags & FL_ZEROFILL)
zfill = '0';
s = s1 = buf; s = s1 = buf;
@ -230,7 +211,7 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
switch (c = *fmt++) { switch (c = *fmt++) {
default: default:
#ifdef CPM #ifdef CPM
if (c == '\n') { if (c == '\n') {
if (putc('\r', stream) == EOF) if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1; return nrchars ? -nrchars : -1;
@ -255,7 +236,7 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
s1 = "(null)"; s1 = "(null)";
s = s1; s = s1;
while (precision || !(flags & FL_PRECSPEC)) { while (precision || !(flags & FL_PRECSPEC)) {
if (*s == '\0') if (*s == '\0')
break; break;
s++; s++;
precision--; precision--;
@ -270,17 +251,19 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
case 'x': case 'x':
case 'X': case 'X':
if (!(flags & FL_PRECSPEC)) precision = 1; if (!(flags & FL_PRECSPEC)) precision = 1;
else if (c != 'p') flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 0); s = o_print(&ap, flags, s, c, precision, 0);
break; break;
case 'd': case 'd':
case 'i': case 'i':
flags |= FL_SIGNEDCONV; flags |= FL_SIGNEDCONV;
if (!(flags & FL_PRECSPEC)) precision = 1; if (!(flags & FL_PRECSPEC)) precision = 1;
else flags &= ~FL_ZEROFILL;
s = o_print(&ap, flags, s, c, precision, 1); s = o_print(&ap, flags, s, c, precision, 1);
break; break;
case 'c': case 'c':
uint = va_arg(ap, unsigned int); uint = va_arg(ap, unsigned int);
for ( i= sizeof(uint) -1 ; i>=0 ; i-- ) { for ( i= sizeof(uint) -1 ; i>=0 ; i-- ) {
if ( *s = uint%256 ) s++; if ( *s = uint%256 ) s++;
uint/= 256 ; uint/= 256 ;
} }
@ -308,6 +291,8 @@ _doprnt(register const char *fmt, va_list ap, FILE *stream)
fmt = va_arg(ap, char *); fmt = va_arg(ap, char *);
continue; continue;
} }
zfill = ' ';
if (flags & FL_ZEROFILL) zfill = '0';
j = s - s1; j = s - s1;
/* between_fill is true under the following conditions: /* between_fill is true under the following conditions:

View file

@ -10,8 +10,9 @@
#include "loc_incl.h" #include "loc_incl.h"
#define NUMLEN 512 #define NUMLEN 512
#define NR_CHARS 256
static char Xtable[128]; static char Xtable[NR_CHARS];
static char inp_buf[NUMLEN]; static char inp_buf[NUMLEN];
/* Collect a number of characters which constitite an ordinal number. /* Collect a number of characters which constitite an ordinal number.
@ -22,58 +23,60 @@ static char inp_buf[NUMLEN];
*/ */
static char * static char *
o_collect(register int c, register FILE *stream, char type, o_collect(register int c, register FILE *stream, char type,
int width, int *base) int width, int *basep)
{ {
register char *bufp = inp_buf; register char *bufp = inp_buf;
register int base;
switch (type) { switch (type) {
case 'i': /* i means octal, decimal or hexadecimal */ case 'i': /* i means octal, decimal or hexadecimal */
case 'p': case 'p':
case 'x': case 'x':
case 'X': *base = 16; break; case 'X': base = 16; break;
case 'd': case 'd':
case 'u': *base = 10; break; case 'u': base = 10; break;
case 'o': *base = 8; break; case 'o': base = 8; break;
case 'b': *base = 2; break; case 'b': base = 2; break;
} }
if (c == '-' || c == '+') { if (c == '-' || c == '+') {
*bufp++ = c; *bufp++ = c;
width--; if (--width);
c = getc(stream); c = getc(stream);
} }
if (width && c == '0' && *base == 16) { if (width && c == '0' && base == 16) {
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
if (c != 'x' && c != 'X') { if (c != 'x' && c != 'X') {
if (type == 'i') *base = 8; if (type == 'i') base = 8;
} }
else if (width) { else if (width) {
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
} }
else if (type == 'i') *base = 10; else if (type == 'i') base = 10;
while (width) { while (width) {
if (((*base == 10) && isdigit(c)) if (((base == 10) && isdigit(c))
|| ((*base == 16) && isxdigit(c)) || ((base == 16) && isxdigit(c))
|| ((*base == 8) && isdigit(c) && (c < '8')) || ((base == 8) && isdigit(c) && (c < '8'))
|| ((*base == 2) && isdigit(c) && (c < '2'))) { || ((base == 2) && isdigit(c) && (c < '2'))) {
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
else break; else break;
} }
if (c != EOF) ungetc(c, stream); if (width && c != EOF) ungetc(c, stream);
if (type == 'i') *base = 0; if (type == 'i') base = 0;
*bufp-- = '\0'; *basep = base;
return bufp; *bufp = '\0';
return bufp - 1;
} }
#ifndef NOFLOAT #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 * 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 * 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 * 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 * 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; register char *bufp = inp_buf;
int digit_seen = 0; int digit_seen = 0;
if (c == '-' || c == '+') { if (c == '-' || c == '+') {
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
while (width && isdigit(c)) { while (width && isdigit(c)) {
digit_seen++; digit_seen++;
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
if (width && c == '.') { if (width && c == '.') {
*bufp++ = c; *bufp++ = c;
width--; if(--width)
c = getc(stream); c = getc(stream);
while (width && isdigit(c)) { while (width && isdigit(c)) {
digit_seen++; digit_seen++;
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
} }
if (!digit_seen) { if (!digit_seen) {
if (c != EOF) ungetc(c, stream); if (width && c != EOF) ungetc(c, stream);
return inp_buf - 1; return inp_buf - 1;
} }
else digit_seen = 0; else digit_seen = 0;
if (width && (c == 'e' || c == 'E')) { if (width && (c == 'e' || c == 'E')) {
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
if (width && (c == '+' || c == '-')) { if (width && (c == '+' || c == '-')) {
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
while (width && isdigit(c)) { while (width && isdigit(c)) {
digit_seen++; digit_seen++;
*bufp++ = c; *bufp++ = c;
width--; if (--width)
c = getc(stream); c = getc(stream);
} }
if (!digit_seen) { if (!digit_seen) {
if (c != EOF) ungetc(c,stream); if (width && c != EOF) ungetc(c,stream);
return inp_buf - 1; return inp_buf - 1;
} }
} }
if (c != EOF) ungetc(c, stream); if (width && c != EOF) ungetc(c, stream);
*bufp-- = '\0'; *bufp = '\0';
return bufp; return bufp - 1;
} }
#endif /* NOFLOAT */ #endif /* NOFLOAT */
@ -165,19 +169,14 @@ _doscan(register FILE *stream, const char *format, va_list ap)
int flags; /* some flags */ int flags; /* some flags */
int reverse; /* reverse the checking in [...] */ int reverse; /* reverse the checking in [...] */
int kind; int kind;
register int ic; register int ic; /* the input character */
#ifndef NOFLOAT #ifndef NOFLOAT
long double ld_val; long double ld_val;
#endif #endif
ic = getc(stream);
if (ic == EOF)
return EOF;
ungetc(ic,stream);
while (1) { while (1) {
if (isspace(*format)) { if (isspace(*format)) {
while (isspace (*format)) while (isspace(*format))
++format; /* skip whitespace */ ++format; /* skip whitespace */
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
@ -185,13 +184,14 @@ _doscan(register FILE *stream, const char *format, va_list ap)
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
} }
if (ic != EOF) ungetc(ic,stream);
nrchars--;
} }
else { if (!*format) break; /* end of format */
ic = getc(stream);
nrchars++; ic = getc(stream);
} nrchars++;
if (!*format)
break; /* end of format */
if (ic == EOF) if (ic == EOF)
return conv ? done : EOF; return conv ? done : EOF;
if (*format != '%') { if (*format != '%') {
@ -238,8 +238,7 @@ _doscan(register FILE *stream, const char *format, va_list ap)
if (kind == ic) continue; if (kind == ic) continue;
break; break;
case 'n': case 'n':
if (ic != EOF) if (ic != EOF) ungetc(ic, stream);
ungetc(ic, stream);
nrchars--; nrchars--;
if (flags & FL_SHORT) if (flags & FL_SHORT)
*va_arg(ap, short *) = (short) nrchars; *va_arg(ap, short *) = (short) nrchars;
@ -258,7 +257,10 @@ _doscan(register FILE *stream, const char *format, va_list ap)
case 'X': /* ditto */ case 'X': /* ditto */
if (!(flags & FL_WIDTHSPEC)) if (!(flags & FL_WIDTHSPEC))
width = NUMLEN; width = NUMLEN;
if (!width) return done; if (!width) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
str = o_collect(ic, stream, kind, width, &base); str = o_collect(ic, stream, kind, width, &base);
if (str < inp_buf) return done; if (str < inp_buf) return done;
nrchars += str - inp_buf + 1; nrchars += str - inp_buf + 1;
@ -281,20 +283,25 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 1; width = 1;
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
tmp_string = va_arg(ap, char *); tmp_string = va_arg(ap, char *);
if (!width) return done; if (!width) {
if (width && !(flags & FL_NOASSIGN) && (ic != EOF)) if (ic != EOF) ungetc(ic, stream);
return done;
}
if (!(flags & FL_NOASSIGN) && (ic != EOF))
++done; ++done;
while (width && ic != EOF) { while (width && ic != EOF) {
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*tmp_string++ = (char) ic; *tmp_string++ = (char) ic;
width--; if (--width) {
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
}
} }
if (ic != EOF) if (width) {
ungetc(ic,stream); if (ic != EOF) ungetc(ic,stream);
nrchars--; nrchars--;
}
break; break;
case 's': case 's':
@ -302,36 +309,45 @@ _doscan(register FILE *stream, const char *format, va_list ap)
width = 0xffff; width = 0xffff;
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
tmp_string = va_arg(ap, char *); tmp_string = va_arg(ap, char *);
if (!width) return done; if (!width) {
if (ic != EOF) ungetc(ic,stream);
return done;
}
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
++done; ++done;
while (width && ic != EOF && !isspace(ic)) { while (width && ic != EOF && !isspace(ic)) {
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
*tmp_string++ = (char) ic; *tmp_string++ = (char) ic;
width--; if (--width) {
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
/* terminate the string */ }
if (!(flags & FL_NOASSIGN)) }
*tmp_string = '\0'; /* terminate the string */
if (!(flags & FL_NOASSIGN))
*tmp_string = '\0';
if (width) {
if (ic != EOF) ungetc(ic,stream);
nrchars--;
} }
if (ic != EOF)
ungetc(ic,stream);
nrchars--;
break; break;
case '[': case '[':
{ int old_nrchars;
if (!(flags & FL_WIDTHSPEC)) if (!(flags & FL_WIDTHSPEC))
width = 0xffff; width = 0xffff;
if (!width) return done; if (!width) {
if (ic != EOF) ungetc(ic, stream);
return done;
}
if ( *(++format) == '^' ) { if ( *(++format) == '^' ) {
reverse = 1; reverse = 1;
format++; format++;
} else } else
reverse = 0; reverse = 0;
for (tmp_string = Xtable; tmp_string < &Xtable[128] for (tmp_string = Xtable; tmp_string < &Xtable[NR_CHARS]
; tmp_string++) ; tmp_string++)
*tmp_string = 0; *tmp_string = 0;
@ -353,26 +369,37 @@ _doscan(register FILE *stream, const char *format, va_list ap)
else Xtable['-'] = 1; else Xtable['-'] = 1;
} }
} }
if (!*format) if (!*format) {
if (ic != EOF) ungetc(ic,stream);
return done; return done;
}
old_nrchars = nrchars;
if (!(flags & FL_NOASSIGN)) if (!(flags & FL_NOASSIGN))
tmp_string = va_arg(ap, char *); tmp_string = va_arg(ap, char *);
while (width && ic != EOF && (Xtable[ic] ^ reverse)) { 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)) if (!(flags & FL_NOASSIGN))
*tmp_string++ = (char) ic; *tmp_string++ = (char) ic;
width--; if (--width) {
ic = getc(stream); ic = getc(stream);
nrchars++; nrchars++;
}
}
if (width) {
if (ic != EOF) ungetc(ic, stream);
nrchars--;
} }
if (ic != EOF)
ungetc(ic, stream);
nrchars--;
if (!(flags & FL_NOASSIGN)) { /* terminate string */ if (!(flags & FL_NOASSIGN)) { /* terminate string */
*tmp_string = '\0'; *tmp_string = '\0';
++done; ++done;
} }
break; break;
}
#ifndef NOFLOAT: #ifndef NOFLOAT:
case 'e': case 'e':
case 'E': case 'E':
@ -402,7 +429,5 @@ _doscan(register FILE *stream, const char *format, va_list ap)
} /* end switch */ } /* end switch */
++format; ++format;
} }
if (ic != EOF)
ungetc(ic, stream);
return conv ? done : EOF; return conv ? done : EOF;
} }

View file

@ -1,16 +1,24 @@
/*
* ecvt.c - conversion routines for printing floating point numbers
*/
/* $Header$ */ /* $Header$ */
#ifndef NOFLOAT #ifndef NOFLOAT
static char *cvt(long double value, int ndigit, int *decpt, int *sign, int ecvtflag); #include "../stdlib/ext_fmt.h"
#define NDIGITS 128 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 * char *
ecvt(long 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);
} }
@ -20,88 +28,4 @@ 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 {
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 */ #endif /* NOFLOAT */

View file

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

View file

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

View file

@ -5,10 +5,8 @@
#include <stdio.h> #include <stdio.h>
#undef ferror
int 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) { if (!stream) {
for(i= 0; i < FOPEN_MAX; i++) for(i= 0; i < FOPEN_MAX; i++)
if (_iotable[i] && fflush(_iotable[i])) if (__iotab[i] && fflush(__iotab[i]))
retval = EOF; retval = EOF;
return retval; return retval;
} }

View file

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

View file

@ -6,7 +6,7 @@
#include <stdio.h> #include <stdio.h>
char * char *
fgets(char *s, int n, FILE *stream) fgets(char *s, register int n, register FILE *stream)
{ {
register int ch; register int ch;
register char *ptr; register char *ptr;
@ -17,8 +17,11 @@ fgets(char *s, int n, FILE *stream)
if ( ch == '\n') if ( ch == '\n')
break; break;
} }
if (ch == EOF && ptr == s) if (ch == EOF) {
return (char *)NULL; if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
}
*ptr = '\0'; *ptr = '\0';
return s; 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 read(int d, char *buf, int nbytes);
int int
_fillbuf(register FILE *stream) __fillbuf(register FILE *stream)
{ {
static unsigned char ch[FOPEN_MAX]; static unsigned char ch[FOPEN_MAX];
@ -33,16 +33,21 @@ _fillbuf(register FILE *stream)
stream->_bufsiz = BUFSIZ; 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) { if (!stream->_buf) {
stream->_buf = &ch[fileno(stream)]; stream->_buf = &ch[fileno(stream)];
stream->_bufsiz = 1; stream->_bufsiz = 1;
} }
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) {

View file

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

View file

@ -7,12 +7,17 @@
#include <stdlib.h> #include <stdlib.h>
#include "loc_incl.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 write(int d, const char *buf, int nbytes);
int isatty(int d); int isatty(int d);
extern int (*_fflush)(FILE *stream);
int int
_flushbuf(int c, FILE * stream) __flushbuf(int c, FILE * stream)
{ {
_fflush = fflush;
if (fileno(stream) < 0) return EOF; if (fileno(stream) < 0) return EOF;
if (!io_testflag(stream, _IOWRITE)) return EOF; if (!io_testflag(stream, _IOWRITE)) return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF; if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
@ -25,22 +30,20 @@ _flushbuf(int c, FILE * stream)
if (!(stream->_buf = if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) { (unsigned char *) malloc(BUFSIZ))) {
stream->_flags |= _IONBF; stream->_flags |= _IONBF;
} } else {
else {
stream->_flags |= _IOLBF; stream->_flags |= _IOLBF;
stream->_bufsiz = BUFSIZ; stream->_bufsiz = BUFSIZ;
stream->_count = -1; stream->_count = -1;
} }
} } else {
else {
if (!(stream->_buf = if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) { (unsigned char *) malloc(BUFSIZ))) {
stream->_flags |= _IONBF; stream->_flags |= _IONBF;
} } else {
else {
stream->_flags |= _IOMYBUF; stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ; stream->_bufsiz = BUFSIZ;
stream->_count = BUFSIZ - 1; if (!io_testflag(stream, _IOLBF))
stream->_count = BUFSIZ - 1;
} }
} }
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
@ -51,33 +54,48 @@ _flushbuf(int c, FILE * stream)
char c1 = c; char c1 = c;
stream->_count = 0; 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) { if (write(fileno(stream), &c1, 1) != 1) {
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} }
return c; return c;
} } else if (io_testflag(stream, _IOLBF)) {
else if (io_testflag(stream, _IOLBF)) {
*stream->_ptr++ = c; *stream->_ptr++ = c;
if (c == '\n' || stream->_count == -stream->_bufsiz) { 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, if (write(fileno(stream), (char *)stream->_buf,
-stream->_count) != -stream->_count) { -stream->_count) != -stream->_count) {
stream->_flags |= _IOERR; stream->_flags |= _IOERR;
return EOF; return EOF;
} } else {
else {
stream->_ptr = stream->_buf; stream->_ptr = stream->_buf;
stream->_count = 0; stream->_count = 0;
} }
} }
} } else {
else {
int count = stream->_ptr - stream->_buf; int count = stream->_ptr - stream->_buf;
stream->_count = stream->_bufsiz - 1; stream->_count = stream->_bufsiz - 1;
stream->_ptr = stream->_buf + 1; stream->_ptr = stream->_buf + 1;
if (count > 0) { 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) if (write(fileno(stream), (char *)stream->_buf, count)
!= count) { != count) {
*(stream->_buf) = c; *(stream->_buf) = c;
@ -89,13 +107,3 @@ _flushbuf(int c, FILE * stream)
} }
return c; 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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/file.h>
#include "loc_incl.h" #include "loc_incl.h"
#define PMODE 0666 #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); int close(int d);
FILE * FILE *
@ -21,7 +48,7 @@ fopen(const char *name, const char *mode)
FILE *stream; FILE *stream;
int fd, flags = 0; int fd, flags = 0;
for (i = 0; _iotable[i] != 0 ; i++) for (i = 0; __iotab[i] != 0 ; i++)
if ( i >= FOPEN_MAX ) if ( i >= FOPEN_MAX )
return (FILE *)NULL; return (FILE *)NULL;
@ -36,7 +63,7 @@ fopen(const char *name, const char *mode)
rwflags = O_CREAT | O_TRUNC; rwflags = O_CREAT | O_TRUNC;
break; break;
case 'a': case 'a':
flags |= _IOWRITE | _IOWRITING; flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
rwmode = O_WRONLY; rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT; rwflags |= O_APPEND | O_CREAT;
break; 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; if (fd < 0) return (FILE *)NULL;
@ -66,13 +99,13 @@ fopen(const char *name, const char *mode)
return (FILE *)NULL; return (FILE *)NULL;
} }
if ((flags & _IOREAD) && (flags & _IOWRITE)) if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING); flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0; stream->_count = 0;
stream->_fd = fd; stream->_fd = fd;
stream->_flags = flags; stream->_flags = flags;
stream->_buf = NULL; stream->_buf = NULL;
_iotable[i] = stream; __iotab[i] = stream;
return stream; return stream;
} }

View file

@ -16,8 +16,6 @@ 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 ( retval >= 0 && io_testflag(stream,_IOLBF) )
if (fflush(stream)) return EOF;
va_end(ap); va_end(ap);

View file

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

View file

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

View file

@ -5,11 +5,23 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/file.h>
#include "loc_incl.h" #include "loc_incl.h"
#define PMODE 0666 #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); int close(int d);
FILE * FILE *
@ -17,57 +29,52 @@ freopen(const char *name, const char *mode, FILE *stream)
{ {
register int i; register int i;
int rwmode = 0, rwflags = 0; 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) fflush(stream); /* ignore errors */
(void) close(fileno(stream)); (void) close(fileno(stream));
switch(*mode++) { switch(*mode++) {
case 'r': case 'r':
flags |= _IOREAD; flags |= _IOREAD;
rwmode = O_RDONLY; rwmode = O_RDONLY;
break; break;
case 'w': case 'w':
flags |= _IOWRITE; flags |= _IOWRITE;
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 | _IOAPPEND;
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;
} }
} }
fd = open(name, rwmode | rwflags, PMODE); if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0)
&& (flags & _IOWRITE)))
fd = creat(name, PMODE);
if (fd < 0) { if (fd < 0) {
for( i = 0; i < FOPEN_MAX; i++) { for( i = 0; i < FOPEN_MAX; i++) {
if (stream == _iotable[i]) { if (stream == __iotab[i]) {
_iotable[i] = 0; __iotab[i] = 0;
break; break;
} }
} }

View file

@ -4,10 +4,16 @@
/* $Header$ */ /* $Header$ */
#include <stdio.h> #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" #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 int
fseek(FILE *stream, long int offset, int whence) fseek(FILE *stream, long int offset, int whence)
@ -15,19 +21,6 @@ fseek(FILE *stream, long int offset, int whence)
int count, adjust = 0; int count, adjust = 0;
long pos; 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); stream->_flags &= ~(_IOEOF | _IOERR);
/* Clear both the end of file and error flags */ /* Clear both the end of file and error flags */
@ -36,15 +29,13 @@ fseek(FILE *stream, long int offset, int whence)
&& stream->_buf && stream->_buf
&& !io_testflag(stream,_IONBF)) && !io_testflag(stream,_IONBF))
adjust = stream->_count; adjust = stream->_count;
pos = lseek(fileno(stream), offset - adjust, swhence); pos = lseek(fileno(stream), offset - adjust, whence);
stream->_count = 0; stream->_count = 0;
} } else if (io_testflag(stream,_IOWRITING)) {
else if (io_testflag(stream,_IOWRITING)) {
fflush(stream); fflush(stream);
pos = lseek(fileno(stream), offset, swhence); pos = lseek(fileno(stream), offset, whence);
} } else /* neither reading nor writing. The buffer must be empty */
else /* neither reading nor writing. The buffer must be empty */ pos = lseek(fileno(stream), offset, whence);
pos = lseek(fileno(stream), offset, swhence);
if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE)) if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
stream->_flags &= ~(_IOREADING | _IOWRITING); stream->_flags &= ~(_IOREADING | _IOWRITING);

View file

@ -4,10 +4,16 @@
/* $Header$ */ /* $Header$ */
#include <stdio.h> #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" #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) long ftell(FILE *stream)
{ {
@ -21,8 +27,8 @@ long ftell(FILE *stream)
&& !io_testflag(stream,_IONBF)) && !io_testflag(stream,_IONBF))
adjust = stream->_ptr - stream->_buf; adjust = stream->_ptr - stream->_buf;
else adjust = 0; else adjust = 0;
result = lseek(fileno(stream), 0, L_INCR); result = lseek(fileno(stream), 0, SEEK_CUR);
if ( result == -1 ) if ( result == -1 )
return result; return result;

View file

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

View file

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

View file

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

View file

@ -8,15 +8,19 @@
char * char *
gets(char *s) gets(char *s)
{ {
register FILE *stream = stdin;
register int ch; register int ch;
register char *ptr; register char *ptr;
ptr = s; ptr = s;
while ((ch = getc(stdin)) != EOF && ch != '\n') while ((ch = getc(stream)) != EOF && ch != '\n')
*ptr++ = ch; *ptr++ = ch;
if (ch == EOF && ptr==s) if (ch == EOF) {
return (char *)NULL; if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
}
*ptr = '\0'; *ptr = '\0';
return s; 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); va_start(ap, format);
retval = _doprnt(format, ap, stdout); retval = _doprnt(format, ap, stdout);
if (retval >= 0 && io_testflag(stdout,_IOLBF))
if (fflush(stdout)) return EOF;
va_end(ap); va_end(ap);

View file

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

View file

@ -8,5 +8,13 @@
int int
puts(register const char *s) 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> #include <stdio.h>
int link(const char *name1, const char *name2); int link(const char *name1, const char *name2);
int unlink(const char *path);
int int
rename(const char *old, const char *new) { rename(const char *old, const char *new) {

View file

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

View file

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

View file

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

View file

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

View file

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