add directory gnu

This commit is contained in:
gohigh
2024-02-19 00:24:47 -05:00
parent 32616db5a4
commit a40f4cadb0
5086 changed files with 1860970 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
# Copyright (C) 1991, 1992 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB. If
# not, write to the Free Software Foundation, Inc., 675 Mass Ave,
# Cambridge, MA 02139, USA.
#
# Specific makefile for stdio.
#
subdir := stdio
headers := stdio.h stdio_lim.h printf.h
routines := \
ctermid cuserid \
feof ferror clearerr fileno \
newstream fopen freopen fdopen fopncook fmemopen \
setbuf setvbuf setbuffer setlinebuf \
fseek ftell rewind fgetpos fsetpos \
vfprintf vprintf printf_fp reg-printf printf-prs \
vsnprintf vsprintf vasprintf \
fprintf printf snprintf sprintf asprintf \
dprintf vdprintf \
__vfscanf vfscanf vscanf __vsscanf vsscanf \
fscanf scanf sscanf \
fread fwrite \
ungetc \
fgetc getc getchar getw \
fputc putc putchar putw \
fgets gets fputs puts \
getdelim getline __getdelim __getline \
perror psignal \
tmpfile tmpnam tempnam \
fclose fflush \
remove rename \
memstream \
internals sysd-stdio pipestream stdio_init
routines := $(strip $(routines))
aux := errlist siglist defs syms-stdio glue
tests := bug1 bug2 bug3 bug4 bug5 tst-printf tstscanf test_rdwr test-popen \
tstgetline
include ../Rules

View File

@@ -0,0 +1,170 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
(and null-terminate it). *LINEPTR is a pointer returned from malloc (or
NULL), pointing to *N characters of space. It is realloc'd as
necessary. Returns the number of characters read (not including the
null terminator), or -1 on error or EOF. */
ssize_t
DEFUN(__getdelim, (lineptr, n, terminator, stream),
char **lineptr AND size_t *n AND int terminator AND FILE *stream)
{
char *line, *p;
size_t size, copy;
if (!__validfp (stream) || lineptr == NULL || n == NULL)
{
errno = EINVAL;
return -1;
}
if (ferror (stream))
return -1;
/* Make sure we have a line buffer to start with. */
if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
{
#ifndef MAX_CANON
#define MAX_CANON 256
#endif
line = realloc (*lineptr, MAX_CANON);
if (line == NULL)
return -1;
*lineptr = line;
*n = MAX_CANON;
}
line = *lineptr;
size = *n;
copy = size;
p = line;
if (stream->__buffer == NULL && stream->__userbuf)
{
/* Unbuffered stream. Not much optimization to do. */
while (1)
{
size_t len;
while (--copy > 0)
{
register int c = getc (stream);
if (c == EOF)
goto lose;
else if ((*p++ = c) == terminator)
goto win;
}
/* Need to enlarge the line buffer. */
len = p - line;
size *= 2;
line = realloc (line, size);
if (line == NULL)
goto lose;
*lineptr = line;
*n = size;
p = line + len;
copy = size - len;
}
}
else
{
/* Leave space for the terminating null. */
--copy;
if (!stream->__seen || stream->__buffer == NULL || stream->__pushed_back)
{
/* Do one with getc to allocate a buffer. */
int c = getc (stream);
if (c == EOF)
goto lose;
*p++ = c;
if (c == terminator)
goto win;
--copy;
}
while (1)
{
size_t i;
char *found;
i = stream->__get_limit - stream->__bufp;
if (i == 0)
{
/* Refill the buffer. */
int c = __fillbf (stream);
if (c == EOF)
goto lose;
*p++ = c;
if (c == terminator)
goto win;
i = stream->__get_limit - stream->__bufp;
}
if (i > copy)
i = copy;
found = (char *) __memccpy ((PTR) p, stream->__bufp, terminator, i);
if (found != NULL)
{
stream->__bufp += found - p;
p = found;
goto win;
}
stream->__bufp += i;
p += i;
copy -= i;
if (copy == 0)
{
/* Need to enlarge the line buffer. */
size_t len = p - line;
size *= 2;
line = realloc (line, size);
if (line == NULL)
goto lose;
*lineptr = line;
*n = size;
p = line + len;
copy = size - len;
/* Leave space for the terminating null. */
--copy;
}
}
}
lose:
if (p == *lineptr)
return -1;
/* Return a partial line since we got an error in the middle. */
win:
*p = '\0';
return p - *lineptr;
}

View File

@@ -0,0 +1,31 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#undef __getline
/* Like getdelim, but always looks for a newline. */
ssize_t
DEFUN(__getline, (lineptr, n, stream),
char **lineptr AND size_t *n AND FILE *stream)
{
return __getdelim (lineptr, n, '\n', stream);
}

View File

@@ -0,0 +1,482 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <localeinfo.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
#define HAVE_LONGLONG
#define LONGLONG long long
#else
#define LONGLONG long
#endif
#define inchar() ((c = getc(s)) == EOF ? EOF : (++read_in, c))
#define conv_error() return((c == EOF || ungetc(c, s)), done)
#define input_error() return(-1)
#define memory_error() return((errno = ENOMEM), EOF)
/* Read formatted input from S according to the format string
FORMAT, using the argument list in ARG.
Return the number of assignments made, or -1 for an input error. */
int
DEFUN(__vfscanf, (s, format, arg),
FILE *s AND CONST char *format AND PTR argptr)
{
va_list arg = (va_list) argptr;
register CONST char *f = format;
register char fc; /* Current character of the format. */
register size_t done = 0; /* Assignments done. */
register size_t read_in = 0; /* Chars read in. */
register int c; /* Last char read. */
register int do_assign; /* Whether to do an assignment. */
register int width; /* Maximum field width. */
/* Type modifiers. */
char is_short, is_long, is_long_double;
#ifdef HAVE_LONGLONG
/* We use the `L' modifier for `long long int'. */
#define is_longlong is_long_double
#else
#define is_longlong 0
#endif
/* Status for reading F-P nums. */
char got_dot, got_e;
/* If a [...] is a [^...]. */
char not_in;
/* Base for integral numbers. */
int base;
/* Integral holding variables. */
long int num;
unsigned long int unum;
/* Floating-point holding variable. */
LONG_DOUBLE fp_num;
/* Character-buffer pointer. */
register char *str;
/* Workspace. */
char work[200];
char *w; /* Pointer into WORK. */
wchar_t decimal; /* Decimal point character. */
if (!__validfp(s) || !s->__mode.__read || format == NULL)
{
errno = EINVAL;
input_error();
}
/* Figure out the decimal point character. */
if (mbtowc(&decimal, _numeric_info->decimal_point,
strlen(_numeric_info->decimal_point)) <= 0)
decimal = (wchar_t) *_numeric_info->decimal_point;
c = inchar();
/* Run through the format string. */
while (*f != '\0')
{
if (!isascii(*f))
{
/* Non-ASCII, may be a multibyte. */
int len = mblen(f, strlen(f));
if (len > 0)
{
while (len-- > 0)
if (c == EOF)
input_error();
else if (c == *f++)
(void) inchar();
else
conv_error();
continue;
}
}
fc = *f++;
if (fc != '%')
{
/* Characters other than format specs must just match. */
if (c == EOF)
input_error();
if (isspace(fc))
{
/* Whitespace characters match any amount of whitespace. */
while (isspace (c))
inchar ();
continue;
}
else if (c == fc)
(void) inchar();
else
conv_error();
continue;
}
/* Check for the assignment-suppressant. */
if (*f == '*')
{
do_assign = 0;
++f;
}
else
do_assign = 1;
/* Find the maximum field width. */
width = 0;
while (isdigit(*f))
{
width *= 10;
width += *f++ - '0';
}
if (width == 0)
width = -1;
/* Check for type modifiers. */
is_short = is_long = is_long_double = 0;
while (*f == 'h' || *f == 'l' || *f == 'L')
switch (*f++)
{
case 'h':
/* int's are short int's. */
is_short = 1;
break;
case 'l':
if (is_long)
/* A double `l' is equivalent to an `L'. */
is_longlong = 1;
else
/* int's are long int's. */
is_long = 1;
break;
case 'L':
/* double's are long double's, and int's are long long int's. */
is_long_double = 1;
break;
}
/* End of the format string? */
if (*f == '\0')
conv_error();
/* Find the conversion specifier. */
w = work;
fc = *f++;
if (fc != '[' && fc != 'c' && fc != 'n')
/* Eat whitespace. */
while (isspace(c))
(void) inchar();
switch (fc)
{
case '%': /* Must match a literal '%'. */
if (c != fc)
conv_error();
break;
case 'n': /* Answer number of assignments done. */
if (do_assign)
*va_arg(arg, int *) = read_in;
break;
case 'c': /* Match characters. */
if (do_assign)
{
str = va_arg(arg, char *);
if (str == NULL)
conv_error();
}
if (c == EOF)
input_error();
if (width == -1)
width = 1;
if (do_assign)
while (inchar() != EOF && width-- > 0)
*str++ = c;
else
while (inchar() != EOF && width-- > 0)
;
if (do_assign)
++done;
if (c == EOF)
input_error();
break;
case 's': /* Read a string. */
if (do_assign)
{
str = va_arg(arg, char *);
if (str == NULL)
conv_error();
}
if (c == EOF)
input_error();
do
{
if (isspace(c))
break;
if (do_assign)
*str++ = c;
if (width > 0 && --width == 0)
break;
} while (inchar() != EOF);
if (do_assign)
{
*str = '\0';
++done;
}
break;
case 'x': /* Hexadecimal integer. */
case 'X': /* Ditto. */
base = 16;
goto number;
case 'o': /* Octal integer. */
base = 8;
goto number;
case 'u': /* Decimal integer. */
case 'd': /* Ditto. */
base = 10;
goto number;
case 'i': /* Generic number. */
base = 0;
number:;
if (c == EOF)
input_error();
/* Check for a sign. */
if (c == '-' || c == '+')
{
*w++ = c;
if (width > 0)
--width;
(void) inchar();
}
/* Look for a leading indication of base. */
if (c == '0')
{
if (width > 0)
--width;
*w++ = '0';
(void) inchar();
if (tolower(c) == 'x')
{
if (base == 0)
base = 16;
if (base == 16)
{
if (width > 0)
--width;
(void) inchar();
}
}
else if (base == 0)
base = 8;
}
if (base == 0)
base = 10;
/* Read the number into WORK. */
do
{
if (base == 16 ? !isxdigit(c) :
(!isdigit(c) || c - '0' >= base))
break;
*w++ = c;
if (width > 0)
--width;
} while (inchar() != EOF && width != 0);
if (w == work ||
(w - work == 1 && (work[0] == '+' || work[0] == '-')))
/* There was on number. */
conv_error();
/* Convert the number. */
*w = '\0';
num = strtol(work, &w, base);
if (w == work)
conv_error();
if (do_assign)
{
if (is_longlong)
*va_arg(arg, LONGLONG int *) = num;
else if (is_long)
*va_arg(arg, long int *) = num;
else if (is_short)
*va_arg(arg, short int *) = (short int) num;
else
*va_arg(arg, int *) = (int) num;
++done;
}
break;
case 'e': /* Floating-point numbers. */
case 'E':
case 'f':
case 'g':
case 'G':
if (c == EOF)
input_error();
/* Check for a sign. */
if (c == '-' || c == '+')
{
*w++ = c;
if (inchar() == EOF)
input_error();
if (width > 0)
--width;
}
got_dot = got_e = 0;
do
{
if (isdigit(c))
*w++ = c;
else if (got_e && w[-1] == 'e' && (c == '-' || c == '+'))
*w++ = c;
else if (!got_e && tolower(c) == 'e')
{
*w++ = 'e';
got_e = got_dot = 1;
}
else if (c == decimal && !got_dot)
{
*w++ = c;
got_dot = 1;
}
else
break;
if (width > 0)
--width;
} while (inchar() != EOF && width != 0);
if (w == work)
conv_error();
if (w[-1] == '-' || w[-1] == '+' || w[-1] == 'e')
conv_error();
/* Convert the number. */
*w = '\0';
fp_num = strtod(work, &w);
if (w == work)
conv_error();
if (do_assign)
{
if (is_long_double)
*va_arg(arg, LONG_DOUBLE *) = fp_num;
else if (is_long)
*va_arg(arg, double *) = (double) fp_num;
else
*va_arg(arg, float *) = (float) fp_num;
++done;
}
break;
case '[': /* Character class. */
if (do_assign)
{
str = va_arg(arg, char *);
if (str == NULL)
conv_error();
}
if (c == EOF)
input_error();
if (*f == '^')
{
++f;
not_in = 1;
}
else
not_in = 0;
while ((fc = *f++) != '\0' && fc != ']')
{
if (fc == '-' && *f != '\0' && *f != ']' &&
w > work && w[-1] <= *f)
/* Add all characters from the one before the '-'
up to (but not including) the next format char. */
for (fc = w[-1] + 1; fc < *f; ++fc)
*w++ = fc;
else
/* Add the character to the list. */
*w++ = fc;
}
if (fc == '\0')
conv_error();
*w = '\0';
unum = read_in;
do
{
if ((strchr(work, c) == NULL) != not_in)
break;
if (do_assign)
*str++ = c;
if (width > 0)
--width;
} while (inchar() != EOF && width != 0);
if (read_in == unum)
conv_error();
if (do_assign)
{
*str = '\0';
++done;
}
break;
case 'p': /* Generic pointer. */
base = 16;
/* A PTR must be the same size as a `long int'. */
is_long = 1;
goto number;
}
}
conv_error();
}

View File

@@ -0,0 +1,56 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#undef vsscanf
/* Read formatted input from S according to the format
string FORMAT, using the argument list in ARG. */
int
DEFUN(__vsscanf, (s, format, arg),
CONST char *s AND CONST char *format AND PTR arg)
{
FILE f;
if (s == NULL)
{
errno = EINVAL;
return -1;
}
memset((PTR) &f, 0, sizeof(f));
f.__magic = _IOMAGIC;
f.__mode.__read = 1;
f.__bufp = f.__buffer = (char *) s;
f.__bufsize = strlen(s);
f.__get_limit = f.__buffer + f.__bufsize;
f.__put_limit = f.__buffer;
/* After the buffer is empty (strlen(S) characters have been read),
any more read attempts will get EOF. */
f.__room_funcs.__input = NULL;
f.__seen = 1;
return __vfscanf(&f, format, arg);
}

View File

@@ -0,0 +1,39 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output from FORMAT to a string which is
allocated with malloc and stored in *STRING_PTR. */
/* VARARGS2 */
int
DEFUN(asprintf, (string_ptr, format),
char **string_ptr AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vasprintf(string_ptr, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,28 @@
#include <ansidecl.h>
#include <stdio.h>
#include <string.h>
int
DEFUN_VOID(main)
{
char *bp;
size_t size;
FILE *stream;
int lose = 0;
stream = open_memstream (&bp, &size);
fprintf (stream, "hello");
fflush (stream);
printf ("buf = %s, size = %d\n", bp, size);
lose |= size != 5;
lose |= strncmp (bp, "hello", size);
fprintf (stream, ", world");
fclose (stream);
printf ("buf = %s, size = %d\n", bp, size);
lose |= size != 12;
lose |= strncmp (bp, "hello, world", 12);
puts (lose ? "Test FAILED!" : "Test succeeded.");
return lose;
}

View File

@@ -0,0 +1 @@
95

View File

@@ -0,0 +1,12 @@
#include <ansidecl.h>
#include <stdio.h>
int
DEFUN_VOID(main)
{
int i;
puts ("This should print \"wow = I\" for I from 0 to 39 inclusive.");
for (i = 0; i < 40; i++)
printf ("%s = %d\n", "wow", i);
return 0;
}

View File

@@ -0,0 +1,52 @@
#include <ansidecl.h>
#include <stdio.h>
#include <string.h>
int
main ()
{
FILE *f;
int i;
f = fopen("bugtest", "w+");
for (i=0; i<9000; i++)
putc ('x', f);
fseek (f, 8180L, 0);
fwrite ("Where does this text go?", 1, 24, f);
fflush (f);
rewind (f);
for (i=0; i<9000; i++)
{
int j;
if ((j = getc(f)) != 'x')
{
if (i != 8180)
{
printf ("Test FAILED!");
return 1;
}
else
{
char buf[25];
buf[0] = j;
fread (buf + 1, 1, 23, f);
buf[24] = '\0';
if (strcmp (buf, "Where does this text go?") != 0)
{
printf ("%s\nTest FAILED!\n", buf);
return 1;
}
i += 23;
}
}
}
fclose(f);
puts ("Test succeeded.");
return 0;
}

View File

@@ -0,0 +1,50 @@
#ifdef LIBC
#include <ansidecl.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int stdio_block_read = 1, stdio_block_write = 1;
int
DEFUN(main, (argc, argv),
int argc AND char **argv)
{
FILE *f;
int i;
char buffer[31];
while ((i = getopt (argc, argv, "rw")) != EOF)
switch (i)
{
case 'r':
stdio_block_read = 0;
break;
case 'w':
stdio_block_write = 0;
break;
}
f = fopen("bugtest", "w+");
for (i=0; i<9000; i++) {
putc('x', f);
}
fseek(f, 8180L, 0);
fwrite("Where does this text come from?", 1, 31, f);
fseek(f, 8180L, 0);
fread(buffer, 1, 31, f);
fwrite(buffer, 1, 31, stdout);
fclose(f);
if (!memcmp (buffer, "Where does this text come from?", 31))
{
puts ("\nTest succeeded.");
return 0;
}
else
{
puts ("\nTest FAILED!");
return 1;
}
}

View File

@@ -0,0 +1,60 @@
/* If stdio is working correctly, after this is run infile and outfile
will have the same contents. If the bug (found in GNU C library 0.3)
exhibits itself, outfile will be missing the 2nd through 1023rd
characters. */
#include <ansidecl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static char buf[8192];
int
DEFUN_VOID(main)
{
FILE *in;
FILE *out;
static char inname[] = "infile";
static char outname[] = "outfile";
int i;
/* Create a test file. */
in = fopen (inname, "w+");
if (in == NULL)
{
perror (inname);
return 1;
}
for (i = 0; i < 1000; ++i)
fprintf (in, "%d\n", i);
out = fopen (outname, "w");
if (out == NULL)
{
perror (outname);
return 1;
}
if (fseek (in, 0L, SEEK_SET) != 0)
abort ();
putc (getc (in), out);
i = fread (buf, 1, sizeof (buf), in);
if (i == 0)
{
perror ("fread");
return 1;
}
if (fwrite (buf, 1, i, out) != i)
{
perror ("fwrite");
return 1;
}
fclose (in);
fclose (out);
puts ("There should be no further output from this test.");
fflush (stdout);
execlp ("cmp", "cmp", inname, outname, (char *) NULL);
perror ("execlp: cmp");
exit (1);
}

View File

@@ -0,0 +1,31 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#undef clearerr
/* Clear the EOF and error indicators for STREAM. */
void
DEFUN(clearerr, (stream), FILE *stream)
{
__clearerr(stream);
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output to D, according to the format string FORMAT. */
/* VARARGS2 */
int
DEFUN(dprintf, (d, format), int d AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vdprintf(d, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,69 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Close a stream. */
int
DEFUN(fclose, (stream), register FILE *stream)
{
int status;
if (stream == NULL)
{
/* Close all streams. */
register FILE *f;
for (f = __stdio_head; f != NULL; f = f->__next)
if (__validfp(f))
(void) fclose(f);
return 0;
}
if (!__validfp(stream))
{
errno = EINVAL;
return EOF;
}
if (stream->__mode.__write &&
/* Flush the buffer. */
__flshfp (stream, EOF) == EOF)
return EOF;
/* Free the buffer's storage. */
if (stream->__buffer != NULL && !stream->__userbuf)
free(stream->__buffer);
/* Close the system file descriptor. */
if (stream->__io_funcs.__close != NULL)
status = (*stream->__io_funcs.__close)(stream->__cookie);
else if (!stream->__seen && stream->__cookie != NULL)
status = __stdio_close(stream->__cookie);
else
status = 0;
/* Nuke the stream, making it available for re-use. */
__invalidate(stream);
return status < 0 ? EOF : 0;
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#undef feof
/* Return non-zero if STREAM has its EOF indicator set. */
int
DEFUN(feof, (stream), FILE *stream)
{
if (!__validfp(stream))
{
errno = EINVAL;
return(-1);
}
return(stream->__eof);
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#undef ferror
/* Return non-zero if STREAM has its error indicator set. */
int
DEFUN(ferror, (stream), FILE *stream)
{
if (!__validfp(stream))
{
errno = EINVAL;
return(-1);
}
return(stream->__error);
}

View File

@@ -0,0 +1,45 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
/* Flush STREAM's buffer.
If STREAM is NULL, flush the buffers of all streams that are writing. */
int
DEFUN(fflush, (stream), register FILE *stream)
{
if (stream == NULL)
{
int lossage = 0;
for (stream = __stdio_head; stream != NULL; stream = stream->__next)
if (__validfp(stream) && stream->__mode.__write)
lossage |= fflush(stream) == EOF;
return lossage ? EOF : 0;
}
if (!__validfp(stream) || !stream->__mode.__write)
{
errno = EINVAL;
return EOF;
}
return __flshfp(stream, EOF);
}

View File

@@ -0,0 +1,35 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
/* Read a character from STREAM. */
int
DEFUN(fgetc, (stream), FILE *stream)
{
if (!__validfp(stream) || !stream->__mode.__read)
{
errno = EINVAL;
return EOF;
}
return __getc(stream);
}

View File

@@ -0,0 +1,40 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#undef fgetpos
/* Put the current position of STREAM in *POS. */
int
DEFUN(fgetpos, (stream, pos), FILE *stream AND fpos_t *pos)
{
if (!__validfp(stream) || pos == NULL)
{
errno = EINVAL;
return(-1);
}
*pos = ftell(stream);
if (*pos < 0L)
return(-1);
return(0);
}

View File

@@ -0,0 +1,121 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
/* Reads characters from STREAM into S, until either a newline character
is read, N - 1 characters have been read, or EOF is seen. Returns
the newline, unlike gets. Finishes by appending a null character and
returning S. If EOF is seen before any characters have been written
to S, the function returns NULL without appending the null character.
If there is a file error, always return NULL. */
char *
DEFUN(fgets, (s, n, stream), char *s AND size_t n AND register FILE *stream)
{
register char *p = s;
if (!__validfp(stream) || s == NULL || n == 0)
{
errno = EINVAL;
return NULL;
}
if (ferror (stream))
return NULL;
if (stream->__buffer == NULL && stream->__userbuf)
{
/* Unbuffered stream. Not much optimization to do. */
register int c = 0;
while (--n > 0 && (c = getc (stream)) != EOF)
if ((*p++ = c) == '\n')
break;
if (c == EOF && (p == s || ferror (stream)))
return NULL;
*p = '\0';
return s;
}
/* Leave space for the null. */
--n;
if (n > 0 &&
(!stream->__seen || stream->__buffer == NULL || stream->__pushed_back))
{
/* Do one with getc to allocate a buffer. */
int c = getc (stream);
if (c == EOF)
return NULL;
*p++ = c;
if (c == '\n')
{
*p = '\0';
return s;
}
else
--n;
}
while (n > 0)
{
size_t i;
char *found;
i = stream->__get_limit - stream->__bufp;
if (i == 0)
{
/* Refill the buffer. */
int c = __fillbf (stream);
if (c == EOF)
break;
*p++ = c;
--n;
if (c == '\n')
{
*p = '\0';
return s;
}
i = stream->__get_limit - stream->__bufp;
}
if (i > n)
i = n;
found = (char *) __memccpy ((PTR) p, stream->__bufp, '\n', i);
if (found != NULL)
{
stream->__bufp += found - p;
p = found;
break;
}
stream->__bufp += i;
n -= i;
p += i;
}
if (p == s)
return NULL;
*p = '\0';
return ferror (stream) ? NULL : s;
}

View File

@@ -0,0 +1,43 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#undef fileno
/* Return the system file descriptor associated with STREAM. */
int
DEFUN(fileno, (stream), CONST FILE *stream)
{
if (!__validfp(stream))
{
errno = EINVAL;
return -1;
}
if (stream->__cookie != &stream->__fileno)
{
errno = ENOSYS;
return -1;
}
return stream->__fileno;
}

View File

@@ -0,0 +1,103 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Defined in fopen.c. */
extern int EXFUN(__getmode, (CONST char *mode, __io_mode *mptr));
/* Open a new stream that will read and/or write from the buffer in
S, which is of LEN bytes. If the mode indicates appending, the
buffer pointer is set to point to the first '\0' in the buffer.
If S is NULL, the buffer is allocated by malloc and will be freed
when the stream is closed. The only purpose of this is to write
things and then read what's been written. If LEN is zero, writes will
always return errors and reads will always return end-of-file.
The stream is set up such that seeks and tells will always fail and
once the buffer is full of written characters or empty of characters
to read, attempted writes always return an output error and attempted
reads always return end-of-file. */
FILE *
DEFUN(fmemopen, (s, len, mode),
PTR s AND size_t len AND CONST char *mode)
{
__io_mode m;
register FILE *stream;
if (!__getmode (mode, &m))
return NULL;
stream = __newstream ();
if (stream == NULL)
return NULL;
stream->__mode = m;
/* Input gets EOF. */
stream->__room_funcs.__input = NULL;
/* Output gets error. */
stream->__room_funcs.__output = NULL;
/* Do nothing for close. */
stream->__io_funcs.__close = NULL;
/* Can't seek outside the buffer. */
stream->__io_funcs.__seek = NULL;
stream->__seen = 1;
stream->__userbuf = s != NULL && len > 0;
if (s == NULL)
{
s = malloc (len);
if (s == NULL)
{
int save = errno;
(void) fclose (stream);
errno = save;
return NULL;
}
}
stream->__buffer = (char *) s;
stream->__bufsize = len;
stream->__bufp = stream->__buffer;
stream->__get_limit = (stream->__buffer +
(stream->__mode.__read ? stream->__bufsize : 0));
stream->__put_limit = (stream->__buffer +
(stream->__mode.__write ? stream->__bufsize : 0));
stream->__fileno = -1;
stream->__cookie = &stream->__fileno;
if (stream->__mode.__append)
while (stream->__bufp < stream->__get_limit &&
*stream->__bufp != '\0')
++stream->__bufp;
else if (stream->__mode.__truncate)
memset ((PTR) stream->__buffer, 0, len);
return stream;
}

View File

@@ -0,0 +1,123 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define badmode() return ((errno = EINVAL), 0)
/* Dissect the given mode string into an __io_mode. */
int
DEFUN(__getmode, (mode, mptr), CONST char *mode AND __io_mode *mptr)
{
register unsigned char i;
if (mode == NULL)
badmode ();
memset ((PTR) mptr, 0, sizeof (*mptr));
switch (*mode)
{
case 'a':
mptr->__write = mptr->__create = mptr->__append = 1;
break;
case 'w':
mptr->__write = mptr->__create = mptr->__truncate = 1;
break;
case 'r':
mptr->__read = 1;
break;
default:
badmode ();
}
for (i = 1; i < 3; ++i)
{
++mode;
if (*mode == '\0')
break;
switch (*mode)
{
case '+':
mptr->__read = mptr->__write = 1;
break;
case 'b':
mptr->__binary = 1;
break;
}
}
if (!mptr->__read && !mptr->__write)
badmode ();
mptr->__exclusive = *mode == 'x';
return 1;
}
/* Open a new stream on the given file. */
FILE *
DEFUN(fopen, (filename, mode), CONST char *filename AND CONST char *mode)
{
FILE *stream;
int fd;
PTR cookie;
__io_mode m;
if (filename == NULL)
{
errno = EINVAL;
return NULL;
}
if (!__getmode (mode, &m))
return NULL;
stream = __newstream ();
if (stream == NULL)
return NULL;
cookie = __stdio_open (filename, m, &fd);
if (cookie == NULL)
{
if (fd < 0)
{
int save = errno;
(void) fclose (stream);
errno = save;
return NULL;
}
stream->__fileno = fd;
stream->__cookie = &stream->__fileno;
}
else
stream->__cookie = cookie;
stream->__mode = m;
if (!m.__append)
stream->__offset = 0;
return stream;
}

View File

@@ -0,0 +1,48 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
/* Defined in fopen.c. */
extern int EXFUN(__getmode, (CONST char *mode, __io_mode *mptr));
/* Open a new stream on the given magic cookie descriptor. */
FILE *
DEFUN(fopencookie, (cookie, mode, functions),
PTR cookie AND CONST char *mode AND __io_functions functions)
{
__io_mode m;
FILE *f;
if (!__getmode (mode, &m))
return NULL;
f = __newstream ();
if (f == NULL)
return NULL;
f->__cookie = cookie;
f->__mode = m;
f->__io_funcs = functions;
f->__room_funcs = __default_room_functions;
f->__seen = 1;
return f;
}

View File

@@ -0,0 +1,38 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output to STREAM from the format string FORMAT. */
/* VARARGS2 */
int
DEFUN(fprintf, (stream, format),
FILE *stream AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vfprintf(stream, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,35 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
/* Write the character C to STREAM. */
int
DEFUN(fputc, (c, stream), int c AND FILE *stream)
{
if (!__validfp(stream) || !stream->__mode.__write)
{
errno = EINVAL;
return EOF;
}
return __putc(c, stream);
}

View File

@@ -0,0 +1,35 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
/* Write the string S to STREAM. */
int
DEFUN(fputs, (s, stream), CONST char *s AND FILE *stream)
{
register CONST size_t len = strlen (s);
if (len == 1)
return putc (*s, stream) == EOF ? EOF : 0;
if (fwrite ((PTR) s, 1, len, stream) != len)
return EOF;
return 0;
}

View File

@@ -0,0 +1,128 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define default_func __default_room_functions.__input
/* Read NMEMB chunks of SIZE bytes each from STREAM into P. */
size_t
DEFUN(fread, (p, size, nmemb, stream),
PTR p AND size_t size AND size_t nmemb AND register FILE *stream)
{
register char *ptr = (char *) p;
register size_t to_read = size * nmemb;
size_t bytes = to_read;
if (!__validfp(stream) || !stream->__mode.__read)
{
errno = EINVAL;
return 0;
}
if (feof(stream) || ferror(stream))
return 0;
if (p == NULL || to_read == 0)
return 0;
if (!stream->__seen || stream->__buffer == NULL || stream->__pushed_back)
{
/* This stream has never been seen before, or it has a character
pushed back. Call __fillbf to deal with those cases. Life will
be simpler after this call. */
int c = __fillbf(stream);
if (c == EOF)
return 0;
*ptr++ = c;
if (--to_read == 0)
return 1;
}
read_from_buffer:;
if (stream->__bufp < stream->__get_limit)
{
/* First off, empty out the buffer. */
register size_t copy = stream->__get_limit - stream->__bufp;
if (copy > to_read)
copy = to_read;
to_read -= copy;
if (copy > 20)
memcpy((PTR) ptr, (PTR) stream->__bufp, copy);
else
{
register size_t i;
for (i = 0; i < copy; ++i)
ptr[i] = stream->__bufp[i];
}
stream->__bufp += copy;
if (to_read == 0)
return nmemb;
ptr += copy;
}
/* Reading directly into the user's buffer doesn't help when
using a user-specified input buffer filling/expanding function,
so we don't do it in that case. */
if (to_read >= stream->__bufsize &&
stream->__room_funcs.__input == default_func &&
stream->__offset == stream->__target)
{
/* Read directly into the user's buffer. */
if (stream->__io_funcs.__read != NULL)
while (to_read > 0)
{
register int count;
count = (*stream->__io_funcs.__read)(stream->__cookie,
ptr, to_read);
if (count > 0)
{
to_read -= count;
stream->__offset += count;
stream->__target += count;
ptr += count;
}
else if (count == 0)
{
stream->__eof = 1;
break;
}
else
{
stream->__error = 1;
break;
}
}
else
stream->__eof = 1;
}
else
{
int c = __fillbf(stream);
if (c == EOF)
return (bytes - to_read) / size;
*ptr++ = (char) c;
--to_read;
if (to_read > 0)
goto read_from_buffer;
}
return (bytes - to_read) / size;
}

View File

@@ -0,0 +1,51 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
/* Replace STREAM, opening it on FILENAME. */
FILE *
DEFUN(freopen, (filename, mode, stream),
CONST char *filename AND CONST char *mode AND register FILE *stream)
{
FILE *head;
if (!__validfp(stream))
{
errno = EINVAL;
return NULL;
}
/* Return value explicitly ignored. */
(void) fclose(stream);
/* Make sure STREAM will be the first one checked. */
head = __stdio_head;
__stdio_head = stream;
/* This will return either STREAM or NULL. */
stream = fopen(filename, mode);
/* Restore the saved value. */
__stdio_head = head;
return stream;
}

View File

@@ -0,0 +1,38 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Read formatted input from STREAM according to the format string FORMAT. */
/* VARARGS2 */
int
DEFUN(fscanf, (stream, format),
FILE *stream AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = __vfscanf(stream, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,161 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
/* Move the file position of STREAM to OFFSET
bytes from the beginning of the file if WHENCE
is SEEK_SET, the end of the file is it is SEEK_END,
or the current position if it is SEEK_CUR. */
int
DEFUN(fseek, (stream, offset, whence),
register FILE *stream AND long int offset AND int whence)
{
long int o;
if (!__validfp(stream))
{
errno = EINVAL;
return EOF;
}
stream->__eof = 0;
if (stream->__put_limit > stream->__buffer)
{
/* Write out the pending data. */
if (__flshfp(stream, EOF) == EOF)
return EOF;
}
/* Make sure we know the current offset info. */
if (__stdio_check_offset(stream) == EOF)
return EOF;
/* Check the WHENCE argument for validity, and process OFFSET
into an absolute position in O. By the end of this switch,
either we have returned, or O contains an absolute position. */
o = offset;
switch (whence)
{
default:
errno = EINVAL;
return EOF;
case SEEK_END:
/* We don't know where the end of the file is,
so seek to the position in the file the user asked
for, and then look where that is. */
if (stream->__io_funcs.__seek == NULL)
{
errno = ESPIPE;
return EOF;
}
else
{
fpos_t pos = (fpos_t) o;
if ((*stream->__io_funcs.__seek)(stream->__cookie, &pos,
SEEK_END) < 0)
{
if (errno == ESPIPE)
stream->__io_funcs.__seek = NULL;
return EOF;
}
stream->__offset = pos;
/* Make O be absolute, rather than
relative to the end of the file. */
o += pos;
}
/* Fall through to try an absolute seek. */
case SEEK_SET:
/* Make O be relative to the buffer. */
o -= stream->__target;
/* Make O be relative to the current position in the buffer. */
o -= stream->__bufp - stream->__buffer;
/* Fall through to see if we can do it by
moving the pointer around in the buffer. */
case SEEK_CUR:
/* If the offset is small enough, we can just
move the pointer around in the buffer. */
#if 0 /* Why did I think this would ever work??? */
if (stream->__put_limit > stream->__buffer)
{
/* We are writing. */
if (stream->__bufp + o >= stream->__buffer &&
stream->__put_limit > stream->__bufp + o &&
stream->__get_limit > stream->__bufp + o)
{
/* We have read all the data we will change soon.
We can just move the pointer around. */
stream->__bufp += o;
return 0;
}
else
{
/* Flush the buffer. */
if (__flshfp(stream, EOF) == EOF)
return EOF;
}
} else
#endif
if (o < 0 ?
(-o <= stream->__bufp - stream->__buffer) :
o < stream->__get_limit - stream->__bufp)
{
stream->__bufp += o;
return 0;
}
/* Turn it into an absolute seek. */
o += stream->__bufp - stream->__buffer;
o += stream->__target;
break;
}
/* O is now an absolute position, the new target. */
stream->__target = o;
/* Set bufp and both end pointers to the beginning of the buffer.
The next i/o will force a call to the input/output room function. */
stream->__bufp
= stream->__get_limit = stream->__put_limit = stream->__buffer;
/* If there is no seek function, seeks always fail. */
if (stream->__io_funcs.__seek == NULL)
{
/* This is preemptive, since we don't actually do the seeking.
But it makes more sense for fseek to to fail with ESPIPE
than for the next reading or writing operation to fail
that way. */
errno = ESPIPE;
return EOF;
}
/* Don't actually seek. The next reading or writing operation
will force a call to the input or output room function,
which will move to the target file position before reading or writing. */
return 0;
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#undef fsetpos
/* Set the file position of STREAM to *POS. */
int
DEFUN(fsetpos, (stream, pos), FILE *stream AND CONST fpos_t *pos)
{
if (pos == NULL)
{
errno = EINVAL;
return EOF;
}
return fseek(stream, *pos, SEEK_SET);
}

View File

@@ -0,0 +1,38 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
/* Return the offset in bytes from the beginning
of the file of the file position of STREAM. */
long int
DEFUN(ftell, (stream), FILE *stream)
{
if (!__validfp (stream))
{
errno = EINVAL;
return -1L;
}
if (__stdio_check_offset (stream) == EOF)
return -1L;
return stream->__target + (stream->__bufp - stream->__buffer);
}

View File

@@ -0,0 +1,181 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
/* Write NMEMB chunks of SIZE bytes each from PTR onto STREAM. */
size_t
DEFUN(fwrite, (ptr, size, nmemb, stream),
CONST PTR ptr AND size_t size AND
size_t nmemb AND register FILE *stream)
{
register CONST char *p = (CONST char *) ptr;
register size_t to_write = size * nmemb;
register size_t written = 0;
int newlinep;
size_t buffer_space;
char default_func;
if (!__validfp(stream) || !stream->__mode.__write)
{
errno = EINVAL;
return 0;
}
if (ferror(stream))
return 0;
if (p == NULL || to_write == 0)
return 0;
if (!stream->__seen || stream->__put_limit == stream->__buffer)
{
/* This stream has never been seen before.
Calling __flshfp will give it a buffer
and I/O functions if it needs them. */
if (__flshfp(stream, *p++) == EOF)
return 0;
if (--to_write == 0)
return 1;
else
++written;
}
default_func
= stream->__room_funcs.__output == __default_room_functions.__output;
if (__stdio_check_offset (stream) == EOF)
{
stream->__error = 1;
goto done;
}
if (stream->__buffer == NULL && default_func &&
stream->__offset == stream->__target)
write_through:
/* This is an unbuffered stream using the standard output
buffer-flushing function, so we just do a straight write. */
{
int count = (stream->__io_funcs.__write == NULL ? to_write :
(*stream->__io_funcs.__write)(stream->__cookie, p,
to_write));
if (count > 0)
{
written += count;
stream->__offset += count;
stream->__target = stream->__offset;
to_write -= count;
p += count;
}
else
stream->__error = 1;
goto done;
}
/* We ignore the end pointer here since we want to find out how much space
is really in the buffer, even for a line-buffered stream. */
buffer_space = stream->__bufsize - (stream->__bufp - stream->__buffer);
newlinep = (stream->__linebuf &&
memchr((CONST PTR) p, '\n', to_write) != NULL);
if (newlinep && stream->__bufp == stream->__buffer &&
stream->__offset == stream->__target)
/* The buffer's empty, and we want to write our data
out soon anyway, so just write it straight out. */
goto write_through;
if (stream->__bufsize == 0 && !default_func)
{
/* No buffer, and a special function.
We can't do much better than putc. */
while (to_write-- > 0)
{
if (__flshfp(stream, *p++) == EOF)
break;
else
++written;
}
}
else if (!default_func || buffer_space >= to_write)
fill_buffer:
/* There is enough room in the buffer for everything we
want to write or the user has specified his own output
buffer-flushing/expanding function. */
while (to_write > 0)
{
register size_t n = to_write;
if (n > buffer_space)
n = buffer_space;
buffer_space -= n;
written += n;
to_write -= n;
if (n < 20)
while (n-- > 0)
*stream->__bufp++ = *p++;
else
{
memcpy((PTR) stream->__bufp, (PTR) p, n);
stream->__bufp += n;
p += n;
}
if (buffer_space == 0 || (to_write == 0 && newlinep))
{
/* We've filled the buffer, so flush it. */
if (fflush(stream) == EOF)
break;
}
}
else
{
/* It won't all fit in the buffer. */
if (stream->__bufp != stream->__buffer)
{
/* There are characters in the buffer. Flush them. */
if (__flshfp(stream, EOF) == EOF)
goto done;
}
/* The buffer has been flushed.
Now either fill it or write directly. */
buffer_space = stream->__put_limit - stream->__bufp;
if (stream->__offset == stream->__target &&
(buffer_space < to_write || newlinep))
/* What we have to write is bigger than the buffer,
or it contains a newline and we're line-buffered,
so write it out. */
goto write_through;
else
/* It will fit in the buffer. */
goto fill_buffer;
}
done:;
return (size_t) written / size;
}

View File

@@ -0,0 +1,5 @@
#include <ansidecl.h>
#include <stdio.h>
#undef getc
#define fgetc getc
#include <fgetc.c>

View File

@@ -0,0 +1,30 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#undef getchar
/* Read a character from stdin. */
int
DEFUN_VOID(getchar)
{
return __getc(stdin);
}

View File

@@ -0,0 +1,30 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <gnu-stabs.h>
#undef getdelim
function_alias (getdelim, __getdelim, ssize_t,
(lineptr, n, terminator, stream),
DEFUN(getdelim, (lineptr, n, terminator, stream),
char **lineptr AND size_t *n AND
int terminator AND FILE *stream))

View File

@@ -0,0 +1,28 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <gnu-stabs.h>
#undef getline
function_alias (getline, __getline, ssize_t, (lineptr, n, stream),
DEFUN(getline, (lineptr, n, stream),
char **lineptr AND size_t *n AND FILE *stream))

View File

@@ -0,0 +1,59 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_GNU_LD
#include <gnu-stabs.h>
warn_references(gets,
"the `gets' function is unreliable and should not be used.");
#endif
/* Read a newline-terminated string from stdin into S,
removing the trailing newline. Return S or NULL. */
char *
DEFUN(gets, (s), char *s)
{
register char *p = s;
register int c;
FILE *stream = stdin;
if (!__validfp(stream) || p == NULL)
{
errno = EINVAL;
return NULL;
}
if (feof(stream) || ferror(stream))
return NULL;
while ((c = getchar()) != EOF)
if (c == '\n')
break;
else
*p++ = c;
*p = '\0';
if (p == s || ferror(stream))
return NULL;
return s;
}

View File

@@ -0,0 +1,33 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
/* Read a word (int) from STREAM. */
int
DEFUN(getw, (stream), FILE *stream)
{
int w;
/* Is there a better way? */
if (fread((PTR) &w, sizeof(w), 1, stream) != 1)
return(EOF);
return(w);
}

View File

@@ -0,0 +1,97 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
/* This file provides glue between Unix stdio and GNU stdio.
It supports use of Unix stdio `getc' and `putc' (and,
by extension, `getchar' and `putchar') macros on GNU stdio streams
(they are slow, but they work). It also supports all stdio
operations (including Unix `getc' and `putc') on Unix's stdin, stdout,
and stderr (the elements of `_iob').
The reasoning behind this is to allow programs (and especially
libraries) compiled with Unix header files to work with the
GNU C library. */
#include <ansidecl.h>
#include <stdio.h>
#include <errno.h>
typedef union
{
struct
{
int magic;
FILE **streamp;
} glue;
struct _iobuf
{
int _cnt;
unsigned char *_ptr;
unsigned char *_base;
int _bufsiz;
short int _flag;
char _file;
} unix_iobuf;
} unix_FILE;
/* These are the Unix stdio's stdin, stdout, and stderr. */
unix_FILE _iob[] =
{
{ { _GLUEMAGIC, &stdin, } },
{ { _GLUEMAGIC, &stdout, } },
{ { _GLUEMAGIC, &stderr, } },
};
/* Called by the Unix stdio `getc' macro. */
int
DEFUN(_filbuf, (file), unix_FILE *file)
{
/* Compensate for getc's decrement. */
switch (++file->glue.magic)
{
case _GLUEMAGIC:
return getc (*file->glue.streamp);
case _IOMAGIC:
return getc ((FILE *) file);
default:
errno = EINVAL;
return EOF;
}
}
/* Called by the Unix stdio `putc' macro. */
int
DEFUN(_flsbuf, (c, file),
int c AND unix_FILE *file)
{
/* Compensate for putc's decrement. */
switch (++file->glue.magic)
{
case _GLUEMAGIC:
return putc (c, *file->glue.streamp);
case _IOMAGIC:
return putc (c, (FILE *) file);
default:
errno = EINVAL;
return EOF;
}
}

View File

@@ -0,0 +1,609 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Minimum size of a buffer we will allocate by default.
If this much memory is not available,
the stream in question will be made unbuffered instead. */
#define MIN_BUFSIZE 128
/* Figure out what kind of buffering (none, line, or full)
and what buffer size to give FP. */
static void
DEFUN(init_stream, (fp), register FILE *fp)
{
if (!fp->__seen)
{
/* Initialize the stream's info, including buffering info.
This may give a buffer, change I/O functions, etc.
If no buffer is set (and the stream is not made explicitly
unbuffered), we allocate a buffer below, using the bufsize
set by this function. */
extern void EXFUN(__stdio_init_stream, (FILE *));
fp->__room_funcs = __default_room_functions;
fp->__io_funcs = __default_io_functions;
__stdio_init_stream(fp);
fp->__seen = 1;
}
if (fp->__buffer == NULL && !fp->__userbuf)
{
int save;
if (fp->__bufsize == 0)
fp->__bufsize = BUFSIZ;
/* Try to get however many bytes of buffering __stdio_pickbuf
specified, but if that much memory isn't available,
try half as much each time until it succeeds or the buffer
size becomes too small to be useful. */
save = errno;
while (fp->__bufsize >= MIN_BUFSIZE)
{
fp->__buffer = (char *) malloc(fp->__bufsize);
if (fp->__buffer == NULL)
fp->__bufsize /= 2;
else
break;
}
errno = save;
if (fp->__buffer == NULL)
{
/* We can't get space for the buffer, so make it unbuffered. */
fp->__userbuf = 1;
fp->__bufsize = 0;
}
}
if (fp->__bufp == NULL)
{
/* Set the buffer pointer to the beginning of the buffer. */
fp->__bufp = fp->__buffer;
fp->__put_limit = fp->__get_limit = fp->__buffer;
}
}
/* Determine the current file position of STREAM if it is unknown. */
int
DEFUN(__stdio_check_offset, (stream), FILE *stream)
{
init_stream (stream);
if (stream->__offset == (fpos_t) -1)
{
/* This stream's offset is unknown or unknowable. */
if (stream->__io_funcs.__seek == NULL)
{
/* Unknowable. */
errno = ESPIPE;
return EOF;
}
else
{
/* Unknown. Find it out. */
fpos_t pos = (fpos_t) 0;
if ((*stream->__io_funcs.__seek)(stream->__cookie,
&pos, SEEK_CUR) < 0)
{
if (errno == ESPIPE)
/* Object is incapable of seeking. */
stream->__io_funcs.__seek = NULL;
return EOF;
}
stream->__offset = pos;
}
}
if (stream->__target == (fpos_t) -1)
/* This stream was opened on an existing object with
an unknown file position. The position is now known.
Make this the target position. */
stream->__target = stream->__offset;
return 0;
}
/* Move FP's file position to its target file position,
seeking as necessary and updating its `offset' field.
Sets ferror(FP) (and possibly errno) for errors. */
static void
DEFUN(seek_to_target, (fp), FILE *fp)
{
if (__stdio_check_offset (fp) == EOF)
fp->__error = 1;
else if (fp->__target != fp->__offset)
{
/* We are not at the target file position.
Seek to that position. */
if (fp->__io_funcs.__seek == NULL)
{
/* We can't seek! */
errno = ESPIPE;
fp->__error = 1;
}
else
{
fpos_t pos = fp->__target;
if ((*fp->__io_funcs.__seek)(fp->__cookie, &pos, SEEK_SET) < 0)
/* Seek failed! */
fp->__error = 1;
else
{
fp->__offset = pos;
if (pos != fp->__target)
/* Seek didn't go to the right place! */
fp->__error = 1;
}
}
}
}
/* Flush the buffer for FP.
If C is not EOF, it is also to be written.
If the stream is line buffered and C is a newline, it is written
to the output, otherwise it is put in the buffer after it has been
flushed to avoid a system call for a single character.
This is the default `output room' function. */
static void
DEFUN(flushbuf, (fp, c),
register FILE *fp AND int c)
{
int flush_only = c == EOF;
size_t buffer_written;
size_t to_write;
/* Set if target and get_limit have already been twiddled appropriately. */
int twiddled = 0;
if (fp->__put_limit == fp->__buffer)
{
/* The stream needs to be primed for writing. */
size_t buffer_offset = 0;
/* If the user has read some of the buffer, the target position
is incremented for each character he has read. */
fp->__target += fp->__bufp - fp->__buffer;
if (fp->__mode.__read && fp->__room_funcs.__input != NULL)
{
int save = errno;
CONST int aligned = (__stdio_check_offset(fp) == EOF ||
fp->__target % fp->__bufsize == 0);
errno = save;
if (!aligned)
{
/* Move to a block (buffer size) boundary and read in a block.
Then the output will be written as a whole block, too. */
CONST size_t o = fp->__target % fp->__bufsize;
fp->__target -= o;
if ((*fp->__room_funcs.__input)(fp) == EOF && ferror(fp))
return;
else
__clearerr(fp);
if (fp->__get_limit - fp->__buffer < o)
/* Oops. We didn't read enough (probably because we got EOF).
Forget we even mentioned it. */
fp->__target += o;
else
/* Start bufp as far into the buffer as we were into
this block before we read it. */
buffer_offset = o;
}
/* The target position is now set to where the beginning of the
buffer maps to; and the get_limit was set by the input-room
function. */
twiddled = 1;
}
if (fp->__buffer != NULL)
{
/* Set up to write output into the buffer. */
fp->__put_limit = fp->__buffer + fp->__bufsize;
fp->__bufp = fp->__buffer + buffer_offset;
if (!flush_only)
{
*fp->__bufp++ = (unsigned char) c;
if (!fp->__linebuf || (unsigned char) c != '\n')
return;
flush_only = 1;
}
}
}
/* If there is read data in the buffer past what was written,
write all of that as well. Otherwise, just write what has been
written into the buffer. */
buffer_written = fp->__bufp - fp->__buffer;
to_write = (buffer_written == 0 ? 0 :
fp->__get_limit > fp->__bufp ?
fp->__get_limit - fp->__buffer :
buffer_written);
if (fp->__io_funcs.__write == NULL || (to_write == 0 && flush_only))
{
/* There is no writing function or we're coming from an fflush
call with nothing in the buffer, so just say the buffer's
been flushed, increment the file offset, and return. */
fp->__bufp = fp->__buffer;
fp->__offset += to_write;
goto end;
}
if (to_write > 0)
{
int wrote;
/* Go to the target file position. */
seek_to_target(fp);
if (!ferror(fp))
{
/* Write out the buffered data. */
wrote = (*fp->__io_funcs.__write)(fp->__cookie, fp->__buffer,
to_write);
if (wrote > 0)
/* Record that we've moved forward in the file. */
fp->__offset += wrote;
if (wrote < (int) to_write)
/* The writing function should always write
the whole buffer unless there is an error. */
fp->__error = 1;
}
}
/* Reset the buffer pointer to the beginning of the buffer. */
fp->__bufp = fp->__buffer;
/* If we're not just flushing, write the last character, C. */
if (!flush_only && !ferror(fp))
{
if (fp->__buffer == NULL || (fp->__linebuf && (unsigned char) c == '\n'))
{
/* Either we're unbuffered, or we're line-buffered and
C is a newline, so really write it out immediately. */
char cc = (unsigned char) c;
if ((*fp->__io_funcs.__write)(fp->__cookie, &cc, 1) < 1)
fp->__error = 1;
else
{
/* Record that we've moved forward in the file. */
++fp->__offset;
++fp->__target;
}
}
else
/* Just put C in the buffer. */
*fp->__bufp++ = (unsigned char) c;
}
end:
if (!twiddled)
{
/* The new target position moves up as
much as the user wrote into the buffer. */
fp->__target += buffer_written;
/* Set the reading limit to the beginning of the buffer,
so the next `getc' will call __fillbf. */
fp->__get_limit = fp->__buffer;
}
if (feof(fp) || ferror(fp))
fp->__bufp = fp->__put_limit;
}
/* Fill the buffer for FP and return the first character read (or EOF).
This is the default `input_room' function. */
static int
DEFUN(fillbuf, (fp), register FILE *fp)
{
/* How far into the buffer we read we want to start bufp. */
size_t buffer_offset = 0;
register char *buffer;
register size_t to_read, nread = 0;
char c;
if (fp->__io_funcs.__read == NULL)
{
/* There is no read function, so always return EOF. */
fp->__eof = 1;
goto end;
}
if (fp->__buffer == NULL)
{
/* We're unbuffered, so we want to read only one character. */
buffer = &c;
to_read = 1;
}
else
{
/* We're buffered, so try to fill the buffer. */
buffer = fp->__buffer;
to_read = fp->__bufsize;
}
/* We're reading, so we're not at the end-of-file. */
fp->__eof = 0;
/* Go to the target file position. */
{
int save = errno;
if (__stdio_check_offset (fp) == 0 && fp->__target != fp->__offset)
{
/* Move to a block (buffer size) boundary. */
if (fp->__bufsize != 0)
{
buffer_offset = fp->__target % fp->__bufsize;
fp->__target -= buffer_offset;
}
seek_to_target (fp);
}
errno = save;
}
while (!ferror(fp) && !feof(fp) && nread <= buffer_offset)
{
/* Try to fill the buffer. */
int count = (*fp->__io_funcs.__read)(fp->__cookie, buffer, to_read);
if (count == 0)
fp->__eof = 1;
else if (count < 0)
fp->__error = 1;
else
{
buffer += count;
nread += count;
to_read -= count;
/* Record that we've moved forward in the file. */
fp->__offset += count;
}
}
if (fp->__buffer == NULL)
/* There is no buffer, so return the character we read
without all the buffer pointer diddling. */
return (feof(fp) || ferror(fp)) ? EOF : c;
/* Reset the buffer pointer to the beginning of the buffer
(plus whatever offset we may have set above). */
fp->__bufp = fp->__buffer + buffer_offset;
end:;
if (feof(fp) || ferror(fp))
{
/* Set both end pointers to the beginning of the buffer so
the next i/o call will force a call to __fillbf/__flshfp. */
fp->__put_limit = fp->__get_limit = fp->__buffer;
return EOF;
}
/* Set the end pointer to one past the last character we read. */
fp->__get_limit = fp->__buffer + nread;
/* Make it so the next `putc' will call __flshfp. */
fp->__put_limit = fp->__buffer;
/* Return the first character in the buffer. */
return (unsigned char) *fp->__bufp++;
}
/* Default I/O and room functions. */
extern __io_read __stdio_read;
extern __io_write __stdio_write;
extern __io_seek __stdio_seek;
extern __io_close __stdio_close;
CONST __io_functions __default_io_functions =
{
__stdio_read, __stdio_write, __stdio_seek, __stdio_close
};
CONST __room_functions __default_room_functions =
{
fillbuf, flushbuf
};
/* Flush the buffer for FP and also write C if FLUSH_ONLY is nonzero.
This is the function used by putc and fflush. */
int
DEFUN(__flshfp, (fp, c),
register FILE *fp AND int c)
{
int flush_only = c == EOF;
if (!__validfp(fp) || !fp->__mode.__write)
{
errno = EINVAL;
return EOF;
}
if (ferror(fp))
return EOF;
/* Make sure the stream is initialized (has functions and buffering). */
init_stream(fp);
if (!flush_only && fp->__get_limit == fp->__buffer &&
fp->__bufp > fp->__buffer && fp->__bufp < fp->__buffer + fp->__bufsize)
{
/* The character will fit in the buffer, so put it there. */
*fp->__bufp++ = (unsigned char) c;
if (fp->__linebuf && (unsigned char) c == '\n')
flush_only = 1;
else
return (unsigned char) c;
}
if (fp->__room_funcs.__output == NULL)
{
/* A NULL `output room' function means
to always return an output error. */
fp->__error = 1;
return EOF;
}
if (!flush_only || fp->__bufp > fp->__buffer)
{
if (fp->__linebuf && fp->__bufp > fp->__buffer)
/* This is a line-buffered stream, so its put-limit is
set to the beginning of the buffer in order to force
a __flshfp call on each putc. We undo this hack here
(by setting the limit to the end of the buffer) to simplify
the interface with the output-room function.
However, we must make sure we don't do this if there isn't
actually anything in the buffer, because in that case we
want to give the output-room function a chance to prime
the stream for writing in whatever fashion turns it on. */
fp->__put_limit = fp->__buffer + fp->__bufsize;
/* Make room in the buffer. */
(*fp->__room_funcs.__output)(fp, flush_only ? EOF : (unsigned char) c);
if (fp->__linebuf)
/* Set the end pointer to the beginning of the buffer,
so the next `putc' call will force a call to this function
(see comment above about line-buffered streams). */
fp->__put_limit = fp->__buffer;
}
if (ferror (fp))
return EOF;
if (flush_only)
return 0;
return (unsigned char) c;
}
/* Fill the buffer for FP and return the first character read.
This is the function used by getc. */
int
DEFUN(__fillbf, (fp), register FILE *fp)
{
register int c;
fpos_t new_target;
if (!__validfp(fp) || !fp->__mode.__read)
{
errno = EINVAL;
return EOF;
}
if (fp->__pushed_back)
{
/* Return the char pushed back by ungetc. */
fp->__bufp = fp->__pushback_bufp;
fp->__pushed_back = 0;
return fp->__pushback;
}
/* Make sure the stream is initialized (has functions and buffering). */
init_stream(fp);
/* If we're trying to read the first character of a new
line of input from an unbuffered or line buffered stream,
we must flush all line-buffered output streams. */
if (fp->__buffer == NULL || fp->__linebuf)
{
register FILE *f;
for (f = __stdio_head; f != NULL; f = f->__next)
if (__validfp(f) && f->__linebuf && f->__mode.__write &&
f->__put_limit > f->__buffer)
(void) __flshfp(f, EOF);
}
/* We want the beginning of the buffer to now
map to just past the last data we read. */
new_target = fp->__target + (fp->__get_limit - fp->__buffer);
if (fp->__put_limit > fp->__buffer)
{
/* There is written data in the buffer.
Flush it out. */
if (fp->__room_funcs.__output == NULL)
fp->__error = 1;
else
(*fp->__room_funcs.__output)(fp, EOF);
}
fp->__target = new_target;
if (ferror(fp))
c = EOF;
else if (fp->__room_funcs.__input != NULL)
{
c = (*fp->__room_funcs.__input)(fp);
if (fp->__buffer == NULL)
/* This is an unbuffered stream, so the target sync above
won't do anything the next time around. Instead, note that
we have read one character. The (nonexistent) buffer now
maps to the position just past that character. */
++fp->__target;
}
else
{
/* A NULL `input_room' function means always return EOF. */
fp->__eof = 1;
c = EOF;
}
return c;
}
/* Nuke a stream, but don't kill its link in the chain. */
void
DEFUN(__invalidate, (stream), register FILE *stream)
{
/* Save its link. */
register FILE *next = stream->__next;
/* Pulverize the fucker. */
memset((PTR) stream, 0, sizeof(FILE));
/* Restore the deceased's link. */
stream->__next = next;
}
/* Abort with an error message. */
__NORETURN
void
DEFUN(__libc_fatal, (message), CONST char *message)
{
__stdio_errmsg(message, strlen(message));
abort();
}

View File

@@ -0,0 +1,122 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <stdlib.h>
struct memstream_info
{
char **buffer;
size_t *bufsize;
};
/* Enlarge STREAM's buffer. */
static void
DEFUN(enlarge_buffer, (stream, c),
register FILE *stream AND int c)
{
struct memstream_info *info = (struct memstream_info *) stream->__cookie;
char *newbuf;
*info->bufsize = stream->__bufp - stream->__buffer;
if (stream->__put_limit - stream->__bufp < 1)
{
stream->__bufsize += 100;
newbuf = (char *) realloc ((PTR) stream->__buffer, stream->__bufsize);
*info->buffer = newbuf;
if (newbuf == NULL)
{
free ((PTR) stream->__buffer);
stream->__buffer = stream->__bufp
= stream->__put_limit = stream->__get_limit = NULL;
stream->__error = 1;
return;
}
stream->__buffer = newbuf;
stream->__bufp = stream->__buffer + *info->bufsize;
stream->__get_limit = stream->__put_limit;
stream->__put_limit = stream->__buffer + stream->__bufsize;
}
if (c != EOF)
*stream->__bufp++ = (unsigned char) c;
else
*stream->__bufp = '\0';
}
static int
DEFUN(free_info, (cookie), PTR cookie)
{
#if 0
struct memstream_info *info = (struct memstream_info *) cookie;
char *buf;
buf = (char *) realloc ((PTR) *info->buffer, *info->bufsize);
if (buf != NULL)
*info->buffer = buf;
#endif
free (cookie);
return 0;
}
/* Open a stream that writes into a malloc'd buffer that is expanded as
necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
and the number of characters written on fflush or fclose. */
FILE *
DEFUN(open_memstream, (bufloc, sizeloc),
char **bufloc AND size_t *sizeloc)
{
FILE *stream;
struct memstream_info *info;
if (bufloc == NULL || sizeloc == NULL)
{
errno = EINVAL;
return NULL;
}
stream = fmemopen ((char *) NULL, BUFSIZ, "w");
if (stream == NULL)
return NULL;
info = (struct memstream_info *) malloc (sizeof (struct memstream_info));
if (info == NULL)
{
int save = errno;
(void) fclose (stream);
errno = save;
return NULL;
}
stream->__room_funcs.__output = enlarge_buffer;
stream->__io_funcs.__close = free_info;
stream->__cookie = (PTR) info;
stream->__userbuf = 1;
info->buffer = bufloc;
info->bufsize = sizeloc;
*bufloc = stream->__buffer;
return stream;
}

View File

@@ -0,0 +1,53 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
/* Return a new, zeroed, stream.
You must set its cookie and io_mode.
The first operation will give it a buffer unless you do.
It will also give it the default functions unless you set the `seen' flag.
Returns NULL if a stream can't be created. */
FILE *
DEFUN_VOID(__newstream)
{
register FILE *stream;
stream = __stdio_head;
while (__validfp(stream))
stream = stream->__next;
if (stream == NULL)
{
/* None to reuse. */
stream = (FILE *) malloc(sizeof(FILE));
if (stream == NULL)
return NULL;
stream->__next = NULL;
}
__invalidate(stream);
stream->__magic = _IOMAGIC;
stream->__offset = (fpos_t) -1;
stream->__target = (fpos_t) -1;
return stream;
}

View File

@@ -0,0 +1,50 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <errno.h>
#ifndef HAVE_GNU_LD
#define _sys_errlist sys_errlist
#define _sys_nerr sys_nerr
#endif
/* Defined in sys_errlist.c. */
extern CONST char *CONST _sys_errlist[];
extern CONST int _sys_nerr;
/* Print a line on stderr consisting of the text in S, a colon, a space,
a message describing the meaning of the contents of `errno' and a newline.
If S is NULL or "", the colon and space are omitted. */
void
DEFUN(perror, (s), register CONST char *s)
{
int errnum = errno;
CONST char *colon;
if (s == NULL || *s == '\0')
s = colon = "";
else
colon = ": ";
if (errnum >= 0 && errnum < _sys_nerr)
(void) fprintf(stderr, "%s%s%s\n", s, colon, _sys_errlist[errnum]);
else
(void) fprintf(stderr, "%s%sUnknown error %d\n", s, colon, errnum);
}

View File

@@ -0,0 +1,207 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <printf.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#ifdef __GNUC__
#define HAVE_LONGLONG
#endif
extern printf_arginfo_function *__printf_arginfo_table[];
size_t
DEFUN(parse_printf_format, (fmt, n, argtypes),
CONST char *fmt AND size_t n AND int *argtypes)
{
register CONST char *f;
size_t need = 0;
for (f = strchr (fmt, '%'); f != NULL; f = strchr (f, '%'))
{
struct printf_info info;
printf_arginfo_function *arginfo;
++f;
info.space = info.showsign = info.left = info.alt = 0;
info.pad = ' ';
while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
switch (*f++)
{
case ' ':
info.space = 1;
break;
case '+':
info.showsign = 1;
break;
case '-':
info.left = 1;
break;
case '#':
info.alt = 1;
break;
case '0':
info.pad = '0';
break;
}
if (info.left)
info.pad = ' ';
/* Get the field width. */
if (*f == '*')
{
if (++need < n)
*argtypes++ = PA_INT;
info.width = INT_MIN;
++f;
}
else
{
info.width = 0;
while (isdigit(*f))
{
info.width *= 10;
info.width += *f++ - '0';
}
}
/* Get the precision. */
/* -1 means none given; 0 means explicit 0. */
info.prec = -1;
if (*f == '.')
{
++f;
if (*f == '*')
{
/* The precision is given in an argument. */
if (++need < n)
*argtypes++ = PA_INT;
info.prec = INT_MIN;
++f;
}
else if (isdigit(*f))
{
info.prec = 0;
while (*f != '\0' && isdigit(*f))
{
info.prec *= 10;
info.prec += *f++ - '0';
}
}
}
/* Check for type modifiers. */
info.is_short = info.is_long = info.is_long_double = 0;
while (*f == 'h' || *f == 'l' || *f == 'L')
switch (*f++)
{
case 'h':
/* int's are short int's. */
info.is_short = 1;
break;
case 'l':
#ifdef HAVE_LONGLONG
if (info.is_long)
/* A double `l' is equivalent to an `L'. */
info.is_long_double = 1;
else
#endif
/* int's are long int's. */
info.is_long = 1;
break;
case 'L':
/* double's are long double's, and int's are long long int's. */
info.is_long_double = 1;
break;
}
if (*f == '\0')
return need;
info.spec = *f++;
arginfo = __printf_arginfo_table[info.spec];
if (arginfo != NULL)
{
size_t nargs
= (*arginfo) (&info, need > n ? 0 : n - need, argtypes);
need += nargs;
argtypes += nargs;
}
else
{
int type;
switch (info.spec)
{
case 'i':
case 'd':
case 'u':
case 'o':
case 'X':
case 'x':
type = PA_INT;
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
type = PA_DOUBLE;
break;
case 'c':
type = PA_CHAR;
break;
case 's':
type = PA_STRING;
break;
case 'p':
type = PA_POINTER;
break;
case 'n':
type = PA_INT | PA_FLAG_PTR;
break;
default:
/* No arg for an unknown spec. */
continue;
}
if (info.is_long_double)
type |= PA_FLAG_LONG_DOUBLE;
if (info.is_long)
type |= PA_FLAG_LONG;
if (info.is_short)
type |= PA_FLAG_SHORT;
if (++need < n)
*argtypes++ = type;
}
}
return need;
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output to stdout from the format string FORMAT. */
/* VARARGS1 */
int
DEFUN(printf, (format), CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vprintf(format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,110 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#ifndef _PRINTF_H
#define _PRINTF_H 1
#include <features.h>
#define __need_FILE
#include <stdio.h>
#define __need_size_t
#include <stddef.h>
#include <stdarg.h> /* Need va_list. */
struct printf_info
{
int prec; /* Precision. */
int width; /* Width. */
char spec; /* Format letter. */
unsigned int is_long_double:1; /* L flag. */
unsigned int is_short:1; /* h flag. */
unsigned int is_long:1; /* l flag. */
unsigned int alt:1; /* # flag. */
unsigned int space:1; /* Space flag. */
unsigned int left:1; /* - flag. */
unsigned int showsign:1; /* + flag. */
char pad; /* Padding character. */
};
/* Type of a printf specifier-handler function.
STREAM is the FILE on which to write output.
INFO gives information about the format specification.
Arguments can be read from ARGS.
The function should return the number of characters written,
or -1 for errors. */
typedef int EXFUN(printf_function, (FILE *__stream,
CONST struct printf_info *__info,
va_list *__args));
typedef int EXFUN(printf_arginfo_function, (CONST struct printf_info *__info,
size_t __n,
int *__argtypes));
/* Register FUNC to be called to format SPEC specifiers.
ARGINFO, if not NULL, is a function used by `parse_printf_format'
to determine how many arguments a SPEC conversion requires,
and what their types are. */
extern int EXFUN(register_printf_function,
(int __spec AND printf_function __func,
printf_arginfo_function __arginfo));
/* Parse FMT, and fill in N elements of ARGTYPES with the
types needed for the conversions FMT specifies. Returns
the number of arguments required by FMT.
The ARGINFO function registered with a user-defined format is passed a
`struct printf_info' describing the format spec being parsed. A width
or precision of INT_MIN means a `*' was used to indicate that the
width/precision will come from an arg. The function should fill in the
array it is passed with the types of the arguments it wants, and return
the number of arguments it wants. */
extern size_t EXFUN(parse_printf_format, (CONST char *__fmt,
size_t __n,
int *__argtypes));
/* Codes returned by `parse_printf_format' for basic types.
These values cover all the standard format specifications.
Users can add new values after PA_LAST for their own types. */
enum
{ /* C type: */
PA_INT, /* int */
PA_CHAR, /* int, cast to char */
PA_STRING, /* const char *, a '\0'-terminated string */
PA_POINTER, /* void * */
PA_FLOAT, /* float */
PA_DOUBLE, /* double */
PA_LAST
};
/* Flag bits that can be set in a type returned by `parse_printf_format'. */
#define PA_FLAG_MASK 0xff00
#define PA_FLAG_LONG_LONG (1 << 8)
#define PA_FLAG_LONG_DOUBLE PA_FLAG_LONG_LONG
#define PA_FLAG_LONG (1 << 9)
#define PA_FLAG_SHORT (1 << 10)
#define PA_FLAG_PTR (1 << 11)
#endif /* printf.h */

View File

@@ -0,0 +1,49 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <signal.h>
#ifndef HAVE_GNU_LD
#define _sys_siglist sys_siglist
#endif
/* Defined in sys_siglist.c. */
extern CONST char *CONST _sys_siglist[];
/* Print out on stderr a line consisting of the test in S, a colon, a space,
a message describing the meaning of the signal number SIG and a newline.
If S is NULL or "", the colon and space are omitted. */
void
DEFUN(psignal, (sig, s), int sig AND register CONST char *s)
{
CONST char *colon;
if (s == NULL || s == '\0')
s = colon = "";
else
colon = ": ";
if (sig >= 0 && sig < NSIG)
(void) fprintf(stderr, "%s%s%s\n", s, colon, _sys_siglist[sig]);
else
(void) fprintf(stderr, "%s%sUnknown signal %d\n", s, colon, sig);
}

View File

@@ -0,0 +1,5 @@
#include <ansidecl.h>
#include <stdio.h>
#undef putc
#define fputc putc
#include <fputc.c>

View File

@@ -0,0 +1,30 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#undef putchar
/* Write the character C on stdout. */
int
DEFUN(putchar, (c), int c)
{
return __putc(c, stdout);
}

View File

@@ -0,0 +1,32 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#undef puts
/* Write the string in S and a newline to stdout. */
int
DEFUN(puts, (s), CONST char *s)
{
return(fputs(s, stdout) || putchar('\n') == EOF ? EOF : 0);
}

View File

@@ -0,0 +1,31 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
/* Write the word (int) W to STREAM. */
int
DEFUN(putw, (w, stream), int w AND FILE *stream)
{
/* Is there a better way? */
if (fwrite((CONST PTR) &w, sizeof(w), 1, stream) < 1)
return(EOF);
return(0);
}

View File

@@ -0,0 +1,47 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <limits.h>
#include <printf.h>
/* Array of functions indexed by format character. */
static printf_function *printf_funcs[UCHAR_MAX + 1];
printf_arginfo_function *__printf_arginfo_table[UCHAR_MAX + 1];
printf_function **__printf_function_table;
/* Register FUNC to be called to format SPEC specifiers. */
int
DEFUN(register_printf_function, (spec, converter, arginfo),
int spec AND printf_function converter AND
printf_arginfo_function arginfo)
{
if (spec < 0 || spec > (int) UCHAR_MAX)
{
errno = EINVAL;
return -1;
}
__printf_function_table = printf_funcs;
__printf_arginfo_table[spec] = arginfo;
printf_funcs[spec] = converter;
return 0;
}

View File

@@ -0,0 +1,26 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <unistd.h>
#include <gnu-stabs.h>
function_alias(remove, __unlink, int, (file),
DEFUN(remove, (file), CONST char *file))

View File

@@ -0,0 +1,33 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#undef rewind
/* Rewind STREAM to the beginning of the
file and clear its error and EOF flags. */
void
DEFUN(rewind, (stream), FILE *stream)
{
clearerr(stream);
(void) fseek(stream, 0L, SEEK_SET);
clearerr(stream);
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Read formatted input from stdin according to the format string FORMAT. */
/* VARARGS1 */
int
DEFUN(scanf, (format), CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vscanf(format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,30 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
/* If BUF is NULL, make STREAM unbuffered.
If not, make BUF, which is BUFSIZ bytes long, be its buffer. */
void
DEFUN(setbuf, (stream, buf), FILE *stream AND char *buf)
{
(void) setvbuf(stream, buf, buf != NULL ? _IOFBF : _IONBF, BUFSIZ);
}

View File

@@ -0,0 +1,30 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
/* If BUF is NULL, make stream unbuffered.
If not, make BUF, which is N bytes long, be its buffer. */
void
DEFUN(setbuffer, (stream, buf, n), FILE *stream AND char *buf AND size_t n)
{
(void) setvbuf(stream, buf, buf != NULL ? _IOFBF : _IONBF, n);
}

View File

@@ -0,0 +1,29 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
/* Make STREAM line buffered. */
void
DEFUN(setlinebuf, (stream), FILE *stream)
{
if (stream->__buffer != NULL || !stream->__userbuf)
stream->__linebuf = 1;
}

View File

@@ -0,0 +1,87 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
/* Make STREAM use the buffering method given in MODE.
If MODE indicates full or line buffering, use BUF,
a buffer of SIZE bytes; if BUF is NULL, malloc a buffer. */
int
DEFUN(setvbuf, (stream, buf, mode, size),
FILE *stream AND char *buf AND int mode AND size_t size)
{
if (!__validfp(stream))
{
errno = EINVAL;
return EOF;
}
/* The ANSI standard says setvbuf can only be called before any I/O is done,
but we allow it to replace an old buffer, flushing it first. */
if (stream->__buffer != NULL)
{
(void) fflush(stream);
/* Free the old buffer if it was malloc'd. */
if (!stream->__userbuf)
free(stream->__buffer);
}
stream->__get_limit = stream->__put_limit = NULL;
stream->__bufp = stream->__buffer = NULL;
stream->__userbuf = stream->__linebuf = 0;
switch (mode)
{
default:
errno = EINVAL;
return EOF;
case _IONBF: /* Unbuffered. */
stream->__buffer = NULL;
stream->__bufsize = 0;
stream->__userbuf = 1;
break;
case _IOLBF: /* Line buffered. */
stream->__linebuf = 1;
case _IOFBF: /* Fully buffered. */
if (size == 0)
{
errno = EINVAL;
return EOF;
}
stream->__bufsize = size;
if (buf != NULL)
stream->__userbuf = 1;
else if ((buf = (char *) malloc(size)) == NULL)
return EOF;
stream->__buffer = buf;
break;
}
stream->__bufp = stream->__buffer;
stream->__get_limit = stream->__buffer;
if (stream->__mode.__write)
stream->__put_limit = stream->__buffer + stream->__bufsize;
else
stream->__put_limit = stream->__buffer;
return 0;
}

View File

@@ -0,0 +1,39 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output into S, according to the format
string FORMAT, writing no more than MAXLEN characters. */
/* VARARGS3 */
int
DEFUN(snprintf, (s, maxlen, format),
char *s AND size_t maxlen AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vsnprintf(s, maxlen, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Write formatted output into S, according to the format string FORMAT. */
/* VARARGS2 */
int
DEFUN(sprintf, (s, format), char *s AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = vsprintf(s, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,37 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
/* Read formatted input from S, according to the format string FORMAT. */
/* VARARGS2 */
int
DEFUN(sscanf, (s, format), CONST char *s AND CONST char *format DOTS)
{
va_list arg;
int done;
va_start(arg, format);
done = __vsscanf(s, format, arg);
va_end(arg);
return done;
}

View File

@@ -0,0 +1,566 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
/*
* ANSI Standard: 4.9 INPUT/OUTPUT <stdio.h>
*/
#ifndef _STDIO_H
#if !defined(__need_FILE)
#define _STDIO_H 1
#include <features.h>
#define __need_size_t
#define __need_NULL
#include <stddef.h>
#include <gnu/types.h>
#endif /* Don't need FILE. */
#undef __need_FILE
#ifndef __FILE_defined
/* The opaque type of streams. */
typedef struct __stdio_file FILE;
#define __FILE_defined 1
#endif /* FILE not defined. */
#ifdef _STDIO_H
/* The type of the second argument to `fgetpos' and `fsetpos'. */
typedef __off_t fpos_t;
/* The mode of I/O, as given in the MODE argument to fopen, etc. */
typedef struct
{
unsigned int __read:1; /* Open for reading. */
unsigned int __write:1; /* Open for writing. */
unsigned int __append:1; /* Open for appending. */
unsigned int __binary:1; /* Opened binary. */
unsigned int __create:1; /* Create the file. */
unsigned int __exclusive:1; /* Error if it already exists. */
unsigned int __truncate:1; /* Truncate the file on opening. */
} __io_mode;
/* Functions to do I/O and file management for a stream. */
typedef __ssize_t EXFUN(__io_read, (PTR __cookie, char *__buf,
size_t __nbytes));
typedef __ssize_t EXFUN(__io_write, (PTR __cookie, CONST char *__buf,
size_t __n));
typedef int EXFUN(__io_seek, (PTR __cookie, fpos_t *__pos, int __w));
typedef int EXFUN(__io_close, (PTR __cookie));
/* Low level interface, independent of FILE representation. */
typedef struct
{
__io_read *__read; /* Read bytes. */
__io_write *__write; /* Write bytes. */
__io_seek *__seek; /* Seek/tell file position. */
__io_close *__close; /* Close file. */
} __io_functions;
/* Higher level interface, dependent on FILE representation. */
typedef struct
{
/* Make room in the input buffer. */
int EXFUN((*__input), (FILE *__stream));
/* Make room in the output buffer. */
void EXFUN((*__output), (FILE *__stream, int __c));
} __room_functions;
extern CONST __io_functions __default_io_functions;
extern CONST __room_functions __default_room_functions;
/* Default close function. */
extern __io_close __stdio_close;
/* Open FILE with mode M, return cookie or NULL to use an int in *DP. */
extern PTR EXFUN(__stdio_open, (CONST char *__file, __io_mode __m, int *__dp));
/* Put out an error message for when stdio needs to die. */
extern void EXFUN(__stdio_errmsg, (CONST char *__msg, size_t __len));
/* Generate a unique file name. */
extern char *EXFUN(__stdio_gen_tempname, (CONST char *__dir, CONST char *__pfx,
int __dir_search, size_t *__lenptr));
#ifndef __NORETURN
#ifdef __GNUC__
#define __NORETURN __volatile
#else /* Not GCC. */
#define __NORETURN
#endif /* GCC. */
#endif /* __NORETURN not defined. */
/* Print out MESSAGE on the error output and abort. */
extern __NORETURN void EXFUN(__libc_fatal, (CONST char *__message));
/* The FILE structure. */
struct __stdio_file
{
/* Magic number for validation. Must be negative in open streams
for the glue to Unix stdio getc/putc to work. */
int __magic;
#define _IOMAGIC 0xfedabeeb /* Magic number to fill `__magic'. */
#define _GLUEMAGIC 0xfeedbabe /* Magic for glued Unix streams. */
char *__bufp; /* Pointer into the buffer. */
char *__get_limit; /* Reading limit. */
char *__put_limit; /* Writing limit. */
char *__buffer; /* Base of buffer. */
size_t __bufsize; /* Size of the buffer. */
FILE *__next; /* Next FILE in the linked list. */
PTR __cookie; /* Magic cookie. */
int __fileno; /* System file descriptor. */
unsigned char __pushback; /* Pushed-back character. */
char *__pushback_bufp; /* Old bufp if char pushed back. */
unsigned int __pushed_back:1; /* A char has been pushed back. */
unsigned int __eof:1; /* End of file encountered. */
unsigned int __error:1; /* Error encountered. */
unsigned int __userbuf:1; /* Buffer is from user. */
unsigned int __linebuf:1; /* Flush on newline. */
unsigned int __seen:1; /* This stream has been seen. */
unsigned int __ispipe:1; /* Nonzero if opened by popen. */
__io_mode __mode; /* File access mode. */
__io_functions __io_funcs; /* I/O functions. */
__room_functions __room_funcs; /* I/O buffer room functions. */
fpos_t __offset; /* Current file position. */
fpos_t __target; /* Target file position. */
};
/* All macros used internally by other macros here and by stdio functions begin
with `__'. All of these may evaluate their arguments more than once. */
/* Nonzero if STREAM is a valid stream. */
#define __validfp(stream) \
(stream != NULL && ((stream->__magic == _GLUEMAGIC && \
(stream = (FILE *) &((int *) stream)[1])), \
(stream->__magic == _IOMAGIC))) \
/* Clear the error and EOF indicators of STREAM. */
#define __clearerr(stream) ((stream)->__error = (stream)->__eof = 0)
/* Nuke STREAM, making it unusable but available for reuse. */
extern void EXFUN(__invalidate, (FILE *__stream));
/* Make sure STREAM->__offset and STREAM->__target are initialized.
Returns 0 if successful, or EOF on
error (but doesn't set STREAM->__error). */
extern int EXFUN(__stdio_check_offset, (FILE *__stream));
/* The possibilities for the third argument to `setvbuf'. */
#define _IOFBF 0x1 /* Full buffering. */
#define _IOLBF 0x2 /* Line buffering. */
#define _IONBF 0x4 /* No buffering. */
/* Default buffer size. */
#define BUFSIZ 1024
/* End of file character.
Some things throughout the library rely on this being -1. */
#define EOF (-1)
/* The possibilities for the third argument to `fseek'.
These values should not be changed. */
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
#ifdef __USE_SVID
/* Default path prefix for `tempnam' and `tmpnam'. */
#define P_tmpdir "/usr/tmp"
#endif
/* Get the values:
L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
TMP_MAX The minimum number of unique filenames generated by tmpnam
(and tempnam when it uses tmpnam's name space),
or tempnam (the two are separate).
L_ctermid How long an array to pass to `ctermid'.
L_cuserid How long an array to pass to `cuserid'.
FOPEN_MAX Mininum number of files that can be open at once.
FILENAME_MAX Maximum length of a filename. */
#include <stdio_lim.h>
/* All the known streams are in a linked list
linked by the `next' field of the FILE structure. */
extern FILE *__stdio_head; /* Head of the list. */
/* Standard streams. */
extern FILE *stdin, *stdout, *stderr;
/* Remove file FILENAME. */
extern int EXFUN(remove, (CONST char *__filename));
/* Rename file OLD to NEW. */
extern int EXFUN(rename, (CONST char *__old, CONST char *__new));
/* Create a temporary file and open it read/write. */
extern FILE *EXFUN(tmpfile, (NOARGS));
/* Generate a temporary filename. */
extern char *EXFUN(tmpnam, (char *__s));
#ifdef __USE_SVID
/* Generate a unique temporary filename using up to five characters of PFX
if it is not NULL. The directory to put this file in is searched for
as follows: First the environment variable "TMPDIR" is checked.
If it contains the name of a writable directory, that directory is used.
If not and if DIR is not NULL, that value is checked. If that fails,
P_tmpdir is tried and finally "/tmp". The storage for the filename
is allocated by `malloc'. */
extern char *EXFUN(tempnam, (CONST char *__dir, CONST char *__pfx));
#endif
/* This performs actual output when necessary, flushing
STREAM's buffer and optionally writing another character. */
extern int EXFUN(__flshfp, (FILE *__stream, int __c));
/* Close STREAM, or all streams if STREAM is NULL. */
extern int EXFUN(fclose, (FILE *__stream));
/* Flush STREAM, or all streams if STREAM is NULL. */
extern int EXFUN(fflush, (FILE *__stream));
/* Open a file and create a new stream for it. */
extern FILE *EXFUN(fopen, (CONST char *__filename, CONST char *__modes));
/* Open a file, replacing an existing stream with it. */
extern FILE *EXFUN(freopen, (CONST char *__filename,
CONST char *__modes, FILE *__stream));
/* Return a new, zeroed, stream.
You must set its cookie and io_mode.
The first operation will give it a buffer unless you do.
It will also give it the default functions unless you set the `seen' flag.
The offset is set to -1, meaning it will be determined by doing a
stationary seek. You can set it to avoid the initial tell call.
The target is set to -1, meaning it will be set to the offset
before the target is needed.
Returns NULL if a stream can't be created. */
extern FILE *EXFUN(__newstream, (NOARGS));
#ifdef __USE_POSIX
/* Create a new stream that refers to an existing system file descriptor. */
extern FILE *EXFUN(fdopen, (int __fd, CONST char *__modes));
#endif
#ifdef __USE_GNU
/* Create a new stream that refers to the given magic cookie,
and uses the given functions for input and output. */
extern FILE *EXFUN(fopencookie, (PTR __magic_cookie, CONST char *__modes,
__io_functions __io_functions));
/* Create a new stream that refers to a memory buffer. */
extern FILE *EXFUN(fmemopen, (PTR __s, size_t __len, CONST char *__modes));
/* Open a stream that writes into a malloc'd buffer that is expanded as
necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
and the number of characters written on fflush or fclose. */
extern FILE *EXFUN(open_memstream, (char **__bufloc, size_t *sizeloc));
#endif
/* If BUF is NULL, make STREAM unbuffered.
Else make it use buffer BUF, of size BUFSIZ. */
extern void EXFUN(setbuf, (FILE *__stream, char *__buf));
/* Make STREAM use buffering mode MODE.
If BUF is not NULL, use N bytes of it for buffering;
else allocate an internal buffer N bytes long. */
extern int EXFUN(setvbuf, (FILE *__stream, char *__buf,
int __modes, size_t __n));
#ifdef __USE_BSD
/* If BUF is NULL, make STREAM unbuffered.
Else make it use SIZE bytes of BUF for buffering. */
extern void EXFUN(setbuffer, (FILE *__stream, char *__buf, size_t __size));
/* Make STREAM line-buffered. */
extern void EXFUN(setlinebuf, (FILE *__stream));
#endif
/* Write formatted output to STREAM. */
extern int EXFUN(fprintf, (FILE *__stream, CONST char *__format, ...));
/* Write formatted output to stdout. */
extern int EXFUN(printf, (CONST char *__format, ...));
/* Write formatted output to S. */
extern int EXFUN(sprintf, (char *__s, CONST char *__format, ...));
/* Write formatted output to S from argument list ARG. */
extern int EXFUN(vfprintf, (FILE *__s, CONST char *__format, PTR __arg));
/* Write formatted output to stdout from argument list ARG. */
extern int EXFUN(vprintf, (CONST char *__format, PTR __arg));
/* Write formatted output to S from argument list ARG. */
extern int EXFUN(vsprintf, (char *__s, CONST char *__format, PTR __arg));
#ifdef __OPTIMIZE__
#define vprintf(fmt, arg) vfprintf(stdout, (fmt), (arg))
#endif /* Optimizing. */
#ifdef __USE_GNU
/* Maximum chars of output to write in MAXLEN. */
extern int EXFUN(snprintf, (char *__s, size_t __maxlen,
CONST char *__format, ...));
extern int EXFUN(vsnprintf, (char *__s, size_t __maxlen,
CONST char *__format, PTR __arg));
/* Write formatted output to a string dynamically allocated with `malloc'.
Store the address of the string in *PTR. */
extern int EXFUN(vasprintf, (char **__ptr, CONST char *__f, PTR __arg));
extern int EXFUN(asprintf, (char **__ptr, CONST char *__fmt, ...));
/* Write formatted output to a file descriptor. */
extern int EXFUN(vdprintf, (int __fd, CONST char *__fmt, PTR __arg));
extern int EXFUN(dprintf, (int __fd, CONST char *__fmt, ...));
#endif
/* Read formatted input from STREAM. */
extern int EXFUN(fscanf, (FILE *__stream, CONST char *__format, ...));
/* Read formatted input from stdin. */
extern int EXFUN(scanf, (CONST char *__format, ...));
/* Read formatted input from S. */
extern int EXFUN(sscanf, (CONST char *__s, CONST char *__format, ...));
#ifdef __USE_GNU
/* Read formatted input from S into argument list ARG. */
extern int EXFUN(__vfscanf, (FILE *__s, CONST char *__format,
PTR __arg));
extern int EXFUN(vfscanf, (FILE *__s, CONST char *__format,
PTR __arg));
/* Read formatted input from stdin into argument list ARG. */
extern int EXFUN(vscanf, (CONST char *__format, PTR __arg));
/* Read formatted input from S into argument list ARG. */
extern int EXFUN(__vsscanf, (CONST char *__s, CONST char *__format,
PTR __arg));
extern int EXFUN(vsscanf, (CONST char *__s, CONST char *__format,
PTR __arg));
#ifdef __OPTIMIZE__
#define vfscanf(s, format, arg) __vfscanf((s), (format), (arg))
#define vscanf(format, arg) __vfscanf(stdin, (format), (arg))
#define vsscanf(s, format, arg) __vsscanf((s), (format), (arg))
#endif /* Optimizing. */
#endif /* Use GNU. */
/* This does actual reading when necessary, filling STREAM's
buffer and returning the first character in it. */
extern int EXFUN(__fillbf, (FILE *__stream));
/* Read a character from STREAM. */
extern int EXFUN(fgetc, (FILE *__stream));
extern int EXFUN(getc, (FILE *__stream));
/* Read a character from stdin. */
extern int EXFUN(getchar, (NOARGS));
/* The C standard explicitly says this can
re-evaluate its argument, so it does. */
#define __getc(stream) \
((stream)->__bufp < (stream)->__get_limit ? \
(int) ((unsigned char) *(stream)->__bufp++) : __fillbf(stream))
/* The C standard explicitly says this is a macro,
so we always do the optimization for it. */
#define getc(stream) __getc(stream)
#ifdef __OPTIMIZE__
#define getchar() __getc(stdin)
#endif /* Optimizing. */
/* Write a character to STREAM. */
extern int EXFUN(fputc, (int __c, FILE *__stream));
extern int EXFUN(putc, (int __c, FILE *__stream));
/* Write a character to stdout. */
extern int EXFUN(putchar, (int __c));
/* The C standard explicitly says this can
re-evaluate its arguments, so it does. */
#define __putc(c, stream) \
((stream)->__bufp < (stream)->__put_limit ? \
(int) (unsigned char) (*(stream)->__bufp++ = (unsigned char) (c)) : \
((stream)->__linebuf && (c) != '\n' && \
(stream)->__bufp - (stream)->__buffer < (stream)->__bufsize) ? \
(*(stream)->__bufp++ = (unsigned char) (c)) : \
__flshfp((stream), (unsigned char) (c)))
/* The C standard explicitly says this can be a macro,
so we always do the optimization for it. */
#define putc(c, stream) __putc((c), (stream))
#ifdef __OPTIMIZE__
#define putchar(c) __putc((c), stdout)
#endif /* Optimizing. */
#if defined(__USE_SVID) || defined(__USE_MISC)
/* Get a word (int) from STREAM. */
extern int EXFUN(getw, (FILE *__stream));
/* Write a word (int) to STREAM. */
extern int EXFUN(putw, (int __w, FILE *__stream));
#endif
/* Get a newline-terminated string of finite length from STREAM. */
extern char *EXFUN(fgets, (char *__s, size_t __n, FILE *__stream));
/* Get a newline-terminated string from stdin, removing the newline.
DO NOT USE THIS FUNCTION!! There is no limit on how much it will read. */
extern char *EXFUN(gets, (char *__s));
#ifdef __USE_GNU
#include <sys/types.h>
/* Read up to (and including) a DELIMITER from STREAM into *LINEPTR
(and null-terminate it). *LINEPTR is a pointer returned from malloc (or
NULL), pointing to *N characters of space. It is realloc'd as
necessary. Returns the number of characters read (not including the
null terminator), or -1 on error or EOF. */
ssize_t EXFUN(__getdelim, (char **lineptr, size_t *n,
int delimiter, FILE *stream));
ssize_t EXFUN(getdelim, (char **lineptr, size_t *n,
int delimiter, FILE *stream));
/* Like `getdelim', but reads up to a newline. */
ssize_t EXFUN(__getline, (char **lineptr, size_t *n, FILE *stream));
ssize_t EXFUN(getline, (char **lineptr, size_t *n, FILE *stream));
#ifdef __OPTIMIZE__
#define getdelim(l, n, d, s) __getdelim ((l), (n), (d), (s))
#define getline(l, n, s) __getline ((l), (n), (s))
#define __getline(l, n, stream) __getdelim ((l), (n), '\n', (stream))
#endif /* Optimizing. */
#endif
/* Write a string to STREAM. */
extern int EXFUN(fputs, (CONST char *__s, FILE *__stream));
/* Write a string, followed by a newline, to stdout. */
extern int EXFUN(puts, (CONST char *__s));
#ifdef __OPTIMIZE__
#define puts(s) ((fputs((s), stdout) || __putc('\n', stdout) == EOF) ? EOF : 0)
#endif /* Optimizing. */
/* Push a character back onto the input buffer of STREAM. */
extern int EXFUN(ungetc, (int __c, FILE *__stream));
/* Read chunks of generic data from STREAM. */
extern size_t EXFUN(fread, (PTR __ptr, size_t __size,
size_t __n, FILE *__stream));
/* Write chunks of generic data to STREAM. */
extern size_t EXFUN(fwrite, (CONST PTR __ptr, size_t __size,
size_t __n, FILE *__s));
/* Seek to a certain position on STREAM. */
extern int EXFUN(fseek, (FILE *__stream, long int __off, int __whence));
/* Return the current position of STREAM. */
extern long int EXFUN(ftell, (FILE *__stream));
/* Rewind to the beginning of STREAM. */
extern void EXFUN(rewind, (FILE *__stream));
/* Get STREAM's position. */
extern int EXFUN(fgetpos, (FILE *__stream, fpos_t *__pos));
/* Set STREAM's position. */
extern int EXFUN(fsetpos, (FILE *__stream, CONST fpos_t *__pos));
/* Clear the error and EOF indicators for STREAM. */
extern void EXFUN(clearerr, (FILE *__stream));
/* Return the EOF indicator for STREAM. */
extern int EXFUN(feof, (FILE *__stream));
/* Return the error indicator for STREAM. */
extern int EXFUN(ferror, (FILE *__stream));
#ifdef __OPTIMIZE__
#define feof(stream) ((stream)->__eof != 0)
#define ferror(stream) ((stream)->__error != 0)
#endif /* Optimizing. */
/* Print a message describing the meaning of the value of errno. */
extern void EXFUN(perror, (CONST char *__s));
#ifdef __USE_MISC
/* Print a message describing the meaning of the given signal number. */
extern void EXFUN(psignal, (int __sig, CONST char *__s));
#endif /* Non strict ANSI and not POSIX only. */
#ifdef __USE_POSIX
/* Return the system file descriptor for STREAM. */
extern int EXFUN(fileno, (CONST FILE *__stream));
#ifdef __OPTIMIZE__
/* The `+ 0' makes this not be an lvalue, so it can't be changed. */
#define fileno(stream) ((stream)->__fileno + 0)
#endif /* Optimizing. */
#endif /* Use POSIX. */
#if (defined (__USE_POSIX2) || defined(__USE_SVID) || defined(__USE_BSD) || \
defined(__USE_MISC))
/* Create a new stream connected to a pipe running the given command. */
extern FILE *EXFUN(popen, (CONST char *__command, CONST char *__modes));
/* Close a stream opened by popen and return the status of its child. */
extern int EXFUN(pclose, (FILE *__stream));
#endif
#ifdef __USE_POSIX
/* Return the name of the controlling terminal. */
extern char *EXFUN(ctermid, (char *__s));
/* Return the name of the current user. */
extern char *EXFUN(cuserid, (char *__s));
#endif
#endif /* <stdio.h> included. */
#endif /* stdio.h */

View File

@@ -0,0 +1,29 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <gnu-stabs.h>
#ifdef HAVE_GNU_LD
symbol_alias(_sys_errlist, sys_errlist);
symbol_alias(_sys_nerr, sys_nerr);
symbol_alias(_sys_siglist, sys_siglist);
#endif

View File

@@ -0,0 +1,50 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Generate a unique temporary filename using up to five characters of PFX
if it is not NULL. The directory to put this file in is searched for
as follows: First the environment variable "TMPDIR" is checked.
If it contains the name of a writable directory, that directory is used.
If not and if DIR is not NULL, that value is checked. If that fails,
P_tmpdir is tried and finally "/tmp". The storage for the filename
is allocated by `malloc'. */
char *
DEFUN(tempnam, (dir, pfx), CONST char *dir AND CONST char *pfx)
{
size_t len;
register char *s;
register char *t = __stdio_gen_tempname(dir, pfx, 1, &len);
if (t == NULL)
return NULL;
s = (char *) malloc(len);
if (s == NULL)
return NULL;
(void) memcpy(s, t, len);
return s;
}

View File

@@ -0,0 +1,66 @@
#include <ansidecl.h>
#include <stdio.h>
#include <stdlib.h>
void
DEFUN(write_data, (stream), FILE *stream)
{
int i;
for (i=0; i<100; i++)
fprintf (stream, "%d\n", i);
if (ferror (stream)) {
fprintf (stderr, "Output to stream failed.\n");
exit (1);
}
}
void
DEFUN(read_data, (stream), FILE *stream)
{
int i, j;
for (i=0; i<100; i++)
{
if (fscanf (stream, "%d\n", &j) != 1 || j != i)
{
if (ferror (stream))
perror ("fscanf");
puts ("Test FAILED!");
exit (1);
}
}
}
int
DEFUN_VOID(main)
{
FILE *output, *input;
int status;
output = popen ("/bin/cat >tstpopen.tmp", "w");
if (output == NULL)
{
perror ("popen");
puts ("Test FAILED!");
exit (1);
}
write_data (output);
status = pclose (output);
fprintf (stderr, "pclose returned %d\n", status);
input = fopen ("tstpopen.tmp", "r");
if (input == NULL)
{
perror ("tstpopen.tmp");
puts ("Test FAILED!");
exit (1);
}
read_data (input);
(void) fclose (input);
puts (status ? "Test FAILED!" : "Test succeeded.");
exit (status);
}

View File

@@ -0,0 +1,129 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
DEFUN(main, (argc, argv), int argc AND char **argv)
{
static CONST char hello[] = "Hello, world.\n";
static CONST char replace[] = "Hewwo, world.\n";
static CONST size_t replace_from = 2, replace_to = 4;
char filename[FILENAME_MAX];
char *name = strrchr(*argv, '/');
char buf[BUFSIZ];
FILE *f;
int lose = 0;
if (name != NULL)
++name;
else
name = *argv;
(void) sprintf(filename, "/tmp/%s.test", name);
f = fopen(filename, "w+");
if (f == NULL)
{
perror(filename);
exit(1);
}
(void) fputs(hello, f);
rewind(f);
(void) fgets(buf, sizeof(buf), f);
rewind(f);
(void) fputs(buf, f);
rewind(f);
{
register size_t i;
for (i = 0; i < replace_from; ++i)
{
int c = getc(f);
if (c == EOF)
{
printf("EOF at %u.\n", i);
lose = 1;
break;
}
else if (c != hello[i])
{
printf("Got '%c' instead of '%c' at %u.\n",
(unsigned char) c, hello[i]);
lose = 1;
break;
}
}
}
{
long int where = ftell(f);
if (where == replace_from)
{
register size_t i;
for (i = replace_from; i < replace_to; ++i)
if (putc(replace[i], f) == EOF)
{
printf("putc('%c') got %s at %u.\n",
replace[i], strerror(errno), i);
lose = 1;
break;
}
}
else if (where == -1L)
{
printf("ftell got %s (should be at %u).\n",
strerror(errno), replace_from);
lose = 1;
}
else
{
printf("ftell returns %u; should be %u.\n", where, replace_from);
lose = 1;
}
}
if (!lose)
{
rewind(f);
if (fgets(buf, sizeof(buf), f) == NULL)
{
printf("fgets got %s.\n", strerror(errno));
lose = 1;
}
else if (strcmp(buf, replace))
{
printf("Read \"%s\" instead of \"%s\".\n", buf, replace);
lose = 1;
}
}
if (lose)
printf("Test FAILED! Losing file is \"%s\".\n", filename);
else
{
(void) remove(filename);
puts("Test succeeded.");
}
exit(lose ? EXIT_FAILURE : EXIT_SUCCESS);
}

View File

@@ -0,0 +1,44 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
/* This returns a new stream opened on a temporary file (generated
by tmpnam) The file is opened with mode "w+b" (binary read/write).
If we couldn't generate a unique filename or the file couldn't
be opened, NULL is returned. */
FILE *
DEFUN_VOID(tmpfile)
{
register char *filename;
register FILE *f;
filename = __stdio_gen_tempname((char *) NULL, "tmpf", 0, (size_t *) NULL);
if (filename == NULL)
return NULL;
f = fopen(filename, "w+b");
if (f == NULL)
return NULL;
/* Note that this relies on the Unix semantics that
a file is not really removed until it is closed. */
(void) remove(filename);
return f;
}

View File

@@ -0,0 +1,42 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/* Generate a unique filename in P_tmpdir. */
char *
DEFUN(tmpnam, (s), register char *s)
{
register char *t = __stdio_gen_tempname((CONST char *) NULL,
(CONST char *) NULL, 0,
(size_t *) NULL);
if (t == NULL)
return NULL;
if (s != NULL)
(void) strcpy(s, t);
else
s = t;
return s;
}

View File

@@ -0,0 +1,133 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#ifdef BSD
#include </usr/include/stdio.h>
#define EXIT_SUCCESS 0
#else
#include <stdio.h>
#include <stdlib.h>
#endif
void
DEFUN(fmtchk, (fmt), CONST char *fmt)
{
(void) fputs(fmt, stdout);
(void) printf(":\t`");
(void) printf(fmt, 0x12);
(void) printf("'\n");
}
void
DEFUN(fmtst1chk, (fmt), CONST char *fmt)
{
(void) fputs(fmt, stdout);
(void) printf(":\t`");
(void) printf(fmt, 4, 0x12);
(void) printf("'\n");
}
void
DEFUN(fmtst2chk, (fmt), CONST char *fmt)
{
(void) fputs(fmt, stdout);
(void) printf(":\t`");
(void) printf(fmt, 4, 4, 0x12);
(void) printf("'\n");
}
int
DEFUN_VOID(main)
{
static char shortstr[] = "Hi, Z.";
static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
I am ready for my first lesson today.";
fmtchk("%.4x");
fmtchk("%04x");
fmtchk("%4.4x");
fmtchk("%04.4x");
fmtchk("%4.3x");
fmtchk("%04.3x");
fmtst1chk("%.*x");
fmtst1chk("%0*x");
fmtst2chk("%*.*x");
fmtst2chk("%0*.*x");
#ifndef BSD
printf("bad format:\t\"%z\"\n");
printf("nil pointer:\t\"%p\"\n", (PTR) NULL);
#endif
printf("decimal negative:\t\"%d\"\n", -2345);
printf("octal negative:\t\"%o\"\n", -2345);
printf("hex negative:\t\"%x\"\n", -2345);
printf("long decimal number:\t\"%ld\"\n", -123456);
printf("long octal negative:\t\"%lo\"\n", -2345L);
printf("long unsigned decimal number:\t\"%lu\"\n", -123456);
printf("zero-padded LDN:\t\"%010ld\"\n", -123456);
printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
printf("space-padded LDN:\t\"%10ld\"\n", -123456);
printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456);
printf("zero-padded string:\t\"%010s\"\n", shortstr);
printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
printf("space-padded string:\t\"%10s\"\n", shortstr);
printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
printf("null string:\t\"%s\"\n", (char *)NULL);
printf("limited string:\t\"%.22s\"\n", longstr);
printf("e-style >= 1:\t\"%e\"\n", 12.34);
printf("e-style >= .1:\t\"%e\"\n", 0.1234);
printf("e-style < .1:\t\"%e\"\n", 0.001234);
printf("e-style big:\t\"%.60e\"\n", 1e20);
printf ("e-style == .1:\t\"%e\"\n", 0.1);
printf("f-style >= 1:\t\"%f\"\n", 12.34);
printf("f-style >= .1:\t\"%f\"\n", 0.1234);
printf("f-style < .1:\t\"%f\"\n", 0.001234);
printf("g-style >= 1:\t\"%g\"\n", 12.34);
printf("g-style >= .1:\t\"%g\"\n", 0.1234);
printf("g-style < .1:\t\"%g\"\n", 0.001234);
printf("g-style big:\t\"%.60g\"\n", 1e20);
printf (" %6.5lf\n", .099999999860301614);
printf (" %6.5lf\n", .1);
#define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
printf (FORMAT, 0.0, 0.0, 0.0);
printf (FORMAT, 1.0, 1.0, 1.0);
printf (FORMAT, -1.0, -1.0, -1.0);
printf (FORMAT, 100.0, 100.0, 100.0);
printf (FORMAT, 1000.0, 1000.0, 1000.0);
printf (FORMAT, 10000.0, 10000.0, 10000.0);
printf (FORMAT, 12345.0, 12345.0, 12345.0);
printf (FORMAT, 100000.0, 100000.0, 100000.0);
printf (FORMAT, 123456.0, 123456.0, 123456.0);
#undef FORMAT
{
char buf[20];
printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
}
exit(EXIT_SUCCESS);
}

View File

@@ -0,0 +1,46 @@
/* Copyright (C) 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdio.h>
int
DEFUN_VOID(main)
{
char *buf = NULL;
size_t size = 0;
ssize_t len;
while ((len = getline (&buf, &size, stdin)) != -1)
{
printf ("bufsize %u; read %d: ", size, len);
if (fwrite (buf, len, 1, stdout) != 1)
{
perror ("fwrite");
return 1;
}
}
if (ferror (stdin))
{
perror ("getline");
return 1;
}
return 0;
}

View File

@@ -0,0 +1,3 @@
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
z

View File

@@ -0,0 +1,100 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#ifdef BSD
#include </usr/include/stdio.h>
#else
#include <stdio.h>
#endif
#include <stdlib.h>
#include <string.h>
int
DEFUN(main, (argc, argv), int argc AND char **argv)
{
char buf[BUFSIZ];
FILE *in = stdin, *out = stdout;
if (argc == 2 && !strcmp (argv[1], "-opipe"))
{
out = popen ("/bin/cat", "w");
if (out == NULL)
{
perror ("popen: /bin/cat");
exit (EXIT_FAILURE);
}
}
else if (argc == 3 && !strcmp (argv[1], "-ipipe"))
{
sprintf (buf, "/bin/cat %s", argv[2]);
in = popen (buf, "r");
}
{
char name[50];
fprintf (out,
"sscanf (\"thompson\", \"%%s\", name) == %d, name == \"%s\"\n",
sscanf ("thompson", "%s", name),
name);
}
fputs ("Testing scanf (vfscanf)\n", out);
fputs ("Test 1:\n", out);
{
int n, i;
float x;
char name[50];
n = fscanf (in, "%d%f%s", &i, &x, name);
fprintf (out, "n = %d, i = %d, x = %f, name = \"%.50s\"\n",
n, i, x, name);
}
fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
fputs ("Test 2:\n", out);
{
int i;
float x;
char name[50];
(void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
}
fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
fputs ("Test 3:\n", out);
{
float quant;
char units[21], item[21];
while (!feof (in) && !ferror (in))
{
int count;
quant = 0.0;
units[0] = item[0] = '\0';
count = fscanf (in, "%f%20s of %20s", &quant, units, item);
(void) fscanf (in, "%*[^\n]");
fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
count, quant, item, units);
}
}
fprintf (out, "Residual: \"%s\"\n", fgets (buf, sizeof (buf), in));
if (out != stdout)
pclose (out);
exit(EXIT_SUCCESS);
}

View File

@@ -0,0 +1,7 @@
25 54.32E-1 thompson
56789 0123 56a72
2 quarts of oil
-12.8degrees Celsius
lots of luck
10.0LBS of fertilizer
100ergs of energy

View File

@@ -0,0 +1,52 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
/* Push the character C back onto the input stream of STREAM. */
int
DEFUN(ungetc, (c, stream), register int c AND register FILE *stream)
{
if (!__validfp(stream) || !stream->__mode.__read)
{
errno = EINVAL;
return EOF;
}
if (c == EOF)
return EOF;
if (stream->__pushed_back)
/* There is already a char pushed back. */
return EOF;
stream->__pushback = (unsigned char) c;
/* Tell __fillbf we've pushed back a char. */
stream->__pushed_back = 1;
stream->__pushback_bufp = stream->__bufp;
/* Make the next getc call __fillbf. It will return C. */
stream->__bufp = stream->__get_limit;
/* We just gave it another character to read, so it's not at EOF. */
stream->__eof = 0;
return stream->__pushback;
}

View File

@@ -0,0 +1,85 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Enlarge STREAM's buffer. */
static void
DEFUN(enlarge_buffer, (stream, c),
register FILE *stream AND int c)
{
ptrdiff_t bufp_offset = stream->__bufp - stream->__buffer;
char *newbuf;
stream->__bufsize += 100;
newbuf = (char *) realloc ((PTR) stream->__buffer, stream->__bufsize);
if (newbuf == NULL)
{
free ((PTR) stream->__buffer);
stream->__buffer = stream->__bufp
= stream->__put_limit = stream->__get_limit = NULL;
stream->__error = 1;
}
else
{
stream->__buffer = newbuf;
stream->__bufp = stream->__buffer + bufp_offset;
stream->__get_limit = stream->__put_limit;
stream->__put_limit = stream->__buffer + stream->__bufsize;
if (c != EOF)
*stream->__bufp++ = (unsigned char) c;
}
}
/* Write formatted output from FORMAT to a string which is
allocated with malloc and stored in *STRING_PTR. */
int
DEFUN(vasprintf, (string_ptr, format, args),
char **string_ptr AND CONST char *format AND PTR args)
{
FILE f;
int done;
memset((PTR) &f, 0, sizeof(f));
f.__magic = _IOMAGIC;
f.__bufsize = 100;
f.__buffer = (char *) malloc(f.__bufsize);
if (f.__buffer == NULL)
return -1;
f.__bufp = f.__buffer;
f.__put_limit = f.__buffer + f.__bufsize;
f.__mode.__write = 1;
f.__room_funcs.__output = enlarge_buffer;
done = vfprintf(&f, format, args);
if (done < 0)
return done;
*string_ptr = realloc (f.__buffer, (f.__bufp - f.__buffer) + 1);
if (*string_ptr == NULL)
*string_ptr = f.__buffer;
(*string_ptr)[f.__bufp - f.__buffer] = '\0';
return done;
}

View File

@@ -0,0 +1,52 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
/* Write formatted output to file descriptor D according to the format string
FORMAT, using the argument list in ARG. */
int
DEFUN(vdprintf, (d, format, arg),
int d AND CONST char *format AND PTR arg)
{
int done;
FILE f;
/* Create an unbuffered stream talking to D on the stack. */
memset ((PTR) &f, 0, sizeof(f));
f.__magic = _IOMAGIC;
f.__mode.__write = 1;
f.__fileno = d;
f.__cookie = (PTR) &f.__fileno;
f.__room_funcs = __default_room_functions;
f.__io_funcs = __default_io_functions;
f.__seen = 1;
f.__userbuf = 1;
/* vfprintf will use a buffer on the stack for the life of the call,
and flush it when finished. */
done = vfprintf (&f, format, arg);
return done;
}

View File

@@ -0,0 +1,669 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <localeinfo.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <printf.h>
/* If it's an unbuffered stream that we provided
temporary buffering for, remove that buffering. */
#define RETURN(x) \
do \
{ \
done = (x); \
goto do_return; \
} while (0)
#define outchar(x) \
do \
{ \
register CONST int outc = (x); \
if (putc(outc, s) == EOF) \
RETURN(-1); \
else \
++done; \
} while (0)
/* Cast the next arg, of type ARGTYPE, into CASTTYPE, and put it in VAR. */
#define castarg(var, argtype, casttype) \
var = (casttype) va_arg(args, argtype)
/* Get the next arg, of type TYPE, and put it in VAR. */
#define nextarg(var, type) castarg(var, type, type)
static printf_function printf_unknown;
extern printf_function **__printf_function_table;
#ifdef __GNUC__
#define HAVE_LONGLONG
#define LONGLONG long long
#else
#define LONGLONG long
#endif
int
DEFUN(vfprintf, (s, format, args),
register FILE *s AND CONST char *format AND PTR argptr)
{
va_list args = (va_list) argptr;
/* Lower-case digits. */
static CONST char lower_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
/* Upper-case digits. */
static CONST char upper_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* Base-36 digits for numbers. */
CONST char *digits = lower_digits;
/* Pointer into the format string. */
register CONST char *f;
/* Number of characters written. */
register size_t done = 0;
/* Nonzero we're providing buffering. */
char our_buffer;
/* Temporary buffer for unbuffered streams. */
char temporary_buffer[BUFSIZ];
if (!__validfp(s) || !s->__mode.__write || format == NULL)
{
errno = EINVAL;
return -1;
}
if (!s->__seen)
{
if (__flshfp(s, EOF) == EOF)
return EOF;
}
our_buffer = s->__buffer == NULL;
if (our_buffer)
{
/* If it's an unbuffered stream, buffer it
at least inside this function call. */
s->__bufp = s->__buffer = temporary_buffer;
s->__bufsize = sizeof(temporary_buffer);
s->__put_limit = s->__buffer + s->__bufsize;
s->__get_limit = s->__buffer;
}
/* Reset multibyte characters to their initial state. */
(void) mblen((char *) NULL, 0);
f = format;
while (*f != '\0')
{
/* Type modifiers. */
char is_short, is_long, is_long_double;
#ifdef HAVE_LONGLONG
/* We use the `L' modifier for `long long int'. */
#define is_longlong is_long_double
#else
#define is_longlong 0
#endif
/* Format spec modifiers. */
char space, showsign, left, alt;
/* Padding character: ' ' or '0'. */
char pad;
/* Width of a field. */
register int width;
/* Precision of a field. */
int prec;
/* Decimal integer is negative. */
char is_neg;
/* Current character of the format. */
char fc;
/* Base of a number to be written. */
int base;
/* Integral values to be written. */
unsigned LONGLONG int num;
LONGLONG int signed_num;
/* Auxiliary function to do output. */
printf_function *function;
if (!isascii(*f))
{
/* Non-ASCII, may be a multibyte. */
int len = mblen(f, strlen(f));
if (len > 0)
{
while (len-- > 0)
outchar(*f++);
continue;
}
}
if (*f != '%')
{
/* This isn't a format spec, so write
everything out until the next one. */
CONST char *next = strchr(f + 1, '%');
if (next == NULL)
next = strchr(f + 1, '\0');
if (next - f > 20)
{
size_t written = fwrite((PTR) f, 1, next - f, s);
done += written;
if (written != next - f)
break;
f += written;
}
else
while (f < next)
outchar(*f++);
continue;
}
++f;
/* Check for "%%". Note that although the ANSI standard lists
'%' as a conversion specifier, it says "The complete format
specification shall be `%%'," so we can avoid all the width
and precision processing. */
if (*f == '%')
{
++f;
outchar('%');
continue;
}
/* Check for spec modifiers. */
space = showsign = left = alt = 0;
pad = ' ';
while (*f == ' ' || *f == '+' || *f == '-' || *f == '#' || *f == '0')
switch (*f++)
{
case ' ':
/* Output a space in place of a sign, when there is no sign. */
space = 1;
break;
case '+':
/* Always output + or - for numbers. */
showsign = 1;
break;
case '-':
/* Left-justify things. */
left = 1;
break;
case '#':
/* Use the "alternate form":
Hex has 0x or 0X, FP always has a decimal point. */
alt = 1;
break;
case '0':
/* Pad with 0s. */
pad = '0';
break;
}
if (left)
pad = ' ';
/* Get the field width. */
width = 0;
if (*f == '*')
{
/* The field width is given in an argument.
A negative field width indicates left justification. */
nextarg(width, int);
if (width < 0)
{
width = - width;
left = 1;
}
++f;
}
else
while (isdigit(*f))
{
width *= 10;
width += *f++ - '0';
}
/* Get the precision. */
/* -1 means none given; 0 means explicit 0. */
prec = -1;
if (*f == '.')
{
++f;
if (*f == '*')
{
/* The precision is given in an argument. */
nextarg(prec, int);
/* Avoid idiocy. */
if (prec < 0)
prec = -1;
++f;
}
else if (isdigit(*f))
{
prec = 0;
while (*f != '\0' && isdigit(*f))
{
prec *= 10;
prec += *f++ - '0';
}
}
}
/* Check for type modifiers. */
is_short = is_long = is_long_double = 0;
while (*f == 'h' || *f == 'l' || *f == 'L')
switch (*f++)
{
case 'h':
/* int's are short int's. */
is_short = 1;
break;
case 'l':
#ifdef HAVE_LONGLONG
if (is_long)
/* A double `l' is equivalent to an `L'. */
is_longlong = 1;
else
#endif
/* int's are long int's. */
is_long = 1;
break;
case 'L':
/* double's are long double's, and int's are long long int's. */
is_long_double = 1;
break;
}
/* Format specification. */
fc = *f++;
function = (__printf_function_table == NULL ? NULL :
__printf_function_table[fc]);
if (function == NULL)
switch (fc)
{
case 'i':
case 'd':
/* Decimal integer. */
base = 10;
if (is_longlong)
nextarg(signed_num, LONGLONG int);
else if (is_long)
nextarg(signed_num, long int);
else if (!is_short)
castarg(signed_num, int, long int);
else
castarg(signed_num, int, short int);
is_neg = signed_num < 0;
num = is_neg ? (- signed_num) : signed_num;
goto number;
case 'Z':
/* `size_t' value. */
#ifdef HAVE_LONGLONG
if (sizeof(size_t) > sizeof(unsigned long long int))
__libc_fatal("`size_t' is bigger than any known type!");
else
is_longlong = sizeof(size_t) > sizeof(unsigned long int);
#endif
is_long = sizeof(size_t) > sizeof(unsigned int);
/* Fall through, to print a `size_t' as a decimal integer. */
case 'u':
/* Decimal unsigned integer. */
base = 10;
goto unsigned_number;
case 'o':
/* Octal unsigned integer. */
base = 8;
goto unsigned_number;
case 'X':
/* Hexadecimal unsigned integer. */
case 'x':
/* Hex with lower-case digits. */
digits = fc == 'X' ? upper_digits : lower_digits;
base = 16;
unsigned_number:;
/* Unsigned number of base BASE. */
if (is_longlong)
castarg(num, LONGLONG int, unsigned LONGLONG int);
else if (is_long)
castarg(num, long int, unsigned long int);
else if (!is_short)
castarg(num, int, unsigned int);
else
castarg(num, int, unsigned short int);
is_neg = 0;
number:;
/* Number of base BASE. */
{
char work[BUFSIZ];
char *CONST workend = &work[sizeof(work) - 1];
register char *w;
/* Supply a default precision if none was given. */
if (prec == -1)
prec = 1;
/* Put the number in WORK. */
w = workend;
while (num > 0)
{
*w-- = digits[num % base];
num /= base;
}
width -= workend - w;
prec -= workend - w;
if (alt && base == 8 && prec <= 0)
{
*w-- = '0';
--width;
}
if (prec > 0)
{
width -= prec;
while (prec-- > 0)
*w-- = '0';
}
if (alt && base == 16)
width -= 2;
if (is_neg || showsign || space)
--width;
if (!left && pad == ' ')
while (width-- > 0)
outchar(' ');
if (is_neg)
outchar('-');
else if (showsign)
outchar('+');
else if (space)
outchar(' ');
if (!left && pad == '0')
while (width-- > 0)
outchar('0');
if (alt && base == 16)
{
outchar ('0');
outchar (fc);
}
/* Write the number. */
while (++w <= workend)
outchar(*w);
if (left)
while (width-- > 0)
outchar(' ');
}
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
{
/* Floating-point number. */
extern printf_function __printf_fp;
function = __printf_fp;
goto use_function;
}
case 'c':
/* Character. */
nextarg(num, int);
if (left)
while (--width > 0)
outchar(' ');
outchar((unsigned char) num);
if (!left)
while (--width > 0)
outchar(' ');
break;
case 's':
/* String. */
{
static CONST char null[] = "(null)";
CONST char *str;
size_t len;
nextarg(str, CONST char *);
if (str == NULL)
/* Write "(null)" if there's space. */
if (prec == -1 || prec >= (int) sizeof(null) - 1)
{
str = null;
len = sizeof(null) - 1;
}
else
{
str = "";
len = 0;
}
else
len = strlen(str);
if (prec != -1 && (size_t) prec < len)
len = prec;
width -= len;
if (!left)
while (width-- > 0)
outchar(' ');
if (len < 20)
while (len-- > 0)
outchar(*str++);
else
if (fwrite(str, 1, len, s) != len)
RETURN(-1);
else
done += len;
if (left)
while (width-- > 0)
outchar(' ');
}
break;
case 'p':
/* Generic pointer. */
{
CONST PTR ptr;
nextarg(ptr, CONST PTR);
if (ptr != NULL)
{
/* If the pointer is not NULL, write it as a %#x spec. */
base = 16;
fc = 'x';
alt = 1;
num = (unsigned LONGLONG int) (unsigned long int) ptr;
is_neg = 0;
goto number;
}
else
{
/* Write "(nil)" for a nil pointer. */
static CONST char nil[] = "(nil)";
register CONST char *p;
width -= sizeof(nil) - 1;
if (left)
while (width-- > 0)
outchar(' ');
for (p = nil; *p != '\0'; ++p)
outchar(*p);
if (!left)
while (width-- > 0)
outchar(' ');
}
}
break;
case 'n':
/* Answer the count of characters written. */
if (is_longlong)
{
LONGLONG int *p;
nextarg(p, LONGLONG int *);
*p = done;
}
else if (is_long)
{
long int *p;
nextarg(p, long int *);
*p = done;
}
else if (!is_short)
{
int *p;
nextarg(p, int *);
*p = done;
}
else
{
short int *p;
nextarg(p, short int *);
*p = done;
}
break;
default:
/* Unrecognized format specifier. */
function = printf_unknown;
goto use_function;
}
else
use_function:
{
int function_done;
struct printf_info info;
info.prec = prec;
info.width = width;
info.spec = fc;
info.is_long_double = is_long_double;
info.is_short = is_short;
info.is_long = is_long;
info.alt = alt;
info.space = space;
info.left = left;
info.showsign = showsign;
info.pad = pad;
function_done = (*function)(s, &info, &args);
if (function_done < 0)
RETURN(-1);
done += function_done;
}
}
do_return:;
if (our_buffer)
{
if (fflush(s) == EOF)
return -1;
s->__buffer = s->__bufp = s->__get_limit = s->__put_limit = NULL;
s->__bufsize = 0;
}
return done;
}
#undef RETURN
#define RETURN return
static int
DEFUN(printf_unknown, (s, type, info, arg),
FILE *s AND CONST struct printf_info *info AND va_list *arg)
{
int done = 0;
char work[BUFSIZ];
char *CONST workend = &work[sizeof(work) - 1];
register char *w;
register int prec = info->prec, width = info->width;
outchar('%');
if (info->alt)
outchar('#');
if (info->showsign)
outchar('+');
else if (info->space)
outchar(' ');
if (info->left)
outchar('-');
if (info->pad == '0')
outchar('0');
w = workend;
while (width > 0)
{
*w-- = '0' + (width % 10);
width /= 10;
}
while (++w <= workend)
outchar(*w);
if (info->prec != -1)
{
outchar('.');
w = workend;
while (prec > 0)
{
*w-- = '0' + (prec % 10);
prec /= 10;
}
while (++w <= workend)
outchar(*w);
}
outchar(info->spec);
return done;
}

View File

@@ -0,0 +1,31 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
#undef vfscanf
int
DEFUN(vfscanf, (s, format, arg),
FILE *s AND CONST char *format AND PTR arg)
{
return __vfscanf(s, format, arg);
}

View File

@@ -0,0 +1,33 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#undef __OPTIMIZE__ /* Avoid inline `vprintf' function. */
#include <stdio.h>
#undef vprintf
/* Write formatted output to stdout according to the
format string FORMAT, using the argument list in ARG. */
int
DEFUN(vprintf, (format, arg), CONST char *format AND PTR arg)
{
return vfprintf (stdout, format, arg);
}

View File

@@ -0,0 +1,32 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
#undef vscanf
/* Read formatted input from stdin according to the format
string in FORMAT, using the argument list in ARG. */
int
DEFUN(vscanf, (format, arg), CONST char *format AND PTR arg)
{
return vfscanf (stdin, format, arg);
}

View File

@@ -0,0 +1,56 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
/*
* Write formatted output to S according to the format string
* FORMAT, using the argument list in ARG, writing no more
* than MAXLEN characters.
*/
int
DEFUN(vsnprintf, (s, maxlen, format, arg),
char *s AND size_t maxlen AND CONST char *format AND PTR arg)
{
int done;
FILE f;
memset((PTR) &f, 0, sizeof(f));
f.__magic = _IOMAGIC;
f.__mode.__write = 1;
/* The buffer size is one less than MAXLEN
so we have space for the null terminator. */
f.__bufp = f.__buffer = (char *) s;
f.__bufsize = maxlen - 1;
f.__put_limit = f.__buffer + f.__bufsize;
f.__get_limit = f.__buffer;
/* After the buffer is full (MAXLEN characters have been written),
any more characters written will go to the bit bucket. */
f.__room_funcs = __default_room_functions;
f.__io_funcs.__write = NULL;
f.__seen = 1;
done = vfprintf(&f, format, arg);
*f.__bufp = '\0';
return done;
}

View File

@@ -0,0 +1,50 @@
/* Copyright (C) 1991 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
/* Write formatted output to S according to the format string
FORMAT, using the argument list in ARG. */
int
DEFUN(vsprintf, (s, format, arg),
char *s AND CONST char *format AND PTR arg)
{
int done;
FILE f;
memset((PTR) &f, 0, sizeof(f));
f.__magic = _IOMAGIC;
f.__mode.__write = 1;
f.__bufp = f.__buffer = (char *) s;
f.__put_limit = (char *) ULONG_MAX;
f.__bufsize = (size_t) (f.__put_limit - f.__bufp);
f.__get_limit = f.__buffer;
f.__room_funcs.__output = NULL;
f.__seen = 1;
done = vfprintf(&f, format, arg);
*f.__bufp = '\0';
return done;
}

View File

@@ -0,0 +1,30 @@
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <stdarg.h>
#include <stdio.h>
#undef vsscanf
#include <gnu-stabs.h>
function_alias(vsscanf, __vsscanf, int, (s, format, arg),
DEFUN(vsscanf, (s, format, arg),
CONST char *s AND CONST char *format AND PTR arg))