Initial revision

This commit is contained in:
Ben Gras
2005-04-21 14:53:53 +00:00
commit 9865aeaa79
2264 changed files with 411685 additions and 0 deletions
+213
View File
@@ -0,0 +1,213 @@
# Makefile for lib/stdio.
CFLAGS = -O -D_MINIX -D_POSIX_SOURCE
CC1 = $(CC) $(CFLAGS) -c
LIBRARY = ../libc.a
all: $(LIBRARY)
OBJECTS = \
$(LIBRARY)(clearerr.o) \
$(LIBRARY)(data.o) \
$(LIBRARY)(doprnt.o) \
$(LIBRARY)(doscan.o) \
$(LIBRARY)(ecvt.o) \
$(LIBRARY)(fclose.o) \
$(LIBRARY)(feof.o) \
$(LIBRARY)(ferror.o) \
$(LIBRARY)(fflush.o) \
$(LIBRARY)(fgetc.o) \
$(LIBRARY)(fgetpos.o) \
$(LIBRARY)(fgets.o) \
$(LIBRARY)(fileno.o) \
$(LIBRARY)(fillbuf.o) \
$(LIBRARY)(flushbuf.o) \
$(LIBRARY)(fopen.o) \
$(LIBRARY)(fprintf.o) \
$(LIBRARY)(fputc.o) \
$(LIBRARY)(fputs.o) \
$(LIBRARY)(fread.o) \
$(LIBRARY)(freopen.o) \
$(LIBRARY)(fscanf.o) \
$(LIBRARY)(fseek.o) \
$(LIBRARY)(fsetpos.o) \
$(LIBRARY)(ftell.o) \
$(LIBRARY)(fwrite.o) \
$(LIBRARY)(getc.o) \
$(LIBRARY)(getchar.o) \
$(LIBRARY)(gets.o) \
$(LIBRARY)(icompute.o) \
$(LIBRARY)(perror.o) \
$(LIBRARY)(printf.o) \
$(LIBRARY)(putc.o) \
$(LIBRARY)(putchar.o) \
$(LIBRARY)(puts.o) \
$(LIBRARY)(remove.o) \
$(LIBRARY)(rewind.o) \
$(LIBRARY)(scanf.o) \
$(LIBRARY)(setbuf.o) \
$(LIBRARY)(setvbuf.o) \
$(LIBRARY)(sprintf.o) \
$(LIBRARY)(sscanf.o) \
$(LIBRARY)(tmpfile.o) \
$(LIBRARY)(tmpnam.o) \
$(LIBRARY)(ungetc.o) \
$(LIBRARY)(vfprintf.o) \
$(LIBRARY)(vprintf.o) \
$(LIBRARY)(vscanf.o) \
$(LIBRARY)(vsprintf.o) \
$(LIBRARY)(vsscanf.o) \
$(LIBRARY): $(OBJECTS)
aal cr $@ *.o
rm *.o
$(LIBRARY)(clearerr.o): clearerr.c
$(CC1) clearerr.c
$(LIBRARY)(data.o): data.c
$(CC1) data.c
$(LIBRARY)(doprnt.o): doprnt.c
$(CC1) doprnt.c
$(LIBRARY)(doscan.o): doscan.c
$(CC1) doscan.c
$(LIBRARY)(ecvt.o): ecvt.c
$(CC1) ecvt.c
$(LIBRARY)(fclose.o): fclose.c
$(CC1) fclose.c
$(LIBRARY)(feof.o): feof.c
$(CC1) feof.c
$(LIBRARY)(ferror.o): ferror.c
$(CC1) ferror.c
$(LIBRARY)(fflush.o): fflush.c
$(CC1) fflush.c
$(LIBRARY)(fgetc.o): fgetc.c
$(CC1) fgetc.c
$(LIBRARY)(fgetpos.o): fgetpos.c
$(CC1) fgetpos.c
$(LIBRARY)(fgets.o): fgets.c
$(CC1) fgets.c
$(LIBRARY)(fileno.o): fileno.c
$(CC1) fileno.c
$(LIBRARY)(fillbuf.o): fillbuf.c
$(CC1) fillbuf.c
$(LIBRARY)(flushbuf.o): flushbuf.c
$(CC1) flushbuf.c
$(LIBRARY)(fopen.o): fopen.c
$(CC1) fopen.c
$(LIBRARY)(fprintf.o): fprintf.c
$(CC1) fprintf.c
$(LIBRARY)(fputc.o): fputc.c
$(CC1) fputc.c
$(LIBRARY)(fputs.o): fputs.c
$(CC1) fputs.c
$(LIBRARY)(fread.o): fread.c
$(CC1) fread.c
$(LIBRARY)(freopen.o): freopen.c
$(CC1) freopen.c
$(LIBRARY)(fscanf.o): fscanf.c
$(CC1) fscanf.c
$(LIBRARY)(fseek.o): fseek.c
$(CC1) fseek.c
$(LIBRARY)(fsetpos.o): fsetpos.c
$(CC1) fsetpos.c
$(LIBRARY)(ftell.o): ftell.c
$(CC1) ftell.c
$(LIBRARY)(fwrite.o): fwrite.c
$(CC1) fwrite.c
$(LIBRARY)(getc.o): getc.c
$(CC1) getc.c
$(LIBRARY)(getchar.o): getchar.c
$(CC1) getchar.c
$(LIBRARY)(gets.o): gets.c
$(CC1) gets.c
$(LIBRARY)(icompute.o): icompute.c
$(CC1) icompute.c
$(LIBRARY)(perror.o): perror.c
$(CC1) perror.c
$(LIBRARY)(printf.o): printf.c
$(CC1) printf.c
$(LIBRARY)(putc.o): putc.c
$(CC1) putc.c
$(LIBRARY)(putchar.o): putchar.c
$(CC1) putchar.c
$(LIBRARY)(puts.o): puts.c
$(CC1) puts.c
$(LIBRARY)(remove.o): remove.c
$(CC1) remove.c
$(LIBRARY)(rewind.o): rewind.c
$(CC1) rewind.c
$(LIBRARY)(scanf.o): scanf.c
$(CC1) scanf.c
$(LIBRARY)(setbuf.o): setbuf.c
$(CC1) setbuf.c
$(LIBRARY)(setvbuf.o): setvbuf.c
$(CC1) setvbuf.c
$(LIBRARY)(sprintf.o): sprintf.c
$(CC1) sprintf.c
$(LIBRARY)(sscanf.o): sscanf.c
$(CC1) sscanf.c
$(LIBRARY)(tmpfile.o): tmpfile.c
$(CC1) tmpfile.c
$(LIBRARY)(tmpnam.o): tmpnam.c
$(CC1) tmpnam.c
$(LIBRARY)(ungetc.o): ungetc.c
$(CC1) ungetc.c
$(LIBRARY)(vfprintf.o): vfprintf.c
$(CC1) vfprintf.c
$(LIBRARY)(vprintf.o): vprintf.c
$(CC1) vprintf.c
$(LIBRARY)(vscanf.o): vscanf.c
$(CC1) vscanf.c
$(LIBRARY)(vsprintf.o): vsprintf.c
$(CC1) vsprintf.c
$(LIBRARY)(vsscanf.o): vsscanf.c
$(CC1) vsscanf.c
+12
View File
@@ -0,0 +1,12 @@
/*
* clearerr.c - clear error and end-of-file indicators of a stream
*/
/* $Header$ */
#include <stdio.h>
void
(clearerr)(FILE *stream)
{
clearerr(stream);
}
+28
View File
@@ -0,0 +1,28 @@
/*
* data.c - this is the initialization for the standard streams
*/
/* $Header$ */
#include <stdio.h>
struct __iobuf __stdin = {
0, 0, _IOREAD, 0,
(unsigned char *)NULL, (unsigned char *)NULL,
};
struct __iobuf __stdout = {
0, 1, _IOWRITE, 0,
(unsigned char *)NULL, (unsigned char *)NULL,
};
struct __iobuf __stderr = {
0, 2, _IOWRITE | _IOLBF, 0,
(unsigned char *)NULL, (unsigned char *)NULL,
};
FILE *__iotab[FOPEN_MAX] = {
&__stdin,
&__stdout,
&__stderr,
0
};
+310
View File
@@ -0,0 +1,310 @@
/*
* doprnt.c - print formatted output
*/
/* $Header$ */
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
/* 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;
if (*f == '*') {
*ip = va_arg((*app), int);
f++;
} else {
i = 0;
while ((c = *f - '0') >= 0 && c <= 9) {
i = i*10 + c;
f++;
}
*ip = i;
}
return f;
}
#if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */
#elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG)
#else
#error garbage pointer size
#define set_pointer(flags) /* compilation might continue */
#endif
/* print an ordinal number */
static char *
o_print(va_list *ap, int flags, char *s, char c, int precision, int is_signed)
{
long signed_val;
unsigned long unsigned_val;
char *old_s = s;
int base;
switch (flags & (FL_SHORT | FL_LONG)) {
case FL_SHORT:
if (is_signed) {
signed_val = (short) va_arg(*ap, int);
} else {
unsigned_val = (unsigned short) va_arg(*ap, unsigned);
}
break;
case FL_LONG:
if (is_signed) {
signed_val = va_arg(*ap, long);
} else {
unsigned_val = va_arg(*ap, unsigned long);
}
break;
default:
if (is_signed) {
signed_val = va_arg(*ap, int);
} else {
unsigned_val = va_arg(*ap, unsigned int);
}
break;
}
if (is_signed) {
if (signed_val < 0) {
*s++ = '-';
signed_val = -signed_val;
} else if (flags & FL_SIGN) *s++ = '+';
else if (flags & FL_SPACE) *s++ = ' ';
unsigned_val = signed_val;
}
if ((flags & FL_ALT) && (c == 'o')) *s++ = '0';
if (!unsigned_val) {
if (!precision)
return s;
} else if (((flags & FL_ALT) && (c == 'x' || c == 'X'))
|| c == 'p') {
*s++ = '0';
*s++ = (c == 'X' ? 'X' : 'x');
}
switch (c) {
case 'b': base = 2; break;
case 'o': base = 8; break;
case 'd':
case 'i':
case 'u': base = 10; break;
case 'x':
case 'X':
case 'p': base = 16; break;
}
s = _i_compute(unsigned_val, base, s, precision);
if (c == 'X')
while (old_s != s) {
*old_s = toupper(*old_s);
old_s++;
}
return s;
}
int
_doprnt(register const char *fmt, va_list ap, FILE *stream)
{
register char *s;
register int j;
int i, c, width, precision, zfill, flags, between_fill;
int nrchars=0;
const char *oldfmt;
char *s1, buf[1025];
while (c = *fmt++) {
if (c != '%') {
#ifdef CPM
if (c == '\n') {
if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
}
#endif
if (putc(c, stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
continue;
}
flags = 0;
do {
switch(*fmt) {
case '-': flags |= FL_LJUST; break;
case '+': flags |= FL_SIGN; break;
case ' ': flags |= FL_SPACE; break;
case '#': flags |= FL_ALT; break;
case '0': flags |= FL_ZEROFILL; break;
default: flags |= FL_NOMORE; continue;
}
fmt++;
} while(!(flags & FL_NOMORE));
oldfmt = fmt;
fmt = gnum(fmt, &width, &ap);
if (fmt != oldfmt) flags |= FL_WIDTHSPEC;
if (*fmt == '.') {
fmt++; oldfmt = fmt;
fmt = gnum(fmt, &precision, &ap);
if (precision >= 0) flags |= FL_PRECSPEC;
}
if ((flags & FL_WIDTHSPEC) && width < 0) {
width = -width;
flags |= FL_LJUST;
}
if (!(flags & FL_WIDTHSPEC)) width = 0;
if (flags & FL_SIGN) flags &= ~FL_SPACE;
if (flags & FL_LJUST) flags &= ~FL_ZEROFILL;
s = s1 = buf;
switch (*fmt) {
case 'h': flags |= FL_SHORT; fmt++; break;
case 'l': flags |= FL_LONG; fmt++; break;
case 'L': flags |= FL_LONGDOUBLE; fmt++; break;
}
switch (c = *fmt++) {
default:
#ifdef CPM
if (c == '\n') {
if (putc('\r', stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
}
#endif
if (putc(c, stream) == EOF)
return nrchars ? -nrchars : -1;
nrchars++;
continue;
case 'n':
if (flags & FL_SHORT)
*va_arg(ap, short *) = (short) nrchars;
else if (flags & FL_LONG)
*va_arg(ap, long *) = (long) nrchars;
else
*va_arg(ap, int *) = (int) nrchars;
continue;
case 's':
s1 = va_arg(ap, char *);
if (s1 == NULL)
s1 = "(null)";
s = s1;
while (precision || !(flags & FL_PRECSPEC)) {
if (*s == '\0')
break;
s++;
precision--;
}
break;
case 'p':
set_pointer(flags);
/* fallthrough */
case 'b':
case 'o':
case 'u':
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':
*s++ = va_arg(ap, int);
break;
#ifndef NOFLOAT
case 'G':
case 'g':
if ((flags & FL_PRECSPEC) && (precision == 0))
precision = 1;
case 'f':
case 'E':
case 'e':
if (!(flags & FL_PRECSPEC))
precision = 6;
if (precision >= sizeof(buf))
precision = sizeof(buf) - 1;
flags |= FL_SIGNEDCONV;
s = _f_print(&ap, flags, s, c, precision);
break;
#endif /* NOFLOAT */
case 'r':
ap = va_arg(ap, va_list);
fmt = va_arg(ap, char *);
continue;
}
zfill = ' ';
if (flags & FL_ZEROFILL) zfill = '0';
j = s - s1;
/* between_fill is true under the following conditions:
* 1- the fill character is '0'
* and
* 2a- the number is of the form 0x... or 0X...
* or
* 2b- the number contains a sign or space
*/
between_fill = 0;
if ((flags & FL_ZEROFILL)
&& (((c == 'x' || c == 'X') && (flags & FL_ALT))
|| (c == 'p')
|| ((flags & FL_SIGNEDCONV)
&& ( *s1 == '+' || *s1 == '-' || *s1 == ' '))))
between_fill++;
if ((i = width - j) > 0)
if (!(flags & FL_LJUST)) { /* right justify */
nrchars += i;
if (between_fill) {
if (flags & FL_SIGNEDCONV) {
j--; nrchars++;
if (putc(*s1++, stream) == EOF)
return nrchars ? -nrchars : -1;
} else {
j -= 2; nrchars += 2;
if ((putc(*s1++, stream) == EOF)
|| (putc(*s1++, stream) == EOF))
return nrchars ? -nrchars : -1;
}
}
do {
if (putc(zfill, stream) == EOF)
return nrchars ? -nrchars : -1;
} while (--i);
}
nrchars += j;
while (--j >= 0) {
if (putc(*s1++, stream) == EOF)
return nrchars ? -nrchars : -1;
}
if (i > 0) nrchars += i;
while (--i >= 0)
if (putc(zfill, stream) == EOF)
return nrchars ? -nrchars : -1;
}
return nrchars;
}
+449
View File
@@ -0,0 +1,449 @@
/*
* doscan.c - scan formatted input
*/
/* $Header$ */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include "loc_incl.h"
#if _EM_WSIZE == _EM_PSIZE
#define set_pointer(flags) /* nothing */
#elif _EM_LSIZE == _EM_PSIZE
#define set_pointer(flags) (flags |= FL_LONG)
#else
#error garbage pointer size
#define set_pointer(flags) /* compilation might continue */
#endif
#define NUMLEN 512
#define NR_CHARS 256
static char Xtable[NR_CHARS];
static char inp_buf[NUMLEN];
/* Collect a number of characters which constitite an ordinal number.
* When the type is 'i', the base can be 8, 10, or 16, depending on the
* first 1 or 2 characters. This means that the base must be adjusted
* according to the format of the number. At the end of the function, base
* is then set to 0, so strtol() will get the right argument.
*/
static char *
o_collect(register int c, register FILE *stream, char type,
unsigned 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 'd':
case 'u': base = 10; break;
case 'o': base = 8; break;
case 'b': base = 2; break;
}
if (c == '-' || c == '+') {
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (width && c == '0' && base == 16) {
*bufp++ = c;
if (--width)
c = getc(stream);
if (c != 'x' && c != 'X') {
if (type == 'i') base = 8;
}
else if (width) {
*bufp++ = c;
if (--width)
c = getc(stream);
}
}
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'))) {
*bufp++ = c;
if (--width)
c = getc(stream);
}
else break;
}
if (width && c != EOF) ungetc(c, stream);
if (type == 'i') base = 0;
*basep = base;
*bufp = '\0';
return bufp - 1;
}
#ifndef NOFLOAT
/* The function f_collect() reads a string that has the format of a
* floating-point number. The function returns as soon as a format-error
* 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, 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, register unsigned int width)
{
register char *bufp = inp_buf;
int digit_seen = 0;
if (c == '-' || c == '+') {
*bufp++ = c;
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (width && c == '.') {
*bufp++ = c;
if(--width)
c = getc(stream);
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
}
if (!digit_seen) {
if (width && c != EOF) ungetc(c, stream);
return inp_buf - 1;
}
else digit_seen = 0;
if (width && (c == 'e' || c == 'E')) {
*bufp++ = c;
if (--width)
c = getc(stream);
if (width && (c == '+' || c == '-')) {
*bufp++ = c;
if (--width)
c = getc(stream);
}
while (width && isdigit(c)) {
digit_seen++;
*bufp++ = c;
if (--width)
c = getc(stream);
}
if (!digit_seen) {
if (width && c != EOF) ungetc(c,stream);
return inp_buf - 1;
}
}
if (width && c != EOF) ungetc(c, stream);
*bufp = '\0';
return bufp - 1;
}
#endif /* NOFLOAT */
/*
* the routine that does the scanning
*/
int
_doscan(register FILE *stream, const char *format, va_list ap)
{
int done = 0; /* number of items done */
int nrchars = 0; /* number of characters read */
int conv = 0; /* # of conversions */
int base; /* conversion base */
unsigned long val; /* an integer value */
register char *str; /* temporary pointer */
char *tmp_string; /* ditto */
unsigned width = 0; /* width of field */
int flags; /* some flags */
int reverse; /* reverse the checking in [...] */
int kind;
register int ic = EOF; /* the input character */
#ifndef NOFLOAT
long double ld_val;
#endif
if (!*format) return 0;
while (1) {
if (isspace(*format)) {
while (isspace(*format))
format++; /* skip whitespace */
ic = getc(stream);
nrchars++;
while (isspace (ic)) {
ic = getc(stream);
nrchars++;
}
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
if (!*format) break; /* end of format */
if (*format != '%') {
ic = getc(stream);
nrchars++;
if (ic != *format++) break; /* error */
continue;
}
format++;
if (*format == '%') {
ic = getc(stream);
nrchars++;
if (ic == '%') {
format++;
continue;
}
else break;
}
flags = 0;
if (*format == '*') {
format++;
flags |= FL_NOASSIGN;
}
if (isdigit (*format)) {
flags |= FL_WIDTHSPEC;
for (width = 0; isdigit (*format);)
width = width * 10 + *format++ - '0';
}
switch (*format) {
case 'h': flags |= FL_SHORT; format++; break;
case 'l': flags |= FL_LONG; format++; break;
case 'L': flags |= FL_LONGDOUBLE; format++; break;
}
kind = *format;
if ((kind != 'c') && (kind != '[') && (kind != 'n')) {
do {
ic = getc(stream);
nrchars++;
} while (isspace(ic));
if (ic == EOF) break; /* outer while */
} else if (kind != 'n') { /* %c or %[ */
ic = getc(stream);
if (ic == EOF) break; /* outer while */
nrchars++;
}
switch (kind) {
default:
/* not recognized, like %q */
return conv || (ic != EOF) ? done : EOF;
break;
case 'n':
if (!(flags & FL_NOASSIGN)) { /* silly, though */
if (flags & FL_SHORT)
*va_arg(ap, short *) = (short) nrchars;
else if (flags & FL_LONG)
*va_arg(ap, long *) = (long) nrchars;
else
*va_arg(ap, int *) = (int) nrchars;
}
break;
case 'p': /* pointer */
set_pointer(flags);
/* fallthrough */
case 'b': /* binary */
case 'd': /* decimal */
case 'i': /* general integer */
case 'o': /* octal */
case 'u': /* unsigned */
case 'x': /* hexadecimal */
case 'X': /* ditto */
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN;
if (!width) return done;
str = o_collect(ic, stream, kind, width, &base);
if (str < inp_buf
|| (str == inp_buf
&& (*str == '-'
|| *str == '+'))) return done;
/*
* Although the length of the number is str-inp_buf+1
* we don't add the 1 since we counted it already
*/
nrchars += str - inp_buf;
if (!(flags & FL_NOASSIGN)) {
if (kind == 'd' || kind == 'i')
val = strtol(inp_buf, &tmp_string, base);
else
val = strtoul(inp_buf, &tmp_string, base);
if (flags & FL_LONG)
*va_arg(ap, unsigned long *) = (unsigned long) val;
else if (flags & FL_SHORT)
*va_arg(ap, unsigned short *) = (unsigned short) val;
else
*va_arg(ap, unsigned *) = (unsigned) val;
}
break;
case 'c':
if (!(flags & FL_WIDTHSPEC))
width = 1;
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char *);
if (!width) return done;
while (width && ic != EOF) {
if (!(flags & FL_NOASSIGN))
*str++ = (char) ic;
if (--width) {
ic = getc(stream);
nrchars++;
}
}
if (width) {
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
break;
case 's':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char *);
if (!width) return done;
while (width && ic != EOF && !isspace(ic)) {
if (!(flags & FL_NOASSIGN))
*str++ = (char) ic;
if (--width) {
ic = getc(stream);
nrchars++;
}
}
/* terminate the string */
if (!(flags & FL_NOASSIGN))
*str = '\0';
if (width) {
if (ic != EOF) ungetc(ic,stream);
nrchars--;
}
break;
case '[':
if (!(flags & FL_WIDTHSPEC))
width = 0xffff;
if (!width) return done;
if ( *++format == '^' ) {
reverse = 1;
format++;
} else
reverse = 0;
for (str = Xtable; str < &Xtable[NR_CHARS]
; str++)
*str = 0;
if (*format == ']') Xtable[*format++] = 1;
while (*format && *format != ']') {
Xtable[*format++] = 1;
if (*format == '-') {
format++;
if (*format
&& *format != ']'
&& *(format) >= *(format -2)) {
int c;
for( c = *(format -2) + 1
; c <= *format ; c++)
Xtable[c] = 1;
format++;
}
else Xtable['-'] = 1;
}
}
if (!*format) return done;
if (!(Xtable[ic] ^ reverse)) {
/* MAT 8/9/96 no match must return character */
ungetc(ic, stream);
return done;
}
if (!(flags & FL_NOASSIGN))
str = va_arg(ap, char *);
do {
if (!(flags & FL_NOASSIGN))
*str++ = (char) ic;
if (--width) {
ic = getc(stream);
nrchars++;
}
} while (width && ic != EOF && (Xtable[ic] ^ reverse));
if (width) {
if (ic != EOF) ungetc(ic, stream);
nrchars--;
}
if (!(flags & FL_NOASSIGN)) { /* terminate string */
*str = '\0';
}
break;
#ifndef NOFLOAT
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
if (!(flags & FL_WIDTHSPEC) || width > NUMLEN)
width = NUMLEN;
if (!width) return done;
str = f_collect(ic, stream, width);
if (str < inp_buf
|| (str == inp_buf
&& (*str == '-'
|| *str == '+'))) return done;
/*
* Although the length of the number is str-inp_buf+1
* we don't add the 1 since we counted it already
*/
nrchars += str - inp_buf;
if (!(flags & FL_NOASSIGN)) {
ld_val = strtod(inp_buf, &tmp_string);
if (flags & FL_LONGDOUBLE)
*va_arg(ap, long double *) = (long double) ld_val;
else
if (flags & FL_LONG)
*va_arg(ap, double *) = (double) ld_val;
else
*va_arg(ap, float *) = (float) ld_val;
}
break;
#endif
} /* end switch */
conv++;
if (!(flags & FL_NOASSIGN) && kind != 'n') done++;
format++;
}
return conv || (ic != EOF) ? done : EOF;
}
+31
View File
@@ -0,0 +1,31 @@
/* $Header$ */
#ifndef NOFLOAT
#include "../ansi/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);
}
char *
_fcvt(long double value, int ndigit, int *decpt, int *sign)
{
return cvt(value, ndigit, decpt, sign, 0);
}
#endif /* NOFLOAT */
+31
View File
@@ -0,0 +1,31 @@
/*
* fclose.c - flush a stream and close the file
*/
/* $Header$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
int _close(int d);
int
fclose(FILE *fp)
{
register int i, retval = 0;
for (i=0; i<FOPEN_MAX; i++)
if (fp == __iotab[i]) {
__iotab[i] = 0;
break;
}
if (i >= FOPEN_MAX)
return EOF;
if (fflush(fp)) retval = EOF;
if (_close(fileno(fp))) retval = EOF;
if ( io_testflag(fp,_IOMYBUF) && fp->_buf )
free((void *)fp->_buf);
if (fp != stdin && fp != stdout && fp != stderr)
free((void *)fp);
return retval;
}
+12
View File
@@ -0,0 +1,12 @@
/*
* feof.c - test if eof on a stream occurred
*/
/* $Header$ */
#include <stdio.h>
int
(feof)(FILE *stream)
{
return feof(stream);
}
+12
View File
@@ -0,0 +1,12 @@
/*
* ferror .c - test if an error on a stream occurred
*/
/* $Header$ */
#include <stdio.h>
int
(ferror)(FILE *stream)
{
return ferror(stream);
}
+79
View File
@@ -0,0 +1,79 @@
/*
* fflush.c - flush stream(s)
*/
/* $Header$ */
#include <sys/types.h>
#include <stdio.h>
#include "loc_incl.h"
ssize_t _write(int d, const char *buf, size_t nbytes);
off_t _lseek(int fildes, off_t offset, int whence);
int
fflush(FILE *stream)
{
int count, c1, i, retval = 0;
if (!stream) {
for(i= 0; i < FOPEN_MAX; i++)
if (__iotab[i] && fflush(__iotab[i]))
retval = EOF;
return retval;
}
if (!stream->_buf
|| (!io_testflag(stream, _IOREADING)
&& !io_testflag(stream, _IOWRITING)))
return 0;
if (io_testflag(stream, _IOREADING)) {
/* (void) fseek(stream, 0L, SEEK_CUR); */
int adjust = 0;
if (stream->_buf && !io_testflag(stream,_IONBF))
adjust = -stream->_count;
stream->_count = 0;
if (_lseek(fileno(stream), (off_t) adjust, SEEK_CUR) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
if (io_testflag(stream, _IOWRITE))
stream->_flags &= ~(_IOREADING | _IOWRITING);
stream->_ptr = stream->_buf;
return 0;
} else if (io_testflag(stream, _IONBF)) return 0;
if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
stream->_flags &= ~_IOWRITING;
count = stream->_ptr - stream->_buf;
stream->_ptr = stream->_buf;
if ( count <= 0 )
return 0;
if (io_testflag(stream, _IOAPPEND)) {
if (_lseek(fileno(stream), 0L, SEEK_END) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
c1 = _write(stream->_fd, (char *)stream->_buf, count);
stream->_count = 0;
if ( count == c1 )
return 0;
stream->_flags |= _IOERR;
return EOF;
}
void
__cleanup(void)
{
register int i;
for(i= 0; i < FOPEN_MAX; i++)
if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
(void) fflush(__iotab[i]);
}
+12
View File
@@ -0,0 +1,12 @@
/*
* fgetc - get an unsigned character and return it as an int
*/
/* $Header$ */
#include <stdio.h>
int
fgetc(FILE *stream)
{
return getc(stream);
}
+14
View File
@@ -0,0 +1,14 @@
/*
* fgetpos.c - get the position in the file
*/
/* $Header$ */
#include <stdio.h>
int
fgetpos(FILE *stream, fpos_t *pos)
{
*pos = ftell(stream);
if (*pos == -1) return -1;
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
/*
* fgets.c - get a string from a file
*/
/* $Header$ */
#include <stdio.h>
char *
fgets(char *s, register int n, register FILE *stream)
{
register int ch;
register char *ptr;
ptr = s;
while (--n > 0 && (ch = getc(stream)) != EOF) {
*ptr++ = ch;
if ( ch == '\n')
break;
}
if (ch == EOF) {
if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
}
*ptr = '\0';
return s;
}
+12
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;
}
+69
View File
@@ -0,0 +1,69 @@
/*
* fillbuf.c - fill a buffer
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
ssize_t _read(ssize_t d, char *buf, size_t nbytes);
int
__fillbuf(register FILE *stream)
{
static unsigned char ch[FOPEN_MAX];
register int i;
stream->_count = 0;
if (fileno(stream) < 0) return EOF;
if (io_testflag(stream, (_IOEOF | _IOERR ))) return EOF;
if (!io_testflag(stream, _IOREAD))
{ stream->_flags |= _IOERR; return EOF; }
if (io_testflag(stream, _IOWRITING))
{ stream->_flags |= _IOERR; return EOF; }
if (!io_testflag(stream, _IOREADING))
stream->_flags |= _IOREADING;
if (!io_testflag(stream, _IONBF) && !stream->_buf) {
stream->_buf = (unsigned char *) malloc(BUFSIZ);
if (!stream->_buf) {
stream->_flags |= _IONBF;
}
else {
stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ;
}
}
/* flush line-buffered output when filling an input buffer */
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);
if (stream->_count <= 0){
if (stream->_count == 0) {
stream->_flags |= _IOEOF;
}
else
stream->_flags |= _IOERR;
return EOF;
}
stream->_count--;
return *stream->_ptr++;
}
+127
View File
@@ -0,0 +1,127 @@
/*
* flushbuf.c - flush a buffer
*/
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#include <sys/types.h>
off_t _lseek(int fildes, off_t offset, int whence);
ssize_t _write(int d, const char *buf, int nbytes);
int _isatty(int d);
extern void (*_clean)(void);
static int
do_write(int d, char *buf, int nbytes)
{
int c;
/* POSIX actually allows write() to return a positive value less
than nbytes, so loop ...
*/
while ((c = _write(d, buf, nbytes)) > 0 && c < nbytes) {
nbytes -= c;
buf += c;
}
return c > 0;
}
int
__flushbuf(int c, FILE * stream)
{
_clean = __cleanup;
if (fileno(stream) < 0) return (unsigned char) c;
if (!io_testflag(stream, _IOWRITE)) return EOF;
if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
stream->_flags &= ~_IOREADING;
stream->_flags |= _IOWRITING;
if (!io_testflag(stream, _IONBF)) {
if (!stream->_buf) {
if (stream == stdout && _isatty(fileno(stdout))) {
if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) {
stream->_flags |= _IONBF;
} else {
stream->_flags |= _IOLBF|_IOMYBUF;
stream->_bufsiz = BUFSIZ;
stream->_count = -1;
}
} else {
if (!(stream->_buf =
(unsigned char *) malloc(BUFSIZ))) {
stream->_flags |= _IONBF;
} else {
stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ;
if (!io_testflag(stream, _IOLBF))
stream->_count = BUFSIZ - 1;
else stream->_count = -1;
}
}
stream->_ptr = stream->_buf;
}
}
if (io_testflag(stream, _IONBF)) {
char c1 = c;
stream->_count = 0;
if (io_testflag(stream, _IOAPPEND)) {
if (_lseek(fileno(stream), 0L, SEEK_END) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
if (_write(fileno(stream), &c1, 1) != 1) {
stream->_flags |= _IOERR;
return EOF;
}
return (unsigned char) c;
} else if (io_testflag(stream, _IOLBF)) {
*stream->_ptr++ = c;
/* stream->_count has been updated in putc macro. */
if (c == '\n' || stream->_count == -stream->_bufsiz) {
int count = -stream->_count;
stream->_ptr = stream->_buf;
stream->_count = 0;
if (io_testflag(stream, _IOAPPEND)) {
if (_lseek(fileno(stream), 0L, SEEK_END) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
if (! do_write(fileno(stream), (char *)stream->_buf,
count)) {
stream->_flags |= _IOERR;
return EOF;
}
}
} 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, SEEK_END) == -1) {
stream->_flags |= _IOERR;
return EOF;
}
}
if (! do_write(fileno(stream), (char *)stream->_buf, count)) {
*(stream->_buf) = c;
stream->_flags |= _IOERR;
return EOF;
}
}
*(stream->_buf) = c;
}
return (unsigned char) c;
}
+120
View File
@@ -0,0 +1,120 @@
/*
* fopen.c - open a stream
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#define PMODE 0666
/* 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, Mode_t mode);
int _close(int d);
FILE *
fopen(const char *name, const char *mode)
{
register int i;
int rwmode = 0, rwflags = 0;
FILE *stream;
int fd, flags = 0;
for (i = 0; __iotab[i] != 0 ; i++)
if ( i >= FOPEN_MAX-1 )
return (FILE *)NULL;
switch(*mode++) {
case 'r':
flags |= _IOREAD | _IOREADING;
rwmode = O_RDONLY;
break;
case 'w':
flags |= _IOWRITE | _IOWRITING;
rwmode = O_WRONLY;
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
flags |= _IOWRITE | _IOWRITING | _IOAPPEND;
rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT;
break;
default:
return (FILE *)NULL;
}
while (*mode) {
switch(*mode++) {
case 'b':
continue;
case '+':
rwmode = O_RDWR;
flags |= _IOREAD | _IOWRITE;
continue;
/* The sequence may be followed by additional characters */
default:
break;
}
break;
}
/* 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)
&& (rwflags & O_CREAT))) {
if (((fd = _creat(name, PMODE)) > 0) && flags | _IOREAD) {
(void) _close(fd);
fd = _open(name, rwmode);
}
}
if (fd < 0) return (FILE *)NULL;
if (( stream = (FILE *) malloc(sizeof(FILE))) == NULL ) {
_close(fd);
return (FILE *)NULL;
}
if ((flags & (_IOREAD | _IOWRITE)) == (_IOREAD | _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0;
stream->_fd = fd;
stream->_flags = flags;
stream->_buf = NULL;
__iotab[i] = stream;
return stream;
}
+23
View File
@@ -0,0 +1,23 @@
/*
* fprintf - write output on a stream
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
fprintf(FILE *stream, const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = _doprnt (format, ap, stream);
va_end(ap);
return retval;
}
+12
View File
@@ -0,0 +1,12 @@
/*
* fputc.c - print an unsigned character
*/
/* $Header$ */
#include <stdio.h>
int
fputc(int c, FILE *stream)
{
return putc(c, stream);
}
+18
View File
@@ -0,0 +1,18 @@
/*
* fputs - print a string
*/
/* $Header$ */
#include <stdio.h>
int
fputs(register const char *s, register FILE *stream)
{
register int i = 0;
while (*s)
if (putc(*s++, stream) == EOF) return EOF;
else i++;
return i;
}
+29
View File
@@ -0,0 +1,29 @@
/*
* fread.c - read a number of members into an array
*/
/* $Header$ */
#include <stdio.h>
size_t
fread(void *ptr, size_t size, size_t nmemb, register FILE *stream)
{
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)
*cp++ = c;
else
return ndone;
} while (--s);
ndone++;
}
return ndone;
}
+99
View File
@@ -0,0 +1,99 @@
/*
* freopen.c - open a file and associate a stream with it
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#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, Mode_t mode);
int _close(int d);
FILE *
freopen(const char *name, const char *mode, FILE *stream)
{
register int i;
int rwmode = 0, rwflags = 0;
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 | _IOAPPEND;
rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT;
break;
default:
return (FILE *)NULL;
}
while (*mode) {
switch(*mode++) {
case 'b':
continue;
case '+':
rwmode = O_RDWR;
flags |= _IOREAD | _IOWRITE;
continue;
/* The sequence may be followed by aditional characters */
default:
break;
}
break;
}
if ((rwflags & O_TRUNC)
|| (((fd = _open(name, rwmode)) < 0)
&& (rwflags & O_CREAT))) {
if (((fd = _creat(name, PMODE)) < 0) && flags | _IOREAD) {
(void) _close(fd);
fd = _open(name, rwmode);
}
}
if (fd < 0) {
for( i = 0; i < FOPEN_MAX; i++) {
if (stream == __iotab[i]) {
__iotab[i] = 0;
break;
}
}
if (stream != stdin && stream != stdout && stream != stderr)
free((void *)stream);
return (FILE *)NULL;
}
stream->_count = 0;
stream->_fd = fd;
stream->_flags = flags;
return stream;
}
+23
View File
@@ -0,0 +1,23 @@
/*
* fscanf.c - read formatted input from stream
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
fscanf(FILE *stream, const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = _doscan(stream, format, ap);
va_end(ap);
return retval;
}
+44
View File
@@ -0,0 +1,44 @@
/*
* fseek.c - perform an fseek
*/
/* $Header$ */
#include <stdio.h>
#if (SEEK_CUR != 1) || (SEEK_END != 2) || (SEEK_SET != 0)
#error SEEK_* values are wrong
#endif
#include "loc_incl.h"
#include <sys/types.h>
off_t _lseek(int fildes, off_t offset, int whence);
int
fseek(FILE *stream, long int offset, int whence)
{
int adjust = 0;
long pos;
stream->_flags &= ~(_IOEOF | _IOERR);
/* Clear both the end of file and error flags */
if (io_testflag(stream, _IOREADING)) {
if (whence == SEEK_CUR
&& stream->_buf
&& !io_testflag(stream,_IONBF))
adjust = stream->_count;
stream->_count = 0;
} else if (io_testflag(stream,_IOWRITING)) {
fflush(stream);
} else /* neither reading nor writing. The buffer must be empty */
/* EMPTY */ ;
pos = _lseek(fileno(stream), offset - adjust, whence);
if (io_testflag(stream, _IOREAD) && io_testflag(stream, _IOWRITE))
stream->_flags &= ~(_IOREADING | _IOWRITING);
stream->_ptr = stream->_buf;
return ((pos == -1) ? -1 : 0);
}
+12
View File
@@ -0,0 +1,12 @@
/*
* fsetpos.c - set the position in the file
*/
/* $Header$ */
#include <stdio.h>
int
fsetpos(FILE *stream, fpos_t *pos)
{
return fseek(stream, *pos, SEEK_SET);
}
+38
View File
@@ -0,0 +1,38 @@
/*
* ftell.c - obtain the value of the file-position indicator of a stream
*/
/* $Header$ */
#include <stdio.h>
#if (SEEK_CUR != 1) || (SEEK_SET != 0) || (SEEK_END != 2)
#error SEEK_* values are wrong
#endif
#include "loc_incl.h"
#include <sys/types.h>
off_t _lseek(int fildes, off_t offset, int whence);
long ftell(FILE *stream)
{
long result;
int adjust = 0;
if (io_testflag(stream,_IOREADING))
adjust = -stream->_count;
else if (io_testflag(stream,_IOWRITING)
&& stream->_buf
&& !io_testflag(stream,_IONBF))
adjust = stream->_ptr - stream->_buf;
else adjust = 0;
result = _lseek(fileno(stream), (off_t)0, SEEK_CUR);
if ( result == -1 )
return result;
result += (long) adjust;
return result;
}
+29
View File
@@ -0,0 +1,29 @@
/*
* fwrite.c - write a number of array elements on a file
*/
/* $Header$ */
#include <stdio.h>
size_t
fwrite(const void *ptr, size_t size, size_t nmemb,
register FILE *stream)
{
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)*cp, stream)
== EOF)
return ndone;
cp++;
}
while (--s);
ndone++;
}
return ndone;
}
+12
View File
@@ -0,0 +1,12 @@
/*
* getc.c - read an unsigned character
*/
/* $Header$ */
#include <stdio.h>
int
(getc)(FILE *stream)
{
return getc(stream);
}
+12
View File
@@ -0,0 +1,12 @@
/*
* getchar.c - read a character from the standard input stream
*/
/* $Header$ */
#include <stdio.h>
int
(getchar)(void)
{
return getchar();
}
+27
View File
@@ -0,0 +1,27 @@
/*
* gets.c - read a line from a stream
*/
/* $Header$ */
#include <stdio.h>
char *
gets(char *s)
{
register FILE *stream = stdin;
register int ch;
register char *ptr;
ptr = s;
while ((ch = getc(stream)) != EOF && ch != '\n')
*ptr++ = ch;
if (ch == EOF) {
if (feof(stream)) {
if (ptr == s) return NULL;
} else return NULL;
}
*ptr = '\0';
return s;
}
+21
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 > 1)
s = _i_compute(val, base, s, nrdigits - 1);
*s++ = (c>9 ? c-10+'a' : c+'0');
return s;
}
+40
View File
@@ -0,0 +1,40 @@
/*
* loc_incl.h - local include file for stdio library
*/
/* $Header$ */
#include <stdio.h>
#define io_testflag(p,x) ((p)->_flags & (x))
#include <stdarg.h>
#ifdef _ANSI
int _doprnt(const char *format, va_list ap, FILE *stream);
int _doscan(FILE * stream, const char *format, va_list ap);
char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
char *_f_print(va_list *ap, int flags, char *s, char c, int precision);
void __cleanup(void);
FILE *popen(const char *command, const char *type);
FILE *fdopen(int fd, const char *mode);
#ifndef NOFLOAT
char *_ecvt(long double value, int ndigit, int *decpt, int *sign);
char *_fcvt(long double value, int ndigit, int *decpt, int *sign);
#endif /* NOFLOAT */
#endif
#define FL_LJUST 0x0001 /* left-justify field */
#define FL_SIGN 0x0002 /* sign in signed conversions */
#define FL_SPACE 0x0004 /* space in signed conversions */
#define FL_ALT 0x0008 /* alternate form */
#define FL_ZEROFILL 0x0010 /* fill with zero's */
#define FL_SHORT 0x0020 /* optional h */
#define FL_LONG 0x0040 /* optional l */
#define FL_LONGDOUBLE 0x0080 /* optional L */
#define FL_WIDTHSPEC 0x0100 /* field width is specified */
#define FL_PRECSPEC 0x0200 /* precision is specified */
#define FL_SIGNEDCONV 0x0400 /* may contain a sign */
#define FL_NOASSIGN 0x0800 /* do not assign (in scanf) */
#define FL_NOMORE 0x1000 /* all flags collected */
+33
View File
@@ -0,0 +1,33 @@
/*
* perror.c - print an error message on the standard error output
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "loc_incl.h"
ssize_t _write(int d, const char *buf, size_t nbytes);
void
perror(const char *s)
{
char *p;
int fd;
p = strerror(errno);
fd = fileno(stderr);
fflush(stdout);
fflush(stderr);
if (s && *s) {
_write(fd, s, strlen(s));
_write(fd, ": ", 2);
}
_write(fd, p, strlen(p));
_write(fd, "\n", 1);
}
+23
View File
@@ -0,0 +1,23 @@
/*
* printf - write on the standard output stream
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
printf(const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = _doprnt(format, ap, stdout);
va_end(ap);
return retval;
}
+12
View File
@@ -0,0 +1,12 @@
/*
* putc.c - print (or buffer) one character
*/
/* $Header$ */
#include <stdio.h>
int
(putc)(int c, FILE *stream)
{
return putc(c, stream);
}
+12
View File
@@ -0,0 +1,12 @@
/*
* putchar.c - print (or buffer) a character on the standard output stream
*/
/* $Header$ */
#include <stdio.h>
int
(putchar)(int c)
{
return putchar(c);
}
+20
View File
@@ -0,0 +1,20 @@
/*
* puts.c - print a string onto the standard output stream
*/
/* $Header$ */
#include <stdio.h>
int
puts(register const char *s)
{
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;
}
+13
View File
@@ -0,0 +1,13 @@
/*
* remove.c - remove a file
*/
/* $Header$ */
#include <stdio.h>
int _unlink(const char *path);
int
remove(const char *filename) {
return _unlink(filename);
}
+14
View File
@@ -0,0 +1,14 @@
/*
* rewind.c - set the file position indicator of a stream to the start
*/
/* $Header$ */
#include <stdio.h>
#include "loc_incl.h"
void
rewind(FILE *stream)
{
(void) fseek(stream, 0L, SEEK_SET);
clearerr(stream);
}
+25
View File
@@ -0,0 +1,25 @@
/*
* scanf.c - read formatted input from the standard input stream
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
scanf(const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = _doscan(stdin, format, ap);
va_end(ap);
return retval;
}
+13
View File
@@ -0,0 +1,13 @@
/*
* setbuf.c - control buffering of a stream
*/
/* $Header$ */
#include <stdio.h>
#include "loc_incl.h"
void
setbuf(register FILE *stream, char *buf)
{
(void) setvbuf(stream, buf, (buf ? _IOFBF : _IONBF), (size_t) BUFSIZ);
}
+48
View File
@@ -0,0 +1,48 @@
/*
* setbuf.c - control buffering of a stream
*/
/* $Id$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
extern void (*_clean)(void);
int
setvbuf(register FILE *stream, char *buf, int mode, size_t size)
{
int retval = 0;
_clean = __cleanup;
if (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)
return EOF;
if (stream->_buf && io_testflag(stream,_IOMYBUF) )
free((void *)stream->_buf);
stream->_flags &= ~(_IOMYBUF | _IONBF | _IOLBF);
if (buf && size <= 0) retval = EOF;
if (!buf && (mode != _IONBF)) {
if (size <= 0 || (buf = (char *) malloc(size)) == NULL) {
retval = EOF;
} else {
stream->_flags |= _IOMYBUF;
}
}
stream->_buf = (unsigned char *) buf;
stream->_count = 0;
stream->_flags |= mode;
stream->_ptr = stream->_buf;
if (!buf) {
stream->_bufsiz = 1;
} else {
stream->_bufsiz = size;
}
return retval;
}
+39
View File
@@ -0,0 +1,39 @@
/*
* sprintf - print formatted output on an array
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include "loc_incl.h"
int
sprintf(char *s, const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = vsnprintf(s, INT_MAX, format, ap);
va_end(ap);
return retval;
}
int
snprintf(char *s, size_t n, const char *format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = vsnprintf(s, n, format, ap);
va_end(ap);
return retval;
}
+30
View File
@@ -0,0 +1,30 @@
/*
* sscanf - read formatted output from a string
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
int sscanf(const char *s, const char *format, ...)
{
va_list ap;
int retval;
FILE tmp_stream;
va_start(ap, format);
tmp_stream._fd = -1;
tmp_stream._flags = _IOREAD + _IONBF + _IOREADING;
tmp_stream._buf = (unsigned char *) s;
tmp_stream._ptr = (unsigned char *) s;
tmp_stream._count = strlen(s);
retval = _doscan(&tmp_stream, format, ap);
va_end(ap);
return retval;
}
+31
View File
@@ -0,0 +1,31 @@
/*
* tmpfile.c - create and open a temporary file
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdio.h>
#include <string.h>
#include "loc_incl.h"
pid_t _getpid(void);
FILE *
tmpfile(void) {
static char name_buffer[L_tmpnam] = "/tmp/tmp." ;
static char *name = NULL;
FILE *file;
if (!name) {
name = name_buffer + strlen(name_buffer);
name = _i_compute(_getpid(), 10, name, 5);
*name = '\0';
}
file = fopen(name_buffer,"wb+");
if (!file) return (FILE *)NULL;
(void) remove(name_buffer);
return file;
}
+31
View File
@@ -0,0 +1,31 @@
/*
* tmpnam.c - create a unique filename
*/
/* $Header$ */
#if defined(_POSIX_SOURCE)
#include <sys/types.h>
#endif
#include <stdio.h>
#include <string.h>
#include "loc_incl.h"
pid_t _getpid(void);
char *
tmpnam(char *s) {
static char name_buffer[L_tmpnam] = "/tmp/tmp.";
static unsigned long count = 0;
static char *name = NULL;
if (!name) {
name = name_buffer + strlen(name_buffer);
name = _i_compute((unsigned long)_getpid(), 10, name, 5);
*name++ = '.';
*name = '\0';
}
if (++count > TMP_MAX) count = 1; /* wrap-around */
*_i_compute(count, 10, name, 3) = '\0';
if (s) return strcpy(s, name_buffer);
else return name_buffer;
}
+26
View File
@@ -0,0 +1,26 @@
/*
* ungetc.c - push a character back onto an input stream
*/
/* $Header$ */
#include <stdio.h>
#include "loc_incl.h"
int
ungetc(int ch, FILE *stream)
{
unsigned char *p;
if (ch == EOF || !io_testflag(stream,_IOREADING))
return EOF;
if (stream->_ptr == stream->_buf) {
if (stream->_count != 0) return EOF;
stream->_ptr++;
}
stream->_count++;
p = --(stream->_ptr); /* ??? Bloody vax assembler !!! */
/* ungetc() in sscanf() shouldn't write in rom */
if (*p != (unsigned char) ch)
*p = (unsigned char) ch;
return ch;
}
+14
View File
@@ -0,0 +1,14 @@
/*
* vfprintf - formatted output without ellipsis
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
vfprintf(FILE *stream, const char *format, va_list arg)
{
return _doprnt (format, arg, stream);
}
+14
View File
@@ -0,0 +1,14 @@
/*
* vprintf - formatted output without ellipsis to the standard output stream
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
vprintf(const char *format, va_list arg)
{
return _doprnt(format, arg, stdout);
}
+13
View File
@@ -0,0 +1,13 @@
/*
* vscanf.c - read formatted input from the standard input stream
*/
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
int
vscanf(const char *format, va_list ap)
{
return _doscan(stdin, format, ap);
}
+34
View File
@@ -0,0 +1,34 @@
/*
* vsprintf - print formatted output without ellipsis on an array
*/
/* $Header$ */
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include "loc_incl.h"
int
vsnprintf(char *s, size_t n, const char *format, va_list arg)
{
int retval;
FILE tmp_stream;
tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) s;
tmp_stream._ptr = (unsigned char *) s;
tmp_stream._count = n-1;
retval = _doprnt(format, arg, &tmp_stream);
tmp_stream._count = 1;
putc('\0',&tmp_stream);
return retval;
}
int
vsprintf(char *s, const char *format, va_list arg)
{
return vsnprintf(s, INT_MAX, format, arg);
}
+21
View File
@@ -0,0 +1,21 @@
/*
* vsscanf - read formatted output from a string
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
int vsscanf(const char *s, const char *format, va_list ap)
{
FILE tmp_stream;
tmp_stream._fd = -1;
tmp_stream._flags = _IOREAD + _IONBF + _IOREADING;
tmp_stream._buf = (unsigned char *) s;
tmp_stream._ptr = (unsigned char *) s;
tmp_stream._count = strlen(s);
return _doscan(&tmp_stream, format, ap);
}