--- /dev/null
+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
-llib
+loc_incl.h
tmpfile.c
tmpnam.c
rename.c
vprintf.c
vsprintf.c
doprnt.c
+icompute.c
fscanf.c
scanf.c
sscanf.c
clearerr.c
feof.c
ferror.c
+fileno.c
fltpr.c
ecvt.c
gcvt.c
fclose.c
flushbuf.c
fflush.c
+isatty.c
data.c
--- /dev/null
+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:
#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
};
#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;
+ register int i, c;
if (*f == '*') {
*ip = va_arg((*app), int);
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)
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';
- return s;
- }
- if (((flags & FL_ALT) && (c == 'x' || c == 'X' || c == 'o'))
- || c == 'p') {
+ if (!precision)
+ return s;
+ } 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) {
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':
break;
case 'e':
case 'E':
- s = _pscien(ld_val, s, precision, flags);
+ s = _pscien(ld_val, s, precision , flags);
break;
case 'g':
case 'G':
int
_doprnt(register const char *fmt, va_list ap, FILE *stream)
{
- register char *s;
- unsigned int uint;
- register int j;
- int i, c, width, precision, zfill, flags, between_fill;
+ register char *s;
+ unsigned int uint;
+ 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 != '%') {
-#ifdef CPM
+#ifdef CPM
if (c == '\n') {
if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
}
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;
switch (c = *fmt++) {
default:
-#ifdef CPM
+#ifdef CPM
if (c == '\n') {
if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
s1 = "(null)";
s = s1;
while (precision || !(flags & FL_PRECSPEC)) {
- if (*s == '\0')
+ if (*s == '\0')
break;
s++;
precision--;
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':
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++;
uint/= 256 ;
}
fmt = va_arg(ap, char *);
continue;
}
+ zfill = ' ';
+ if (flags & FL_ZEROFILL) zfill = '0';
j = s - s1;
/* between_fill is true under the following conditions:
#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.
*/
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--;
- c = getc(stream);
+ if (--width);
+ c = getc(stream);
}
- if (width && c == '0' && *base == 16) {
+ if (width && c == '0' && base == 16) {
*bufp++ = c;
- width--;
- c = getc(stream);
+ 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--;
- c = getc(stream);
+ 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--;
- c = getc(stream);
+ 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
* 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--;
- c = getc(stream);
+ if (--width)
+ c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
- width--;
- c = getc(stream);
+ if (--width)
+ c = getc(stream);
}
if (width && c == '.') {
*bufp++ = c;
- width--;
- c = getc(stream);
+ if(--width)
+ c = getc(stream);
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
- width--;
- c = getc(stream);
+ 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--;
- c = getc(stream);
+ if (--width)
+ c = getc(stream);
if (width && (c == '+' || c == '-')) {
*bufp++ = c;
- width--;
- c = getc(stream);
+ if (--width)
+ c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
- width--;
- c = getc(stream);
+ 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 */
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))
+ while (isspace(*format))
++format; /* skip whitespace */
ic = getc(stream);
nrchars++;
ic = getc(stream);
nrchars++;
}
+ if (ic != EOF) ungetc(ic,stream);
+ nrchars--;
}
- else {
- ic = getc(stream);
- nrchars++;
- }
- if (!*format)
- break; /* end of format */
+ if (!*format) break; /* end of format */
+
+ ic = getc(stream);
+ nrchars++;
+
if (ic == EOF)
return conv ? done : EOF;
if (*format != '%') {
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;
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;
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--;
- ic = getc(stream);
- nrchars++;
+ if (--width) {
+ ic = getc(stream);
+ nrchars++;
+ }
}
- if (ic != EOF)
- ungetc(ic,stream);
- nrchars--;
+ if (width) {
+ if (ic != EOF) ungetc(ic,stream);
+ nrchars--;
+ }
break;
case 's':
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--;
- ic = getc(stream);
- nrchars++;
- /* terminate the string */
- if (!(flags & FL_NOASSIGN))
- *tmp_string = '\0';
+ if (--width) {
+ ic = getc(stream);
+ nrchars++;
+ }
+ }
+ /* 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;
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;
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 *);
- 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))
*tmp_string++ = (char) ic;
- width--;
- ic = getc(stream);
- nrchars++;
+ if (--width) {
+ ic = getc(stream);
+ nrchars++;
+ }
+ }
+ if (width) {
+ if (ic != EOF) ungetc(ic, stream);
+ nrchars--;
}
- if (ic != EOF)
- ungetc(ic, stream);
- nrchars--;
if (!(flags & FL_NOASSIGN)) { /* terminate string */
*tmp_string = '\0';
++done;
}
break;
+ }
#ifndef NOFLOAT:
case 'e':
case 'E':
} /* end switch */
++format;
}
- if (ic != EOF)
- ungetc(ic, stream);
return conv ? done : EOF;
}
-/*
- * 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);
}
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 */
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)
#include <stdio.h>
-#undef feof
-
int
-feof(FILE *stream)
+(feof)(FILE *stream)
{
- return (stream->_flags & _IOEOF) != 0;
+ return feof(stream);
}
#include <stdio.h>
-#undef ferror
-
int
-ferror(FILE *stream)
+(ferror)(FILE *stream)
{
- return (stream->_flags & _IOERR) != 0;
+ return ferror(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;
}
/* $Header$ */
#include <stdio.h>
-#include <sys/file.h>
int
fgetpos(FILE *stream, fpos_t *pos)
#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;
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;
}
--- /dev/null
+/*
+ * fileno .c - map a stream to a file descriptor
+ */
+/* $Header$ */
+
+#include <stdio.h>
+
+int
+(fileno)(FILE *stream)
+{
+ return stream->_fd;
+}
int read(int d, char *buf, int nbytes);
int
-_fillbuf(register FILE *stream)
+__fillbuf(register FILE *stream)
{
static unsigned char ch[FOPEN_MAX];
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) {
*s++ = *s1++;
if ((n > 0) || (flags & FL_ALT))
*s++ = '.';
- while (--n>0)
+ while (--n >= 0)
if (*s1) *s++ = *s1++;
else *s++ = '0';
*s++ = 'e';
#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;
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;
- stream->_count = BUFSIZ - 1;
+ if (!io_testflag(stream, _IOLBF))
+ stream->_count = BUFSIZ - 1;
}
}
stream->_ptr = stream->_buf;
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;
}
return c;
}
-
-void
-_cleanup(void) /* fflush((FILE *)NULL) ??? */
-{
- register int i;
-
- for( i = 0; i < FOPEN_MAX; i++ )
- if (_iotable[i])
- fclose(_iotable[i]);
-}
#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 *
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;
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
- flags |= _IOWRITE | _IOWRITING;
+ flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT;
break;
}
}
- 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;
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;
}
va_start(ap, format);
retval = _doprnt (format, ap, stream);
- if ( retval >= 0 && io_testflag(stream,_IOLBF) )
- if (fflush(stream)) return EOF;
va_end(ap);
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;
}
#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);
#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);
+#define PMODE 0666
+
+/* 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 *
{
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;
}
}
/* $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)
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 */
&& 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);
/* $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)
{
&& !io_testflag(stream,_IONBF))
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;
#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++;
#include <stdio.h>
-#undef getc
-
int
-getc(FILE *stream)
+(getc)(FILE *stream)
{
- return fgetc(stream);
+ return getc(stream);
}
#include <stdio.h>
-#undef getchar
-
int
-getchar(void)
+(getchar)(void)
{
- return getc(stdin);
+ return getchar();
}
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;
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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;
+}
va_start(ap, format);
retval = _doprnt(format, ap, stdout);
- if (retval >= 0 && io_testflag(stdout,_IOLBF))
- if (fflush(stdout)) return EOF;
va_end(ap);
#include <stdio.h>
-#undef putc
-
int
-putc(int c, FILE *stream)
+(putc)(int c, FILE *stream)
{
- return fputc(c, stream);
+ return putc(c, stream);
}
#include <stdio.h>
-#undef putchar
-
int
-putchar(int c)
+(putchar)(int c)
{
- return putc(c, stdout);
+ return putchar(c);
}
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;
}
#include <stdio.h>
int link(const char *name1, const char *name2);
-int unlink(const char *path);
int
rename(const char *old, const char *new) {
#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;
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;
#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;
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);
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);
}
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);
}