mirror of
https://github.com/GnoConsortium/gno.git
synced 2025-03-29 08:32:41 +00:00
Replace ORCALib stdio implementation with BSD implementation.
This commit is contained in:
parent
ca13d459c1
commit
5c7af603fd
@ -1,11 +1,58 @@
|
||||
#
|
||||
# $Id: Makefile,v 1.3 1997/07/27 23:09:09 gdr Exp $
|
||||
# gno/lib/libc/stdio/Makefile
|
||||
#
|
||||
# Devin Reade, 1997
|
||||
#
|
||||
# $Id: Makefile,v 1.4 1997/09/05 06:18:46 gdr Exp $
|
||||
#
|
||||
|
||||
.INCLUDE: ../../../paths.mk
|
||||
.INCLUDE: ../../const.mk
|
||||
|
||||
OBJS = fdopen.o fgetln.o mktemp.o perror.o tempnam.o
|
||||
OBJS = clrerr.o fclose.o fdopen.o feof.o ferror.o fflush.o fgetc.o \
|
||||
fgetln.o fgetpos.o fgets.o fileno.o findfp.o flags.o fopen.o \
|
||||
fprintf.o fpurge.o fputc.o fputs.o fread.o freopen.o fscanf.o \
|
||||
fseek.o fsetpos.o ftell.o funopen.o fvwrite.o fwalk.o fwrite.o \
|
||||
getc.o getchar.o gets.o getw.o makebuf.o mktemp.o perror.o printf.o \
|
||||
putc.o putchar.o puts.o putw.o refill.o remove.o rewind.o rget.o \
|
||||
scanf.o setbuf.o setbuffer.o setvbuf.o snprintf.o sprintf.o sscanf.o \
|
||||
stdio.o tempnam.o tmpfile.o tmpnam.o ungetc.o vfprintf.o vfscanf.o \
|
||||
vprintf.o vscanf.o vsnprintf.o vsprintf.o vsscanf.o wbuf.o wsetup.o
|
||||
|
||||
default: $(OBJS)
|
||||
|
||||
.INCLUDE: ../rules.mk
|
||||
|
||||
# Additional dependancies:
|
||||
fclose.o:: local.h
|
||||
fdopen.o:: local.h
|
||||
fflush.o:: local.h
|
||||
fgetln.o:: local.h
|
||||
fgets.o:: local.h
|
||||
findfp.o:: local.h glue.h
|
||||
fopen.o:: local.h
|
||||
fpurge.o:: local.h
|
||||
fputs.o:: fvwrite.h
|
||||
fread.o:: local.h
|
||||
freopen.o:: local.h
|
||||
fseek.o:: local.h
|
||||
ftell.o:: local.h
|
||||
funopen.o:: local.h
|
||||
fvwrite.o:: local.h fvwrite.h
|
||||
fwalk.o:: local.h glue.h
|
||||
fwrite.o:: local.h fvwrite.h
|
||||
makebuf.o:: local.h
|
||||
puts.o:: fvwrite.h
|
||||
putw.o:: fvwrite.h
|
||||
refill.o:: local.h
|
||||
rget.o:: local.h
|
||||
setbuf.o:: local.h
|
||||
setvbuf.o:: local.h
|
||||
sprintf.o:: local.h
|
||||
sscanf.o:: local.h
|
||||
stdio.o:: local.h
|
||||
ungetc.o:: local.h
|
||||
vfprintf.o:: local.h fvwrite.h floatio.h
|
||||
vfscanf.o:: local.h floatio.h
|
||||
wbuf.o:: local.h
|
||||
wsetup.o:: local.h
|
||||
|
52
lib/libc/stdio/clrerr.c
Normal file
52
lib/libc/stdio/clrerr.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)clrerr.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#undef clearerr
|
||||
|
||||
void
|
||||
clearerr(FILE *fp)
|
||||
{
|
||||
__sclearerr(fp);
|
||||
}
|
71
lib/libc/stdio/fclose.c
Normal file
71
lib/libc/stdio/fclose.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fclose.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fclose(register FILE *fp)
|
||||
{
|
||||
register int r;
|
||||
|
||||
if (fp->_flags == 0) { /* not open! */
|
||||
errno = EBADF;
|
||||
return (EOF);
|
||||
}
|
||||
r = fp->_flags & __SWR ? __sflush(fp) : 0;
|
||||
if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
|
||||
r = EOF;
|
||||
if (fp->_flags & __SMBF)
|
||||
free((char *)fp->_bf._base);
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
if (HASLB(fp))
|
||||
FREELB(fp);
|
||||
fp->_flags = 0; /* Release this FILE for reuse. */
|
||||
fp->_r = fp->_w = 0; /* Mess up if reaccessed. */
|
||||
return (r);
|
||||
}
|
@ -1,154 +1,96 @@
|
||||
/*
|
||||
* fdopen(3) implementation.
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Devin Reade, April 1997
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* This file is formatted with tab stops every 8 columns
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* $Id: fdopen.c,v 1.1 1997/07/27 23:15:09 gdr Exp $
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#pragma optimize 0
|
||||
#pragma debug 0
|
||||
#pragma memorymodel 0
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fdopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
FILE *
|
||||
fdopen(int fildes, const char *cmode)
|
||||
fdopen(int fd, const char *mode)
|
||||
{
|
||||
FILE *result;
|
||||
struct stat *statbuf;
|
||||
unsigned int mode, whence, isBinary;
|
||||
register FILE *fp;
|
||||
static int nofile;
|
||||
int flags, oflags, fdflags, tmp;
|
||||
|
||||
/* allocate buffers */
|
||||
if ((statbuf = malloc(sizeof(struct stat))) == NULL) {
|
||||
goto fail1;
|
||||
}
|
||||
if ((result = malloc(sizeof(FILE))) == NULL) {
|
||||
goto fail2;
|
||||
}
|
||||
if ((result->_base = malloc(BUFSIZ)) == NULL) {
|
||||
goto fail3;
|
||||
}
|
||||
if (nofile == 0)
|
||||
nofile = getdtablesize();
|
||||
|
||||
/* stat the file descriptor */
|
||||
if ((fstat(fildes, statbuf)) == -1) {
|
||||
goto fail4;
|
||||
}
|
||||
if ((flags = __sflags(mode, &oflags)) == 0)
|
||||
return (NULL);
|
||||
|
||||
/* extract the mode */
|
||||
result->_flag = _IOFBF | _IOMYBUF;
|
||||
mode = 0;
|
||||
whence = SEEK_SET;
|
||||
isBinary = 0;
|
||||
switch(*cmode) {
|
||||
case 'r':
|
||||
mode = _IOREAD;
|
||||
break;
|
||||
case 'a':
|
||||
whence = SEEK_END;
|
||||
/*FALLTHROUGH*/
|
||||
case 'w':
|
||||
mode = _IOWRT;
|
||||
break;
|
||||
default:
|
||||
/* Make sure the mode the user wants is a subset of the actual mode. */
|
||||
#ifdef __ORCAC__
|
||||
/* ORCA/C: Provide only required args to variadic functions */
|
||||
if ((fdflags = fcntl(fd, F_GETFL)) < 0)
|
||||
#else
|
||||
if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
|
||||
#endif
|
||||
return (NULL);
|
||||
tmp = fdflags & O_ACCMODE;
|
||||
if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) {
|
||||
errno = EINVAL;
|
||||
goto fail4;
|
||||
return (NULL);
|
||||
}
|
||||
cmode++;
|
||||
switch (*cmode) {
|
||||
case 0:
|
||||
break;
|
||||
case 'b':
|
||||
isBinary = 1;
|
||||
cmode++;
|
||||
switch(*cmode) {
|
||||
case 0:
|
||||
break;
|
||||
case '+':
|
||||
mode = _IORW;
|
||||
break;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
goto fail4;
|
||||
}
|
||||
case '+':
|
||||
mode = _IORW;
|
||||
/*
|
||||
* We don't document this behavior, but we will allow
|
||||
* the 'b' to come in the third position as well, since
|
||||
* it appears that this is the case for the ORCA fopen
|
||||
* implementation.
|
||||
*/
|
||||
cmode++;
|
||||
if (*cmode == 'b') {
|
||||
isBinary = 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
goto fail4;
|
||||
}
|
||||
if (!isBinary) {
|
||||
result->_flag |= _IOTEXT;
|
||||
}
|
||||
result->_flag |= mode;
|
||||
|
||||
if ((fp = __sfp()) == NULL)
|
||||
return (NULL);
|
||||
fp->_flags = flags;
|
||||
/*
|
||||
* There is a bug in the beta v2.0.6 kernel that, when stat'ing
|
||||
* a character special device, only the S_IFCHR bit is set. A
|
||||
* similar situation is occuring for pipes/sockets.
|
||||
* If opened for appending, but underlying descriptor does not have
|
||||
* O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
|
||||
* end before each write.
|
||||
*/
|
||||
if (S_ISSOCK(statbuf->st_mode) || S_ISCHR(statbuf->st_mode)) {
|
||||
goto pass;
|
||||
}
|
||||
|
||||
/* verify that the req mode agrees with the file descriptor mode */
|
||||
if ((result->_flag & (_IORW | _IOWRT)) &&
|
||||
!(statbuf->st_mode & S_IWUSR)) {
|
||||
errno = EPERM;
|
||||
goto fail4;
|
||||
}
|
||||
if ((result->_flag & (_IORW | _IOREAD)) &&
|
||||
!(statbuf->st_mode & S_IRUSR)) {
|
||||
errno = EPERM;
|
||||
goto fail4;
|
||||
}
|
||||
if (lseek(fildes, 0L, whence) == -1) {
|
||||
if (errno != ENOTBLK) {
|
||||
goto fail4;
|
||||
}
|
||||
}
|
||||
|
||||
/* set up remaining FILE struct members */
|
||||
pass:
|
||||
result->next = stderr->next;
|
||||
stderr->next = result;
|
||||
result->_ptr = result->_base;
|
||||
result->_end = NULL;
|
||||
result->_size = BUFSIZ;
|
||||
result->_cnt = 0L;
|
||||
result->_pbk[0] = -1;
|
||||
result->_pbk[1] = -1;
|
||||
result->_file = fildes;
|
||||
return result;
|
||||
|
||||
fail4:
|
||||
free(result->_base);
|
||||
fail3:
|
||||
free(result);
|
||||
fail2:
|
||||
free(statbuf);
|
||||
fail1:
|
||||
return NULL;
|
||||
if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
|
||||
fp->_flags |= __SAPP;
|
||||
fp->_file = fd;
|
||||
fp->_cookie = fp;
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
return (fp);
|
||||
}
|
||||
|
56
lib/libc/stdio/feof.c
Normal file
56
lib/libc/stdio/feof.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)feof.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro feof.
|
||||
*/
|
||||
#undef feof
|
||||
|
||||
int
|
||||
feof(FILE *fp)
|
||||
{
|
||||
return (__sfeof(fp));
|
||||
}
|
56
lib/libc/stdio/ferror.c
Normal file
56
lib/libc/stdio/ferror.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)ferror.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro ferror.
|
||||
*/
|
||||
#undef ferror
|
||||
|
||||
int
|
||||
ferror(FILE *fp)
|
||||
{
|
||||
return (__sferror(fp));
|
||||
}
|
111
lib/libc/stdio/fflush.c
Normal file
111
lib/libc/stdio/fflush.c
Normal file
@ -0,0 +1,111 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fflush.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fflush.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/* Flush a single file, or (if fp is NULL) all files. */
|
||||
int
|
||||
fflush(register FILE *fp)
|
||||
{
|
||||
|
||||
if (fp == NULL)
|
||||
return (_fwalk(__sflush));
|
||||
if ((fp->_flags & (__SWR | __SRW)) == 0) {
|
||||
errno = EBADF;
|
||||
return (EOF);
|
||||
}
|
||||
return (__sflush(fp));
|
||||
}
|
||||
|
||||
int
|
||||
__sflush(register FILE *fp)
|
||||
{
|
||||
register unsigned char *p;
|
||||
register int n, t;
|
||||
#ifdef __GNO__
|
||||
register char *nl_p, *nl_q;
|
||||
#endif
|
||||
|
||||
t = fp->_flags;
|
||||
if ((t & __SWR) == 0)
|
||||
return (0);
|
||||
|
||||
if ((p = fp->_bf._base) == NULL)
|
||||
return (0);
|
||||
|
||||
n = fp->_p - p; /* write this much */
|
||||
|
||||
/*
|
||||
* Set these immediately to avoid problems with longjmp and to allow
|
||||
* exchange buffering (via setvbuf) in user write function.
|
||||
*/
|
||||
fp->_p = p;
|
||||
fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
|
||||
|
||||
#ifdef __GNO__
|
||||
/* newline conversion -- THIS MODIFIES THE BUFFER */
|
||||
if ((fp->_flags & __SBIN) == 0) {
|
||||
nl_p = p;
|
||||
nl_q = p + n;
|
||||
while (nl_p < nl_q) {
|
||||
if (*nl_p == '\n') {
|
||||
*nl_p = '\r';
|
||||
}
|
||||
nl_p++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
for (; n > 0; n -= t, p += t) {
|
||||
t = (*fp->_write)(fp->_cookie, (char *)p, n);
|
||||
if (t <= 0) {
|
||||
fp->_flags |= __SERR;
|
||||
return (EOF);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
51
lib/libc/stdio/fgetc.c
Normal file
51
lib/libc/stdio/fgetc.c
Normal file
@ -0,0 +1,51 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fgetc.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fgetc(FILE *fp)
|
||||
{
|
||||
return (__sgetc(fp));
|
||||
}
|
@ -1,65 +1,161 @@
|
||||
/*
|
||||
* fgetln(3) implementation.
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Devin Reade, April 1997
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* This file is formatted with tab stops every 8 columns
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* $Id: fgetln.c,v 1.2 1997/07/27 23:13:28 gdr Exp $
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#pragma optimize 0
|
||||
#pragma debug 0
|
||||
#pragma memorymodel 0
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fgetln.c 8.2 (Berkeley) 1/2/94";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
#define LINESIZE 20
|
||||
|
||||
char *
|
||||
fgetln (FILE *stream, size_t *len)
|
||||
/*
|
||||
* Expand the line buffer. Return -1 on error.
|
||||
#ifdef notdef
|
||||
* The `new size' does not account for a terminating '\0',
|
||||
* so we add 1 here.
|
||||
#endif
|
||||
*/
|
||||
int
|
||||
__slbexpand(FILE *fp, size_t newsize)
|
||||
{
|
||||
static char *buffer = NULL;
|
||||
static size_t currentBufferSize = 0;
|
||||
char *p, *q;
|
||||
void *p;
|
||||
|
||||
if (buffer == NULL) {
|
||||
currentBufferSize = LINESIZE;
|
||||
if ((buffer = malloc (currentBufferSize)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
p = fgets(buffer, currentBufferSize, stream);
|
||||
if (p == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if ((p = strchr(buffer, '\n')) != NULL) {
|
||||
break;
|
||||
}
|
||||
p = buffer + strlen(buffer);
|
||||
if ((q = realloc(buffer, currentBufferSize + LINESIZE)) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
buffer = q;
|
||||
currentBufferSize += LINESIZE;
|
||||
if (fgets(p, LINESIZE, stream) == NULL) {
|
||||
if (ferror(stream)) {
|
||||
return NULL;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*len = strlen(buffer);
|
||||
return buffer;
|
||||
#ifdef notdef
|
||||
++newsize;
|
||||
#endif
|
||||
if (fp->_lb._size >= newsize)
|
||||
return (0);
|
||||
if ((p = realloc(fp->_lb._base, newsize)) == NULL)
|
||||
return (-1);
|
||||
fp->_lb._base = p;
|
||||
fp->_lb._size = newsize;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get an input line. The returned pointer often (but not always)
|
||||
* points into a stdio buffer. Fgetln does not alter the text of
|
||||
* the returned line (which is thus not a C string because it will
|
||||
* not necessarily end with '\0'), but does allow callers to modify
|
||||
* it if they wish. Thus, we set __SMOD in case the caller does.
|
||||
*/
|
||||
char *
|
||||
fgetln(register FILE *fp, size_t *lenp)
|
||||
{
|
||||
register unsigned char *p;
|
||||
register size_t len;
|
||||
size_t off;
|
||||
|
||||
/* make sure there is input */
|
||||
if (fp->_r <= 0 && __srefill(fp)) {
|
||||
*lenp = 0;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* look for a newline in the input */
|
||||
if ((p = memchr((void *)fp->_p, '\n', fp->_r)) != NULL) {
|
||||
register char *ret;
|
||||
|
||||
/*
|
||||
* Found one. Flag buffer as modified to keep fseek from
|
||||
* `optimising' a backward seek, in case the user stomps on
|
||||
* the text.
|
||||
*/
|
||||
p++; /* advance over it */
|
||||
ret = (char *)fp->_p;
|
||||
*lenp = len = p - fp->_p;
|
||||
fp->_flags |= __SMOD;
|
||||
fp->_r -= len;
|
||||
fp->_p = p;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* We have to copy the current buffered data to the line buffer.
|
||||
* As a bonus, though, we can leave off the __SMOD.
|
||||
*
|
||||
* OPTIMISTIC is length that we (optimistically) expect will
|
||||
* accomodate the `rest' of the string, on each trip through the
|
||||
* loop below.
|
||||
*/
|
||||
#define OPTIMISTIC 80
|
||||
|
||||
for (len = fp->_r, off = 0;; len += fp->_r) {
|
||||
register size_t diff;
|
||||
|
||||
/*
|
||||
* Make sure there is room for more bytes. Copy data from
|
||||
* file buffer to line buffer, refill file and look for
|
||||
* newline. The loop stops only when we find a newline.
|
||||
*/
|
||||
if (__slbexpand(fp, len + OPTIMISTIC))
|
||||
goto error;
|
||||
(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
|
||||
len - off);
|
||||
off = len;
|
||||
if (__srefill(fp))
|
||||
break; /* EOF or error: return partial line */
|
||||
if ((p = memchr((void *)fp->_p, '\n', fp->_r)) == NULL)
|
||||
continue;
|
||||
|
||||
/* got it: finish up the line (like code above) */
|
||||
p++;
|
||||
diff = p - fp->_p;
|
||||
len += diff;
|
||||
if (__slbexpand(fp, len))
|
||||
goto error;
|
||||
(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
|
||||
diff);
|
||||
fp->_r -= diff;
|
||||
fp->_p = p;
|
||||
break;
|
||||
}
|
||||
*lenp = len;
|
||||
#ifdef notdef
|
||||
fp->_lb._base[len] = 0;
|
||||
#endif
|
||||
return ((char *)fp->_lb._base);
|
||||
|
||||
error:
|
||||
*lenp = 0; /* ??? */
|
||||
return (NULL); /* ??? */
|
||||
}
|
||||
|
51
lib/libc/stdio/fgetpos.c
Normal file
51
lib/libc/stdio/fgetpos.c
Normal file
@ -0,0 +1,51 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fgetpos.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fgetpos(FILE *fp, fpos_t *pos)
|
||||
{
|
||||
return((*pos = ftell(fp)) == (fpos_t)-1);
|
||||
}
|
107
lib/libc/stdio/fgets.c
Normal file
107
lib/libc/stdio/fgets.c
Normal file
@ -0,0 +1,107 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fgets.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Read at most n-1 characters from the given file.
|
||||
* Stop when a newline has been read, or the count runs out.
|
||||
* Return first argument, or NULL if no characters were read.
|
||||
*/
|
||||
char *
|
||||
fgets(char *buf, register size_t n, register FILE *fp)
|
||||
{
|
||||
register size_t len;
|
||||
register char *s;
|
||||
register unsigned char *p, *t;
|
||||
|
||||
if (n == 0) /* sanity check */
|
||||
return (NULL);
|
||||
|
||||
s = buf;
|
||||
n--; /* leave space for NUL */
|
||||
while (n != 0) {
|
||||
/*
|
||||
* If the buffer is empty, refill it.
|
||||
*/
|
||||
if ((len = fp->_r) <= 0) {
|
||||
if (__srefill(fp)) {
|
||||
/* EOF/error: stop with partial or no line */
|
||||
if (s == buf)
|
||||
return (NULL);
|
||||
break;
|
||||
}
|
||||
len = fp->_r;
|
||||
}
|
||||
p = fp->_p;
|
||||
|
||||
/*
|
||||
* Scan through at most n bytes of the current buffer,
|
||||
* looking for '\n'. If found, copy up to and including
|
||||
* newline, and stop. Otherwise, copy entire chunk
|
||||
* and loop.
|
||||
*/
|
||||
if (len > n)
|
||||
len = n;
|
||||
t = memchr((void *)p, '\n', len);
|
||||
if (t != NULL) {
|
||||
len = ++t - p;
|
||||
fp->_r -= len;
|
||||
fp->_p = t;
|
||||
(void)memcpy((void *)s, (void *)p, len);
|
||||
s[len] = 0;
|
||||
return (buf);
|
||||
}
|
||||
fp->_r -= len;
|
||||
fp->_p += len;
|
||||
(void)memcpy((void *)s, (void *)p, len);
|
||||
s += len;
|
||||
n -= len;
|
||||
}
|
||||
*s = 0;
|
||||
return (buf);
|
||||
}
|
56
lib/libc/stdio/fileno.c
Normal file
56
lib/libc/stdio/fileno.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fileno.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro fileno.
|
||||
*/
|
||||
#undef fileno
|
||||
|
||||
int
|
||||
fileno(FILE *fp)
|
||||
{
|
||||
return (__sfileno(fp));
|
||||
}
|
186
lib/libc/stdio/findfp.c
Normal file
186
lib/libc/stdio/findfp.c
Normal file
@ -0,0 +1,186 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:findfp.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
#include "glue.h"
|
||||
|
||||
int __sdidinit;
|
||||
|
||||
#define NDYNAMIC 10 /* add ten more whenever necessary */
|
||||
|
||||
#define std(flags, file) \
|
||||
{0,0,0,flags,file,{0},0,__sF+file-STDIN_FILENO,__sclose,__sread,__sseek,__swrite}
|
||||
/* p r w flags file _bf z cookie close read seek write */
|
||||
|
||||
/* the usual - (stdin + stdout + stderr) */
|
||||
static FILE usual[FOPEN_MAX - 3];
|
||||
static struct glue uglue = { NULL, FOPEN_MAX - 3, usual };
|
||||
|
||||
FILE __sF[3] = {
|
||||
std(__SRD, STDIN_FILENO), /* stdin */
|
||||
std(__SWR, STDOUT_FILENO), /* stdout */
|
||||
std(__SWR |__SNBF, STDERR_FILENO) /* stderr */
|
||||
};
|
||||
|
||||
/*
|
||||
* We use the ORCA implementation of assert(3). Unfortunately, it calls
|
||||
* stderr by name which of course comes up as unresolved. We don't want
|
||||
* to export "stderr" as a symbol from this file because then someone,
|
||||
* somewhere, will try assigning to it and all hell will break loose.
|
||||
*/
|
||||
#ifdef __GNO__
|
||||
FILE *__assertfp = stderr;
|
||||
#endif
|
||||
|
||||
#undef LF_TRANS
|
||||
|
||||
struct glue __sglue = { &uglue, 3, __sF };
|
||||
|
||||
static struct glue *
|
||||
moreglue(register int n)
|
||||
{
|
||||
register struct glue *g;
|
||||
register FILE *p;
|
||||
static FILE empty;
|
||||
|
||||
g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
|
||||
if (g == NULL)
|
||||
return (NULL);
|
||||
p = (FILE *)ALIGN(g + 1);
|
||||
g->next = NULL;
|
||||
g->niobs = n;
|
||||
g->iobs = p;
|
||||
while (--n >= 0)
|
||||
*p++ = empty;
|
||||
return (g);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a free FILE for fopen et al.
|
||||
*/
|
||||
FILE *
|
||||
__sfp(void)
|
||||
{
|
||||
register FILE *fp;
|
||||
register int n;
|
||||
register struct glue *g;
|
||||
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
for (g = &__sglue;; g = g->next) {
|
||||
for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
|
||||
if (fp->_flags == 0)
|
||||
goto found;
|
||||
if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
|
||||
break;
|
||||
}
|
||||
return (NULL);
|
||||
found:
|
||||
fp->_flags = 1; /* reserve this slot; caller sets real flags */
|
||||
fp->_p = NULL; /* no current pointer */
|
||||
fp->_w = 0; /* nothing to read or write */
|
||||
fp->_r = 0;
|
||||
fp->_bf._base = NULL; /* no buffer */
|
||||
fp->_bf._size = 0;
|
||||
fp->_lbfsize = 0; /* not line buffered */
|
||||
fp->_file = -1; /* no file */
|
||||
/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
|
||||
fp->_ub._base = NULL; /* no ungetc buffer */
|
||||
fp->_ub._size = 0;
|
||||
fp->_lb._base = NULL; /* no line buffer */
|
||||
fp->_lb._size = 0;
|
||||
return (fp);
|
||||
}
|
||||
|
||||
/*
|
||||
* XXX. Force immediate allocation of internal memory. Not used by stdio,
|
||||
* but documented historically for certain applications. Bad applications.
|
||||
*/
|
||||
int
|
||||
f_prealloc(void)
|
||||
{
|
||||
register struct glue *g;
|
||||
int n;
|
||||
|
||||
n = getdtablesize() - FOPEN_MAX + 20; /* 20 for slop. */
|
||||
for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
|
||||
/* void */;
|
||||
if (n > 0)
|
||||
g->next = moreglue(n);
|
||||
}
|
||||
|
||||
/*
|
||||
* exit() calls _cleanup() through *__cleanup, set whenever we
|
||||
* open or buffer a file. This chicanery is done so that programs
|
||||
* that do not use stdio need not link it all in.
|
||||
*
|
||||
* The name `_cleanup' is, alas, fairly well known outside stdio.
|
||||
*/
|
||||
void
|
||||
_cleanup(void)
|
||||
{
|
||||
/* (void) _fwalk(fclose); */
|
||||
(void) _fwalk(__sflush); /* `cheating' */
|
||||
#ifdef __GNO__
|
||||
__sExitDoUnlinks();
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* __sinit() is called whenever stdio's internal variables must be set up.
|
||||
*/
|
||||
void
|
||||
__sinit(void)
|
||||
{
|
||||
/* make sure we clean up on exit */
|
||||
__cleanup = _cleanup; /* conservative */
|
||||
__sdidinit = 1;
|
||||
}
|
115
lib/libc/stdio/flags.c
Normal file
115
lib/libc/stdio/flags.c
Normal file
@ -0,0 +1,115 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:flags.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)flags.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
/*
|
||||
* Return the (stdio) flags for a given mode. Store the flags
|
||||
* to be passed to an open() syscall through *optr.
|
||||
* Return 0 on error.
|
||||
*/
|
||||
int
|
||||
__sflags(register char *mode, int *optr)
|
||||
{
|
||||
register int ret, m, o;
|
||||
#ifdef __GNO__
|
||||
register int b=0;
|
||||
#endif
|
||||
|
||||
switch (*mode++) {
|
||||
|
||||
case 'r': /* open for reading */
|
||||
ret = __SRD;
|
||||
m = O_RDONLY;
|
||||
o = 0;
|
||||
break;
|
||||
|
||||
case 'w': /* open for writing */
|
||||
ret = __SWR;
|
||||
m = O_WRONLY;
|
||||
o = O_CREAT | O_TRUNC;
|
||||
break;
|
||||
|
||||
case 'a': /* open for appending */
|
||||
ret = __SWR;
|
||||
m = O_WRONLY;
|
||||
o = O_CREAT | O_APPEND;
|
||||
break;
|
||||
|
||||
default: /* illegal mode */
|
||||
errno = EINVAL;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* [rwa]\+ or [rwa]b\+ means read and write */
|
||||
#ifdef __GNO__
|
||||
if (*mode == 'b') {
|
||||
b = __SBIN;
|
||||
mode++;
|
||||
if (*mode == '+') {
|
||||
ret = __SRW;
|
||||
m = O_RDWR;
|
||||
}
|
||||
} else if (*mode == '+') {
|
||||
ret = __SRW;
|
||||
m = O_RDWR;
|
||||
mode++;
|
||||
if (*mode == 'b') {
|
||||
b = __SBIN;
|
||||
}
|
||||
}
|
||||
ret |= b;
|
||||
#else
|
||||
if (*mode == '+' || (*mode == 'b' && mode[1] == '+')) {
|
||||
ret = __SRW;
|
||||
m = O_RDWR;
|
||||
}
|
||||
#endif
|
||||
*optr = m | o;
|
||||
return (ret);
|
||||
}
|
46
lib/libc/stdio/floatio.h
Normal file
46
lib/libc/stdio/floatio.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)floatio.h 8.1 (Berkeley) 6/4/93
|
||||
*/
|
||||
|
||||
/*
|
||||
* Floating point scanf/printf (input/output) definitions.
|
||||
*/
|
||||
|
||||
/* 11-bit exponent (VAX G floating point) is 308 decimal digits */
|
||||
#define MAXEXP 308
|
||||
/* 128 bit fraction takes up 39 decimal digits; max reasonable precision */
|
||||
#define MAXFRACT 39
|
97
lib/libc/stdio/fopen.c
Normal file
97
lib/libc/stdio/fopen.c
Normal file
@ -0,0 +1,97 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fopen.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
FILE *
|
||||
fopen(const char *file, const char *mode)
|
||||
{
|
||||
register FILE *fp;
|
||||
register int f;
|
||||
int flags, oflags;
|
||||
|
||||
if ((flags = __sflags(mode, &oflags)) == 0)
|
||||
return (NULL);
|
||||
if ((fp = __sfp()) == NULL)
|
||||
return (NULL);
|
||||
#ifdef __ORCAC__
|
||||
/*
|
||||
* ORCA/C uses "callee cleans up", so the third arg to open(2)
|
||||
* can be given if and only if it is expected.
|
||||
*/
|
||||
f = (oflags & O_CREAT) ? open(file, oflags, DEFFILEMODE) :
|
||||
open(file, oflags);
|
||||
if (f < 0) {
|
||||
#else
|
||||
if ((f = open(file, oflags, DEFFILEMODE)) < 0) {
|
||||
#endif
|
||||
fp->_flags = 0; /* release */
|
||||
return (NULL);
|
||||
}
|
||||
fp->_file = f;
|
||||
fp->_flags = flags;
|
||||
fp->_cookie = fp;
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
|
||||
/*
|
||||
* When opening in append mode, even though we use O_APPEND,
|
||||
* we need to seek to the end so that ftell() gets the right
|
||||
* answer. If the user then alters the seek pointer, or
|
||||
* the file extends, this will fail, but there is not much
|
||||
* we can do about this. (We could set __SAPP and check in
|
||||
* fseek and ftell.)
|
||||
*/
|
||||
if (oflags & O_APPEND)
|
||||
(void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
|
||||
return (fp);
|
||||
}
|
76
lib/libc/stdio/fprintf.c
Normal file
76
lib/libc/stdio/fprintf.c
Normal file
@ -0,0 +1,76 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fprintf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
fprintf(FILE *fp, const char *fmt, ...)
|
||||
#else
|
||||
fprintf(fp, fmt, va_alist)
|
||||
FILE *fp;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
ret = vfprintf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
68
lib/libc/stdio/fpurge.c
Normal file
68
lib/libc/stdio/fpurge.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fpurge.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* fpurge: like fflush, but without writing anything: leave the
|
||||
* given FILE's buffer empty.
|
||||
*/
|
||||
int
|
||||
fpurge(register FILE *fp)
|
||||
{
|
||||
if (!fp->_flags) {
|
||||
errno = EBADF;
|
||||
return(EOF);
|
||||
}
|
||||
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_p = fp->_bf._base;
|
||||
fp->_r = 0;
|
||||
fp->_w = fp->_flags & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
|
||||
return (0);
|
||||
}
|
52
lib/libc/stdio/fputc.c
Normal file
52
lib/libc/stdio/fputc.c
Normal file
@ -0,0 +1,52 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fputc.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fputc.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fputc(int c, register FILE *fp)
|
||||
{
|
||||
return (putc(c, fp));
|
||||
}
|
63
lib/libc/stdio/fputs.c
Normal file
63
lib/libc/stdio/fputs.c
Normal file
@ -0,0 +1,63 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fputs.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "fvwrite.h"
|
||||
|
||||
/*
|
||||
* Write the given string to the given file.
|
||||
*/
|
||||
int
|
||||
fputs(const char *s, FILE *fp)
|
||||
{
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
iov.iov_base = (void *)s;
|
||||
iov.iov_len = uio.uio_resid = strlen(s);
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
return (__sfvwrite(fp, &uio));
|
||||
}
|
83
lib/libc/stdio/fread.c
Normal file
83
lib/libc/stdio/fread.c
Normal file
@ -0,0 +1,83 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fread.c 8.2 (Berkeley) 12/11/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
size_t
|
||||
fread(void *buf, size_t size, size_t count, register FILE *fp)
|
||||
{
|
||||
register size_t resid;
|
||||
register char *p;
|
||||
register int r;
|
||||
size_t total;
|
||||
|
||||
/*
|
||||
* The ANSI standard requires a return value of 0 for a count
|
||||
* or a size of 0. Peculiarily, it imposes no such requirements
|
||||
* on fwrite; it only requires fread to be broken.
|
||||
*/
|
||||
if ((resid = count * size) == 0)
|
||||
return (0);
|
||||
if (fp->_r < 0)
|
||||
fp->_r = 0;
|
||||
total = resid;
|
||||
p = buf;
|
||||
while (resid > (r = fp->_r)) {
|
||||
(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
|
||||
fp->_p += r;
|
||||
/* fp->_r = 0 ... done in __srefill */
|
||||
p += r;
|
||||
resid -= r;
|
||||
if (__srefill(fp)) {
|
||||
/* no more input: return partial result */
|
||||
return ((total - resid) / size);
|
||||
}
|
||||
}
|
||||
(void)memcpy((void *)p, (void *)fp->_p, resid);
|
||||
fp->_r -= resid;
|
||||
fp->_p += resid;
|
||||
return (count);
|
||||
}
|
170
lib/libc/stdio/freopen.c
Normal file
170
lib/libc/stdio/freopen.c
Normal file
@ -0,0 +1,170 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Re-direct an existing, open (probably) file to some other file.
|
||||
* ANSI is written such that the original file gets closed if at
|
||||
* all possible, no matter what.
|
||||
*/
|
||||
FILE *
|
||||
freopen(const char *file, const char *mode, register FILE *fp)
|
||||
{
|
||||
register int f;
|
||||
int flags, isopen, oflags, sverrno, wantfd;
|
||||
|
||||
if ((flags = __sflags(mode, &oflags)) == 0) {
|
||||
(void) fclose(fp);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
|
||||
/*
|
||||
* There are actually programs that depend on being able to "freopen"
|
||||
* descriptors that weren't originally open. Keep this from breaking.
|
||||
* Remember whether the stream was open to begin with, and which file
|
||||
* descriptor (if any) was associated with it. If it was attached to
|
||||
* a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
|
||||
* should work. This is unnecessary if it was not a Unix file.
|
||||
*/
|
||||
if (fp->_flags == 0) {
|
||||
fp->_flags = __SEOF; /* hold on to it */
|
||||
isopen = 0;
|
||||
wantfd = -1;
|
||||
} else {
|
||||
/* flush the stream; ANSI doesn't require this. */
|
||||
if (fp->_flags & __SWR)
|
||||
(void) __sflush(fp);
|
||||
/* if close is NULL, closing is a no-op, hence pointless */
|
||||
isopen = fp->_close != NULL;
|
||||
if ((wantfd = fp->_file) < 0 && isopen) {
|
||||
(void) (*fp->_close)(fp->_cookie);
|
||||
isopen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get a new descriptor to refer to the new file. */
|
||||
#ifdef __ORCAC__
|
||||
/*
|
||||
* ORCA/C uses "callee cleans up", so the third arg to open(2)
|
||||
* can be given if and only if it is expected.
|
||||
*/
|
||||
f = (oflags & O_CREAT) ? open(file, oflags, DEFFILEMODE) :
|
||||
open(file, oflags);
|
||||
#else
|
||||
f = open(file, oflags, DEFFILEMODE);
|
||||
#endif
|
||||
if (f < 0 && isopen) {
|
||||
/* If out of fd's close the old one and try again. */
|
||||
#ifdef __GNO__
|
||||
if (errno == EMFILE) { /* no ENFILE for GNO */
|
||||
#else
|
||||
if (errno == ENFILE || errno == EMFILE) {
|
||||
#endif
|
||||
(void) (*fp->_close)(fp->_cookie);
|
||||
isopen = 0;
|
||||
f = open(file, oflags, DEFFILEMODE);
|
||||
}
|
||||
}
|
||||
sverrno = errno;
|
||||
|
||||
/*
|
||||
* Finish closing fp. Even if the open succeeded above, we cannot
|
||||
* keep fp->_base: it may be the wrong size. This loses the effect
|
||||
* of any setbuffer calls, but stdio has always done this before.
|
||||
*/
|
||||
if (isopen)
|
||||
(void) (*fp->_close)(fp->_cookie);
|
||||
if (fp->_flags & __SMBF)
|
||||
free((char *)fp->_bf._base);
|
||||
fp->_w = 0;
|
||||
fp->_r = 0;
|
||||
fp->_p = NULL;
|
||||
fp->_bf._base = NULL;
|
||||
fp->_bf._size = 0;
|
||||
fp->_lbfsize = 0;
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_ub._size = 0;
|
||||
if (HASLB(fp))
|
||||
FREELB(fp);
|
||||
fp->_lb._size = 0;
|
||||
|
||||
if (f < 0) { /* did not get it after all */
|
||||
fp->_flags = 0; /* set it free */
|
||||
errno = sverrno; /* restore in case _close clobbered */
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* If reopening something that was open before on a real file, try
|
||||
* to maintain the descriptor. Various C library routines (perror)
|
||||
* assume stderr is always fd STDERR_FILENO, even if being freopen'd.
|
||||
*/
|
||||
if (wantfd >= 0 && f != wantfd) {
|
||||
if (dup2(f, wantfd) >= 0) {
|
||||
(void) close(f);
|
||||
f = wantfd;
|
||||
}
|
||||
}
|
||||
|
||||
fp->_flags = flags;
|
||||
fp->_file = f;
|
||||
fp->_cookie = fp;
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
return (fp);
|
||||
}
|
76
lib/libc/stdio/fscanf.c
Normal file
76
lib/libc/stdio/fscanf.c
Normal file
@ -0,0 +1,76 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fscanf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fscanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
fscanf(FILE *fp, char const *fmt, ...) {
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
fscanf(fp, fmt, va_alist)
|
||||
FILE *fp;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap);
|
||||
#endif
|
||||
ret = __svfscanf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
247
lib/libc/stdio/fseek.c
Normal file
247
lib/libc/stdio/fseek.c
Normal file
@ -0,0 +1,247 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
#define POS_ERR (-(fpos_t)1)
|
||||
|
||||
/*
|
||||
* Seek the given file to the given offset.
|
||||
* `Whence' must be one of the three SEEK_* macros.
|
||||
*/
|
||||
int
|
||||
fseek(register FILE *fp, long offset, int whence)
|
||||
{
|
||||
register fpos_t (*seekfn) __P((void *, fpos_t, int));
|
||||
fpos_t target, curoff;
|
||||
size_t n;
|
||||
struct stat st;
|
||||
int havepos;
|
||||
|
||||
/* make sure stdio is set up */
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
|
||||
/*
|
||||
* Have to be able to seek.
|
||||
*/
|
||||
if ((seekfn = fp->_seek) == NULL) {
|
||||
errno = ESPIPE; /* historic practice */
|
||||
return (EOF);
|
||||
}
|
||||
|
||||
/*
|
||||
* Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
|
||||
* After this, whence is either SEEK_SET or SEEK_END.
|
||||
*/
|
||||
switch (whence) {
|
||||
|
||||
case SEEK_CUR:
|
||||
/*
|
||||
* In order to seek relative to the current stream offset,
|
||||
* we have to first find the current stream offset a la
|
||||
* ftell (see ftell for details).
|
||||
*/
|
||||
if (fp->_flags & __SOFF)
|
||||
curoff = fp->_offset;
|
||||
else {
|
||||
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
||||
if (curoff == -1L)
|
||||
return (EOF);
|
||||
}
|
||||
if (fp->_flags & __SRD) {
|
||||
curoff -= fp->_r;
|
||||
if (HASUB(fp))
|
||||
curoff -= fp->_ur;
|
||||
} else if (fp->_flags & __SWR && fp->_p != NULL)
|
||||
curoff += fp->_p - fp->_bf._base;
|
||||
|
||||
offset += curoff;
|
||||
whence = SEEK_SET;
|
||||
havepos = 1;
|
||||
break;
|
||||
|
||||
case SEEK_SET:
|
||||
case SEEK_END:
|
||||
curoff = 0; /* XXX just to keep gcc quiet */
|
||||
havepos = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return (EOF);
|
||||
}
|
||||
|
||||
/*
|
||||
* Can only optimise if:
|
||||
* reading (and not reading-and-writing);
|
||||
* not unbuffered; and
|
||||
* this is a `regular' Unix file (and hence seekfn==__sseek).
|
||||
* We must check __NBF first, because it is possible to have __NBF
|
||||
* and __SOPT both set.
|
||||
*/
|
||||
if (fp->_bf._base == NULL)
|
||||
__smakebuf(fp);
|
||||
if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
|
||||
goto dumb;
|
||||
if ((fp->_flags & __SOPT) == 0) {
|
||||
if (seekfn != __sseek ||
|
||||
fp->_file < 0 || fstat(fp->_file, &st) ||
|
||||
(st.st_mode & S_IFMT) != S_IFREG) {
|
||||
fp->_flags |= __SNPT;
|
||||
goto dumb;
|
||||
}
|
||||
fp->_blksize = st.st_blksize;
|
||||
fp->_flags |= __SOPT;
|
||||
}
|
||||
|
||||
/*
|
||||
* We are reading; we can try to optimise.
|
||||
* Figure out where we are going and where we are now.
|
||||
*/
|
||||
if (whence == SEEK_SET)
|
||||
target = offset;
|
||||
else {
|
||||
if (fstat(fp->_file, &st))
|
||||
goto dumb;
|
||||
target = st.st_size + offset;
|
||||
}
|
||||
|
||||
if (!havepos) {
|
||||
if (fp->_flags & __SOFF)
|
||||
curoff = fp->_offset;
|
||||
else {
|
||||
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
||||
if (curoff == POS_ERR)
|
||||
goto dumb;
|
||||
}
|
||||
curoff -= fp->_r;
|
||||
if (HASUB(fp))
|
||||
curoff -= fp->_ur;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the number of bytes in the input buffer (pretending
|
||||
* that any ungetc() input has been discarded). Adjust current
|
||||
* offset backwards by this count so that it represents the
|
||||
* file offset for the first byte in the current input buffer.
|
||||
*/
|
||||
if (HASUB(fp)) {
|
||||
curoff += fp->_r; /* kill off ungetc */
|
||||
n = fp->_up - fp->_bf._base;
|
||||
curoff -= n;
|
||||
n += fp->_ur;
|
||||
} else {
|
||||
n = fp->_p - fp->_bf._base;
|
||||
curoff -= n;
|
||||
n += fp->_r;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the target offset is within the current buffer,
|
||||
* simply adjust the pointers, clear EOF, undo ungetc(),
|
||||
* and return. (If the buffer was modified, we have to
|
||||
* skip this; see fgetln.c.)
|
||||
*/
|
||||
if ((fp->_flags & __SMOD) == 0 &&
|
||||
target >= curoff && target < curoff + n) {
|
||||
register int o = target - curoff;
|
||||
|
||||
fp->_p = fp->_bf._base + o;
|
||||
fp->_r = n - o;
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_flags &= ~__SEOF;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* The place we want to get to is not within the current buffer,
|
||||
* but we can still be kind to the kernel copyout mechanism.
|
||||
* By aligning the file offset to a block boundary, we can let
|
||||
* the kernel use the VM hardware to map pages instead of
|
||||
* copying bytes laboriously. Using a block boundary also
|
||||
* ensures that we only read one block, rather than two.
|
||||
*/
|
||||
curoff = target & ~(fp->_blksize - 1);
|
||||
if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
|
||||
goto dumb;
|
||||
fp->_r = 0;
|
||||
fp->_p = fp->_bf._base;
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_flags &= ~__SEOF;
|
||||
n = target - curoff;
|
||||
if (n) {
|
||||
if (__srefill(fp) || fp->_r < n)
|
||||
goto dumb;
|
||||
fp->_p += n;
|
||||
fp->_r -= n;
|
||||
}
|
||||
return (0);
|
||||
|
||||
/*
|
||||
* We get here if we cannot optimise the seek ... just
|
||||
* do it. Allow the seek function to change fp->_bf._base.
|
||||
*/
|
||||
dumb:
|
||||
if (__sflush(fp) ||
|
||||
(*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) {
|
||||
return (EOF);
|
||||
}
|
||||
/* success: clear EOF indicator and discard ungetc() data */
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_p = fp->_bf._base;
|
||||
fp->_r = 0;
|
||||
/* fp->_w = 0; */ /* unnecessary (I think...) */
|
||||
fp->_flags &= ~__SEOF;
|
||||
return (0);
|
||||
}
|
54
lib/libc/stdio/fsetpos.c
Normal file
54
lib/libc/stdio/fsetpos.c
Normal file
@ -0,0 +1,54 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fsetpos.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* fsetpos: like fseek.
|
||||
*/
|
||||
int
|
||||
fsetpos(FILE *iop, const fpos_t *pos)
|
||||
{
|
||||
return (fseek(iop, (long)*pos, SEEK_SET));
|
||||
}
|
91
lib/libc/stdio/ftell.c
Normal file
91
lib/libc/stdio/ftell.c
Normal file
@ -0,0 +1,91 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)ftell.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* ftell: return current offset.
|
||||
*/
|
||||
long
|
||||
ftell(register const FILE *fp)
|
||||
{
|
||||
register fpos_t pos;
|
||||
|
||||
if (fp->_seek == NULL) {
|
||||
errno = ESPIPE; /* historic practice */
|
||||
return (-1L);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find offset of underlying I/O object, then
|
||||
* adjust for buffered bytes.
|
||||
*/
|
||||
if (fp->_flags & __SOFF)
|
||||
pos = fp->_offset;
|
||||
else {
|
||||
pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
||||
if (pos == -1L)
|
||||
return (pos);
|
||||
}
|
||||
if (fp->_flags & __SRD) {
|
||||
/*
|
||||
* Reading. Any unread characters (including
|
||||
* those from ungetc) cause the position to be
|
||||
* smaller than that in the underlying object.
|
||||
*/
|
||||
pos -= fp->_r;
|
||||
if (HASUB(fp))
|
||||
pos -= fp->_ur;
|
||||
} else if (fp->_flags & __SWR && fp->_p != NULL) {
|
||||
/*
|
||||
* Writing. Any buffered characters cause the
|
||||
* position to be greater than that in the
|
||||
* underlying object.
|
||||
*/
|
||||
pos += fp->_p - fp->_bf._base;
|
||||
}
|
||||
return (pos);
|
||||
}
|
83
lib/libc/stdio/funopen.c
Normal file
83
lib/libc/stdio/funopen.c
Normal file
@ -0,0 +1,83 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)funopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "local.h"
|
||||
|
||||
typedef ssize_t (*readfn_t)(void *, char *, size_t);
|
||||
typedef ssize_t (*writefn_t)(void *, const char *, size_t);
|
||||
typedef fpos_t (*seekfn_t)(void *cookie, fpos_t off, int whence);
|
||||
typedef int (*closefn_t)(void *);
|
||||
|
||||
FILE *
|
||||
funopen(const void *cookie, readfn_t readfn, writefn_t writefn, seekfn_t
|
||||
seekfn, closefn_t closefn)
|
||||
{
|
||||
register FILE *fp;
|
||||
int flags;
|
||||
|
||||
if (readfn == NULL) {
|
||||
if (writefn == NULL) { /* illegal */
|
||||
errno = EINVAL;
|
||||
return (NULL);
|
||||
} else
|
||||
flags = __SWR; /* write only */
|
||||
} else {
|
||||
if (writefn == NULL)
|
||||
flags = __SRD; /* read only */
|
||||
else
|
||||
flags = __SRW; /* read-write */
|
||||
}
|
||||
if ((fp = __sfp()) == NULL)
|
||||
return (NULL);
|
||||
fp->_flags = flags;
|
||||
fp->_file = -1;
|
||||
fp->_cookie = (void *)cookie;
|
||||
fp->_read = readfn;
|
||||
fp->_write = writefn;
|
||||
fp->_seek = seekfn;
|
||||
fp->_close = closefn;
|
||||
return (fp);
|
||||
}
|
217
lib/libc/stdio/fvwrite.c
Normal file
217
lib/libc/stdio/fvwrite.c
Normal file
@ -0,0 +1,217 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
#ifdef __GNO__
|
||||
|
||||
ssize_t write(int, const void *, size_t);
|
||||
|
||||
#define CONVERT_BUFFER(flags, buf, len) \
|
||||
if (((flags) & __SBIN) == 0) { \
|
||||
nl_p = (buf); \
|
||||
nl_q = nl_p + (len); \
|
||||
while (nl_p < nl_q) { \
|
||||
if (*nl_p == '\n') { \
|
||||
*nl_p = '\r'; \
|
||||
} \
|
||||
nl_p++; \
|
||||
} \
|
||||
}
|
||||
#else
|
||||
#define CONVERT_BUFFER(buf, len)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Write some memory regions. Return zero on success, EOF on error.
|
||||
*
|
||||
* This routine is large and unsightly, but most of the ugliness due
|
||||
* to the three different kinds of output buffering is handled here.
|
||||
*/
|
||||
int
|
||||
__sfvwrite(register FILE *fp, register struct __suio *uio)
|
||||
{
|
||||
register size_t len;
|
||||
register char *p;
|
||||
register struct __siov *iov;
|
||||
register int w, s;
|
||||
char *nl;
|
||||
#ifdef __GNO__
|
||||
register char *nl_p, *nl_q;
|
||||
#endif
|
||||
int nlknown, nldist;
|
||||
|
||||
if ((len = uio->uio_resid) == 0)
|
||||
return (0);
|
||||
/* make sure we can write */
|
||||
if (cantwrite(fp))
|
||||
return (EOF);
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define COPY(n) (void)memcpy((void *)fp->_p, (void *)p, (size_t)(n))
|
||||
|
||||
iov = uio->uio_iov;
|
||||
p = iov->iov_base;
|
||||
len = iov->iov_len;
|
||||
iov++;
|
||||
#define GETIOV(extra_work) \
|
||||
while (len == 0) { \
|
||||
extra_work; \
|
||||
p = iov->iov_base; \
|
||||
len = iov->iov_len; \
|
||||
iov++; \
|
||||
}
|
||||
if (fp->_flags & __SNBF) {
|
||||
/*
|
||||
* Unbuffered: write up to BUFSIZ bytes at a time.
|
||||
*/
|
||||
do {
|
||||
GETIOV(;);
|
||||
CONVERT_BUFFER(fp->_flags, p, MIN(len, BUFSIZ));
|
||||
w = (*fp->_write)(fp->_cookie, p, MIN(len, BUFSIZ));
|
||||
if (w <= 0)
|
||||
goto err;
|
||||
p += w;
|
||||
len -= w;
|
||||
} while ((uio->uio_resid -= w) != 0);
|
||||
} else if ((fp->_flags & __SLBF) == 0) {
|
||||
/*
|
||||
* Fully buffered: fill partially full buffer, if any,
|
||||
* and then flush. If there is no partial buffer, write
|
||||
* one _bf._size byte chunk directly (without copying).
|
||||
*
|
||||
* String output is a special case: write as many bytes
|
||||
* as fit, but pretend we wrote everything. This makes
|
||||
* snprintf() return the number of bytes needed, rather
|
||||
* than the number used, and avoids its write function
|
||||
* (so that the write function can be invalid).
|
||||
*/
|
||||
do {
|
||||
GETIOV(;);
|
||||
w = fp->_w;
|
||||
if (fp->_flags & __SSTR) {
|
||||
if (len < w)
|
||||
w = len;
|
||||
COPY(w); /* copy MIN(fp->_w,len), */
|
||||
fp->_w -= w;
|
||||
fp->_p += w;
|
||||
w = len; /* but pretend copied all */
|
||||
} else if (fp->_p > fp->_bf._base && len > w) {
|
||||
/* fill and flush */
|
||||
COPY(w);
|
||||
/* fp->_w -= w; */ /* unneeded */
|
||||
fp->_p += w;
|
||||
if (fflush(fp))
|
||||
goto err;
|
||||
} else if (len >= (w = fp->_bf._size)) {
|
||||
/* write directly */
|
||||
CONVERT_BUFFER(fp->_flags, p, w);
|
||||
w = (*fp->_write)(fp->_cookie, p, w);
|
||||
if (w <= 0)
|
||||
goto err;
|
||||
} else {
|
||||
/* fill and done */
|
||||
w = len;
|
||||
COPY(w);
|
||||
fp->_w -= w;
|
||||
fp->_p += w;
|
||||
}
|
||||
p += w;
|
||||
len -= w;
|
||||
} while ((uio->uio_resid -= w) != 0);
|
||||
} else {
|
||||
/*
|
||||
* Line buffered: like fully buffered, but we
|
||||
* must check for newlines. Compute the distance
|
||||
* to the first newline (including the newline),
|
||||
* or `infinity' if there is none, then pretend
|
||||
* that the amount to write is MIN(len,nldist).
|
||||
*/
|
||||
nlknown = 0;
|
||||
nldist = 0; /* XXX just to keep gcc happy */
|
||||
do {
|
||||
GETIOV(nlknown = 0);
|
||||
if (!nlknown) {
|
||||
nl = memchr((void *)p, '\n', len);
|
||||
nldist = nl ? nl + 1 - p : len + 1;
|
||||
nlknown = 1;
|
||||
}
|
||||
s = MIN(len, nldist);
|
||||
w = fp->_w + fp->_bf._size;
|
||||
if (fp->_p > fp->_bf._base && s > w) {
|
||||
COPY(w);
|
||||
/* fp->_w -= w; */
|
||||
fp->_p += w;
|
||||
if (fflush(fp))
|
||||
goto err;
|
||||
} else if (s >= (w = fp->_bf._size)) {
|
||||
CONVERT_BUFFER(fp->_flags, p, w);
|
||||
w = (*fp->_write)(fp->_cookie, p, w);
|
||||
if (w <= 0)
|
||||
goto err;
|
||||
} else {
|
||||
w = s;
|
||||
COPY(w);
|
||||
fp->_w -= w;
|
||||
fp->_p += w;
|
||||
}
|
||||
if ((nldist -= w) == 0) {
|
||||
/* copied the newline: flush and forget */
|
||||
if (fflush(fp))
|
||||
goto err;
|
||||
nlknown = 0;
|
||||
}
|
||||
p += w;
|
||||
len -= w;
|
||||
} while ((uio->uio_resid -= w) != 0);
|
||||
}
|
||||
return (0);
|
||||
|
||||
err:
|
||||
fp->_flags |= __SERR;
|
||||
return (EOF);
|
||||
}
|
56
lib/libc/stdio/fvwrite.h
Normal file
56
lib/libc/stdio/fvwrite.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)fvwrite.h 8.1 (Berkeley) 6/4/93
|
||||
*/
|
||||
|
||||
/*
|
||||
* I/O descriptors for __sfvwrite().
|
||||
*/
|
||||
struct __siov {
|
||||
void *iov_base;
|
||||
size_t iov_len;
|
||||
};
|
||||
struct __suio {
|
||||
struct __siov *uio_iov;
|
||||
int uio_iovcnt;
|
||||
int uio_resid;
|
||||
};
|
||||
|
||||
#if __STDC__ || c_plusplus
|
||||
extern int __sfvwrite(FILE *, struct __suio *);
|
||||
#else
|
||||
extern int __sfvwrite();
|
||||
#endif
|
64
lib/libc/stdio/fwalk.c
Normal file
64
lib/libc/stdio/fwalk.c
Normal file
@ -0,0 +1,64 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:fwalk.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fwalk.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
#include "glue.h"
|
||||
|
||||
int
|
||||
_fwalk(register int (*function)(FILE *))
|
||||
{
|
||||
register FILE *fp;
|
||||
register int n, ret;
|
||||
register struct glue *g;
|
||||
|
||||
ret = 0;
|
||||
for (g = &__sglue; g != NULL; g = g->next)
|
||||
for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
|
||||
if (fp->_flags != 0)
|
||||
ret |= (*function)(fp);
|
||||
return (ret);
|
||||
}
|
73
lib/libc/stdio/fwrite.c
Normal file
73
lib/libc/stdio/fwrite.c
Normal file
@ -0,0 +1,73 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)fwrite.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
/*
|
||||
* Write `count' objects (each size `size') from memory to the given file.
|
||||
* Return the number of whole objects written.
|
||||
*/
|
||||
size_t
|
||||
fwrite(const void *buf, size_t size, size_t count, FILE *fp)
|
||||
{
|
||||
size_t n;
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
iov.iov_base = (void *)buf;
|
||||
uio.uio_resid = iov.iov_len = n = count * size;
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
|
||||
/*
|
||||
* The usual case is success (__sfvwrite returns 0);
|
||||
* skip the divide if this happens, since divides are
|
||||
* generally slow and since this occurs whenever size==0.
|
||||
*/
|
||||
if (__sfvwrite(fp, &uio) == 0)
|
||||
return (count);
|
||||
return ((n - uio.uio_resid) / size);
|
||||
}
|
56
lib/libc/stdio/getc.c
Normal file
56
lib/libc/stdio/getc.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)getc.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro getc.
|
||||
*/
|
||||
#undef getc
|
||||
|
||||
int
|
||||
getc(register FILE *fp)
|
||||
{
|
||||
return (__sgetc(fp));
|
||||
}
|
56
lib/libc/stdio/getchar.c
Normal file
56
lib/libc/stdio/getchar.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)getchar.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro getchar.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
#undef getchar
|
||||
|
||||
int
|
||||
getchar(void)
|
||||
{
|
||||
return (getc(stdin));
|
||||
}
|
71
lib/libc/stdio/gets.c
Normal file
71
lib/libc/stdio/gets.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)gets.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char *
|
||||
gets(char *buf)
|
||||
{
|
||||
register int c;
|
||||
register char *s;
|
||||
static int warned=0;
|
||||
static char w[] =
|
||||
"warning: this program uses gets(), which is unsafe.\r";
|
||||
|
||||
if (!warned) {
|
||||
(void) write(STDERR_FILENO, w, sizeof(w) - 1);
|
||||
warned = 1;
|
||||
}
|
||||
for (s = buf; (c = getchar()) != '\n';)
|
||||
if (c == EOF)
|
||||
if (s == buf)
|
||||
return (NULL);
|
||||
else
|
||||
break;
|
||||
else
|
||||
*s++ = c;
|
||||
*s = 0;
|
||||
return (buf);
|
||||
}
|
53
lib/libc/stdio/getw.c
Normal file
53
lib/libc/stdio/getw.c
Normal file
@ -0,0 +1,53 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)getw.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
getw(FILE *fp)
|
||||
{
|
||||
int x;
|
||||
|
||||
return (fread((void *)&x, sizeof(x), 1, fp) == 1 ? x : EOF);
|
||||
}
|
49
lib/libc/stdio/glue.h
Normal file
49
lib/libc/stdio/glue.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)glue.h 8.1 (Berkeley) 6/4/93
|
||||
*/
|
||||
|
||||
/*
|
||||
* The first few FILEs are statically allocated; others are dynamically
|
||||
* allocated and linked in via this glue structure.
|
||||
*/
|
||||
struct glue {
|
||||
struct glue *next;
|
||||
int niobs;
|
||||
FILE *iobs;
|
||||
};
|
||||
|
||||
extern struct glue __sglue;
|
92
lib/libc/stdio/local.h
Normal file
92
lib/libc/stdio/local.h
Normal file
@ -0,0 +1,92 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)local.h 8.2 (Berkeley) 1/2/94
|
||||
*/
|
||||
|
||||
/*
|
||||
* Information local to this implementation of stdio,
|
||||
* in particular, macros and private variables.
|
||||
*/
|
||||
|
||||
int __sflush __P((FILE *));
|
||||
FILE *__sfp __P((void));
|
||||
int __srefill __P((FILE *));
|
||||
ssize_t __sread __P((void *, char *, size_t));
|
||||
ssize_t __swrite __P((void *, char const *, size_t));
|
||||
fpos_t __sseek __P((void *, fpos_t, int));
|
||||
int __sclose __P((void *));
|
||||
void __sinit __P((void));
|
||||
void _cleanup __P((void));
|
||||
#ifdef __GNO__
|
||||
void __sUnlinkAtExit __P((const char *fname));
|
||||
void __sExitDoUnlinks __P((void));
|
||||
extern /* __cleanup is defined in orcalib/vars.asm */
|
||||
#endif
|
||||
void (*__cleanup) __P((void));
|
||||
void __smakebuf __P((FILE *));
|
||||
int __swhatbuf __P((FILE *, size_t *, int *));
|
||||
int _fwalk __P((int (*)(FILE *)));
|
||||
int __swsetup __P((FILE *));
|
||||
int __sflags __P((const char *, int *));
|
||||
|
||||
extern int __sdidinit;
|
||||
|
||||
/*
|
||||
* Return true iff the given FILE cannot be written now.
|
||||
*/
|
||||
#define cantwrite(fp) \
|
||||
((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \
|
||||
__swsetup(fp))
|
||||
|
||||
/*
|
||||
* Test whether the given stdio file has an active ungetc buffer;
|
||||
* release such a buffer, without restoring ordinary unread data.
|
||||
*/
|
||||
#define HASUB(fp) ((fp)->_ub._base != NULL)
|
||||
#define FREEUB(fp) { \
|
||||
if ((fp)->_ub._base != (fp)->_ubuf) \
|
||||
free((char *)(fp)->_ub._base); \
|
||||
(fp)->_ub._base = NULL; \
|
||||
}
|
||||
|
||||
/*
|
||||
* test for an fgetln() buffer.
|
||||
*/
|
||||
#define HASLB(fp) ((fp)->_lb._base != NULL)
|
||||
#define FREELB(fp) { \
|
||||
free((char *)(fp)->_lb._base); \
|
||||
(fp)->_lb._base = NULL; \
|
||||
}
|
118
lib/libc/stdio/makebuf.c
Normal file
118
lib/libc/stdio/makebuf.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)makebuf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Allocate a file buffer, or switch to unbuffered I/O.
|
||||
* Per the ANSI C standard, ALL tty devices default to line buffered.
|
||||
*
|
||||
* As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
|
||||
* optimisation) right after the fstat() that finds the buffer size.
|
||||
*/
|
||||
void
|
||||
__smakebuf(register FILE *fp)
|
||||
{
|
||||
register void *p;
|
||||
register int flags;
|
||||
size_t size;
|
||||
int couldbetty;
|
||||
|
||||
if (fp->_flags & __SNBF) {
|
||||
fp->_bf._base = fp->_p = fp->_nbuf;
|
||||
fp->_bf._size = 1;
|
||||
return;
|
||||
}
|
||||
flags = __swhatbuf(fp, &size, &couldbetty);
|
||||
if ((p = malloc(size)) == NULL) {
|
||||
fp->_flags |= __SNBF;
|
||||
fp->_bf._base = fp->_p = fp->_nbuf;
|
||||
fp->_bf._size = 1;
|
||||
return;
|
||||
}
|
||||
__cleanup = _cleanup;
|
||||
flags |= __SMBF;
|
||||
fp->_bf._base = fp->_p = p;
|
||||
fp->_bf._size = size;
|
||||
if (couldbetty && isatty(fp->_file))
|
||||
flags |= __SLBF;
|
||||
fp->_flags |= flags;
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal routine to determine `proper' buffering for a file.
|
||||
*/
|
||||
int
|
||||
__swhatbuf(register FILE *fp, size_t *bufsize, int *couldbetty)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (fp->_file < 0 || fstat(fp->_file, &st) < 0) {
|
||||
*couldbetty = 0;
|
||||
*bufsize = BUFSIZ;
|
||||
return (__SNPT);
|
||||
}
|
||||
|
||||
/* could be a tty iff it is a character device */
|
||||
*couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
|
||||
if (st.st_blksize <= 0) {
|
||||
*bufsize = BUFSIZ;
|
||||
return (__SNPT);
|
||||
}
|
||||
|
||||
/*
|
||||
* Optimise fseek() only if it is a regular file. (The test for
|
||||
* __sseek is mainly paranoia.) It is safe to set _blksize
|
||||
* unconditionally; it will only be used if __SOPT is also set.
|
||||
*/
|
||||
*bufsize = st.st_blksize;
|
||||
fp->_blksize = st.st_blksize;
|
||||
return ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ?
|
||||
__SOPT : __SNPT);
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:mktemp.c"
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
@ -31,20 +32,10 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is formatted with tab stops every 8 columns
|
||||
*
|
||||
* $Id: mktemp.c,v 1.1 1997/02/28 05:12:49 gdr Exp $
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#pragma optimize 0
|
||||
#pragma debug 0
|
||||
#pragma memorymodel 0
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
@ -57,7 +48,7 @@ static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
||||
#include <ctype.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int _gettemp(char *path, int *doopen);
|
||||
static int _gettemp(char *path, register int *doopen);
|
||||
|
||||
int
|
||||
mkstemp(char *path)
|
||||
@ -80,7 +71,6 @@ _gettemp(char *path, register int *doopen)
|
||||
register char *start, *trv;
|
||||
struct stat sbuf;
|
||||
u_int pid;
|
||||
char savechar;
|
||||
|
||||
pid = getpid();
|
||||
for (trv = path; *trv; ++trv); /* extra X's get set to 0's */
|
||||
@ -96,8 +86,7 @@ _gettemp(char *path, register int *doopen)
|
||||
for (start = trv + 1;; --trv) {
|
||||
if (trv <= path)
|
||||
break;
|
||||
if (*trv == '/' || *trv == ':') {
|
||||
savechar = *trv;
|
||||
if (*trv == '/') {
|
||||
*trv = '\0';
|
||||
if (stat(path, &sbuf))
|
||||
return(0);
|
||||
@ -105,7 +94,7 @@ _gettemp(char *path, register int *doopen)
|
||||
errno = ENOTDIR;
|
||||
return(0);
|
||||
}
|
||||
*trv = savechar;
|
||||
*trv = '/';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1,129 +1,79 @@
|
||||
/*
|
||||
* Implementation by Devin Reade.
|
||||
* Copyright (c) 1988, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* $Id: perror.c,v 1.2 1997/07/27 23:10:57 gdr Exp $
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* This file is formatted with tab stops every 8 columns.
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* I have to do this until I can modify ORCALib */
|
||||
#if 0
|
||||
#define sys_errlist _gno_sys_errlist
|
||||
#define sys_nerr _gno_sys_nerr
|
||||
#endif
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#pragma databank 1
|
||||
#pragma optimize 0
|
||||
#pragma debug 0
|
||||
#pragma memorymodel 0
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)perror.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/errno.h> /* for ELAST */
|
||||
#include <stdio.h> /* for remainder */
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char * const sys_errlist[] = {
|
||||
|
||||
/* the following are used by both GNO and the Orca/Shell */
|
||||
|
||||
"unknown error", /* 0 */
|
||||
"domain error", /* 1 */
|
||||
"result too large", /* 2 */
|
||||
"not enough memory", /* 3 */
|
||||
"no such file or directory", /* 4 */
|
||||
"I/O error", /* 5 */
|
||||
"invalid argument", /* 6 */
|
||||
"bad file descriptor", /* 7 */
|
||||
"too many open files", /* 8 */
|
||||
"permission denied", /* 9 */
|
||||
"file exists", /* 10 */
|
||||
"no space left on device", /* 11 */
|
||||
|
||||
/* the following are GNO-specific */
|
||||
|
||||
"operation not permitted", /* 12 */
|
||||
"no such process", /* 13 */
|
||||
"interrupted system call", /* 14 */
|
||||
"arg list too long", /* 15 */
|
||||
"exec format error", /* 16 */
|
||||
"no child processes", /* 17 */
|
||||
"resource unavailable", /* 18 */
|
||||
"not a directory", /* 19 */
|
||||
"inappropriate ioctl for device", /* 20 */
|
||||
"broken pipe", /* 21 */
|
||||
"illegal seek", /* 22 */
|
||||
"block device required", /* 23 */
|
||||
"is a directory", /* 24 */
|
||||
"not a socket", /* 25 */
|
||||
"destination address required", /* 26 */
|
||||
"message too long", /* 27 */
|
||||
"wrong protocol for socket", /* 28 */
|
||||
"protocol not available", /* 29 */
|
||||
"protocol not supported", /* 30 */
|
||||
"socket type not supported", /* 31 */
|
||||
"operation not supported on socket", /* 32 */
|
||||
"protocol family not supported", /* 33 */
|
||||
"address family not supported", /* 34 */
|
||||
"address already in use", /* 35 */
|
||||
"can't assign requested address", /* 36 */
|
||||
"network is down", /* 37 */
|
||||
"network is unreachable", /* 38 */
|
||||
"network dropped connection on reset", /* 39 */
|
||||
"connection aborted", /* 40 */
|
||||
"connection reset by peer", /* 41 */
|
||||
"no buffer space available", /* 42 */
|
||||
"socket is already connected", /* 43 */
|
||||
"socket is not connected", /* 44 */
|
||||
"can't send after socket shutdown", /* 45 */
|
||||
"too many references: can't splice", /* 46 */
|
||||
"connection timed out", /* 47 */
|
||||
"connection refused", /* 48 */
|
||||
"operation would block", /* 49 */
|
||||
"operation now in progress", /* 50 */
|
||||
"operation already in progress", /* 51 */
|
||||
"bad address", /* 52 */
|
||||
"no such device", /* 53 */
|
||||
"host is down", /* 54 */
|
||||
"no route to host", /* 55 */
|
||||
#define SYS_NERR 56 /* 55 + 1 for zeroth entry */
|
||||
};
|
||||
|
||||
#if (ELAST + 1 != SYS_NERR)
|
||||
#error message table out of sync
|
||||
#endif
|
||||
|
||||
const int
|
||||
sys_nerr = SYS_NERR;
|
||||
|
||||
const char * const *
|
||||
_errnoText = sys_errlist; /* backward compatible */
|
||||
|
||||
char *
|
||||
strerror (int errnum)
|
||||
#if 0
|
||||
/* I need to implement writev(2) */
|
||||
void
|
||||
perror(const char *s)
|
||||
{
|
||||
/*
|
||||
* the size of buff must be greater than
|
||||
* strlen(sys_errlist[0]) + max number of digits in an int + 3
|
||||
* == 13 + 5 + 3 == 21
|
||||
*/
|
||||
static char buff[30];
|
||||
|
||||
if (errnum > 0 || errnum < sys_nerr) {
|
||||
return sys_errlist[errnum];
|
||||
register struct iovec *v;
|
||||
struct iovec iov[4];
|
||||
|
||||
v = iov;
|
||||
if (s && *s) {
|
||||
v->iov_base = (char *)s;
|
||||
v->iov_len = strlen(s);
|
||||
v++;
|
||||
v->iov_base = ": ";
|
||||
v->iov_len = 2;
|
||||
v++;
|
||||
}
|
||||
sprintf(buff, "unknown error: %d", errnum);
|
||||
return buff;
|
||||
v->iov_base = strerror(errno);
|
||||
v->iov_len = strlen(v->iov_base);
|
||||
v++;
|
||||
v->iov_base = "\n";
|
||||
v->iov_len = 1;
|
||||
(void)writev(STDERR_FILENO, iov, (v - iov) + 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* This implementation of perror should be replaced with one similar to
|
||||
* that for 4.4BSD, so that stdio doesn't need to get linked in.
|
||||
*/
|
||||
|
||||
#else
|
||||
void
|
||||
perror (char *s)
|
||||
perror (const char *s)
|
||||
{
|
||||
char *s1, *s2;
|
||||
|
||||
@ -139,3 +89,4 @@ perror (char *s)
|
||||
fprintf(stderr,"%s%s%s\n", s1, s2, sys_errlist[errno]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
75
lib/libc/stdio/printf.c
Normal file
75
lib/libc/stdio/printf.c
Normal file
@ -0,0 +1,75 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:printf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)printf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
printf(char const *fmt, ...)
|
||||
#else
|
||||
printf(fmt, va_alist)
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
ret = vfprintf(stdout, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
56
lib/libc/stdio/putc.c
Normal file
56
lib/libc/stdio/putc.c
Normal file
@ -0,0 +1,56 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)putc.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putc.
|
||||
*/
|
||||
#undef putc
|
||||
|
||||
int
|
||||
putc(int c, register FILE *fp)
|
||||
{
|
||||
return (__sputc(c, fp));
|
||||
}
|
58
lib/libc/stdio/putchar.c
Normal file
58
lib/libc/stdio/putchar.c
Normal file
@ -0,0 +1,58 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)putchar.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#undef putchar
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putchar
|
||||
*/
|
||||
int
|
||||
putchar(int c)
|
||||
{
|
||||
register FILE *so = stdout;
|
||||
|
||||
return (__sputc(c, so));
|
||||
}
|
67
lib/libc/stdio/puts.c
Normal file
67
lib/libc/stdio/puts.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)puts.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "fvwrite.h"
|
||||
|
||||
/*
|
||||
* Write the given string to stdout, appending a newline.
|
||||
*/
|
||||
int
|
||||
puts(char const *s)
|
||||
{
|
||||
size_t c = strlen(s);
|
||||
struct __suio uio;
|
||||
struct __siov iov[2];
|
||||
|
||||
iov[0].iov_base = (void *)s;
|
||||
iov[0].iov_len = c;
|
||||
iov[1].iov_base = "\n";
|
||||
iov[1].iov_len = 1;
|
||||
uio.uio_resid = c + 1;
|
||||
uio.uio_iov = &iov[0];
|
||||
uio.uio_iovcnt = 2;
|
||||
return (__sfvwrite(stdout, &uio) ? EOF : '\n');
|
||||
}
|
59
lib/libc/stdio/putw.c
Normal file
59
lib/libc/stdio/putw.c
Normal file
@ -0,0 +1,59 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)putw.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "fvwrite.h"
|
||||
|
||||
int
|
||||
putw(int w, FILE *fp)
|
||||
{
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
iov.iov_base = &w;
|
||||
iov.iov_len = uio.uio_resid = sizeof(w);
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
return (__sfvwrite(fp, &uio));
|
||||
}
|
148
lib/libc/stdio/refill.c
Normal file
148
lib/libc/stdio/refill.c
Normal file
@ -0,0 +1,148 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:refill.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)refill.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
static int
|
||||
lflush(FILE *fp)
|
||||
{
|
||||
|
||||
if ((fp->_flags & (__SLBF|__SWR)) == __SLBF|__SWR)
|
||||
return (__sflush(fp));
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Refill a stdio buffer.
|
||||
* Return EOF on eof or error, 0 otherwise.
|
||||
*/
|
||||
int
|
||||
__srefill(register FILE *fp)
|
||||
{
|
||||
#ifdef __GNO__
|
||||
char *nl_p, *nl_q;
|
||||
#endif
|
||||
|
||||
/* make sure stdio is set up */
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
|
||||
fp->_r = 0; /* largely a convenience for callers */
|
||||
|
||||
/* SysV does not make this test; take it out for compatibility */
|
||||
if (fp->_flags & __SEOF)
|
||||
return (EOF);
|
||||
|
||||
/* if not already reading, have to be reading and writing */
|
||||
if ((fp->_flags & __SRD) == 0) {
|
||||
if ((fp->_flags & __SRW) == 0) {
|
||||
errno = EBADF;
|
||||
return (EOF);
|
||||
}
|
||||
/* switch to reading */
|
||||
if (fp->_flags & __SWR) {
|
||||
if (__sflush(fp))
|
||||
return (EOF);
|
||||
fp->_flags &= ~__SWR;
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = 0;
|
||||
}
|
||||
fp->_flags |= __SRD;
|
||||
} else {
|
||||
/*
|
||||
* We were reading. If there is an ungetc buffer,
|
||||
* we must have been reading from that. Drop it,
|
||||
* restoring the previous buffer (if any). If there
|
||||
* is anything in that buffer, return.
|
||||
*/
|
||||
if (HASUB(fp)) {
|
||||
FREEUB(fp);
|
||||
if ((fp->_r = fp->_ur) != 0) {
|
||||
fp->_p = fp->_up;
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fp->_bf._base == NULL)
|
||||
__smakebuf(fp);
|
||||
|
||||
/*
|
||||
* Before reading from a line buffered or unbuffered file,
|
||||
* flush all line buffered output files, per the ANSI C
|
||||
* standard.
|
||||
*/
|
||||
if (fp->_flags & (__SLBF|__SNBF))
|
||||
(void) _fwalk(lflush);
|
||||
fp->_p = fp->_bf._base;
|
||||
fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size);
|
||||
fp->_flags &= ~__SMOD; /* buffer contents are again pristine */
|
||||
if (fp->_r <= 0) {
|
||||
if (fp->_r == 0)
|
||||
fp->_flags |= __SEOF;
|
||||
else {
|
||||
fp->_r = 0;
|
||||
fp->_flags |= __SERR;
|
||||
}
|
||||
return (EOF);
|
||||
}
|
||||
#ifdef __GNO__
|
||||
/* convert newlines inline -- no side effects */
|
||||
if ((fp->_flags & __SBIN) == 0) {
|
||||
nl_p = (char *)fp->_p;
|
||||
nl_q = nl_p + fp->_r;
|
||||
while (nl_p < nl_q) {
|
||||
if (*nl_p == '\r') {
|
||||
*nl_p = '\n';
|
||||
}
|
||||
nl_p++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return (0);
|
||||
}
|
52
lib/libc/stdio/remove.c
Normal file
52
lib/libc/stdio/remove.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)remove.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
remove(const char *file)
|
||||
{
|
||||
return (unlink(file));
|
||||
}
|
54
lib/libc/stdio/rewind.c
Normal file
54
lib/libc/stdio/rewind.c
Normal file
@ -0,0 +1,54 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)rewind.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void
|
||||
rewind(register FILE *fp)
|
||||
{
|
||||
(void) fseek(fp, 0L, SEEK_SET);
|
||||
clearerr(fp);
|
||||
errno = 0; /* not required, but seems reasonable */
|
||||
}
|
61
lib/libc/stdio/rget.c
Normal file
61
lib/libc/stdio/rget.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)rget.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Handle getc() when the buffer ran out:
|
||||
* Refill, then return the first character
|
||||
* in the newly-filled buffer.
|
||||
*/
|
||||
int
|
||||
__srget(register FILE *fp)
|
||||
{
|
||||
if (__srefill(fp) == 0) {
|
||||
fp->_r--;
|
||||
return (*fp->_p++);
|
||||
}
|
||||
return (EOF);
|
||||
}
|
75
lib/libc/stdio/scanf.c
Normal file
75
lib/libc/stdio/scanf.c
Normal file
@ -0,0 +1,75 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:scanf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)scanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
scanf(char const *fmt, ...)
|
||||
#else
|
||||
scanf(fmt, va_alist)
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
ret = __svfscanf(stdin, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
52
lib/libc/stdio/setbuf.c
Normal file
52
lib/libc/stdio/setbuf.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)setbuf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
void
|
||||
setbuf(FILE *fp, char *buf)
|
||||
{
|
||||
(void) setvbuf(fp, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
|
||||
}
|
62
lib/libc/stdio/setbuffer.c
Normal file
62
lib/libc/stdio/setbuffer.c
Normal file
@ -0,0 +1,62 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)setbuffer.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void
|
||||
setbuffer(register FILE *fp, char *buf, int size)
|
||||
{
|
||||
|
||||
(void)setvbuf(fp, buf, buf ? _IOFBF : _IONBF, size);
|
||||
}
|
||||
|
||||
/*
|
||||
* set line buffering
|
||||
*/
|
||||
int
|
||||
setlinebuf(FILE *fp)
|
||||
{
|
||||
|
||||
return (setvbuf(fp, (char *)NULL, _IOLBF, (size_t)0));
|
||||
}
|
161
lib/libc/stdio/setvbuf.c
Normal file
161
lib/libc/stdio/setvbuf.c
Normal file
@ -0,0 +1,161 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)setvbuf.c 8.2 (Berkeley) 11/16/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Set one of the three kinds of buffering, optionally including
|
||||
* a buffer.
|
||||
*/
|
||||
int
|
||||
setvbuf(register FILE *fp, char *buf, register int mode, register size_t size)
|
||||
{
|
||||
register int ret, flags;
|
||||
size_t iosize;
|
||||
int ttyflag;
|
||||
|
||||
/*
|
||||
* Verify arguments. The `int' limit on `size' is due to this
|
||||
* particular implementation. Note, buf and size are ignored
|
||||
* when setting _IONBF.
|
||||
*/
|
||||
if (mode != _IONBF)
|
||||
if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
|
||||
return (EOF);
|
||||
|
||||
/*
|
||||
* Write current buffer, if any. Discard unread input (including
|
||||
* ungetc data), cancel line buffering, and free old buffer if
|
||||
* malloc()ed. We also clear any eof condition, as if this were
|
||||
* a seek.
|
||||
*/
|
||||
ret = 0;
|
||||
(void)__sflush(fp);
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_r = fp->_lbfsize = 0;
|
||||
flags = fp->_flags;
|
||||
if (flags & __SMBF)
|
||||
free((void *)fp->_bf._base);
|
||||
flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
|
||||
|
||||
/* If setting unbuffered mode, skip all the hard work. */
|
||||
if (mode == _IONBF)
|
||||
goto nbf;
|
||||
|
||||
/*
|
||||
* Find optimal I/O size for seek optimization. This also returns
|
||||
* a `tty flag' to suggest that we check isatty(fd), but we do not
|
||||
* care since our caller told us how to buffer.
|
||||
*/
|
||||
flags |= __swhatbuf(fp, &iosize, &ttyflag);
|
||||
if (size == 0) {
|
||||
buf = NULL; /* force local allocation */
|
||||
size = iosize;
|
||||
}
|
||||
|
||||
/* Allocate buffer if needed. */
|
||||
if (buf == NULL) {
|
||||
if ((buf = malloc(size)) == NULL) {
|
||||
/*
|
||||
* Unable to honor user's request. We will return
|
||||
* failure, but try again with file system size.
|
||||
*/
|
||||
ret = EOF;
|
||||
if (size != iosize) {
|
||||
size = iosize;
|
||||
buf = malloc(size);
|
||||
}
|
||||
}
|
||||
if (buf == NULL) {
|
||||
/* No luck; switch to unbuffered I/O. */
|
||||
nbf:
|
||||
fp->_flags = flags | __SNBF;
|
||||
fp->_w = 0;
|
||||
fp->_bf._base = fp->_p = fp->_nbuf;
|
||||
fp->_bf._size = 1;
|
||||
return (ret);
|
||||
}
|
||||
flags |= __SMBF;
|
||||
}
|
||||
|
||||
/*
|
||||
* Kill any seek optimization if the buffer is not the
|
||||
* right size.
|
||||
*
|
||||
* SHOULD WE ALLOW MULTIPLES HERE (i.e., ok iff (size % iosize) == 0)?
|
||||
*/
|
||||
if (size != iosize)
|
||||
flags |= __SNPT;
|
||||
|
||||
/*
|
||||
* Fix up the FILE fields, and set __cleanup for output flush on
|
||||
* exit (since we are buffered in some way).
|
||||
*/
|
||||
if (mode == _IOLBF)
|
||||
flags |= __SLBF;
|
||||
fp->_flags = flags;
|
||||
fp->_bf._base = fp->_p = (unsigned char *)buf;
|
||||
fp->_bf._size = size;
|
||||
/* fp->_lbfsize is still 0 */
|
||||
if (flags & __SWR) {
|
||||
/*
|
||||
* Begin or continue writing: see __swsetup(). Note
|
||||
* that __SNBF is impossible (it was handled earlier).
|
||||
*/
|
||||
if (flags & __SLBF) {
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = -fp->_bf._size;
|
||||
} else
|
||||
fp->_w = size;
|
||||
} else {
|
||||
/* begin/continue reading, or stay in intermediate state */
|
||||
fp->_w = 0;
|
||||
}
|
||||
__cleanup = _cleanup;
|
||||
|
||||
return (ret);
|
||||
}
|
84
lib/libc/stdio/snprintf.c
Normal file
84
lib/libc/stdio/snprintf.c
Normal file
@ -0,0 +1,84 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:snprintf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)snprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
snprintf(char *str, size_t n, char const *fmt, ...)
|
||||
#else
|
||||
snprintf(str, n, fmt, va_alist)
|
||||
char *str;
|
||||
size_t n;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
if ((int)n < 1)
|
||||
return (EOF);
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *)str;
|
||||
f._bf._size = f._w = n - 1;
|
||||
ret = vfprintf(&f, fmt, ap);
|
||||
*f._p = 0;
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
83
lib/libc/stdio/sprintf.c
Normal file
83
lib/libc/stdio/sprintf.c
Normal file
@ -0,0 +1,83 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:sprintf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)sprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include <limits.h>
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
sprintf(char *str, char const *fmt, ...)
|
||||
#else
|
||||
sprintf(str, fmt, va_alist)
|
||||
char *str;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *)str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
ret = vfprintf(&f, fmt, ap);
|
||||
va_end(ap);
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
96
lib/libc/stdio/sscanf.c
Normal file
96
lib/libc/stdio/sscanf.c
Normal file
@ -0,0 +1,96 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:sscanf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)sscanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
/* ARGSUSED */
|
||||
static ssize_t
|
||||
eofread(void *cookie, char *buf, size_t len)
|
||||
{
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
#ifdef __ORCAC__
|
||||
#pragma optimize 72
|
||||
#pragma debug 0
|
||||
#endif
|
||||
|
||||
int
|
||||
#if __STDC__
|
||||
sscanf(const char *str, char const *fmt, ...)
|
||||
#else
|
||||
sscanf(str, fmt, va_alist)
|
||||
char *str;
|
||||
char *fmt;
|
||||
va_dcl
|
||||
#endif
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SRD;
|
||||
f._bf._base = f._p = (unsigned char *)str;
|
||||
f._bf._size = f._r = strlen(str);
|
||||
f._read = eofread;
|
||||
f._ub._base = NULL;
|
||||
f._lb._base = NULL;
|
||||
#if __STDC__
|
||||
va_start(ap, fmt);
|
||||
#else
|
||||
va_start(ap);
|
||||
#endif
|
||||
ret = __svfscanf(&f, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
103
lib/libc/stdio/stdio.c
Normal file
103
lib/libc/stdio/stdio.c
Normal file
@ -0,0 +1,103 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:stdio.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)stdio.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Small standard I/O/seek/close functions.
|
||||
* These maintain the `known seek offset' for seek optimisation.
|
||||
*/
|
||||
ssize_t
|
||||
__sread(void *cookie, char *buf, size_t n)
|
||||
{
|
||||
register FILE *fp = cookie;
|
||||
register int ret;
|
||||
|
||||
ret = read(fp->_file, buf, n);
|
||||
/* if the read succeeded, update the current offset */
|
||||
if (ret >= 0) {
|
||||
fp->_offset += ret;
|
||||
} else {
|
||||
fp->_flags &= ~__SOFF; /* paranoia */
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
ssize_t
|
||||
__swrite(void *cookie, char const *buf, size_t n)
|
||||
{
|
||||
register FILE *fp = cookie;
|
||||
|
||||
if (fp->_flags & __SAPP)
|
||||
(void) lseek(fp->_file, (off_t)0, SEEK_END);
|
||||
fp->_flags &= ~__SOFF; /* in case FAPPEND mode is set */
|
||||
return (write(fp->_file, buf, n));
|
||||
}
|
||||
|
||||
fpos_t
|
||||
__sseek(void *cookie, fpos_t offset, int whence)
|
||||
{
|
||||
register FILE *fp = cookie;
|
||||
register off_t ret;
|
||||
|
||||
ret = lseek(fp->_file, (off_t)offset, whence);
|
||||
if (ret == -1L)
|
||||
fp->_flags &= ~__SOFF;
|
||||
else {
|
||||
fp->_flags |= __SOFF;
|
||||
fp->_offset = ret;
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
__sclose(void *cookie)
|
||||
{
|
||||
|
||||
return (close(((FILE *)cookie)->_file));
|
||||
}
|
@ -1,112 +1,90 @@
|
||||
/*
|
||||
* Temporary file and filename routines.
|
||||
* Copyright (c) 1988, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* $Id: tempnam.c,v 1.1 1997/02/28 05:12:49 gdr Exp $
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* This file is formatted with tab stops every 8 characters.
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#pragma optimize 0
|
||||
#pragma debug 0
|
||||
#pragma memorymodel 0
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)tempnam.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define max(A,B) (((A)<(B))?(B):(A))
|
||||
|
||||
/*
|
||||
* tempnam
|
||||
*
|
||||
* Generate a pathname for a temporary file.
|
||||
*
|
||||
* tempnam will select a directory for the temporary file by using the
|
||||
* following criteria:
|
||||
*
|
||||
* If dir is not the NULL pointer, tempnam uses the pathname pointed to by
|
||||
* dir as the directory,
|
||||
*
|
||||
* otherwise, tmpdir uses the value of the TMPDIR environment variable if
|
||||
* the variable is defined,
|
||||
*
|
||||
* otherwise the directory defined by P_tmpdir in the stdio.h header file
|
||||
* if that directory is writable by the caller,
|
||||
*
|
||||
* otherwise, tempnam will use "/tmp" as a last resort.
|
||||
*/
|
||||
|
||||
|
||||
static char seed[4]="AAA";
|
||||
|
||||
/*
|
||||
* cpdir - copy <str> into <buf>, removing the trailing directory separator
|
||||
* if necessary.
|
||||
*/
|
||||
|
||||
static char *
|
||||
cpdir(char *buf, char *str)
|
||||
{
|
||||
char *p, pbrk;
|
||||
|
||||
if(str != NULL) {
|
||||
strcpy(buf, str);
|
||||
p = buf + strlen(buf) -1;
|
||||
pbrk = strchr(buf,':') ? ':' : '/'; /* for GS/OS */
|
||||
if(*p == pbrk) *p = '\0';
|
||||
}
|
||||
return(buf);
|
||||
}
|
||||
|
||||
/*
|
||||
* tempnam
|
||||
* dir -- use this directory please (if non-NULL)
|
||||
* prefix -- use this (if non-NULL) as filename prefix
|
||||
*/
|
||||
#include <paths.h>
|
||||
#include <string.h>
|
||||
|
||||
char *
|
||||
tempnam (const char *dir, const char *prefix)
|
||||
tempnam(const char *dir, const char *pfx)
|
||||
{
|
||||
register char *p, *q, *tmpdir, pbrk;
|
||||
int tl=0, dl=0, pl;
|
||||
int sverrno;
|
||||
char *f, *name;
|
||||
|
||||
/* create a buffer <p> that's as large as necessary */
|
||||
pl = strlen(P_tmpdir);
|
||||
if( (tmpdir = getenv("TMPDIR")) != NULL ) tl = strlen(tmpdir);
|
||||
if( dir != NULL ) dl = strlen(dir);
|
||||
if( (p = malloc((unsigned int)(max(max(dl,tl),pl)+16))) == NULL )
|
||||
if (!(name = malloc(MAXPATHLEN)))
|
||||
return(NULL);
|
||||
*p = '\0';
|
||||
|
||||
#define PERM W_OK
|
||||
if (!pfx)
|
||||
pfx = "tmp.";
|
||||
|
||||
if( (dl == 0) || (access( cpdir(p, dir), PERM) != 0) )
|
||||
if( (tl == 0) || (access( cpdir(p, tmpdir), PERM) != 0) )
|
||||
if( access( cpdir(p, P_tmpdir), PERM) != 0 )
|
||||
if( access( cpdir(p, "/tmp"), PERM) != 0 )
|
||||
return(NULL);
|
||||
|
||||
pbrk = strchr(p,':') ? ':' : '/';
|
||||
q = p + strlen(p);
|
||||
*q++ = pbrk;
|
||||
*q = '\0';
|
||||
if(prefix) {
|
||||
*(p+strlen(p)+5) = '\0';
|
||||
(void)strncat(p, prefix, 5);
|
||||
if (f = getenv("TMPDIR")) {
|
||||
(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
|
||||
*(f + strlen(f) - 1) == '/'? "": "/", pfx);
|
||||
if (f = mktemp(name))
|
||||
return(f);
|
||||
}
|
||||
|
||||
strcat(p, seed);
|
||||
strcat(p, "XXXXXX");
|
||||
if (f = (char *)dir) {
|
||||
(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
|
||||
*(f + strlen(f) - 1) == '/'? "": "/", pfx);
|
||||
if (f = mktemp(name))
|
||||
return(f);
|
||||
}
|
||||
|
||||
q = seed;
|
||||
while(*q == 'Z') *q++ = 'A';
|
||||
++*q;
|
||||
f = P_tmpdir;
|
||||
(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
|
||||
if (f = mktemp(name))
|
||||
return(f);
|
||||
|
||||
if(*mktemp(p) == '\0') return(NULL);
|
||||
return(p);
|
||||
f = _PATH_TMP;
|
||||
(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
|
||||
if (f = mktemp(name))
|
||||
return(f);
|
||||
|
||||
sverrno = errno;
|
||||
free(name);
|
||||
errno = sverrno;
|
||||
return(NULL);
|
||||
}
|
||||
|
151
lib/libc/stdio/tmpfile.c
Normal file
151
lib/libc/stdio/tmpfile.c
Normal file
@ -0,0 +1,151 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)tmpfile.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <paths.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
FILE *
|
||||
tmpfile(void)
|
||||
{
|
||||
sigset_t set, oset;
|
||||
FILE *fp;
|
||||
int fd, sverrno;
|
||||
#define TRAILER "tmp.XXXXXX"
|
||||
#ifndef __GNO__
|
||||
char buf[sizeof(_PATH_TMP) + sizeof(TRAILER)];
|
||||
#else
|
||||
static char *buf = NULL;
|
||||
|
||||
if (buf == NULL) {
|
||||
buf = malloc(sizeof(_PATH_TMP) + sizeof(TRAILER) + 2);
|
||||
if (buf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
(void)memcpy(buf, _PATH_TMP, sizeof(_PATH_TMP) - 1);
|
||||
(void)memcpy(buf + sizeof(_PATH_TMP) - 1, TRAILER, sizeof(TRAILER));
|
||||
|
||||
sigfillset(&set);
|
||||
(void)sigprocmask(SIG_BLOCK, &set, &oset);
|
||||
|
||||
fd = mkstemp(buf);
|
||||
if (fd != -1)
|
||||
#ifdef __GNO__
|
||||
__sUnlinkAtExit(buf);
|
||||
#else
|
||||
(void)unlink(buf);
|
||||
#endif
|
||||
|
||||
(void)sigprocmask(SIG_SETMASK, &oset, NULL);
|
||||
|
||||
if (fd == -1)
|
||||
return (NULL);
|
||||
|
||||
if ((fp = fdopen(fd, "w+")) == NULL) {
|
||||
sverrno = errno;
|
||||
(void)close(fd);
|
||||
errno = sverrno;
|
||||
return (NULL);
|
||||
}
|
||||
return (fp);
|
||||
}
|
||||
|
||||
#ifdef __GNO__
|
||||
|
||||
typedef struct __sUnlinkList_t {
|
||||
struct __sUnlinkList_t *next;
|
||||
char *name;
|
||||
} __sUnlinkList_t;
|
||||
|
||||
static __sUnlinkList_t *unlinkList = NULL;
|
||||
|
||||
/*
|
||||
* __sUnlinkAtExit() - maintains a linked list of file names that
|
||||
* must be deleted when the program exits.
|
||||
*
|
||||
* __sExitDoUnlinks() - called at exit to delete the files
|
||||
*/
|
||||
|
||||
void
|
||||
__sUnlinkAtExit (const char *fname)
|
||||
{
|
||||
__sUnlinkList_t *elem;
|
||||
|
||||
/* make sure __sExitDoUnlinks() will be called */
|
||||
__cleanup = _cleanup;
|
||||
|
||||
elem = malloc(sizeof(__sUnlinkList_t) + strlen(fname) + 1);
|
||||
if (elem == NULL) {
|
||||
return; /* it won't get deleted; tough luck */
|
||||
}
|
||||
elem->next = unlinkList;
|
||||
elem->name = (char *)((unsigned long) elem + sizeof(__sUnlinkList_t));
|
||||
strcpy(elem->name, fname);
|
||||
unlinkList = elem;
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
__sExitDoUnlinks(void)
|
||||
{
|
||||
__sUnlinkList_t *elem;
|
||||
|
||||
elem = unlinkList;
|
||||
while (elem != NULL) {
|
||||
unlink(elem->name);
|
||||
elem = elem->next;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#endif
|
61
lib/libc/stdio/tmpnam.c
Normal file
61
lib/libc/stdio/tmpnam.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993, 1994
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)tmpnam.c 8.3 (Berkeley) 3/28/94";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char *
|
||||
tmpnam(char *s)
|
||||
{
|
||||
static u_long tmpcount;
|
||||
static char buf[L_tmpnam];
|
||||
|
||||
if (s == NULL)
|
||||
s = buf;
|
||||
(void)snprintf(s, L_tmpnam, "%stmp.%lu.XXXXXX", P_tmpdir, tmpcount);
|
||||
++tmpcount;
|
||||
return (mktemp(s));
|
||||
}
|
150
lib/libc/stdio/ungetc.c
Normal file
150
lib/libc/stdio/ungetc.c
Normal file
@ -0,0 +1,150 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)ungetc.c 8.2 (Berkeley) 11/3/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Expand the ungetc buffer `in place'. That is, adjust fp->_p when
|
||||
* the buffer moves, so that it points the same distance from the end,
|
||||
* and move the bytes in the buffer around as necessary so that they
|
||||
* are all at the end (stack-style).
|
||||
*/
|
||||
static int
|
||||
__submore(register FILE *fp)
|
||||
{
|
||||
register int i;
|
||||
register unsigned char *p;
|
||||
|
||||
if (fp->_ub._base == fp->_ubuf) {
|
||||
/*
|
||||
* Get a new buffer (rather than expanding the old one).
|
||||
*/
|
||||
if ((p = malloc((size_t)BUFSIZ)) == NULL)
|
||||
return (EOF);
|
||||
fp->_ub._base = p;
|
||||
fp->_ub._size = BUFSIZ;
|
||||
p += BUFSIZ - sizeof(fp->_ubuf);
|
||||
for (i = sizeof(fp->_ubuf); --i >= 0;)
|
||||
p[i] = fp->_ubuf[i];
|
||||
fp->_p = p;
|
||||
return (0);
|
||||
}
|
||||
i = fp->_ub._size;
|
||||
p = realloc(fp->_ub._base, i << 1);
|
||||
if (p == NULL)
|
||||
return (EOF);
|
||||
/* no overlap (hence can use memcpy) because we doubled the size */
|
||||
(void)memcpy((void *)(p + i), (void *)p, (size_t)i);
|
||||
fp->_p = p + i;
|
||||
fp->_ub._base = p;
|
||||
fp->_ub._size = i << 1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
ungetc(int c, register FILE *fp)
|
||||
{
|
||||
if (c == EOF)
|
||||
return (EOF);
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
if ((fp->_flags & __SRD) == 0) {
|
||||
/*
|
||||
* Not already reading: no good unless reading-and-writing.
|
||||
* Otherwise, flush any current write stuff.
|
||||
*/
|
||||
if ((fp->_flags & __SRW) == 0)
|
||||
return (EOF);
|
||||
if (fp->_flags & __SWR) {
|
||||
if (__sflush(fp))
|
||||
return (EOF);
|
||||
fp->_flags &= ~__SWR;
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = 0;
|
||||
}
|
||||
fp->_flags |= __SRD;
|
||||
}
|
||||
c = (unsigned char)c;
|
||||
|
||||
/*
|
||||
* If we are in the middle of ungetc'ing, just continue.
|
||||
* This may require expanding the current ungetc buffer.
|
||||
*/
|
||||
if (HASUB(fp)) {
|
||||
if (fp->_r >= fp->_ub._size && __submore(fp))
|
||||
return (EOF);
|
||||
*--fp->_p = c;
|
||||
fp->_r++;
|
||||
return (c);
|
||||
}
|
||||
fp->_flags &= ~__SEOF;
|
||||
|
||||
/*
|
||||
* If we can handle this by simply backing up, do so,
|
||||
* but never replace the original character.
|
||||
* (This makes sscanf() work when scanning `const' data.)
|
||||
*/
|
||||
if (fp->_bf._base != NULL && fp->_p > fp->_bf._base &&
|
||||
fp->_p[-1] == c) {
|
||||
fp->_p--;
|
||||
fp->_r++;
|
||||
return (c);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create an ungetc buffer.
|
||||
* Initially, we will use the `reserve' buffer.
|
||||
*/
|
||||
fp->_ur = fp->_r;
|
||||
fp->_up = fp->_p;
|
||||
fp->_ub._base = fp->_ubuf;
|
||||
fp->_ub._size = sizeof(fp->_ubuf);
|
||||
fp->_ubuf[sizeof(fp->_ubuf) - 1] = c;
|
||||
fp->_p = &fp->_ubuf[sizeof(fp->_ubuf) - 1];
|
||||
fp->_r = 1;
|
||||
return (c);
|
||||
}
|
915
lib/libc/stdio/vfprintf.c
Normal file
915
lib/libc/stdio/vfprintf.c
Normal file
@ -0,0 +1,915 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:vfprintf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "libc_stdio";
|
||||
#pragma debug -1
|
||||
#pragma optimize 72
|
||||
#endif
|
||||
|
||||
#ifdef __GNO__
|
||||
#define STATIC static
|
||||
#else
|
||||
#define STATIC
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
* Actual printf innards.
|
||||
*
|
||||
* This code is large and complicated...
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
/* Define FLOATING_POINT to get floating point. */
|
||||
#define FLOATING_POINT
|
||||
|
||||
/*
|
||||
* Flush out all the vectors defined by the given uio,
|
||||
* then reset it so that it can be reused.
|
||||
*/
|
||||
static int
|
||||
__sprint(FILE *fp, register struct __suio *uio)
|
||||
{
|
||||
register int err;
|
||||
|
||||
if (uio->uio_resid == 0) {
|
||||
uio->uio_iovcnt = 0;
|
||||
return (0);
|
||||
}
|
||||
err = __sfvwrite(fp, uio);
|
||||
uio->uio_resid = 0;
|
||||
uio->uio_iovcnt = 0;
|
||||
return (err);
|
||||
}
|
||||
|
||||
/*
|
||||
* Helper function for `fprintf to unbuffered unix file': creates a
|
||||
* temporary buffer. We only work on write-only files; this avoids
|
||||
* worries about ungetc buffers and so forth.
|
||||
*/
|
||||
static int
|
||||
__sbprintf(register FILE *fp, const char *fmt, va_list ap)
|
||||
{
|
||||
int ret;
|
||||
FILE fake;
|
||||
/* ifdef __GNO__ */
|
||||
#if 0
|
||||
char *buf;
|
||||
if (buf = malloc(BUFSIZ)) {
|
||||
/* not much we can do here */
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
unsigned char buf[BUFSIZ];
|
||||
#endif
|
||||
|
||||
/* copy the important variables */
|
||||
fake._flags = fp->_flags & ~__SNBF;
|
||||
fake._file = fp->_file;
|
||||
fake._cookie = fp->_cookie;
|
||||
fake._write = fp->_write;
|
||||
|
||||
/* set up the buffer */
|
||||
fake._bf._base = fake._p = buf;
|
||||
fake._bf._size = fake._w = sizeof(buf);
|
||||
fake._lbfsize = 0; /* not actually used, but Just In Case */
|
||||
|
||||
/* do the work, then copy any error status */
|
||||
ret = vfprintf(&fake, fmt, ap);
|
||||
if (ret >= 0 && fflush(&fake))
|
||||
ret = EOF;
|
||||
if (fake._flags & __SERR)
|
||||
fp->_flags |= __SERR;
|
||||
/* ifdef __GNO__ */
|
||||
#if 0
|
||||
free(buf);
|
||||
#endif
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Macros for converting digits to letters and vice versa
|
||||
*/
|
||||
#define to_digit(c) ((c) - '0')
|
||||
#define is_digit(c) ((unsigned)to_digit(c) <= 9)
|
||||
#define to_char(n) ((n) + '0')
|
||||
|
||||
/*
|
||||
* Convert an unsigned long to ASCII for printf purposes, returning
|
||||
* a pointer to the first character of the string representation.
|
||||
* Octal numbers can be forced to have a leading zero; hex numbers
|
||||
* use the given digits.
|
||||
*/
|
||||
static char *
|
||||
__ultoa(register u_long val, char *endp, int base, int octzero, char *xdigs)
|
||||
{
|
||||
register char *cp = endp;
|
||||
register long sval;
|
||||
|
||||
/*
|
||||
* Handle the three cases separately, in the hope of getting
|
||||
* better/faster code.
|
||||
*/
|
||||
switch (base) {
|
||||
case 10:
|
||||
if (val < 10) { /* many numbers are 1 digit */
|
||||
*--cp = to_char(val);
|
||||
return (cp);
|
||||
}
|
||||
/*
|
||||
* On many machines, unsigned arithmetic is harder than
|
||||
* signed arithmetic, so we do at most one unsigned mod and
|
||||
* divide; this is sufficient to reduce the range of
|
||||
* the incoming value to where signed arithmetic works.
|
||||
*/
|
||||
if (val > LONG_MAX) {
|
||||
*--cp = to_char(val % 10);
|
||||
sval = val / 10;
|
||||
} else
|
||||
sval = val;
|
||||
do {
|
||||
*--cp = to_char(sval % 10);
|
||||
sval /= 10;
|
||||
} while (sval != 0);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
do {
|
||||
*--cp = to_char(val & 7);
|
||||
val >>= 3;
|
||||
} while (val);
|
||||
if (octzero && *cp != '0')
|
||||
*--cp = '0';
|
||||
break;
|
||||
|
||||
case 16:
|
||||
do {
|
||||
*--cp = xdigs[val & 15];
|
||||
val >>= 4;
|
||||
} while (val);
|
||||
break;
|
||||
|
||||
default: /* oops */
|
||||
abort();
|
||||
}
|
||||
return (cp);
|
||||
}
|
||||
|
||||
#ifdef HAS_QUAD_T
|
||||
/* Identical to __ultoa, but for quads. */
|
||||
static char *
|
||||
__uqtoa(register u_quad_t val, char *endp, int base, int octzero, char *xdigs)
|
||||
{
|
||||
register char *cp = endp;
|
||||
register quad_t sval;
|
||||
|
||||
/* quick test for small values; __ultoa is typically much faster */
|
||||
/* (perhaps instead we should run until small, then call __ultoa?) */
|
||||
if (val <= ULONG_MAX)
|
||||
return (__ultoa((u_long)val, endp, base, octzero, xdigs));
|
||||
switch (base) {
|
||||
case 10:
|
||||
if (val < 10) {
|
||||
*--cp = to_char(val % 10);
|
||||
return (cp);
|
||||
}
|
||||
if (val > QUAD_MAX) {
|
||||
*--cp = to_char(val % 10);
|
||||
sval = val / 10;
|
||||
} else
|
||||
sval = val;
|
||||
do {
|
||||
*--cp = to_char(sval % 10);
|
||||
sval /= 10;
|
||||
} while (sval != 0);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
do {
|
||||
*--cp = to_char(val & 7);
|
||||
val >>= 3;
|
||||
} while (val);
|
||||
if (octzero && *cp != '0')
|
||||
*--cp = '0';
|
||||
break;
|
||||
|
||||
case 16:
|
||||
do {
|
||||
*--cp = xdigs[val & 15];
|
||||
val >>= 4;
|
||||
} while (val);
|
||||
break;
|
||||
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
return (cp);
|
||||
}
|
||||
#endif /* HAS_QUAD_T */
|
||||
|
||||
#ifdef FLOATING_POINT
|
||||
#include <math.h>
|
||||
#include "floatio.h"
|
||||
|
||||
#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */
|
||||
#define DEFPREC 6
|
||||
|
||||
#ifdef __ORCAC__
|
||||
typedef extended double_spec_t;
|
||||
typedef extended ldouble_spec_t;
|
||||
#else
|
||||
typedef double double_spec_t;
|
||||
typedef long double ldouble_spec_t;
|
||||
#endif
|
||||
|
||||
static char *cvt __P((double_spec_t, int, int, char *, int *, int, int *));
|
||||
static int exponent __P((char *, int, int));
|
||||
|
||||
#else /* no FLOATING_POINT */
|
||||
|
||||
#define BUF 68
|
||||
|
||||
#endif /* FLOATING_POINT */
|
||||
|
||||
/*
|
||||
* Flags used during conversion.
|
||||
*/
|
||||
#define ALT 0x001 /* alternate form */
|
||||
#define HEXPREFIX 0x002 /* add 0x or 0X prefix */
|
||||
#define LADJUST 0x004 /* left adjustment */
|
||||
#define LONGDBL 0x008 /* long double; unimplemented */
|
||||
#define LONGINT 0x010 /* long integer */
|
||||
#ifdef HAS_QUAD_T
|
||||
#define QUADINT 0x020 /* quad integer */
|
||||
#endif
|
||||
#define SHORTINT 0x040 /* short integer */
|
||||
#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
|
||||
#define FPT 0x100 /* Floating point number */
|
||||
|
||||
int
|
||||
vfprintf(FILE *fp, const char *fmt0, va_list ap)
|
||||
{
|
||||
register char *fmt; /* format string */
|
||||
register int ch; /* character from fmt */
|
||||
register int n; /* handy integer (short term usage) */
|
||||
register char *cp; /* handy char pointer (short term usage) */
|
||||
register struct __siov *iovp;/* for PRINT macro */
|
||||
register int flags; /* flags as above */
|
||||
int ret; /* return value accumulator */
|
||||
int width; /* width from format (%8d), or 0 */
|
||||
int prec; /* precision from format (%.3d), or -1 */
|
||||
char sign; /* sign prefix (' ', '+', '-', or \0) */
|
||||
#ifdef FLOATING_POINT
|
||||
char softsign; /* temporary negative sign for floats */
|
||||
double_spec_t _double; /* double precision arguments %[eEfgG] */
|
||||
int expt; /* integer value of exponent */
|
||||
int expsize; /* character count for expstr */
|
||||
int ndig; /* actual number of digits returned by cvt */
|
||||
char expstr[7]; /* buffer for exponent string */
|
||||
#endif
|
||||
u_long ulval; /* integer arguments %[diouxX] */
|
||||
#ifdef HAS_QUAD_T
|
||||
u_quad_t uqval; /* %q integers */
|
||||
#endif
|
||||
int base; /* base for [diouxX] conversion */
|
||||
int dprec; /* a copy of prec if [diouxX], 0 otherwise */
|
||||
int fieldsz; /* field size expanded by sign, etc */
|
||||
int realsz; /* field size expanded by dprec */
|
||||
int size; /* size of converted field or string */
|
||||
char *xdigs; /* digits for [xX] conversion */
|
||||
#define NIOV 8
|
||||
struct __suio uio; /* output information: summary */
|
||||
struct __siov iov[NIOV];/* ... and individual io vectors */
|
||||
char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */
|
||||
char ox[2]; /* space for 0x hex-prefix */
|
||||
|
||||
/*
|
||||
* Choose PADSIZE to trade efficiency vs. size. If larger printf
|
||||
* fields occur frequently, increase PADSIZE and make the initialisers
|
||||
* below longer.
|
||||
*/
|
||||
#define PADSIZE 16 /* pad chunk size */
|
||||
static char blanks[PADSIZE] =
|
||||
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
|
||||
static char zeroes[PADSIZE] =
|
||||
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
|
||||
|
||||
/*
|
||||
* BEWARE, these `goto error' on error, and PAD uses `n'.
|
||||
*/
|
||||
#define PRINT(ptr, len) { \
|
||||
iovp->iov_base = (ptr); \
|
||||
iovp->iov_len = (len); \
|
||||
uio.uio_resid += (len); \
|
||||
iovp++; \
|
||||
if (++uio.uio_iovcnt >= NIOV) { \
|
||||
if (__sprint(fp, &uio)) \
|
||||
goto error; \
|
||||
iovp = iov; \
|
||||
} \
|
||||
}
|
||||
#define PAD(howmany, with) { \
|
||||
if ((n = (howmany)) > 0) { \
|
||||
while (n > PADSIZE) { \
|
||||
PRINT(with, PADSIZE); \
|
||||
n -= PADSIZE; \
|
||||
} \
|
||||
PRINT(with, n); \
|
||||
} \
|
||||
}
|
||||
#define FLUSH() { \
|
||||
if (uio.uio_resid && __sprint(fp, &uio)) \
|
||||
goto error; \
|
||||
uio.uio_iovcnt = 0; \
|
||||
iovp = iov; \
|
||||
}
|
||||
|
||||
/*
|
||||
* To extend shorts properly, we need both signed and unsigned
|
||||
* argument extraction methods.
|
||||
*/
|
||||
#define SARG() \
|
||||
(flags&LONGINT ? va_arg(ap, long) : \
|
||||
flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
|
||||
(long)va_arg(ap, int))
|
||||
#define UARG() \
|
||||
(flags&LONGINT ? va_arg(ap, u_long) : \
|
||||
flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
|
||||
(u_long)va_arg(ap, u_int))
|
||||
|
||||
/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
|
||||
if (cantwrite(fp))
|
||||
return (EOF);
|
||||
|
||||
/* optimise fprintf(stderr) (and other unbuffered Unix files) */
|
||||
if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
|
||||
fp->_file >= 0)
|
||||
return (__sbprintf(fp, fmt0, ap));
|
||||
|
||||
fmt = (char *)fmt0;
|
||||
uio.uio_iov = iovp = iov;
|
||||
uio.uio_resid = 0;
|
||||
uio.uio_iovcnt = 0;
|
||||
ret = 0;
|
||||
|
||||
/*
|
||||
* Scan the format for conversions (`%' character).
|
||||
*/
|
||||
for (;;) {
|
||||
for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
|
||||
/* void */;
|
||||
if ((n = fmt - cp) != 0) {
|
||||
PRINT(cp, n);
|
||||
ret += n;
|
||||
}
|
||||
if (ch == '\0')
|
||||
goto done;
|
||||
fmt++; /* skip over '%' */
|
||||
|
||||
flags = 0;
|
||||
dprec = 0;
|
||||
width = 0;
|
||||
prec = -1;
|
||||
sign = '\0';
|
||||
|
||||
rflag: ch = *fmt++;
|
||||
reswitch: switch (ch) {
|
||||
case ' ':
|
||||
/*
|
||||
* ``If the space and + flags both appear, the space
|
||||
* flag will be ignored.''
|
||||
* -- ANSI X3J11
|
||||
*/
|
||||
if (!sign)
|
||||
sign = ' ';
|
||||
goto rflag;
|
||||
case '#':
|
||||
flags |= ALT;
|
||||
goto rflag;
|
||||
case '*':
|
||||
/*
|
||||
* ``A negative field width argument is taken as a
|
||||
* - flag followed by a positive field width.''
|
||||
* -- ANSI X3J11
|
||||
* They don't exclude field widths read from args.
|
||||
*/
|
||||
if ((width = va_arg(ap, int)) >= 0)
|
||||
goto rflag;
|
||||
width = -width;
|
||||
/* FALLTHROUGH */
|
||||
case '-':
|
||||
flags |= LADJUST;
|
||||
goto rflag;
|
||||
case '+':
|
||||
sign = '+';
|
||||
goto rflag;
|
||||
case '.':
|
||||
if ((ch = *fmt++) == '*') {
|
||||
n = va_arg(ap, int);
|
||||
prec = n < 0 ? -1 : n;
|
||||
goto rflag;
|
||||
}
|
||||
n = 0;
|
||||
while (is_digit(ch)) {
|
||||
n = 10 * n + to_digit(ch);
|
||||
ch = *fmt++;
|
||||
}
|
||||
prec = n < 0 ? -1 : n;
|
||||
goto reswitch;
|
||||
case '0':
|
||||
/*
|
||||
* ``Note that 0 is taken as a flag, not as the
|
||||
* beginning of a field width.''
|
||||
* -- ANSI X3J11
|
||||
*/
|
||||
flags |= ZEROPAD;
|
||||
goto rflag;
|
||||
case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
n = 0;
|
||||
do {
|
||||
n = 10 * n + to_digit(ch);
|
||||
ch = *fmt++;
|
||||
} while (is_digit(ch));
|
||||
width = n;
|
||||
goto reswitch;
|
||||
#ifdef FLOATING_POINT
|
||||
case 'L':
|
||||
flags |= LONGDBL;
|
||||
goto rflag;
|
||||
#endif
|
||||
case 'h':
|
||||
flags |= SHORTINT;
|
||||
goto rflag;
|
||||
case 'l':
|
||||
flags |= LONGINT;
|
||||
goto rflag;
|
||||
#ifdef HAS_QUAD_T
|
||||
case 'q':
|
||||
flags |= QUADINT;
|
||||
goto rflag;
|
||||
#endif
|
||||
case 'c':
|
||||
*(cp = buf) = va_arg(ap, int);
|
||||
size = 1;
|
||||
sign = '\0';
|
||||
break;
|
||||
case 'D':
|
||||
flags |= LONGINT;
|
||||
/*FALLTHROUGH*/
|
||||
case 'd':
|
||||
case 'i':
|
||||
#ifdef HAS_QUAD_T
|
||||
if (flags & QUADINT) {
|
||||
uqval = va_arg(ap, quad_t);
|
||||
if ((quad_t)uqval < 0) {
|
||||
uqval = -uqval;
|
||||
sign = '-';
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
ulval = SARG();
|
||||
if ((long)ulval < 0) {
|
||||
ulval = -ulval;
|
||||
sign = '-';
|
||||
}
|
||||
}
|
||||
base = 10;
|
||||
goto number;
|
||||
#ifdef FLOATING_POINT
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'f':
|
||||
goto fp_begin;
|
||||
case 'g':
|
||||
case 'G':
|
||||
if (prec == 0)
|
||||
prec = 1;
|
||||
fp_begin: if (prec == -1)
|
||||
prec = DEFPREC;
|
||||
#ifndef __ORCAC__
|
||||
if (flags & LONGDBL)
|
||||
_double = (double_spec_t)va_arg(ap, ldouble_spec_t);
|
||||
else
|
||||
#endif
|
||||
_double = va_arg(ap, double_spec_t);
|
||||
/* do this before tricky precision changes */
|
||||
if (isinf(_double)) {
|
||||
if (_double < 0)
|
||||
sign = '-';
|
||||
cp = "Inf";
|
||||
size = 3;
|
||||
break;
|
||||
}
|
||||
if (isnan(_double)) {
|
||||
cp = "NaN";
|
||||
size = 3;
|
||||
break;
|
||||
}
|
||||
flags |= FPT;
|
||||
cp = cvt(_double, prec, flags, &softsign,
|
||||
&expt, ch, &ndig);
|
||||
if (ch == 'g' || ch == 'G') {
|
||||
if (expt <= -4 || expt > prec)
|
||||
ch = (ch == 'g') ? 'e' : 'E';
|
||||
else
|
||||
ch = 'g';
|
||||
}
|
||||
if (ch <= 'e') { /* 'e' or 'E' fmt */
|
||||
--expt;
|
||||
expsize = exponent(expstr, expt, ch);
|
||||
size = expsize + ndig;
|
||||
if (ndig > 1 || flags & ALT)
|
||||
++size;
|
||||
} else if (ch == 'f') { /* f fmt */
|
||||
if (expt > 0) {
|
||||
size = expt;
|
||||
if (prec || flags & ALT)
|
||||
size += prec + 1;
|
||||
} else /* "0.X" */
|
||||
size = prec + 2;
|
||||
} else if (expt >= ndig) { /* fixed g fmt */
|
||||
size = expt;
|
||||
if (flags & ALT)
|
||||
++size;
|
||||
} else
|
||||
size = ndig + (expt > 0 ?
|
||||
1 : 2 - expt);
|
||||
|
||||
if (softsign)
|
||||
sign = '-';
|
||||
break;
|
||||
#endif /* FLOATING_POINT */
|
||||
case 'n':
|
||||
#ifdef HAS_QUAD_T
|
||||
if (flags & QUADINT)
|
||||
*va_arg(ap, quad_t *) = ret;
|
||||
else
|
||||
#endif
|
||||
if (flags & LONGINT)
|
||||
*va_arg(ap, long *) = ret;
|
||||
else if (flags & SHORTINT)
|
||||
*va_arg(ap, short *) = ret;
|
||||
else
|
||||
*va_arg(ap, int *) = ret;
|
||||
continue; /* no output */
|
||||
case 'O':
|
||||
flags |= LONGINT;
|
||||
/*FALLTHROUGH*/
|
||||
case 'o':
|
||||
#ifdef HAS_QUAD_T
|
||||
if (flags & QUADINT)
|
||||
uqval = va_arg(ap, u_quad_t);
|
||||
else
|
||||
#endif
|
||||
ulval = UARG();
|
||||
base = 8;
|
||||
goto nosign;
|
||||
case 'p':
|
||||
/*
|
||||
* ``The argument shall be a pointer to void. The
|
||||
* value of the pointer is converted to a sequence
|
||||
* of printable characters, in an implementation-
|
||||
* defined manner.''
|
||||
* -- ANSI X3J11
|
||||
*/
|
||||
ulval = (u_long)va_arg(ap, void *);
|
||||
base = 16;
|
||||
xdigs = "0123456789abcdef";
|
||||
#ifdef HAS_QUAD_T
|
||||
flags = (flags & ~QUADINT) | HEXPREFIX;
|
||||
#else
|
||||
flags |= HEXPREFIX;
|
||||
#endif
|
||||
ch = 'x';
|
||||
goto nosign;
|
||||
case 's':
|
||||
if ((cp = va_arg(ap, char *)) == NULL)
|
||||
cp = "(null)";
|
||||
if (prec >= 0) {
|
||||
/*
|
||||
* can't use strlen; can only look for the
|
||||
* NUL in the first `prec' characters, and
|
||||
* strlen() will go further.
|
||||
*/
|
||||
char *p = memchr(cp, 0, prec);
|
||||
|
||||
if (p != NULL) {
|
||||
size = p - cp;
|
||||
if (size > prec)
|
||||
size = prec;
|
||||
} else
|
||||
size = prec;
|
||||
} else
|
||||
size = strlen(cp);
|
||||
sign = '\0';
|
||||
break;
|
||||
case 'U':
|
||||
flags |= LONGINT;
|
||||
/*FALLTHROUGH*/
|
||||
case 'u':
|
||||
#ifdef HAS_QUAD_T
|
||||
if (flags & QUADINT)
|
||||
uqval = va_arg(ap, u_quad_t);
|
||||
else
|
||||
#endif
|
||||
ulval = UARG();
|
||||
base = 10;
|
||||
goto nosign;
|
||||
case 'X':
|
||||
xdigs = "0123456789ABCDEF";
|
||||
goto hex;
|
||||
case 'x':
|
||||
xdigs = "0123456789abcdef";
|
||||
hex:
|
||||
#ifdef HAS_QUAD_T
|
||||
if (flags & QUADINT)
|
||||
uqval = va_arg(ap, u_quad_t);
|
||||
else
|
||||
#endif
|
||||
ulval = UARG();
|
||||
base = 16;
|
||||
/* leading 0x/X only if non-zero */
|
||||
if (flags & ALT &&
|
||||
#ifdef HAS_QUAD_T
|
||||
(flags & QUADINT ? uqval != 0 : ulval != 0))
|
||||
#else
|
||||
(ulval != 0))
|
||||
#endif
|
||||
flags |= HEXPREFIX;
|
||||
|
||||
/* unsigned conversions */
|
||||
nosign: sign = '\0';
|
||||
/*
|
||||
* ``... diouXx conversions ... if a precision is
|
||||
* specified, the 0 flag will be ignored.''
|
||||
* -- ANSI X3J11
|
||||
*/
|
||||
number: if ((dprec = prec) >= 0)
|
||||
flags &= ~ZEROPAD;
|
||||
|
||||
/*
|
||||
* ``The result of converting a zero value with an
|
||||
* explicit precision of zero is no characters.''
|
||||
* -- ANSI X3J11
|
||||
*/
|
||||
cp = buf + BUF;
|
||||
#ifdef HAS_QUAD_T
|
||||
if (flags & QUADINT) {
|
||||
if (uqval != 0 || prec != 0)
|
||||
cp = __uqtoa(uqval, cp, base,
|
||||
flags & ALT, xdigs);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
if (ulval != 0 || prec != 0)
|
||||
cp = __ultoa(ulval, cp, base,
|
||||
flags & ALT, xdigs);
|
||||
}
|
||||
size = buf + BUF - cp;
|
||||
break;
|
||||
default: /* "%?" prints ?, unless ? is NUL */
|
||||
if (ch == '\0')
|
||||
goto done;
|
||||
/* pretend it was %c with argument ch */
|
||||
cp = buf;
|
||||
*cp = ch;
|
||||
size = 1;
|
||||
sign = '\0';
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* All reasonable formats wind up here. At this point, `cp'
|
||||
* points to a string which (if not flags&LADJUST) should be
|
||||
* padded out to `width' places. If flags&ZEROPAD, it should
|
||||
* first be prefixed by any sign or other prefix; otherwise,
|
||||
* it should be blank padded before the prefix is emitted.
|
||||
* After any left-hand padding and prefixing, emit zeroes
|
||||
* required by a decimal [diouxX] precision, then print the
|
||||
* string proper, then emit zeroes required by any leftover
|
||||
* floating precision; finally, if LADJUST, pad with blanks.
|
||||
*
|
||||
* Compute actual size, so we know how much to pad.
|
||||
* fieldsz excludes decimal prec; realsz includes it.
|
||||
*/
|
||||
fieldsz = size;
|
||||
if (sign)
|
||||
fieldsz++;
|
||||
else if (flags & HEXPREFIX)
|
||||
fieldsz += 2;
|
||||
realsz = dprec > fieldsz ? dprec : fieldsz;
|
||||
|
||||
/* right-adjusting blank padding */
|
||||
if ((flags & (LADJUST|ZEROPAD)) == 0)
|
||||
PAD(width - realsz, blanks);
|
||||
|
||||
/* prefix */
|
||||
if (sign) {
|
||||
PRINT(&sign, 1);
|
||||
} else if (flags & HEXPREFIX) {
|
||||
ox[0] = '0';
|
||||
ox[1] = ch;
|
||||
PRINT(ox, 2);
|
||||
}
|
||||
|
||||
/* right-adjusting zero padding */
|
||||
if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
|
||||
PAD(width - realsz, zeroes);
|
||||
|
||||
/* leading zeroes from decimal precision */
|
||||
PAD(dprec - fieldsz, zeroes);
|
||||
|
||||
/* the string or number proper */
|
||||
#ifdef FLOATING_POINT
|
||||
if ((flags & FPT) == 0) {
|
||||
PRINT(cp, size);
|
||||
} else { /* glue together f_p fragments */
|
||||
if (ch >= 'f') { /* 'f' or 'g' */
|
||||
if (_double == 0) {
|
||||
/* kludge for __dtoa irregularity */
|
||||
if (expt >= ndig &&
|
||||
(flags & ALT) == 0) {
|
||||
PRINT("0", 1);
|
||||
} else {
|
||||
PRINT("0.", 2);
|
||||
PAD(ndig - 1, zeroes);
|
||||
}
|
||||
} else if (expt <= 0) {
|
||||
PRINT("0.", 2);
|
||||
PAD(-expt, zeroes);
|
||||
PRINT(cp, ndig);
|
||||
} else if (expt >= ndig) {
|
||||
PRINT(cp, ndig);
|
||||
PAD(expt - ndig, zeroes);
|
||||
if (flags & ALT)
|
||||
PRINT(".", 1);
|
||||
} else {
|
||||
PRINT(cp, expt);
|
||||
cp += expt;
|
||||
PRINT(".", 1);
|
||||
PRINT(cp, ndig-expt);
|
||||
}
|
||||
} else { /* 'e' or 'E' */
|
||||
if (ndig > 1 || flags & ALT) {
|
||||
ox[0] = *cp++;
|
||||
ox[1] = '.';
|
||||
PRINT(ox, 2);
|
||||
if (_double) {
|
||||
PRINT(cp, ndig-1);
|
||||
} else /* 0.[0..] */
|
||||
/* __dtoa irregularity */
|
||||
PAD(ndig - 1, zeroes);
|
||||
} else /* XeYYY */
|
||||
PRINT(cp, 1);
|
||||
PRINT(expstr, expsize);
|
||||
}
|
||||
}
|
||||
#else
|
||||
PRINT(cp, size);
|
||||
#endif
|
||||
/* left-adjusting padding (always blank) */
|
||||
if (flags & LADJUST)
|
||||
PAD(width - realsz, blanks);
|
||||
|
||||
/* finally, adjust ret */
|
||||
ret += width > realsz ? width : realsz;
|
||||
|
||||
FLUSH(); /* copy out the I/O vectors */
|
||||
}
|
||||
done:
|
||||
FLUSH();
|
||||
error:
|
||||
return (__sferror(fp) ? EOF : ret);
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
#ifdef FLOATING_POINT
|
||||
|
||||
extern char *__dtoa __P((double, int, int, int *, int *, char **));
|
||||
|
||||
static char *
|
||||
cvt(double_spec_t value, int ndigits, int flags, char *sign, int *decpt, int ch,
|
||||
int *length)
|
||||
{
|
||||
int mode, dsgn;
|
||||
char *digits, *bp, *rve;
|
||||
|
||||
if (ch == 'f')
|
||||
mode = 3; /* ndigits after the decimal point */
|
||||
else {
|
||||
/*
|
||||
* To obtain ndigits after the decimal point for the 'e'
|
||||
* and 'E' formats, round to ndigits + 1 significant
|
||||
* figures.
|
||||
*/
|
||||
if (ch == 'e' || ch == 'E')
|
||||
ndigits++;
|
||||
mode = 2; /* ndigits significant digits */
|
||||
}
|
||||
if (value < 0) {
|
||||
value = -value;
|
||||
*sign = '-';
|
||||
} else
|
||||
*sign = '\000';
|
||||
digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
|
||||
if ((ch != 'g' && ch != 'G') || flags & ALT) {
|
||||
/* print trailing zeros */
|
||||
bp = digits + ndigits;
|
||||
if (ch == 'f') {
|
||||
if (*digits == '0' && value)
|
||||
*decpt = -ndigits + 1;
|
||||
bp += *decpt;
|
||||
}
|
||||
if (value == 0) /* kludge for __dtoa irregularity */
|
||||
rve = bp;
|
||||
while (rve < bp)
|
||||
*rve++ = '0';
|
||||
}
|
||||
*length = rve - digits;
|
||||
return (digits);
|
||||
}
|
||||
|
||||
static int
|
||||
exponent(char *p0, int exp, int fmtch)
|
||||
{
|
||||
register char *p, *t;
|
||||
STATIC char expbuf[MAXEXP];
|
||||
|
||||
p = p0;
|
||||
*p++ = fmtch;
|
||||
if (exp < 0) {
|
||||
exp = -exp;
|
||||
*p++ = '-';
|
||||
}
|
||||
else
|
||||
*p++ = '+';
|
||||
t = expbuf + MAXEXP;
|
||||
if (exp > 9) {
|
||||
do {
|
||||
*--t = to_char(exp % 10);
|
||||
} while ((exp /= 10) > 9);
|
||||
*--t = to_char(exp);
|
||||
for (; t < expbuf + MAXEXP; *p++ = *t++);
|
||||
}
|
||||
else {
|
||||
*p++ = '0';
|
||||
*p++ = to_char(exp);
|
||||
}
|
||||
return (p - p0);
|
||||
}
|
||||
#endif /* FLOATING_POINT */
|
749
lib/libc/stdio/vfscanf.c
Normal file
749
lib/libc/stdio/vfscanf.c
Normal file
@ -0,0 +1,749 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#if __STDC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
#include <varargs.h>
|
||||
#endif
|
||||
#include "local.h"
|
||||
|
||||
#undef FLOATING_POINT
|
||||
|
||||
#include "floatio.h"
|
||||
#define BUF 513 /* Maximum length of numeric string. */
|
||||
|
||||
/*
|
||||
* Flags used during conversion.
|
||||
*/
|
||||
#define LONG 0x01 /* l: long or double */
|
||||
#define LONGDBL 0x02 /* L: long double; unimplemented */
|
||||
#define SHORT 0x04 /* h: short */
|
||||
#define SUPPRESS 0x08 /* suppress assignment */
|
||||
#define POINTER 0x10 /* weird %p pointer (`fake hex') */
|
||||
#define NOSKIP 0x20 /* do not skip blanks */
|
||||
|
||||
/*
|
||||
* The following are used in numeric conversions only:
|
||||
* SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
|
||||
* SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
|
||||
*/
|
||||
#define SIGNOK 0x40 /* +/- is (still) legal */
|
||||
#define NDIGITS 0x80 /* no digits detected */
|
||||
|
||||
#define DPTOK 0x100 /* (float) decimal point is still legal */
|
||||
#define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
|
||||
|
||||
#define PFXOK 0x100 /* 0x prefix is (still) legal */
|
||||
#define NZDIGITS 0x200 /* no zero digits detected */
|
||||
|
||||
/*
|
||||
* Conversion types.
|
||||
*/
|
||||
#define CT_CHAR 0 /* %c conversion */
|
||||
#define CT_CCL 1 /* %[...] conversion */
|
||||
#define CT_STRING 2 /* %s conversion */
|
||||
#define CT_INT 3 /* integer, i.e., strtol or strtoul */
|
||||
#define CT_FLOAT 4 /* floating, i.e., strtod */
|
||||
|
||||
#define u_char unsigned char
|
||||
#define u_long unsigned long
|
||||
|
||||
static u_char *__sccl(register char *tab, register u_char *fmt);
|
||||
|
||||
/*
|
||||
* vfscanf
|
||||
*/
|
||||
int
|
||||
__svfscanf(register FILE *fp, char const *fmt0, va_list ap)
|
||||
{
|
||||
register u_char *fmt = (u_char *)fmt0;
|
||||
register int c; /* character from format, or conversion */
|
||||
register size_t width; /* field width, or 0 */
|
||||
register char *p; /* points into all kinds of strings */
|
||||
register int n; /* handy integer */
|
||||
register int flags; /* flags as defined above */
|
||||
register char *p0; /* saves original value of p when necessary */
|
||||
int nassigned; /* number of fields assigned */
|
||||
int nread; /* number of characters consumed from fp */
|
||||
int base; /* base argument to strtol/strtoul */
|
||||
u_long (*ccfn)(const char *, char **, int);
|
||||
/* conversion function (strtol/strtoul) */
|
||||
char ccltab[256]; /* character class table for %[...] */
|
||||
char buf[BUF]; /* buffer for numeric conversions */
|
||||
|
||||
/* `basefix' is used to avoid `if' tests in the integer scanner */
|
||||
static short basefix[17] =
|
||||
{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
|
||||
|
||||
nassigned = 0;
|
||||
nread = 0;
|
||||
base = 0; /* XXX just to keep gcc happy */
|
||||
ccfn = NULL; /* XXX just to keep gcc happy */
|
||||
for (;;) {
|
||||
c = *fmt++;
|
||||
if (c == 0)
|
||||
return (nassigned);
|
||||
if (isspace(c)) {
|
||||
for (;;) {
|
||||
if (fp->_r <= 0 && __srefill(fp))
|
||||
return (nassigned);
|
||||
if (!isspace(*fp->_p))
|
||||
break;
|
||||
nread++, fp->_r--, fp->_p++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (c != '%')
|
||||
goto literal;
|
||||
width = 0;
|
||||
flags = 0;
|
||||
/*
|
||||
* switch on the format. continue if done;
|
||||
* break once format type is derived.
|
||||
*/
|
||||
again: c = *fmt++;
|
||||
switch (c) {
|
||||
case '%':
|
||||
literal:
|
||||
if (fp->_r <= 0 && __srefill(fp))
|
||||
goto input_failure;
|
||||
if (*fp->_p != c)
|
||||
goto match_failure;
|
||||
fp->_r--, fp->_p++;
|
||||
nread++;
|
||||
continue;
|
||||
|
||||
case '*':
|
||||
flags |= SUPPRESS;
|
||||
goto again;
|
||||
case 'l':
|
||||
flags |= LONG;
|
||||
goto again;
|
||||
case 'L':
|
||||
flags |= LONGDBL;
|
||||
goto again;
|
||||
case 'h':
|
||||
flags |= SHORT;
|
||||
goto again;
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
width = width * 10 + c - '0';
|
||||
goto again;
|
||||
|
||||
/*
|
||||
* Conversions.
|
||||
* Those marked `compat' are for 4.[123]BSD compatibility.
|
||||
*
|
||||
* (According to ANSI, E and X formats are supposed
|
||||
* to the same as e and x. Sorry about that.)
|
||||
*/
|
||||
case 'D': /* compat */
|
||||
flags |= LONG;
|
||||
/* FALLTHROUGH */
|
||||
case 'd':
|
||||
c = CT_INT;
|
||||
ccfn = (u_long (*)())strtol;
|
||||
base = 10;
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
c = CT_INT;
|
||||
ccfn = (u_long (*)())strtol;
|
||||
base = 0;
|
||||
break;
|
||||
|
||||
case 'O': /* compat */
|
||||
flags |= LONG;
|
||||
/* FALLTHROUGH */
|
||||
case 'o':
|
||||
c = CT_INT;
|
||||
ccfn = strtoul;
|
||||
base = 8;
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
c = CT_INT;
|
||||
ccfn = strtoul;
|
||||
base = 10;
|
||||
break;
|
||||
|
||||
case 'X': /* compat XXX */
|
||||
flags |= LONG;
|
||||
/* FALLTHROUGH */
|
||||
case 'x':
|
||||
flags |= PFXOK; /* enable 0x prefixing */
|
||||
c = CT_INT;
|
||||
ccfn = strtoul;
|
||||
base = 16;
|
||||
break;
|
||||
|
||||
#ifdef FLOATING_POINT
|
||||
case 'E': /* compat XXX */
|
||||
case 'F': /* compat */
|
||||
flags |= LONG;
|
||||
/* FALLTHROUGH */
|
||||
case 'e': case 'f': case 'g':
|
||||
c = CT_FLOAT;
|
||||
break;
|
||||
#endif
|
||||
|
||||
case 's':
|
||||
c = CT_STRING;
|
||||
break;
|
||||
|
||||
case '[':
|
||||
fmt = __sccl(ccltab, fmt);
|
||||
flags |= NOSKIP;
|
||||
c = CT_CCL;
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
flags |= NOSKIP;
|
||||
c = CT_CHAR;
|
||||
break;
|
||||
|
||||
case 'p': /* pointer format is like hex */
|
||||
flags |= POINTER | PFXOK;
|
||||
c = CT_INT;
|
||||
ccfn = strtoul;
|
||||
base = 16;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
if (flags & SUPPRESS) /* ??? */
|
||||
continue;
|
||||
if (flags & SHORT)
|
||||
*va_arg(ap, short *) = nread;
|
||||
else if (flags & LONG)
|
||||
*va_arg(ap, long *) = nread;
|
||||
else
|
||||
*va_arg(ap, int *) = nread;
|
||||
continue;
|
||||
|
||||
/*
|
||||
* Disgusting backwards compatibility hacks. XXX
|
||||
*/
|
||||
case '\0': /* compat */
|
||||
return (EOF);
|
||||
|
||||
default: /* compat */
|
||||
if (isupper(c))
|
||||
flags |= LONG;
|
||||
c = CT_INT;
|
||||
ccfn = (u_long (*)())strtol;
|
||||
base = 10;
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* We have a conversion that requires input.
|
||||
*/
|
||||
if (fp->_r <= 0 && __srefill(fp))
|
||||
goto input_failure;
|
||||
|
||||
/*
|
||||
* Consume leading white space, except for formats
|
||||
* that suppress this.
|
||||
*/
|
||||
if ((flags & NOSKIP) == 0) {
|
||||
while (isspace(*fp->_p)) {
|
||||
nread++;
|
||||
if (--fp->_r > 0)
|
||||
fp->_p++;
|
||||
else if (__srefill(fp))
|
||||
goto input_failure;
|
||||
}
|
||||
/*
|
||||
* Note that there is at least one character in
|
||||
* the buffer, so conversions that do not set NOSKIP
|
||||
* ca no longer result in an input failure.
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
* Do the conversion.
|
||||
*/
|
||||
switch (c) {
|
||||
|
||||
case CT_CHAR:
|
||||
/* scan arbitrary characters (sets NOSKIP) */
|
||||
if (width == 0)
|
||||
width = 1;
|
||||
if (flags & SUPPRESS) {
|
||||
size_t sum = 0;
|
||||
for (;;) {
|
||||
if ((n = fp->_r) < width) {
|
||||
sum += n;
|
||||
width -= n;
|
||||
fp->_p += n;
|
||||
if (__srefill(fp)) {
|
||||
if (sum == 0)
|
||||
goto input_failure;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
sum += width;
|
||||
fp->_r -= width;
|
||||
fp->_p += width;
|
||||
break;
|
||||
}
|
||||
}
|
||||
nread += sum;
|
||||
} else {
|
||||
size_t r = fread((void *)va_arg(ap, char *), 1,
|
||||
width, fp);
|
||||
|
||||
if (r == 0)
|
||||
goto input_failure;
|
||||
nread += r;
|
||||
nassigned++;
|
||||
}
|
||||
break;
|
||||
|
||||
case CT_CCL:
|
||||
/* scan a (nonempty) character class (sets NOSKIP) */
|
||||
if (width == 0)
|
||||
width = ~0; /* `infinity' */
|
||||
/* take only those things in the class */
|
||||
if (flags & SUPPRESS) {
|
||||
n = 0;
|
||||
while (ccltab[*fp->_p]) {
|
||||
n++, fp->_r--, fp->_p++;
|
||||
if (--width == 0)
|
||||
break;
|
||||
if (fp->_r <= 0 && __srefill(fp)) {
|
||||
if (n == 0)
|
||||
goto input_failure;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (n == 0)
|
||||
goto match_failure;
|
||||
} else {
|
||||
p0 = p = va_arg(ap, char *);
|
||||
while (ccltab[*fp->_p]) {
|
||||
fp->_r--;
|
||||
*p++ = *fp->_p++;
|
||||
if (--width == 0)
|
||||
break;
|
||||
if (fp->_r <= 0 && __srefill(fp)) {
|
||||
if (p == p0)
|
||||
goto input_failure;
|
||||
break;
|
||||
}
|
||||
}
|
||||
n = p - p0;
|
||||
if (n == 0)
|
||||
goto match_failure;
|
||||
*p = 0;
|
||||
nassigned++;
|
||||
}
|
||||
nread += n;
|
||||
break;
|
||||
|
||||
case CT_STRING:
|
||||
/* like CCL, but zero-length string OK, & no NOSKIP */
|
||||
if (width == 0)
|
||||
width = ~0;
|
||||
if (flags & SUPPRESS) {
|
||||
n = 0;
|
||||
while (!isspace(*fp->_p)) {
|
||||
n++, fp->_r--, fp->_p++;
|
||||
if (--width == 0)
|
||||
break;
|
||||
if (fp->_r <= 0 && __srefill(fp))
|
||||
break;
|
||||
}
|
||||
nread += n;
|
||||
} else {
|
||||
p0 = p = va_arg(ap, char *);
|
||||
while (!isspace(*fp->_p)) {
|
||||
fp->_r--;
|
||||
*p++ = *fp->_p++;
|
||||
if (--width == 0)
|
||||
break;
|
||||
if (fp->_r <= 0 && __srefill(fp))
|
||||
break;
|
||||
}
|
||||
*p = 0;
|
||||
nread += p - p0;
|
||||
nassigned++;
|
||||
}
|
||||
continue;
|
||||
|
||||
case CT_INT:
|
||||
/* scan an integer as if by strtol/strtoul */
|
||||
#ifdef hardway
|
||||
if (width == 0 || width > sizeof(buf) - 1)
|
||||
width = sizeof(buf) - 1;
|
||||
#else
|
||||
/* size_t is unsigned, hence this optimisation */
|
||||
if (--width > sizeof(buf) - 2)
|
||||
width = sizeof(buf) - 2;
|
||||
width++;
|
||||
#endif
|
||||
flags |= SIGNOK | NDIGITS | NZDIGITS;
|
||||
for (p = buf; width; width--) {
|
||||
c = *fp->_p;
|
||||
/*
|
||||
* Switch on the character; `goto ok'
|
||||
* if we accept it as a part of number.
|
||||
*/
|
||||
switch (c) {
|
||||
|
||||
/*
|
||||
* The digit 0 is always legal, but is
|
||||
* special. For %i conversions, if no
|
||||
* digits (zero or nonzero) have been
|
||||
* scanned (only signs), we will have
|
||||
* base==0. In that case, we should set
|
||||
* it to 8 and enable 0x prefixing.
|
||||
* Also, if we have not scanned zero digits
|
||||
* before this, do not turn off prefixing
|
||||
* (someone else will turn it off if we
|
||||
* have scanned any nonzero digits).
|
||||
*/
|
||||
case '0':
|
||||
if (base == 0) {
|
||||
base = 8;
|
||||
flags |= PFXOK;
|
||||
}
|
||||
if (flags & NZDIGITS)
|
||||
flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
|
||||
else
|
||||
flags &= ~(SIGNOK|PFXOK|NDIGITS);
|
||||
goto ok;
|
||||
|
||||
/* 1 through 7 always legal */
|
||||
case '1': case '2': case '3':
|
||||
case '4': case '5': case '6': case '7':
|
||||
base = basefix[base];
|
||||
flags &= ~(SIGNOK | PFXOK | NDIGITS);
|
||||
goto ok;
|
||||
|
||||
/* digits 8 and 9 ok iff decimal or hex */
|
||||
case '8': case '9':
|
||||
base = basefix[base];
|
||||
if (base <= 8)
|
||||
break; /* not legal here */
|
||||
flags &= ~(SIGNOK | PFXOK | NDIGITS);
|
||||
goto ok;
|
||||
|
||||
/* letters ok iff hex */
|
||||
case 'A': case 'B': case 'C':
|
||||
case 'D': case 'E': case 'F':
|
||||
case 'a': case 'b': case 'c':
|
||||
case 'd': case 'e': case 'f':
|
||||
/* no need to fix base here */
|
||||
if (base <= 10)
|
||||
break; /* not legal here */
|
||||
flags &= ~(SIGNOK | PFXOK | NDIGITS);
|
||||
goto ok;
|
||||
|
||||
/* sign ok only as first character */
|
||||
case '+': case '-':
|
||||
if (flags & SIGNOK) {
|
||||
flags &= ~SIGNOK;
|
||||
goto ok;
|
||||
}
|
||||
break;
|
||||
|
||||
/* x ok iff flag still set & 2nd char */
|
||||
case 'x': case 'X':
|
||||
if (flags & PFXOK && p == buf + 1) {
|
||||
base = 16; /* if %i */
|
||||
flags &= ~PFXOK;
|
||||
goto ok;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we got here, c is not a legal character
|
||||
* for a number. Stop accumulating digits.
|
||||
*/
|
||||
break;
|
||||
ok:
|
||||
/*
|
||||
* c is legal: store it and look at the next.
|
||||
*/
|
||||
*p++ = c;
|
||||
if (--fp->_r > 0)
|
||||
fp->_p++;
|
||||
else if (__srefill(fp))
|
||||
break; /* EOF */
|
||||
}
|
||||
/*
|
||||
* If we had only a sign, it is no good; push
|
||||
* back the sign. If the number ends in `x',
|
||||
* it was [sign] '0' 'x', so push back the x
|
||||
* and treat it as [sign] '0'.
|
||||
*/
|
||||
if (flags & NDIGITS) {
|
||||
if (p > buf)
|
||||
(void) ungetc(*(u_char *)--p, fp);
|
||||
goto match_failure;
|
||||
}
|
||||
c = ((u_char *)p)[-1];
|
||||
if (c == 'x' || c == 'X') {
|
||||
--p;
|
||||
(void) ungetc(c, fp);
|
||||
}
|
||||
if ((flags & SUPPRESS) == 0) {
|
||||
u_long res;
|
||||
|
||||
*p = 0;
|
||||
res = (*ccfn)(buf, (char **)NULL, base);
|
||||
if (flags & POINTER)
|
||||
*va_arg(ap, void **) = (void *)res;
|
||||
else if (flags & SHORT)
|
||||
*va_arg(ap, short *) = res;
|
||||
else if (flags & LONG)
|
||||
*va_arg(ap, long *) = res;
|
||||
else
|
||||
*va_arg(ap, int *) = res;
|
||||
nassigned++;
|
||||
}
|
||||
nread += p - buf;
|
||||
break;
|
||||
|
||||
#ifdef FLOATING_POINT
|
||||
case CT_FLOAT:
|
||||
/* scan a floating point number as if by strtod */
|
||||
#ifdef hardway
|
||||
if (width == 0 || width > sizeof(buf) - 1)
|
||||
width = sizeof(buf) - 1;
|
||||
#else
|
||||
/* size_t is unsigned, hence this optimisation */
|
||||
if (--width > sizeof(buf) - 2)
|
||||
width = sizeof(buf) - 2;
|
||||
width++;
|
||||
#endif
|
||||
flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
|
||||
for (p = buf; width; width--) {
|
||||
c = *fp->_p;
|
||||
/*
|
||||
* This code mimicks the integer conversion
|
||||
* code, but is much simpler.
|
||||
*/
|
||||
switch (c) {
|
||||
|
||||
case '0': case '1': case '2': case '3':
|
||||
case '4': case '5': case '6': case '7':
|
||||
case '8': case '9':
|
||||
flags &= ~(SIGNOK | NDIGITS);
|
||||
goto fok;
|
||||
|
||||
case '+': case '-':
|
||||
if (flags & SIGNOK) {
|
||||
flags &= ~SIGNOK;
|
||||
goto fok;
|
||||
}
|
||||
break;
|
||||
case '.':
|
||||
if (flags & DPTOK) {
|
||||
flags &= ~(SIGNOK | DPTOK);
|
||||
goto fok;
|
||||
}
|
||||
break;
|
||||
case 'e': case 'E':
|
||||
/* no exponent without some digits */
|
||||
if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
|
||||
flags =
|
||||
(flags & ~(EXPOK|DPTOK)) |
|
||||
SIGNOK | NDIGITS;
|
||||
goto fok;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
fok:
|
||||
*p++ = c;
|
||||
if (--fp->_r > 0)
|
||||
fp->_p++;
|
||||
else if (__srefill(fp))
|
||||
break; /* EOF */
|
||||
}
|
||||
/*
|
||||
* If no digits, might be missing exponent digits
|
||||
* (just give back the exponent) or might be missing
|
||||
* regular digits, but had sign and/or decimal point.
|
||||
*/
|
||||
if (flags & NDIGITS) {
|
||||
if (flags & EXPOK) {
|
||||
/* no digits at all */
|
||||
while (p > buf)
|
||||
ungetc(*(u_char *)--p, fp);
|
||||
goto match_failure;
|
||||
}
|
||||
/* just a bad exponent (e and maybe sign) */
|
||||
c = *(u_char *)--p;
|
||||
if (c != 'e' && c != 'E') {
|
||||
(void) ungetc(c, fp);/* sign */
|
||||
c = *(u_char *)--p;
|
||||
}
|
||||
(void) ungetc(c, fp);
|
||||
}
|
||||
if ((flags & SUPPRESS) == 0) {
|
||||
double res;
|
||||
|
||||
*p = 0;
|
||||
res = strtod(buf,(char **) NULL);
|
||||
if (flags & LONG)
|
||||
*va_arg(ap, double *) = res;
|
||||
else
|
||||
*va_arg(ap, float *) = res;
|
||||
nassigned++;
|
||||
}
|
||||
nread += p - buf;
|
||||
break;
|
||||
#endif /* FLOATING_POINT */
|
||||
}
|
||||
}
|
||||
input_failure:
|
||||
return (nassigned ? nassigned : -1);
|
||||
match_failure:
|
||||
return (nassigned);
|
||||
}
|
||||
|
||||
/*
|
||||
* Fill in the given table from the scanset at the given format
|
||||
* (just after `['). Return a pointer to the character past the
|
||||
* closing `]'. The table has a 1 wherever characters should be
|
||||
* considered part of the scanset.
|
||||
*/
|
||||
static u_char *
|
||||
__sccl(register char *tab, register u_char *fmt)
|
||||
{
|
||||
register int c, n, v;
|
||||
|
||||
/* first `clear' the whole table */
|
||||
c = *fmt++; /* first char hat => negated scanset */
|
||||
if (c == '^') {
|
||||
v = 1; /* default => accept */
|
||||
c = *fmt++; /* get new first char */
|
||||
} else
|
||||
v = 0; /* default => reject */
|
||||
/* should probably use memset here */
|
||||
for (n = 0; n < 256; n++)
|
||||
tab[n] = v;
|
||||
if (c == 0)
|
||||
return (fmt - 1);/* format ended before closing ] */
|
||||
|
||||
/*
|
||||
* Now set the entries corresponding to the actual scanset
|
||||
* to the opposite of the above.
|
||||
*
|
||||
* The first character may be ']' (or '-') without being special;
|
||||
* the last character may be '-'.
|
||||
*/
|
||||
v = 1 - v;
|
||||
for (;;) {
|
||||
tab[c] = v; /* take character c */
|
||||
doswitch:
|
||||
n = *fmt++; /* and examine the next */
|
||||
switch (n) {
|
||||
|
||||
case 0: /* format ended too soon */
|
||||
return (fmt - 1);
|
||||
|
||||
case '-':
|
||||
/*
|
||||
* A scanset of the form
|
||||
* [01+-]
|
||||
* is defined as `the digit 0, the digit 1,
|
||||
* the character +, the character -', but
|
||||
* the effect of a scanset such as
|
||||
* [a-zA-Z0-9]
|
||||
* is implementation defined. The V7 Unix
|
||||
* scanf treats `a-z' as `the letters a through
|
||||
* z', but treats `a-a' as `the letter a, the
|
||||
* character -, and the letter a'.
|
||||
*
|
||||
* For compatibility, the `-' is not considerd
|
||||
* to define a range if the character following
|
||||
* it is either a close bracket (required by ANSI)
|
||||
* or is not numerically greater than the character
|
||||
* we just stored in the table (c).
|
||||
*/
|
||||
n = *fmt;
|
||||
if (n == ']' || n < c) {
|
||||
c = '-';
|
||||
break; /* resume the for(;;) */
|
||||
}
|
||||
fmt++;
|
||||
do { /* fill in the range */
|
||||
tab[++c] = v;
|
||||
} while (c < n);
|
||||
#if 1 /* XXX another disgusting compatibility hack */
|
||||
/*
|
||||
* Alas, the V7 Unix scanf also treats formats
|
||||
* such as [a-c-e] as `the letters a through e'.
|
||||
* This too is permitted by the standard....
|
||||
*/
|
||||
goto doswitch;
|
||||
#else
|
||||
c = *fmt++;
|
||||
if (c == 0)
|
||||
return (fmt - 1);
|
||||
if (c == ']')
|
||||
return (fmt);
|
||||
#endif
|
||||
break;
|
||||
|
||||
case ']': /* end of scanset */
|
||||
return (fmt);
|
||||
|
||||
default: /* just another character */
|
||||
c = n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* NOTREACHED */
|
||||
}
|
51
lib/libc/stdio/vprintf.c
Normal file
51
lib/libc/stdio/vprintf.c
Normal file
@ -0,0 +1,51 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
vprintf(char const *fmt, _BSD_VA_LIST_ ap)
|
||||
{
|
||||
return (vfprintf(stdout, fmt, ap));
|
||||
}
|
52
lib/libc/stdio/vscanf.c
Normal file
52
lib/libc/stdio/vscanf.c
Normal file
@ -0,0 +1,52 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Donn Seeley at UUNET Technologies, Inc.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vscanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
vscanf(const char *fmt, _BSD_VA_LIST_ ap)
|
||||
{
|
||||
|
||||
return (__svfscanf(stdin, fmt, ap));
|
||||
}
|
61
lib/libc/stdio/vsnprintf.c
Normal file
61
lib/libc/stdio/vsnprintf.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
vsnprintf(char *str, size_t n, const char *fmt, _BSD_VA_LIST_ ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
|
||||
if ((int)n < 1)
|
||||
return (EOF);
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *)str;
|
||||
f._bf._size = f._w = n - 1;
|
||||
ret = vfprintf(&f, fmt, ap);
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
60
lib/libc/stdio/vsprintf.c
Normal file
60
lib/libc/stdio/vsprintf.c
Normal file
@ -0,0 +1,60 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vsprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int
|
||||
vsprintf(char *str, const char *fmt, _BSD_VA_LIST_ ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SWR | __SSTR;
|
||||
f._bf._base = f._p = (unsigned char *)str;
|
||||
f._bf._size = f._w = INT_MAX;
|
||||
ret = vfprintf(&f, fmt, ap);
|
||||
*f._p = 0;
|
||||
return (ret);
|
||||
}
|
69
lib/libc/stdio/vsscanf.c
Normal file
69
lib/libc/stdio/vsscanf.c
Normal file
@ -0,0 +1,69 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Donn Seeley at UUNET Technologies, Inc.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)vsscanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* ARGSUSED */
|
||||
static ssize_t
|
||||
eofread(void *cookie, char *buf, size_t len)
|
||||
{
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
vsscanf(const char *str, const char *fmt, _BSD_VA_LIST_ ap)
|
||||
{
|
||||
int ret;
|
||||
FILE f;
|
||||
|
||||
f._flags = __SRD;
|
||||
f._bf._base = f._p = (unsigned char *)str;
|
||||
f._bf._size = f._r = strlen(str);
|
||||
f._read = eofread;
|
||||
f._ub._base = NULL;
|
||||
f._lb._base = NULL;
|
||||
return (__svfscanf(&f, fmt, ap));
|
||||
}
|
92
lib/libc/stdio/wbuf.c
Normal file
92
lib/libc/stdio/wbuf.c
Normal file
@ -0,0 +1,92 @@
|
||||
#line 1 ":src:gno:lib:libc:stdio:wbuf.c"
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)wbuf.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Write the given character into the (probably full) buffer for
|
||||
* the given file. Flush the buffer out if it is or becomes full,
|
||||
* or if c=='\n' and the file is line buffered.
|
||||
*/
|
||||
int
|
||||
__swbuf(register int c, register FILE *fp)
|
||||
{
|
||||
register int n;
|
||||
|
||||
/*
|
||||
* In case we cannot write, or longjmp takes us out early,
|
||||
* make sure _w is 0 (if fully- or un-buffered) or -_bf._size
|
||||
* (if line buffered) so that we will get called again.
|
||||
* If we did not do this, a sufficient number of putc()
|
||||
* calls might wrap _w from negative to positive.
|
||||
*/
|
||||
fp->_w = fp->_lbfsize;
|
||||
if (cantwrite(fp))
|
||||
return (EOF);
|
||||
c = (unsigned char)c;
|
||||
|
||||
/*
|
||||
* If it is completely full, flush it out. Then, in any case,
|
||||
* stuff c into the buffer. If this causes the buffer to fill
|
||||
* completely, or if c is '\n' and the file is line buffered,
|
||||
* flush it (perhaps a second time). The second flush will always
|
||||
* happen on unbuffered streams, where _bf._size==1; fflush()
|
||||
* guarantees that putc() will always call wbuf() by setting _w
|
||||
* to 0, so we need not do anything else.
|
||||
*/
|
||||
n = fp->_p - fp->_bf._base;
|
||||
if (n >= fp->_bf._size) {
|
||||
if (fflush(fp))
|
||||
return (EOF);
|
||||
n = 0;
|
||||
}
|
||||
fp->_w--;
|
||||
*fp->_p++ = c;
|
||||
if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n'))
|
||||
if (fflush(fp))
|
||||
return (EOF);
|
||||
return (c);
|
||||
}
|
94
lib/libc/stdio/wsetup.c
Normal file
94
lib/libc/stdio/wsetup.c
Normal file
@ -0,0 +1,94 @@
|
||||
/*-
|
||||
* Copyright (c) 1990, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to Berkeley by
|
||||
* Chris Torek.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef __ORCAC__
|
||||
segment "gno_stdio_";
|
||||
#endif
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)wsetup.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Various output routines call wsetup to be sure it is safe to write,
|
||||
* because either _flags does not include __SWR, or _buf is NULL.
|
||||
* _wsetup returns 0 if OK to write, nonzero otherwise.
|
||||
*/
|
||||
int
|
||||
__swsetup(register FILE *fp)
|
||||
{
|
||||
/* make sure stdio is set up */
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
|
||||
/*
|
||||
* If we are not writing, we had better be reading and writing.
|
||||
*/
|
||||
if ((fp->_flags & __SWR) == 0) {
|
||||
if ((fp->_flags & __SRW) == 0)
|
||||
return (EOF);
|
||||
if (fp->_flags & __SRD) {
|
||||
/* clobber any ungetc data */
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_flags &= ~(__SRD|__SEOF);
|
||||
fp->_r = 0;
|
||||
fp->_p = fp->_bf._base;
|
||||
}
|
||||
fp->_flags |= __SWR;
|
||||
}
|
||||
|
||||
/*
|
||||
* Make a buffer if necessary, then set _w.
|
||||
*/
|
||||
if (fp->_bf._base == NULL)
|
||||
__smakebuf(fp);
|
||||
if (fp->_flags & __SLBF) {
|
||||
/*
|
||||
* It is line buffered, so make _lbfsize be -_bufsize
|
||||
* for the putc() macro. We will change _lbfsize back
|
||||
* to 0 whenever we turn off __SWR.
|
||||
*/
|
||||
fp->_w = 0;
|
||||
fp->_lbfsize = -fp->_bf._size;
|
||||
} else
|
||||
fp->_w = fp->_flags & __SNBF ? 0 : fp->_bf._size;
|
||||
return (0);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user