Import unmodified NetBSD libc in trunk
This patch imports the unmodified current version of NetBSD libc. The NetBSD includes are in /nbsd_include, while the libc code itself is split between lib/nbsd_libc and common/lib/libc.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
|
||||
# $NetBSD: Makefile.inc,v 1.40 2010/12/22 16:59:10 christos Exp $
|
||||
|
||||
# stdio sources
|
||||
.PATH: ${.CURDIR}/stdio
|
||||
|
||||
CPPFLAGS+=-DWIDE_DOUBLE
|
||||
|
||||
SRCS+= asprintf.c clrerr.c dprintf.c fclose.c fdopen.c feof.c ferror.c \
|
||||
fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetstr.c fgetwc.c \
|
||||
fgetwln.c fgetws.c fileno.c findfp.c flags.c flockfile.c fopen.c \
|
||||
fparseln.c fprintf.c fpurge.c fputc.c fputs.c fputwc.c fputws.c \
|
||||
fread.c freopen.c fscanf.c fseek.c fseeko.c fsetpos.c ftell.c ftello.c \
|
||||
funopen.c fvwrite.c fwalk.c fwide.c fwprintf.c fwrite.c fwscanf.c \
|
||||
getc.c getchar.c getdelim.c getline.c gettemp.c getw.c getwc.c \
|
||||
getwchar.c makebuf.c mkdtemp.c mkstemp.c perror.c printf.c putc.c \
|
||||
putchar.c puts.c putw.c putwc.c putwchar.c refill.c remove.c rewind.c \
|
||||
rget.c scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \
|
||||
sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \
|
||||
vasprintf.c vdprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c \
|
||||
vprintf.c vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c \
|
||||
vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
|
||||
SRCS+= fmemopen.c
|
||||
|
||||
.if !defined(AUDIT)
|
||||
SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c
|
||||
.endif
|
||||
|
||||
# namespace purity wrappers
|
||||
SRCS+= _fileno.c _fseeko.c _ftello.c
|
||||
|
||||
MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fgetws.3 \
|
||||
flockfile.3 fmemopen.3 fopen.3 fparseln.3 fputs.3 fputws.3 fread.3 \
|
||||
fseek.3 funopen.3 fwide.3 getc.3 getdelim.3 getwc.3 mktemp.3 printf.3 \
|
||||
putc.3 putwc.3 remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 \
|
||||
ungetc.3 ungetwc.3 wprintf.3 wscanf.3
|
||||
|
||||
MLINKS+=ferror.3 clearerr.3 ferror.3 feof.3 ferror.3 fileno.3
|
||||
MLINKS+=fflush.3 fpurge.3
|
||||
MLINKS+=flockfile.3 ftrylockfile.3 flockfile.3 funlockfile.3
|
||||
MLINKS+=fgets.3 gets.3
|
||||
MLINKS+=fopen.3 fdopen.3 fopen.3 freopen.3
|
||||
MLINKS+=fputs.3 puts.3
|
||||
MLINKS+=fread.3 fwrite.3
|
||||
MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.3 \
|
||||
fseek.3 ftello.3 fseek.3 rewind.3
|
||||
MLINKS+=funopen.3 fropen.3 funopen.3 fwopen.3
|
||||
MLINKS+=getc.3 fgetc.3 getc.3 getc_unlocked.3 getc.3 getchar.3 \
|
||||
getc.3 getchar_unlocked.3 getc.3 getw.3
|
||||
MLINKS+=getdelim.3 getline.3
|
||||
MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3
|
||||
MLINKS+=printf.3 asprintf.3 printf.3 fprintf.3 printf.3 snprintf.3 \
|
||||
printf.3 sprintf.3 printf.3 vasprintf.3 printf.3 vfprintf.3 \
|
||||
printf.3 vprintf.3 printf.3 vsnprintf.3 printf.3 vsprintf.3 \
|
||||
printf.3 dprintf.3 printf.3 vdprintf.3 printf.3 vsnprintf_ss.3
|
||||
MLINKS+=putc.3 fputc.3 putc.3 putc_unlocked.3 putc.3 putchar.3 \
|
||||
putc.3 putchar_unlocked.3 putc.3 putw.3
|
||||
MLINKS+=scanf.3 fscanf.3 scanf.3 sscanf.3 scanf.3 vfscanf.3 scanf.3 vscanf.3 \
|
||||
scanf.3 vsscanf.3
|
||||
MLINKS+=setbuf.3 setbuffer.3 setbuf.3 setlinebuf.3 setbuf.3 setvbuf.3
|
||||
MLINKS+=tmpnam.3 tempnam.3 tmpnam.3 tmpfile.3
|
||||
MLINKS+=putwc.3 fputwc.3 putwc.3 putwchar.3
|
||||
MLINKS+=getwc.3 fgetwc.3 getwc.3 getwchar.3
|
||||
MLINKS+=wprintf.3 fwprintf.3 wprintf.3 swprintf.3
|
||||
MLINKS+=wprintf.3 vwprintf.3 wprintf.3 vfwprintf.3 wprintf.3 vswprintf.3
|
||||
MLINKS+=wscanf.3 fwscanf.3 wscanf.3 swscanf.3 wscanf.3 vwscanf.3
|
||||
MLINKS+=wscanf.3 vswscanf.3 wscanf.3 vfwscanf.3
|
||||
@@ -0,0 +1,46 @@
|
||||
/* $NetBSD: _fileno.c,v 1.5 2009/10/21 01:07:45 snj Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: _fileno.c,v 1.5 2009/10/21 01:07:45 snj Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#if defined(__indr_reference)
|
||||
__indr_reference(_fileno, fileno)
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
#undef fileno
|
||||
int _fileno (FILE *);
|
||||
|
||||
int
|
||||
fileno(FILE *stream)
|
||||
{
|
||||
|
||||
return _fileno(stream);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,45 @@
|
||||
/* $NetBSD: _fseeko.c,v 1.5 2009/10/21 01:07:45 snj Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: _fseeko.c,v 1.5 2009/10/21 01:07:45 snj Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#if defined(__indr_reference)
|
||||
__indr_reference(_fseeko, fseeko)
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
int _fseeko(FILE *, off_t, int);
|
||||
|
||||
int
|
||||
fseeko(FILE *stream, off_t offset, int whence)
|
||||
{
|
||||
|
||||
return _fseeko(stream, offset, whence);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,45 @@
|
||||
/* $NetBSD: _ftello.c,v 1.5 2009/10/21 01:07:45 snj Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1996 Christos Zoulas. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: _ftello.c,v 1.5 2009/10/21 01:07:45 snj Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#if defined(__indr_reference)
|
||||
__indr_reference(_ftello, ftello)
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
off_t _ftello(FILE *);
|
||||
|
||||
off_t
|
||||
ftello(FILE *stream)
|
||||
{
|
||||
|
||||
return _ftello(stream);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,85 @@
|
||||
/* $NetBSD: asprintf.c,v 1.18 2009/10/25 20:44:13 christos Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: asprintf.c,v 1.18 2009/10/25 20:44:13 christos Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(asprintf, _asprintf)
|
||||
#endif
|
||||
|
||||
int
|
||||
asprintf(char **str, char const *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
FILE f;
|
||||
struct __sfileext fext;
|
||||
unsigned char *_base;
|
||||
|
||||
_DIAGASSERT(str != NULL);
|
||||
|
||||
_FILEEXT_SETUP(&f, &fext);
|
||||
f._file = -1;
|
||||
f._flags = __SWR | __SSTR | __SALC;
|
||||
f._bf._base = f._p = malloc((size_t)128);
|
||||
if (f._bf._base == NULL)
|
||||
goto err;
|
||||
f._bf._size = f._w = 127; /* Leave room for the NUL */
|
||||
va_start(ap, fmt);
|
||||
ret = __vfprintf_unlocked(&f, fmt, ap);
|
||||
va_end(ap);
|
||||
if (ret < 0)
|
||||
goto err;
|
||||
*f._p = '\0';
|
||||
_base = realloc(f._bf._base, (size_t)ret + 1);
|
||||
if (_base == NULL)
|
||||
goto err;
|
||||
*str = (char *)_base;
|
||||
return (ret);
|
||||
|
||||
err:
|
||||
if (f._bf._base)
|
||||
free(f._bf._base);
|
||||
*str = NULL;
|
||||
errno = ENOMEM;
|
||||
return (-1);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/* $NetBSD: clrerr.c,v 1.10 2003/08/07 16:43:21 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)clrerr.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif
|
||||
__RCSID("$NetBSD: clrerr.c,v 1.10 2003/08/07 16:43:21 agc Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#undef clearerr
|
||||
|
||||
void
|
||||
clearerr(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
__sclearerr(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/* $NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: dprintf.c,v 1.1 2010/09/06 14:52:55 christos Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
dprintf(int fd, const char * __restrict fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vdprintf(fd, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
.\" $NetBSD: fclose.3,v 1.9 2003/08/07 16:43:21 agc Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fclose.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd June 4, 1993
|
||||
.Dt FCLOSE 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fclose
|
||||
.Nd close a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn fclose "FILE *stream"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fclose
|
||||
function
|
||||
dissociates the named
|
||||
.Fa stream
|
||||
from its underlying file or set of functions.
|
||||
If the stream was being used for output, any buffered data is written
|
||||
first, using
|
||||
.Xr fflush 3 .
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion 0 is returned.
|
||||
Otherwise,
|
||||
.Dv EOF
|
||||
is returned and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
In either case no further access to the stream is possible.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The argument
|
||||
.Fa stream
|
||||
is not an open stream.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn fclose
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr close 2
|
||||
or
|
||||
.Xr fflush 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr close 2 ,
|
||||
.Xr fflush 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr setbuf 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fclose
|
||||
function
|
||||
conforms to
|
||||
.St -ansiC .
|
||||
@@ -0,0 +1,78 @@
|
||||
/* $NetBSD: fclose.c,v 1.17 2010/01/11 20:39:29 joerg Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fclose.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif
|
||||
__RCSID("$NetBSD: fclose.c,v 1.17 2010/01/11 20:39:29 joerg Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fclose(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
if (fp->_flags == 0) { /* not open! */
|
||||
errno = EBADF;
|
||||
return (EOF);
|
||||
}
|
||||
FLOCKFILE(fp);
|
||||
WCIO_FREE(fp);
|
||||
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);
|
||||
FREELB(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
fp->_file = -1;
|
||||
fp->_flags = 0; /* Release this FILE for reuse. */
|
||||
fp->_r = fp->_w = 0; /* Mess up if reaccessed. */
|
||||
return (r);
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/* $NetBSD: fdopen.c,v 1.15 2008/03/13 15:40:00 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fdopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fdopen.c,v 1.15 2008/03/13 15:40:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(fdopen,_fdopen)
|
||||
#endif
|
||||
|
||||
FILE *
|
||||
fdopen(fd, mode)
|
||||
int fd;
|
||||
const char *mode;
|
||||
{
|
||||
FILE *fp;
|
||||
int flags, oflags, fdflags, tmp;
|
||||
|
||||
_DIAGASSERT(fd != -1);
|
||||
|
||||
/*
|
||||
* File descriptors are a full int, but _file is only a short.
|
||||
* If we get a valid file descriptor that is greater or equal to
|
||||
* USHRT_MAX, then the fd will get sign-extended into an
|
||||
* invalid file descriptor. Handle this case by failing the
|
||||
* open. (We treat the short as unsigned, and special-case -1).
|
||||
*/
|
||||
if (fd >= USHRT_MAX) {
|
||||
errno = EMFILE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags = __sflags(mode, &oflags)) == 0)
|
||||
return (NULL);
|
||||
|
||||
/* Make sure the mode the user wants is a subset of the actual mode. */
|
||||
if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
|
||||
return (NULL);
|
||||
tmp = fdflags & O_ACCMODE;
|
||||
if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) {
|
||||
errno = EINVAL;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
if (oflags & O_NONBLOCK) {
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == -1) {
|
||||
return (NULL);
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
errno = EFTYPE;
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if ((fp = __sfp()) == NULL)
|
||||
return (NULL);
|
||||
fp->_flags = flags;
|
||||
/*
|
||||
* 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 ((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);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* $NetBSD: feof.c,v 1.11 2003/08/07 16:43:22 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)feof.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: feof.c,v 1.11 2003/08/07 16:43:22 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro feof.
|
||||
*/
|
||||
#undef feof
|
||||
|
||||
int
|
||||
feof(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sfeof(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
.\" $NetBSD: ferror.3,v 1.14 2010/05/08 11:35:14 wiz Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)ferror.3 8.2 (Berkeley) 4/19/94
|
||||
.\"
|
||||
.Dd May 6, 2010
|
||||
.Dt FERROR 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm clearerr ,
|
||||
.Nm feof ,
|
||||
.Nm ferror ,
|
||||
.Nm fileno
|
||||
.Nd check and reset stream status
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft void
|
||||
.Fn clearerr "FILE *stream"
|
||||
.Ft int
|
||||
.Fn feof "FILE *stream"
|
||||
.Ft int
|
||||
.Fn ferror "FILE *stream"
|
||||
.Ft int
|
||||
.Fn fileno "FILE *stream"
|
||||
.Sh DESCRIPTION
|
||||
The function
|
||||
.Fn clearerr
|
||||
clears the end-of-file and error indicators for the stream pointed
|
||||
to by
|
||||
.Fa stream .
|
||||
.Pp
|
||||
The function
|
||||
.Fn feof
|
||||
tests the end-of-file indicator for the stream pointed to by
|
||||
.Fa stream ,
|
||||
returning non-zero if it is set.
|
||||
The end-of-file indicator can only be cleared by the function
|
||||
.Fn clearerr .
|
||||
.Pp
|
||||
The function
|
||||
.Fn ferror
|
||||
tests the error indicator for the stream pointed to by
|
||||
.Fa stream ,
|
||||
returning non-zero if it is set.
|
||||
The error indicator can only be reset by the
|
||||
.Fn clearerr
|
||||
function.
|
||||
.Pp
|
||||
The function
|
||||
.Fn fileno
|
||||
examines the argument
|
||||
.Fa stream
|
||||
and returns its integer descriptor.
|
||||
.Sh ERRORS
|
||||
The functions
|
||||
.Fn clearerr ,
|
||||
.Fn feof ,
|
||||
and
|
||||
.Fn ferror
|
||||
should neither fail nor set the external variable
|
||||
.Va errno .
|
||||
However, the function
|
||||
.Fn fileno
|
||||
may fail and return \-1 in case the argument
|
||||
.Fa stream
|
||||
is not associated with a valid file descriptor.
|
||||
(In this case the
|
||||
.Nx
|
||||
implementation does not follow the optional
|
||||
.Tn POSIX
|
||||
recommendation to set the
|
||||
.Va errno
|
||||
variable to
|
||||
.Er EBADF . )
|
||||
.Sh SEE ALSO
|
||||
.Xr open 2 ,
|
||||
.Xr stdio 3
|
||||
.Sh STANDARDS
|
||||
The functions
|
||||
.Fn clearerr ,
|
||||
.Fn feof ,
|
||||
and
|
||||
.Fn ferror
|
||||
conform to
|
||||
.St -ansiC .
|
||||
The function
|
||||
.Fn fileno
|
||||
conforms to
|
||||
.St -p1003.1-90 .
|
||||
@@ -0,0 +1,66 @@
|
||||
/* $NetBSD: ferror.c,v 1.11 2003/08/07 16:43:22 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)ferror.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: ferror.c,v 1.11 2003/08/07 16:43:22 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro ferror.
|
||||
*/
|
||||
#undef ferror
|
||||
|
||||
int
|
||||
ferror(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sferror(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
.\" $NetBSD: fflush.3,v 1.12 2003/08/07 16:43:22 agc Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fflush.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd June 4, 1993
|
||||
.Dt FFLUSH 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fflush ,
|
||||
.Nm fpurge
|
||||
.Nd flush a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn fflush "FILE *stream"
|
||||
.Ft int
|
||||
.Fn fpurge "FILE *stream"
|
||||
.Sh DESCRIPTION
|
||||
The function
|
||||
.Fn fflush
|
||||
forces a write of all buffered data for the given output or update
|
||||
.Fa stream
|
||||
via the stream's underlying write function.
|
||||
The open status of the stream is unaffected.
|
||||
.Pp
|
||||
If the
|
||||
.Fa stream
|
||||
argument is
|
||||
.Dv NULL ,
|
||||
.Fn fflush
|
||||
flushes
|
||||
.Em all
|
||||
open output streams.
|
||||
.Pp
|
||||
The function
|
||||
.Fn fpurge
|
||||
erases any input or output buffered in the given
|
||||
.Fa stream .
|
||||
For output streams this discards any unwritten output.
|
||||
For input streams this discards any input read from the underlying object
|
||||
but not yet obtained via
|
||||
.Xr getc 3 ;
|
||||
this includes any text pushed back via
|
||||
.Xr ungetc 3 .
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion 0 is returned.
|
||||
Otherwise,
|
||||
.Dv EOF
|
||||
is returned and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
.Fa stream
|
||||
is not an open stream, or, in the case of
|
||||
.Fn fflush ,
|
||||
not a stream open for writing.
|
||||
.El
|
||||
.Pp
|
||||
The function
|
||||
.Fn fflush
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routine
|
||||
.Xr write 2 .
|
||||
.Sh SEE ALSO
|
||||
.Xr write 2 ,
|
||||
.Xr fclose 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr setbuf 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fflush
|
||||
function
|
||||
conforms to
|
||||
.St -ansiC .
|
||||
@@ -0,0 +1,112 @@
|
||||
/* $NetBSD: fflush.c,v 1.15 2003/08/07 16:43:22 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fflush.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fflush.c,v 1.15 2003/08/07 16:43:22 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef _REENTRANT
|
||||
extern rwlock_t __sfp_lock;
|
||||
#endif
|
||||
|
||||
/* Flush a single file, or (if fp is NULL) all files. */
|
||||
int
|
||||
fflush(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
if (fp == NULL) {
|
||||
rwlock_rdlock(&__sfp_lock);
|
||||
r = _fwalk(__sflush);
|
||||
rwlock_unlock(&__sfp_lock);
|
||||
return r;
|
||||
}
|
||||
|
||||
FLOCKFILE(fp);
|
||||
if ((fp->_flags & (__SWR | __SRW)) == 0) {
|
||||
errno = EBADF;
|
||||
r = EOF;
|
||||
} else {
|
||||
r = __sflush(fp);
|
||||
}
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
__sflush(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
unsigned char *p;
|
||||
int n, t;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/* $NetBSD: fgetc.c,v 1.11 2003/08/07 16:43:22 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fgetc.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fgetc.c,v 1.11 2003/08/07 16:43:22 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fgetc(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sgetc(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
.\" $NetBSD: fgetln.3,v 1.14 2004/05/10 17:15:28 drochner Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fgetln.3 8.3 (Berkeley) 4/19/94
|
||||
.\"
|
||||
.Dd April 21, 2004
|
||||
.Dt FGETLN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetln
|
||||
.Nd get a line from a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft char *
|
||||
.Fn fgetln "FILE * restrict stream" "size_t * restrict len"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fgetln
|
||||
function
|
||||
returns a pointer to the next line from the stream referenced by
|
||||
.Fa stream .
|
||||
This line is
|
||||
.Em not
|
||||
a C string as it does not end with a terminating
|
||||
.Dv NUL
|
||||
character.
|
||||
The length of the line, including the final newline,
|
||||
is stored in the memory location to which
|
||||
.Fa len
|
||||
points.
|
||||
(Note, however, that if the line is the last
|
||||
in a file that does not end in a newline,
|
||||
the returned text will not contain a newline.)
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion a pointer is returned;
|
||||
this pointer becomes invalid after the next
|
||||
.Tn I/O
|
||||
operation on
|
||||
.Fa stream
|
||||
(whether successful or not)
|
||||
or as soon as the stream is closed.
|
||||
Otherwise,
|
||||
.Dv NULL
|
||||
is returned.
|
||||
The
|
||||
.Fn fgetln
|
||||
function
|
||||
does not distinguish between end-of-file and error; the routines
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
must be used
|
||||
to determine which occurred.
|
||||
If an error occurs, the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
The end-of-file condition is remembered, even on a terminal, and all
|
||||
subsequent attempts to read will return
|
||||
.Dv NULL
|
||||
until the condition is
|
||||
cleared with
|
||||
.Xr clearerr 3 .
|
||||
.Pp
|
||||
The text to which the returned pointer points may be modified,
|
||||
provided that no changes are made beyond the returned size.
|
||||
These changes are lost as soon as the pointer becomes invalid.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width [EBADF]
|
||||
.It Bq Er EBADF
|
||||
The argument
|
||||
.Fa stream
|
||||
is not a stream open for reading.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn fgetln
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr fflush 3 ,
|
||||
.Xr malloc 3 ,
|
||||
.Xr read 2 ,
|
||||
.Xr stat 2 ,
|
||||
or
|
||||
.Xr realloc 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fgets 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr putc 3
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn fgetln
|
||||
function first appeared in
|
||||
.Bx 4.4 .
|
||||
.Sh CAVEATS
|
||||
Since the returned buffer is not a C string (it is not null terminated), a
|
||||
common practice is to replace the newline character with
|
||||
.Sq \e0 .
|
||||
However, if the last line in a file does not contain a newline,
|
||||
the returned text won't contain a newline either.
|
||||
The following code demonstrates how to deal with this problem by allocating a
|
||||
temporary buffer:
|
||||
.Bd -literal
|
||||
char *buf, *lbuf;
|
||||
size_t len;
|
||||
|
||||
lbuf = NULL;
|
||||
while ((buf = fgetln(fp, &len))) {
|
||||
if (buf[len - 1] == '\en')
|
||||
buf[len - 1] = '\e0';
|
||||
else {
|
||||
if ((lbuf = (char *)malloc(len + 1)) == NULL)
|
||||
err(1, NULL);
|
||||
memcpy(lbuf, buf, len);
|
||||
lbuf[len] = '\e0';
|
||||
buf = lbuf;
|
||||
}
|
||||
printf("%s\en", buf);
|
||||
|
||||
if (lbuf != NULL) {
|
||||
free(lbuf);
|
||||
lbuf = NULL;
|
||||
}
|
||||
}
|
||||
.Ed
|
||||
@@ -0,0 +1,64 @@
|
||||
/* $NetBSD: fgetln.c,v 1.16 2009/12/02 09:03:13 roy Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: fgetln.c,v 1.16 2009/12/02 09:03:13 roy Exp $");
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(fgetln,_fgetln)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Get an input line.
|
||||
* This now uses getdelim(3) for a code reduction.
|
||||
* The upside is that strings are now always NULL terminated, but relying
|
||||
* on this is non portable - better to use the POSIX getdelim(3) function.
|
||||
*/
|
||||
char *
|
||||
fgetln(FILE *fp, size_t *lenp)
|
||||
{
|
||||
char *p;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
p = __fgetstr(fp, lenp, '\n');
|
||||
FUNLOCKFILE(fp);
|
||||
return p;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/* $NetBSD: fgetpos.c,v 1.11 2003/08/07 16:43:23 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fgetpos.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fgetpos.c,v 1.11 2003/08/07 16:43:23 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fgetpos(fp, pos)
|
||||
FILE *fp;
|
||||
fpos_t *pos;
|
||||
{
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(pos != NULL);
|
||||
|
||||
return((*pos = (off_t)ftello(fp)) == (off_t)-1);
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
.\" $NetBSD: fgets.3,v 1.22 2010/05/13 18:38:24 jruoho Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fgets.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd May 13, 2010
|
||||
.Dt FGETS 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgets ,
|
||||
.Nm gets
|
||||
.Nd get a line from a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft char *
|
||||
.Fn fgets "char * restrict str" "int size" "FILE * restrict stream"
|
||||
.Ft char *
|
||||
.Fn gets "char *str"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fgets
|
||||
function
|
||||
reads at most one less than the number of characters specified by
|
||||
.Fa size
|
||||
from the given
|
||||
.Fa stream
|
||||
and stores them in the string
|
||||
.Fa str .
|
||||
Reading stops when a newline character is found,
|
||||
at end-of-file or error.
|
||||
The newline, if any, is retained, and a
|
||||
.Ql \e0
|
||||
character is appended to end the string.
|
||||
.Pp
|
||||
The
|
||||
.Fn gets
|
||||
function
|
||||
is equivalent to
|
||||
.Fn fgets
|
||||
with an infinite
|
||||
.Fa size
|
||||
and a
|
||||
.Fa stream
|
||||
of
|
||||
.Em stdin ,
|
||||
except that the newline character (if any) is not stored in the string.
|
||||
It is the caller's responsibility to ensure that the input line,
|
||||
if any, is sufficiently short to fit in the string.
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion,
|
||||
.Fn fgets
|
||||
and
|
||||
.Fn gets
|
||||
return
|
||||
a pointer to the string.
|
||||
If end-of-file or an error occurs before any characters are read,
|
||||
they return
|
||||
.Dv NULL .
|
||||
The
|
||||
.Fn fgets
|
||||
and
|
||||
.Fn gets
|
||||
functions
|
||||
do not distinguish between end-of-file and error, and callers must use
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
to determine which occurred.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The given
|
||||
.Fa stream
|
||||
is not a readable stream.
|
||||
.El
|
||||
.Pp
|
||||
The function
|
||||
.Fn fgets
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr fflush 3 ,
|
||||
.Xr fstat 2 ,
|
||||
.Xr read 2 ,
|
||||
or
|
||||
.Xr malloc 3 .
|
||||
.Pp
|
||||
The function
|
||||
.Fn gets
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routine
|
||||
.Xr getchar 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr feof 3 ,
|
||||
.Xr ferror 3 ,
|
||||
.Xr fgetln 3
|
||||
.Sh STANDARDS
|
||||
The functions
|
||||
.Fn fgets
|
||||
and
|
||||
.Fn gets
|
||||
conform to
|
||||
.St -ansiC
|
||||
and
|
||||
.St -p1003.1-2001 .
|
||||
The
|
||||
.St -p1003.1-2008
|
||||
revision marked
|
||||
.Fn gets
|
||||
as obsolescent.
|
||||
.Sh CAVEATS
|
||||
The following bit of code illustrates a case where the programmer assumes a
|
||||
string is too long if it does not contain a newline:
|
||||
.Bd -literal
|
||||
char buf[1024], *p;
|
||||
|
||||
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
||||
if ((p = strchr(buf, '\en')) == NULL) {
|
||||
fprintf(stderr, "input line too long.\en");
|
||||
exit(1);
|
||||
}
|
||||
*p = '\e0';
|
||||
printf("%s\en", buf);
|
||||
}
|
||||
.Ed
|
||||
.Pp
|
||||
While the error would be true if a line longer than 1023 characters
|
||||
were read, it would be false in two other cases:
|
||||
.Bl -enum -offset indent
|
||||
.It
|
||||
If the last line in a file does not contain a newline, the string returned by
|
||||
.Fn fgets
|
||||
will not contain a newline either.
|
||||
Thus
|
||||
.Fn strchr
|
||||
will return
|
||||
.Dv NULL
|
||||
and the program will terminate, even if the line was valid.
|
||||
.It
|
||||
All C string functions, including
|
||||
.Fn strchr ,
|
||||
correctly assume the end of the string is represented by a null
|
||||
.Pq Sq \e0
|
||||
character.
|
||||
If the first character of a line returned by
|
||||
.Fn fgets
|
||||
were null,
|
||||
.Fn strchr
|
||||
would immediately return without considering the rest of the returned text
|
||||
which may indeed include a newline.
|
||||
.El
|
||||
.Pp
|
||||
Consider using
|
||||
.Xr fgetln 3
|
||||
instead when dealing with untrusted input.
|
||||
.Sh SECURITY CONSIDERATIONS
|
||||
Since it is usually impossible to ensure that the next input line
|
||||
is less than some arbitrary length, and because overflowing the
|
||||
input buffer is almost invariably a security violation, programs
|
||||
should
|
||||
.Em NEVER
|
||||
use
|
||||
.Fn gets .
|
||||
The
|
||||
.Fn gets
|
||||
function
|
||||
exists purely to conform to
|
||||
.St -ansiC .
|
||||
@@ -0,0 +1,134 @@
|
||||
/* $NetBSD: fgets.c,v 1.27 2009/10/25 20:44:13 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fgets.c,v 1.27 2009/10/25 20:44:13 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#ifdef _FORTIFY_SOURCE
|
||||
#undef fgets
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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(buf, n, fp)
|
||||
char *buf;
|
||||
int n;
|
||||
FILE *fp;
|
||||
{
|
||||
int len;
|
||||
char *s;
|
||||
unsigned char *p, *t;
|
||||
|
||||
_DIAGASSERT(buf != NULL);
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
_SET_ORIENTATION(fp, -1);
|
||||
s = buf;
|
||||
n--; /* leave space for NUL */
|
||||
do {
|
||||
/*
|
||||
* If the buffer is empty, refill it.
|
||||
*/
|
||||
if (fp->_r <= 0) {
|
||||
if (__srefill(fp)) {
|
||||
/* EOF/error: stop with partial or no line */
|
||||
if (s == buf) {
|
||||
FUNLOCKFILE(fp);
|
||||
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) {
|
||||
if (n < 0) {
|
||||
/*
|
||||
* Caller's length <= 0
|
||||
* We can't write into the buffer, so cannot
|
||||
* return a string, so must return NULL.
|
||||
* Set errno and __SERR so it is consistent.
|
||||
* TOG gives no indication of what to do here!
|
||||
*/
|
||||
errno = EINVAL;
|
||||
fp->_flags |= __SERR;
|
||||
FUNLOCKFILE(fp);
|
||||
return NULL;
|
||||
}
|
||||
len = n;
|
||||
}
|
||||
t = memchr(p, '\n', (size_t)len);
|
||||
if (t != NULL) {
|
||||
len = (int)(++t - p);
|
||||
fp->_r -= len;
|
||||
fp->_p = t;
|
||||
(void)memcpy(s, p, (size_t)len);
|
||||
s[len] = 0;
|
||||
FUNLOCKFILE(fp);
|
||||
return (buf);
|
||||
}
|
||||
fp->_r -= len;
|
||||
fp->_p += len;
|
||||
(void)memcpy(s, p, (size_t)len);
|
||||
s += len;
|
||||
n -= len;
|
||||
} while (n != 0);
|
||||
*s = 0;
|
||||
FUNLOCKFILE(fp);
|
||||
return (buf);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* $NetBSD: fgetstr.c,v 1.11 2010/01/11 20:39:29 joerg Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Roy Marples.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: fgetstr.c,v 1.11 2010/01/11 20:39:29 joerg Exp $");
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Get an input line.
|
||||
* This now uses getdelim(3) for a code reduction.
|
||||
* The upside is that strings are now always NULL terminated, but relying
|
||||
* on this is non portable - better to use the POSIX getdelim(3) function.
|
||||
*/
|
||||
char *
|
||||
__fgetstr(FILE *__restrict fp, size_t *__restrict lenp, int sep)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(lenp != NULL);
|
||||
|
||||
n = __getdelim(&_EXT(fp)->_fgetstr_buf, &_EXT(fp)->_fgetstr_len, sep, fp);
|
||||
if (n == -1) {
|
||||
*lenp = 0;
|
||||
if (__sferror(fp) && errno == EOVERFLOW) /* fixup errno */
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
*lenp = n;
|
||||
return _EXT(fp)->_fgetstr_buf;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/* $NetBSD: fgetwc.c,v 1.11 2009/10/25 20:44:13 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fgetwc.c,v 1.11 2009/10/25 20:44:13 christos Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
wint_t
|
||||
__fgetwc_unlock(FILE *fp)
|
||||
{
|
||||
struct wchar_io_data *wcio;
|
||||
wchar_t wc;
|
||||
size_t nr;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
_SET_ORIENTATION(fp, 1);
|
||||
wcio = WCIO_GET(fp);
|
||||
_DIAGASSERT(wcio != NULL);
|
||||
|
||||
/* if there're ungetwc'ed wchars, use them */
|
||||
if (wcio->wcio_ungetwc_inbuf)
|
||||
return wcio->wcio_ungetwc_buf[--wcio->wcio_ungetwc_inbuf];
|
||||
|
||||
if (fp->_r <= 0) {
|
||||
restart:
|
||||
if (__srefill(fp) != 0)
|
||||
return WEOF;
|
||||
}
|
||||
nr = mbrtowc(&wc, (const char *)fp->_p,
|
||||
(size_t)fp->_r, &wcio->wcio_mbstate_in);
|
||||
if (nr == (size_t)-1) {
|
||||
fp->_flags |= __SERR;
|
||||
return WEOF;
|
||||
} else if (nr == (size_t)-2) {
|
||||
fp->_p += fp->_r;
|
||||
fp->_r = 0;
|
||||
goto restart;
|
||||
}
|
||||
if (wc == L'\0') {
|
||||
while (*fp->_p != '\0') {
|
||||
++fp->_p;
|
||||
--fp->_r;
|
||||
}
|
||||
nr = 1;
|
||||
}
|
||||
fp->_p += nr;
|
||||
fp->_r -= nr;
|
||||
|
||||
return wc;
|
||||
}
|
||||
|
||||
wint_t
|
||||
fgetwc(FILE *fp)
|
||||
{
|
||||
wint_t r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __fgetwc_unlock(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
.\" $NetBSD: fgetwln.3,v 1.2 2010/12/16 17:42:27 wiz Exp $
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" 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.
|
||||
.\"
|
||||
.\" @(#)fgetln.3 8.3 (Berkeley) 4/19/94
|
||||
.\" $FreeBSD: src/lib/libc/stdio/fgetwln.3,v 1.1 2004/07/16 06:06:09 tjr Exp $
|
||||
.\"
|
||||
.Dd July 16, 2004
|
||||
.Dt FGETWLN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetwln
|
||||
.Nd get a line of wide characters from a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.In wchar.h
|
||||
.Ft wchar_t *
|
||||
.Fn fgetwln "FILE * restrict stream" "size_t * restrict len"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fgetwln
|
||||
function
|
||||
returns a pointer to the next line from the stream referenced by
|
||||
.Fa stream .
|
||||
This line is
|
||||
.Em not
|
||||
a standard wide-character string as it does not end with a terminating
|
||||
null wide character.
|
||||
The length of the line, including the final newline,
|
||||
is stored in the memory location to which
|
||||
.Fa len
|
||||
points.
|
||||
(Note, however, that if the line is the last
|
||||
in a file that does not end in a newline,
|
||||
the returned text will not contain a newline.)
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion a pointer is returned;
|
||||
this pointer becomes invalid after the next
|
||||
.Tn I/O
|
||||
operation on
|
||||
.Fa stream
|
||||
(whether successful or not)
|
||||
or as soon as the stream is closed.
|
||||
Otherwise,
|
||||
.Dv NULL
|
||||
is returned.
|
||||
The
|
||||
.Fn fgetwln
|
||||
function
|
||||
does not distinguish between end-of-file and error; the routines
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
must be used
|
||||
to determine which occurred.
|
||||
If an error occurs, the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
The end-of-file condition is remembered, even on a terminal, and all
|
||||
subsequent attempts to read will return
|
||||
.Dv NULL
|
||||
until the condition is
|
||||
cleared with
|
||||
.Xr clearerr 3 .
|
||||
.Pp
|
||||
The text to which the returned pointer points may be modified,
|
||||
provided that no changes are made beyond the returned size.
|
||||
These changes are lost as soon as the pointer becomes invalid.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The argument
|
||||
.Fa stream
|
||||
is not a stream open for reading.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn fgetwln
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr mbrtowc 3 ,
|
||||
.Xr realloc 3 ,
|
||||
or
|
||||
.Xr read 2 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fgetln 3 ,
|
||||
.Xr fgetws 3 ,
|
||||
.Xr fopen 3
|
||||
@@ -0,0 +1,108 @@
|
||||
/* $NetBSD: fgetwln.c,v 1.4 2010/01/11 20:39:29 joerg Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002-2004 Tim J. Robbins.
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
__FBSDID("$FreeBSD: src/lib/libc/stdio/fgetwln.c,v 1.2 2004/08/06 17:00:09 tjr Exp $");
|
||||
#else
|
||||
__RCSID("$NetBSD: fgetwln.c,v 1.4 2010/01/11 20:39:29 joerg Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(fgetwln,_fgetwln)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
static int
|
||||
__slbexpand(FILE *fp, size_t newsize)
|
||||
{
|
||||
void *p;
|
||||
|
||||
#ifdef notdef
|
||||
++newsize;
|
||||
#endif
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
if (_EXT(fp)->_fgetstr_len >= newsize)
|
||||
return (0);
|
||||
if ((p = realloc(_EXT(fp)->_fgetstr_buf, newsize)) == NULL)
|
||||
return (-1);
|
||||
_EXT(fp)->_fgetstr_buf = p;
|
||||
_EXT(fp)->_fgetstr_len = newsize;
|
||||
return (0);
|
||||
}
|
||||
|
||||
wchar_t *
|
||||
fgetwln(FILE * __restrict fp, size_t *lenp)
|
||||
{
|
||||
wint_t wc;
|
||||
size_t len;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
_SET_ORIENTATION(fp, 1);
|
||||
|
||||
len = 0;
|
||||
while ((wc = __fgetwc_unlock(fp)) != WEOF) {
|
||||
#define GROW 512
|
||||
if (len * sizeof(wchar_t) >= _EXT(fp)->_fgetstr_len &&
|
||||
__slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
|
||||
goto error;
|
||||
*((wchar_t *)(void *)_EXT(fp)->_fgetstr_buf + len++) = wc;
|
||||
if (wc == L'\n')
|
||||
break;
|
||||
}
|
||||
if (len == 0)
|
||||
goto error;
|
||||
|
||||
FUNLOCKFILE(fp);
|
||||
*lenp = len;
|
||||
return ((wchar_t *)(void *)_EXT(fp)->_fgetstr_buf);
|
||||
|
||||
error:
|
||||
FUNLOCKFILE(fp);
|
||||
*lenp = 0;
|
||||
return (NULL);
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
.\" $NetBSD: fgetws.3,v 1.3 2010/12/16 17:42:27 wiz Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fgets.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.\" Original version ID:
|
||||
.\" FreeBSD: src/lib/libc/stdio/fgets.3,v 1.16 2002/05/31 05:01:17 archie Exp
|
||||
.\" FreeBSD: src/lib/libc/stdio/fgetws.3,v 1.2 2002/09/06 11:23:55 tjr Exp
|
||||
.\"
|
||||
.Dd August 6, 2002
|
||||
.Dt FGETWS 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetws
|
||||
.Nd get a line of wide characters from a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.In wchar.h
|
||||
.Ft "wchar_t *"
|
||||
.Fn fgetws "wchar_t * restrict ws" "int n" "FILE * restrict fp"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fgetws
|
||||
function
|
||||
reads at most one less than the number of characters specified by
|
||||
.Fa n
|
||||
from the given
|
||||
.Fa fp
|
||||
and stores them in the wide-character string
|
||||
.Fa ws .
|
||||
Reading stops when a newline character is found,
|
||||
at end-of-file or error.
|
||||
The newline, if any, is retained.
|
||||
If any characters are read and there is no error, a
|
||||
.Ql \e0
|
||||
character is appended to end the string.
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion,
|
||||
.Fn fgetws
|
||||
returns
|
||||
.Fa ws .
|
||||
If end-of-file occurs before any characters are read,
|
||||
.Fn fgetws
|
||||
returns
|
||||
.Dv NULL
|
||||
and the buffer contents remain unchanged.
|
||||
If an error occurs,
|
||||
.Fn fgetws
|
||||
returns
|
||||
.Dv NULL
|
||||
and the buffer contents are indeterminate.
|
||||
The
|
||||
.Fn fgetws
|
||||
function
|
||||
does not distinguish between end-of-file and error, and callers must use
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
to determine which occurred.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The given
|
||||
.Fa fp
|
||||
argument is not a readable stream.
|
||||
.It Bq Er EILSEQ
|
||||
The data obtained from the input stream does not form a valid
|
||||
multibyte character.
|
||||
.El
|
||||
.Pp
|
||||
The function
|
||||
.Fn fgetws
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr fflush 3 ,
|
||||
.Xr fstat 2 ,
|
||||
.Xr read 2 ,
|
||||
or
|
||||
.Xr malloc 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr feof 3 ,
|
||||
.Xr ferror 3 ,
|
||||
.Xr fgets 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fgetws
|
||||
function
|
||||
conforms to
|
||||
.St -p1003.1-2001 .
|
||||
@@ -0,0 +1,91 @@
|
||||
/* $NetBSD: fgetws.c,v 1.2 2006/07/03 17:06:36 tnozaki Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 Tim J. Robbins.
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* Original version ID:
|
||||
* FreeBSD: src/lib/libc/stdio/fgetws.c,v 1.4 2002/09/20 13:25:40 tjr Exp
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIB_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fgetws.c,v 1.2 2006/07/03 17:06:36 tnozaki Exp $");
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
wchar_t *
|
||||
fgetws(ws, n, fp)
|
||||
wchar_t * __restrict ws;
|
||||
int n;
|
||||
FILE * __restrict fp;
|
||||
{
|
||||
wchar_t *wsp;
|
||||
wint_t wc;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(ws != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
_SET_ORIENTATION(fp, 1);
|
||||
|
||||
if (n <= 0) {
|
||||
errno = EINVAL;
|
||||
goto error;
|
||||
}
|
||||
|
||||
wsp = ws;
|
||||
while (n-- > 1) {
|
||||
wc = __fgetwc_unlock(fp);
|
||||
if (__sferror(fp) != 0)
|
||||
goto error;
|
||||
if (__sfeof(fp) != 0) {
|
||||
if (wsp == ws) {
|
||||
/* EOF/error, no characters read yet. */
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
*wsp++ = (wchar_t)wc;
|
||||
if (wc == L'\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*wsp++ = L'\0';
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
return (ws);
|
||||
|
||||
error:
|
||||
FUNLOCKFILE(fp);
|
||||
return (NULL);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/* $NetBSD: fileext.h,v 1.6 2010/01/11 20:39:29 joerg Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
/*
|
||||
* file extension
|
||||
*/
|
||||
struct __sfileext {
|
||||
struct __sbuf _ub; /* ungetc buffer */
|
||||
struct wchar_io_data _wcio; /* wide char i/o status */
|
||||
size_t _fgetstr_len;
|
||||
char *_fgetstr_buf;
|
||||
#ifdef _REENTRANT
|
||||
mutex_t _lock; /* Lock for FLOCKFILE/FUNLOCKFILE */
|
||||
cond_t _lockcond; /* Condition variable for signalling lock releases */
|
||||
thr_t _lockowner; /* The thread currently holding the lock */
|
||||
int _lockcount; /* Count of recursive locks */
|
||||
int _lockinternal; /* Flag of whether the lock is held inside stdio */
|
||||
int _lockcancelstate; /* Stashed cancellation state on internal lock */
|
||||
#endif
|
||||
};
|
||||
|
||||
#define _EXT(fp) ((struct __sfileext *)(void *)((fp)->_ext._base))
|
||||
#define _UB(fp) _EXT(fp)->_ub
|
||||
#ifdef _REENTRANT
|
||||
#define _LOCK(fp) (_EXT(fp)->_lock)
|
||||
#define _LOCKCOND(fp) (_EXT(fp)->_lockcond)
|
||||
#define _LOCKOWNER(fp) (_EXT(fp)->_lockowner)
|
||||
#define _LOCKCOUNT(fp) (_EXT(fp)->_lockcount)
|
||||
#define _LOCKINTERNAL(fp) (_EXT(fp)->_lockinternal)
|
||||
#define _LOCKCANCELSTATE(fp) (_EXT(fp)->_lockcancelstate)
|
||||
#define _FILEEXT_SETUP(f, fext) do { \
|
||||
/* LINTED */(f)->_ext._base = (unsigned char *)(fext); \
|
||||
(fext)->_fgetstr_len = 0; \
|
||||
(fext)->_fgetstr_buf = NULL; \
|
||||
mutex_init(&_LOCK(f), NULL); \
|
||||
cond_init(&_LOCKCOND(f), 0, NULL); \
|
||||
_LOCKOWNER(f) = NULL; \
|
||||
_LOCKCOUNT(f) = 0; \
|
||||
_LOCKINTERNAL(f) = 0; \
|
||||
} while (/* CONSTCOND */ 0)
|
||||
#else
|
||||
#define _FILEEXT_SETUP(f, fext) do { \
|
||||
/* LINTED */(f)->_ext._base = (unsigned char *)(fext); \
|
||||
(fext)->_fgetstr_len = 0; \
|
||||
(fext)->_fgetstr_buf = NULL; \
|
||||
} while (/* CONSTCOND */ 0)
|
||||
#endif
|
||||
@@ -0,0 +1,71 @@
|
||||
/* $NetBSD: fileno.c,v 1.12 2004/05/09 17:27:53 kleink Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fileno.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fileno.c,v 1.12 2004/05/09 17:27:53 kleink Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro fileno.
|
||||
*/
|
||||
#undef fileno
|
||||
int _fileno __P((FILE *)); /* XXX */
|
||||
|
||||
__weak_alias(fileno,_fileno)
|
||||
|
||||
int
|
||||
_fileno(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sfileno(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
/* $NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94";
|
||||
#else
|
||||
__RCSID("$NetBSD: findfp.c,v 1.25 2010/09/06 14:52:55 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#include "glue.h"
|
||||
|
||||
int __sdidinit;
|
||||
|
||||
#define NDYNAMIC 10 /* add ten more whenever necessary */
|
||||
|
||||
#define std(flags, file) \
|
||||
/* p r w flags file bf lfbsize cookie close */ \
|
||||
{ NULL, 0, 0, flags, file, { NULL, 0 }, 0, __sF + file, __sclose, \
|
||||
/* read seek write ext up */ \
|
||||
__sread, __sseek, __swrite, { (void *)(__sFext + file), 0 }, NULL, \
|
||||
/* ur ubuf, nbuf lb blksize offset */ \
|
||||
0, { '\0', '\0', '\0' }, { '\0' }, { NULL, 0 }, 0, (fpos_t)0 }
|
||||
|
||||
/* the usual - (stdin + stdout + stderr) */
|
||||
static FILE usual[FOPEN_MAX - 3];
|
||||
static struct __sfileext usualext[FOPEN_MAX - 3];
|
||||
static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
|
||||
|
||||
#if defined(_REENTRANT) && !defined(__lint__) /* XXX lint is busted */
|
||||
#define STDEXT { ._lock = MUTEX_INITIALIZER, ._lockcond = COND_INITIALIZER }
|
||||
struct __sfileext __sFext[3] = { STDEXT,
|
||||
STDEXT,
|
||||
STDEXT};
|
||||
#else
|
||||
struct __sfileext __sFext[3];
|
||||
#endif
|
||||
|
||||
FILE __sF[3] = {
|
||||
std(__SRD, STDIN_FILENO), /* stdin */
|
||||
std(__SWR, STDOUT_FILENO), /* stdout */
|
||||
std(__SWR|__SNBF, STDERR_FILENO) /* stderr */
|
||||
};
|
||||
struct glue __sglue = { &uglue, 3, __sF };
|
||||
|
||||
static struct glue *moreglue __P((int));
|
||||
void f_prealloc __P((void));
|
||||
|
||||
#ifdef _REENTRANT
|
||||
rwlock_t __sfp_lock = RWLOCK_INITIALIZER;
|
||||
#endif
|
||||
|
||||
static struct glue *
|
||||
moreglue(n)
|
||||
int n;
|
||||
{
|
||||
struct glue *g;
|
||||
FILE *p;
|
||||
struct __sfileext *pext;
|
||||
static FILE empty;
|
||||
|
||||
g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE)
|
||||
+ n * sizeof(struct __sfileext));
|
||||
if (g == NULL)
|
||||
return (NULL);
|
||||
p = (FILE *)ALIGN((u_long)(g + 1));
|
||||
g->next = NULL;
|
||||
g->niobs = n;
|
||||
g->iobs = p;
|
||||
pext = (void *)(p + n);
|
||||
while (--n >= 0) {
|
||||
*p = empty;
|
||||
_FILEEXT_SETUP(p, pext);
|
||||
p++;
|
||||
pext++;
|
||||
}
|
||||
return (g);
|
||||
}
|
||||
|
||||
void
|
||||
__sfpinit(FILE *fp)
|
||||
{
|
||||
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 */
|
||||
_UB(fp)._base = NULL; /* no ungetc buffer */
|
||||
_UB(fp)._size = 0;
|
||||
memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data));
|
||||
}
|
||||
|
||||
/*
|
||||
* Find a free FILE for fopen et al.
|
||||
*/
|
||||
FILE *
|
||||
__sfp()
|
||||
{
|
||||
FILE *fp;
|
||||
int n;
|
||||
struct glue *g;
|
||||
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
|
||||
rwlock_wrlock(&__sfp_lock);
|
||||
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;
|
||||
}
|
||||
rwlock_unlock(&__sfp_lock);
|
||||
return (NULL);
|
||||
found:
|
||||
__sfpinit(fp);
|
||||
rwlock_unlock(&__sfp_lock);
|
||||
return (fp);
|
||||
}
|
||||
|
||||
/*
|
||||
* XXX. Force immediate allocation of internal memory. Not used by stdio,
|
||||
* but documented historically for certain applications. Bad applications.
|
||||
*/
|
||||
void
|
||||
f_prealloc()
|
||||
{
|
||||
struct glue *g;
|
||||
int n;
|
||||
|
||||
n = (int)sysconf(_SC_OPEN_MAX) - 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) _fwalk(fclose); */
|
||||
(void) fflush(NULL); /* `cheating' */
|
||||
}
|
||||
|
||||
/*
|
||||
* __sinit() is called whenever stdio's internal variables must be set up.
|
||||
*/
|
||||
void
|
||||
__sinit()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < FOPEN_MAX - 3; i++)
|
||||
_FILEEXT_SETUP(&usual[i], &usualext[i]);
|
||||
|
||||
/* make sure we clean up on exit */
|
||||
__cleanup = _cleanup; /* conservative */
|
||||
__sdidinit = 1;
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/* $NetBSD: flags.c,v 1.14 2003/08/07 16:43:23 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)flags.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: flags.c,v 1.14 2003/08/07 16:43:23 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.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(mode, optr)
|
||||
const char *mode;
|
||||
int *optr;
|
||||
{
|
||||
int ret, m, o;
|
||||
|
||||
_DIAGASSERT(mode != NULL);
|
||||
|
||||
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
|
||||
* f means open only plain files.
|
||||
*/
|
||||
for (; *mode; mode++)
|
||||
switch (*mode) {
|
||||
case '+':
|
||||
ret = __SRW;
|
||||
m = O_RDWR;
|
||||
break;
|
||||
case 'f':
|
||||
o |= O_NONBLOCK;
|
||||
break;
|
||||
case 'b':
|
||||
break;
|
||||
default: /* We could produce a warning here */
|
||||
break;
|
||||
}
|
||||
|
||||
*optr = m | o;
|
||||
return (ret);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/* $NetBSD: floatio.h,v 1.5 2005/05/14 23:51:02 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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
|
||||
/*
|
||||
* MAXEXPDIG is the maximum number of decimal digits needed to store a
|
||||
* floating point exponent in the largest supported format. It should
|
||||
* be ceil(log10(LDBL_MAX_10_EXP)) or, if hexadecimal floating point
|
||||
* conversions are supported, ceil(log10(LDBL_MAX_EXP)). But since it
|
||||
* is presently never greater than 5 in practice, we fudge it.
|
||||
*/
|
||||
#define MAXEXPDIG 6
|
||||
#if LDBL_MAX_EXP > 999999
|
||||
#error "floating point buffers too small"
|
||||
#endif
|
||||
@@ -0,0 +1,120 @@
|
||||
.\" $NetBSD: flockfile.3,v 1.4 2008/04/30 13:10:51 martin Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 2003 The NetBSD Foundation, Inc.
|
||||
.\" All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to The NetBSD Foundation
|
||||
.\" by Klaus Klein.
|
||||
.\"
|
||||
.\" 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.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||
.\"
|
||||
.Dd January 28, 2003
|
||||
.Dt FLOCKFILE 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm flockfile ,
|
||||
.Nm ftrylockfile ,
|
||||
.Nm funlockfile
|
||||
.Nd stdio stream locking functions
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft void
|
||||
.Fn flockfile "FILE *file"
|
||||
.Ft int
|
||||
.Fn ftrylockfile "FILE *file"
|
||||
.Ft void
|
||||
.Fn funlockfile "FILE *file"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn flockfile ,
|
||||
.Fn ftrylockfile ,
|
||||
and
|
||||
.Fn funlockfile
|
||||
functions provide applications with explicit control of locking of
|
||||
stdio stream objects.
|
||||
They can be used by a thread to execute a sequence of I/O operations
|
||||
as a unit, without interference from another thread.
|
||||
.Pp
|
||||
Locks on stdio streams are recursive, and a lock count is maintained.
|
||||
stdio streams are created unlocked, with a lock count of zero.
|
||||
After successful acquisition of the lock, its count is incremented
|
||||
to one, indicating locked state of the stdio stream.
|
||||
Each subsequent relock operation performed by the owner thread
|
||||
increments the lock count by one, and each subsequent unlock
|
||||
operation performed by the owner thread decrements the lock count by one,
|
||||
allowing matching lock and unlock operations to be nested.
|
||||
After its lock count is decremented to zero, the stdio stream returns
|
||||
to unlocked state, and ownership of the stdio stream is relinquished.
|
||||
.Pp
|
||||
The
|
||||
.Fn flockfile
|
||||
function acquires the ownership of
|
||||
.Fa file
|
||||
for the calling thread.
|
||||
If
|
||||
.Fa file
|
||||
is already owned by another thread, the calling thread is suspended
|
||||
until the acquisition is possible (i.e.,
|
||||
.Fa file
|
||||
is relinquished again and the calling thread is scheduled to acquire it).
|
||||
.Pp
|
||||
The
|
||||
.Fn ftrylockfile
|
||||
function acquires the ownership of
|
||||
.Fa file
|
||||
for the calling thread only if
|
||||
.Fa file
|
||||
is available.
|
||||
.Pp
|
||||
The
|
||||
.Fn funlockfile
|
||||
function relinquishes the ownership of
|
||||
.Fa file
|
||||
previously granted to the calling thread.
|
||||
Only the current owner of
|
||||
.Fa file
|
||||
may
|
||||
.Fn funlockfile
|
||||
it.
|
||||
.Sh RETURN VALUES
|
||||
If successful, the
|
||||
.Fn ftrylockfile
|
||||
function returns 0.
|
||||
Otherwise, it returns non-zero to indicate that the lock cannot be acquired.
|
||||
.Sh SEE ALSO
|
||||
.Xr getc_unlocked 3 ,
|
||||
.Xr getchar_unlocked 3 ,
|
||||
.Xr putc_unlocked 3 ,
|
||||
.Xr putchar_unlocked 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn flockfile ,
|
||||
.Fn ftrylockfile
|
||||
and
|
||||
.Fn funlockfile
|
||||
functions conform to
|
||||
.St -p1003.1-2001 .
|
||||
.Sh BUGS
|
||||
The design of these interfaces does not allow for addressing the
|
||||
problem of priority inversion.
|
||||
@@ -0,0 +1,183 @@
|
||||
/* $NetBSD: flockfile.c,v 1.10 2010/12/08 02:07:03 joerg Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Nathan J. Williams.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: flockfile.c,v 1.10 2010/12/08 02:07:03 joerg Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(flockfile,_flockfile)
|
||||
__weak_alias(ftrylockfile,_ftrylockfile)
|
||||
__weak_alias(funlockfile,_funlockfile)
|
||||
#endif
|
||||
|
||||
#ifdef _REENTRANT
|
||||
/*
|
||||
* XXX This code makes the assumption that a thr_t (pthread_t) is a
|
||||
* XXX pointer.
|
||||
*/
|
||||
|
||||
void
|
||||
flockfile(FILE *fp)
|
||||
{
|
||||
|
||||
__flockfile_internal(fp, 0);
|
||||
}
|
||||
|
||||
int
|
||||
ftrylockfile(FILE *fp)
|
||||
{
|
||||
int retval;
|
||||
|
||||
if (__isthreaded == 0)
|
||||
return 0;
|
||||
|
||||
retval = 0;
|
||||
mutex_lock(&_LOCK(fp));
|
||||
|
||||
if (_LOCKOWNER(fp) == thr_self()) {
|
||||
_LOCKCOUNT(fp)++;
|
||||
} else if (_LOCKOWNER(fp) == NULL) {
|
||||
_LOCKOWNER(fp) = thr_self();
|
||||
_LOCKCOUNT(fp) = 1;
|
||||
} else
|
||||
retval = -1;
|
||||
|
||||
mutex_unlock(&_LOCK(fp));
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void
|
||||
funlockfile(FILE *fp)
|
||||
{
|
||||
|
||||
__funlockfile_internal(fp, 0);
|
||||
}
|
||||
|
||||
void
|
||||
__flockfile_internal(FILE *fp, int internal)
|
||||
{
|
||||
|
||||
if (__isthreaded == 0)
|
||||
return;
|
||||
|
||||
mutex_lock(&_LOCK(fp));
|
||||
|
||||
if (_LOCKOWNER(fp) == thr_self()) {
|
||||
_LOCKCOUNT(fp)++;
|
||||
if (internal)
|
||||
_LOCKINTERNAL(fp)++;
|
||||
} else {
|
||||
/* danger! cond_wait() is a cancellation point. */
|
||||
int oldstate;
|
||||
thr_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
|
||||
while (_LOCKOWNER(fp) != NULL)
|
||||
cond_wait(&_LOCKCOND(fp), &_LOCK(fp));
|
||||
thr_setcancelstate(oldstate, NULL);
|
||||
_LOCKOWNER(fp) = thr_self();
|
||||
_LOCKCOUNT(fp) = 1;
|
||||
if (internal)
|
||||
_LOCKINTERNAL(fp) = 1;
|
||||
}
|
||||
|
||||
if (_LOCKINTERNAL(fp) == 1)
|
||||
/* stash cancellation state and disable */
|
||||
thr_setcancelstate(PTHREAD_CANCEL_DISABLE,
|
||||
&_LOCKCANCELSTATE(fp));
|
||||
|
||||
mutex_unlock(&_LOCK(fp));
|
||||
}
|
||||
|
||||
void
|
||||
__funlockfile_internal(FILE *fp, int internal)
|
||||
{
|
||||
|
||||
if (__isthreaded == 0)
|
||||
return;
|
||||
|
||||
mutex_lock(&_LOCK(fp));
|
||||
|
||||
if (internal) {
|
||||
_LOCKINTERNAL(fp)--;
|
||||
if (_LOCKINTERNAL(fp) == 0)
|
||||
thr_setcancelstate(_LOCKCANCELSTATE(fp), NULL);
|
||||
}
|
||||
|
||||
_LOCKCOUNT(fp)--;
|
||||
if (_LOCKCOUNT(fp) == 0) {
|
||||
_LOCKOWNER(fp) = NULL;
|
||||
cond_signal(&_LOCKCOND(fp));
|
||||
}
|
||||
|
||||
mutex_unlock(&_LOCK(fp));
|
||||
}
|
||||
|
||||
#else /* _REENTRANT */
|
||||
|
||||
void
|
||||
flockfile(FILE *fp)
|
||||
{
|
||||
/* LINTED deliberate lack of effect */
|
||||
(void)fp;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
ftrylockfile(FILE *fp)
|
||||
{
|
||||
/* LINTED deliberate lack of effect */
|
||||
(void)fp;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
funlockfile(FILE *fp)
|
||||
{
|
||||
/* LINTED deliberate lack of effect */
|
||||
(void)fp;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#endif /* _REENTRANT */
|
||||
@@ -0,0 +1,193 @@
|
||||
.\" $NetBSD: fmemopen.3,v 1.5 2010/10/07 00:14:14 enami Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
|
||||
.\" All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to The NetBSD Foundation
|
||||
.\" by Christos Zoulas.
|
||||
.\"
|
||||
.\" 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 NetBSD
|
||||
.\" Foundation, Inc. and its contributors.
|
||||
.\" 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||
.\"
|
||||
.Dd September 24, 2010
|
||||
.Dt FMEMOPEN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fmemopen
|
||||
.Nd open a stream that points to the given buffer
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft FILE *
|
||||
.Fn fmemopen "void *restrict buffer" "size_t size" "const char *restrict mode"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fmemopen
|
||||
function
|
||||
associates a stream with the given
|
||||
.Fa buffer
|
||||
and
|
||||
.Fa size .
|
||||
The
|
||||
.Fa buffer
|
||||
can be either
|
||||
.Dv NULL ,
|
||||
or must be of the given
|
||||
.Fa size .
|
||||
If the
|
||||
.Fa buffer
|
||||
is
|
||||
.Dv NULL ,
|
||||
a
|
||||
.Fa buffer
|
||||
of the given
|
||||
.Fa size
|
||||
will be dynamically allocated using
|
||||
.Xr malloc 3
|
||||
and freed when
|
||||
.Xr fclose 3
|
||||
is called.
|
||||
.Pp
|
||||
The
|
||||
.Fa mode
|
||||
argument has the same meaning as in
|
||||
.Xr fopen 3 .
|
||||
.Pp
|
||||
The stream treats the buffer as it would treat a file tracking the current
|
||||
position to perform I/O operations.
|
||||
For example, in the beginning the stream points to the beginning of the buffer,
|
||||
unless
|
||||
.Dv a
|
||||
was specified in the
|
||||
.Fa mode
|
||||
argument, and then it points to the first
|
||||
.Dv NUL
|
||||
byte.
|
||||
If a
|
||||
.Dv NULL
|
||||
.Fa buffer
|
||||
was specified, then the stream will always point at the first byte of the
|
||||
.Fa buffer .
|
||||
.Pp
|
||||
The stream also keeps track of the
|
||||
.Fa size
|
||||
of the
|
||||
.Fa buffer .
|
||||
The
|
||||
.Fa size
|
||||
is initialized depending on the mode:
|
||||
.Bl -tag -width r/w+
|
||||
.It Dv r/r+
|
||||
Set to the
|
||||
.Fa size
|
||||
argument.
|
||||
.It Dv w/w+
|
||||
Set to
|
||||
.Dv 0 .
|
||||
.It Dv a/a+
|
||||
Set to the first
|
||||
.Dv NUL
|
||||
byte, or the
|
||||
.Fa size
|
||||
argument if one is not found.
|
||||
.El
|
||||
.Pp
|
||||
Read or write operations advance the buffer, but not to exceed the given
|
||||
.Fa size
|
||||
of the
|
||||
.Fa buffer .
|
||||
Trying to read beyond the
|
||||
.Fa size
|
||||
of the
|
||||
.Fa buffer
|
||||
results in
|
||||
.Dv EOF
|
||||
returned.
|
||||
.Dv NUL
|
||||
bytes are read normally.
|
||||
Trying to write beyond the
|
||||
.Fa size
|
||||
of the
|
||||
.Fa buffer
|
||||
has no effect.
|
||||
.Pp
|
||||
When a stream open for writing is either flushed or closed, a
|
||||
.Dv NUL
|
||||
byte is written at the current position or at the end of the current
|
||||
.Fa size
|
||||
as kept internally, if there is room.
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion,
|
||||
.Fn fmemopen
|
||||
returns a
|
||||
.Dv FILE
|
||||
pointer.
|
||||
Otherwise,
|
||||
.Dv NULL
|
||||
is returned and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EINVAL
|
||||
The
|
||||
.Fa size
|
||||
was
|
||||
.Dv 0 ;
|
||||
or the
|
||||
.Fa mode
|
||||
argument is invalid;
|
||||
or the
|
||||
.Fa buffer
|
||||
argument is
|
||||
.Dv NULL
|
||||
and the
|
||||
.Fa mode
|
||||
argument does not specify a
|
||||
.Dv + .
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn fmemopen
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors
|
||||
specified for the routine
|
||||
.Xr malloc 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr fclose 3 ,
|
||||
.Xr fflush 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr malloc 3
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn fmemopen
|
||||
functions first appeared in
|
||||
.Nx 6.0 .
|
||||
@@ -0,0 +1,220 @@
|
||||
/* $NetBSD: fmemopen.c,v 1.5 2010/09/27 17:08:29 tnozaki Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2007, 2010 Takehiko NOZAKI,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fmemopen.c,v 1.5 2010/09/27 17:08:29 tnozaki Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
struct fmemopen_cookie {
|
||||
char *head, *tail, *cur, *eob;
|
||||
};
|
||||
|
||||
static int
|
||||
fmemopen_read(void *cookie, char *buf, int nbytes)
|
||||
{
|
||||
struct fmemopen_cookie *p;
|
||||
char *s;
|
||||
|
||||
_DIAGASSERT(cookie != NULL);
|
||||
_DIAGASSERT(buf != NULL && nbytes > 0);
|
||||
|
||||
p = (struct fmemopen_cookie *)cookie;
|
||||
s = p->cur;
|
||||
do {
|
||||
if (p->cur == p->tail)
|
||||
break;
|
||||
*buf++ = *p->cur++;
|
||||
} while (--nbytes > 0);
|
||||
|
||||
return (int)(p->cur - s);
|
||||
}
|
||||
|
||||
static int
|
||||
fmemopen_write(void *cookie, const char *buf, int nbytes)
|
||||
{
|
||||
struct fmemopen_cookie *p;
|
||||
char *s;
|
||||
|
||||
_DIAGASSERT(cookie != NULL);
|
||||
_DIAGASSERT(buf != NULL && nbytes > 0);
|
||||
|
||||
p = (struct fmemopen_cookie *)cookie;
|
||||
if (p->cur >= p->tail)
|
||||
return 0;
|
||||
s = p->cur;
|
||||
do {
|
||||
if (p->cur == p->tail - 1) {
|
||||
if (*buf == '\0') {
|
||||
*p->cur++ = '\0';
|
||||
goto ok;
|
||||
}
|
||||
break;
|
||||
}
|
||||
*p->cur++ = *buf++;
|
||||
} while (--nbytes > 0);
|
||||
*p->cur = '\0';
|
||||
ok:
|
||||
if (p->cur > p->eob)
|
||||
p->eob = p->cur;
|
||||
|
||||
return (int)(p->cur - s);
|
||||
}
|
||||
|
||||
static fpos_t
|
||||
fmemopen_seek(void *cookie, fpos_t offset, int whence)
|
||||
{
|
||||
struct fmemopen_cookie *p;
|
||||
|
||||
_DIAGASSERT(cookie != NULL);
|
||||
|
||||
p = (struct fmemopen_cookie *)cookie;
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
offset += p->cur - p->head;
|
||||
break;
|
||||
case SEEK_END:
|
||||
offset += p->eob - p->head;
|
||||
break;
|
||||
default:
|
||||
errno = EINVAL;
|
||||
goto error;
|
||||
}
|
||||
if (offset >= (fpos_t)0 && offset <= p->tail - p->head) {
|
||||
p->cur = p->head + (ptrdiff_t)offset;
|
||||
return (fpos_t)(p->cur - p->head);
|
||||
}
|
||||
error:
|
||||
return (fpos_t)-1;
|
||||
}
|
||||
|
||||
static int
|
||||
fmemopen_close0(void *cookie)
|
||||
{
|
||||
_DIAGASSERT(cookie != NULL);
|
||||
|
||||
free(cookie);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
fmemopen_close1(void *cookie)
|
||||
{
|
||||
struct fmemopen_cookie *p;
|
||||
|
||||
_DIAGASSERT(cookie != NULL);
|
||||
|
||||
p = (struct fmemopen_cookie *)cookie;
|
||||
free(p->head);
|
||||
free(p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
FILE *
|
||||
fmemopen(void * __restrict buf, size_t size, const char * __restrict mode)
|
||||
{
|
||||
int flags, oflags;
|
||||
FILE *fp;
|
||||
struct fmemopen_cookie *cookie;
|
||||
|
||||
if (size < (size_t)1)
|
||||
goto invalid;
|
||||
|
||||
flags = __sflags(mode, &oflags);
|
||||
if (flags == 0)
|
||||
return NULL;
|
||||
|
||||
if ((oflags & O_RDWR) == 0 && buf == NULL)
|
||||
goto invalid;
|
||||
|
||||
fp = __sfp();
|
||||
if (fp == NULL)
|
||||
return NULL;
|
||||
fp->_file = -1;
|
||||
|
||||
cookie = malloc(sizeof(*cookie));
|
||||
if (cookie == NULL)
|
||||
goto release;
|
||||
|
||||
if (buf == NULL) {
|
||||
cookie->head = malloc(size);
|
||||
if (cookie->head == NULL) {
|
||||
free(cookie);
|
||||
goto release;
|
||||
}
|
||||
*cookie->head = '\0';
|
||||
fp->_close = &fmemopen_close1;
|
||||
} else {
|
||||
cookie->head = (char *)buf;
|
||||
if (oflags & O_TRUNC)
|
||||
*cookie->head = '\0';
|
||||
fp->_close = &fmemopen_close0;
|
||||
}
|
||||
|
||||
cookie->tail = cookie->head + size;
|
||||
cookie->eob = cookie->head;
|
||||
do {
|
||||
if (*cookie->eob == '\0')
|
||||
break;
|
||||
++cookie->eob;
|
||||
} while (--size > 0);
|
||||
|
||||
cookie->cur = (oflags & O_APPEND) ? cookie->eob : cookie->head;
|
||||
|
||||
fp->_flags = flags;
|
||||
fp->_write = (flags & __SRD) ? NULL : &fmemopen_write;
|
||||
fp->_read = (flags & __SWR) ? NULL : &fmemopen_read;
|
||||
fp->_seek = &fmemopen_seek;
|
||||
fp->_cookie = (void *)cookie;
|
||||
|
||||
return fp;
|
||||
|
||||
invalid:
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
|
||||
release:
|
||||
fp->_flags = 0;
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
.\" $NetBSD: fopen.3,v 1.24 2010/04/05 21:34:47 joerg Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd June 4, 1993
|
||||
.Dt FOPEN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fopen ,
|
||||
.Nm fdopen ,
|
||||
.Nm freopen
|
||||
.Nd stream open functions
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft FILE *
|
||||
.Fn fopen "const char * restrict path" "const char * restrict mode"
|
||||
.Ft FILE *
|
||||
.Fn fdopen "int fildes" "const char *mode"
|
||||
.Ft FILE *
|
||||
.Fn freopen "const char * restrict path" "const char * restrict mode" "FILE * restrict stream"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fopen
|
||||
function
|
||||
opens the file whose name is the string pointed to by
|
||||
.Fa path
|
||||
and associates a stream with it.
|
||||
.Pp
|
||||
The argument
|
||||
.Fa mode
|
||||
points to a string beginning with one of the following
|
||||
sequences (Additional characters may follow these sequences.):
|
||||
.Bl -tag -width indent
|
||||
.It Dq Li r
|
||||
Open for reading.
|
||||
.It Dq Li r+
|
||||
Open for reading and writing.
|
||||
.It Dq Li w
|
||||
Open for writing.
|
||||
Truncate file to zero length or create file.
|
||||
.It Dq Li w+
|
||||
Open for reading and writing.
|
||||
Truncate file to zero length or create file.
|
||||
.It Dq Li a
|
||||
Append; open for writing.
|
||||
The file is created if it does not exist.
|
||||
.It Dq Li a+
|
||||
Append; open for reading and writing.
|
||||
The file is created if it does not exist.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fa mode
|
||||
string can also include the letter ``b'' either as a last character or
|
||||
as a character between the characters in any of the two-character strings
|
||||
described above.
|
||||
This is strictly for compatibility with
|
||||
.St -ansiC
|
||||
and has no effect; the ``b'' is ignored.
|
||||
.Pp
|
||||
The letter ``f'' in the mode string restricts fopen to regular
|
||||
files; if the file opened is not a regular file,
|
||||
.Fn fopen
|
||||
will fail.
|
||||
This is a non
|
||||
.St -ansiC
|
||||
extension.
|
||||
.Pp
|
||||
Any created files will have mode
|
||||
.Pf \*q Dv S_IRUSR
|
||||
\&|
|
||||
.Dv S_IWUSR
|
||||
\&|
|
||||
.Dv S_IRGRP
|
||||
\&|
|
||||
.Dv S_IWGRP
|
||||
\&|
|
||||
.Dv S_IROTH
|
||||
\&|
|
||||
.Dv S_IWOTH Ns \*q
|
||||
.Pq Li 0666 ,
|
||||
as modified by the process'
|
||||
umask value (see
|
||||
.Xr umask 2 ) .
|
||||
.Pp
|
||||
Opening a file with append mode causes all subsequent writes to it
|
||||
to be forced to the then current end of file, regardless of intervening
|
||||
repositioning of the stream.
|
||||
.Pp
|
||||
The
|
||||
.Fn fopen
|
||||
and
|
||||
.Fn freopen
|
||||
functions initially position the stream at the start of the file
|
||||
unless the file is opened with append mode,
|
||||
in which case the stream is initially positioned at the end of the file.
|
||||
.\" PR 6072 claims this paragraph is not correct.
|
||||
.\" .Pp
|
||||
.\" Reads and writes may be intermixed on read/write streams in any order,
|
||||
.\" and do not require an intermediate seek as in previous versions of
|
||||
.\" .Em stdio .
|
||||
.\" This is not portable to other systems, however;
|
||||
.\" .Tn ANSI C
|
||||
.\" requires that
|
||||
.\" a file positioning function intervene between output and input, unless
|
||||
.\" an input operation encounters end-of-file.
|
||||
.Pp
|
||||
The
|
||||
.Fn fdopen
|
||||
function associates a stream with the existing file descriptor,
|
||||
.Fa fildes .
|
||||
The
|
||||
.Fa mode
|
||||
of the stream must be compatible with the mode of the file descriptor.
|
||||
The stream is positioned at the file offset of the file descriptor.
|
||||
.Pp
|
||||
The
|
||||
.Fn freopen
|
||||
function
|
||||
opens the file whose name is the string pointed to by
|
||||
.Fa path
|
||||
and associates the stream pointed to by
|
||||
.Fa stream
|
||||
with it.
|
||||
The original stream (if it exists) is closed.
|
||||
The
|
||||
.Fa mode
|
||||
argument is used just as in the
|
||||
.Fn fopen
|
||||
function.
|
||||
The primary use of the
|
||||
.Fn freopen
|
||||
function
|
||||
is to change the file associated with a
|
||||
standard text stream
|
||||
.Pf ( Em stderr ,
|
||||
.Em stdin ,
|
||||
or
|
||||
.Em stdout ) .
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion
|
||||
.Fn fopen ,
|
||||
.Fn fdopen
|
||||
and
|
||||
.Fn freopen
|
||||
return a
|
||||
.Tn FILE
|
||||
pointer.
|
||||
Otherwise,
|
||||
.Dv NULL
|
||||
is returned and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EINVAL
|
||||
The
|
||||
.Fa mode
|
||||
provided to
|
||||
.Fn fopen ,
|
||||
.Fn fdopen ,
|
||||
or
|
||||
.Fn freopen
|
||||
was invalid.
|
||||
.It Bq Er EFTYPE
|
||||
The file is not a regular file and the character ``f'' is specified
|
||||
in the mode.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn fopen ,
|
||||
.Fn fdopen
|
||||
and
|
||||
.Fn freopen
|
||||
functions
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routine
|
||||
.Xr malloc 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn fopen
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routine
|
||||
.Xr open 2 .
|
||||
.Pp
|
||||
The
|
||||
.Fn fdopen
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routine
|
||||
.Xr fcntl 2 .
|
||||
.Pp
|
||||
The
|
||||
.Fn freopen
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr open 2 ,
|
||||
.Xr fclose 3
|
||||
and
|
||||
.Xr fflush 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr open 2 ,
|
||||
.Xr fclose 3 ,
|
||||
.Xr fileno 3 ,
|
||||
.Xr fseek 3 ,
|
||||
.Xr funopen 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fopen
|
||||
and
|
||||
.Fn freopen
|
||||
functions
|
||||
conform to
|
||||
.St -ansiC .
|
||||
The
|
||||
.Fn fdopen
|
||||
function conforms to
|
||||
.St -p1003.1-90 .
|
||||
.Sh CAVEATS
|
||||
Proper code using
|
||||
.Fn fdopen
|
||||
with error checking should
|
||||
.Xr close 2
|
||||
.Fa fildes
|
||||
in case of failure, and
|
||||
.Xr fclose 3
|
||||
the resulting FILE * in case of success.
|
||||
.Bd -literal
|
||||
FILE *file;
|
||||
int fd;
|
||||
|
||||
if ((file = fdopen(fd, "r")) != NULL) {
|
||||
/* perform operations on the FILE * */
|
||||
fclose(file);
|
||||
} else {
|
||||
/* failure, report the error */
|
||||
close(fd);
|
||||
}
|
||||
.Ed
|
||||
@@ -0,0 +1,119 @@
|
||||
/* $NetBSD: fopen.c,v 1.13 2008/03/13 15:40:00 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fopen.c,v 1.13 2008/03/13 15:40:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
FILE *
|
||||
fopen(file, mode)
|
||||
const char *file;
|
||||
const char *mode;
|
||||
{
|
||||
FILE *fp;
|
||||
int f;
|
||||
int flags, oflags;
|
||||
|
||||
_DIAGASSERT(file != NULL);
|
||||
if ((flags = __sflags(mode, &oflags)) == 0)
|
||||
return (NULL);
|
||||
if ((fp = __sfp()) == NULL)
|
||||
return (NULL);
|
||||
if ((f = open(file, oflags, DEFFILEMODE)) < 0)
|
||||
goto release;
|
||||
if (oflags & O_NONBLOCK) {
|
||||
struct stat st;
|
||||
if (fstat(f, &st) == -1) {
|
||||
int sverrno = errno;
|
||||
(void)close(f);
|
||||
errno = sverrno;
|
||||
goto release;
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
(void)close(f);
|
||||
errno = EFTYPE;
|
||||
goto release;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* File descriptors are a full int, but _file is only a short.
|
||||
* If we get a valid file descriptor that is greater or equal to
|
||||
* USHRT_MAX, then the fd will get sign-extended into an
|
||||
* invalid file descriptor. Handle this case by failing the
|
||||
* open. (We treat the short as unsigned, and special-case -1).
|
||||
*/
|
||||
if (f >= USHRT_MAX) {
|
||||
errno = EMFILE;
|
||||
goto release;
|
||||
}
|
||||
|
||||
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);
|
||||
release:
|
||||
fp->_flags = 0; /* release */
|
||||
return (NULL);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
.\" $NetBSD: fparseln.3,v 1.4 2009/10/21 01:07:45 snj Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1997 Christos Zoulas. All rights reserved.
|
||||
.\"
|
||||
.\" 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.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
.\"
|
||||
.Dd November 30, 2002
|
||||
.Dt FPARSELN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fparseln
|
||||
.Nd return the next logical line from a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft "char *"
|
||||
.Fo "fparseln"
|
||||
.Fa "FILE *stream" "size_t *len" "size_t *lineno"
|
||||
.Fa "const char delim[3]" "int flags"
|
||||
.Fc
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fparseln
|
||||
function
|
||||
returns a pointer to the next logical line from the stream referenced by
|
||||
.Fa stream .
|
||||
This string is
|
||||
.Dv NUL
|
||||
terminated and it is dynamically allocated on each invocation.
|
||||
It is the responsibility of the caller to free the pointer.
|
||||
.Pp
|
||||
By default, if a character is escaped, both it and the preceding escape
|
||||
character will be present in the returned string.
|
||||
Various
|
||||
.Fa flags
|
||||
alter this behaviour.
|
||||
.Pp
|
||||
The meaning of the arguments is as follows:
|
||||
.Bl -tag -width "lineno"
|
||||
.It Fa stream
|
||||
The stream to read from.
|
||||
.It Fa len
|
||||
If not
|
||||
.Dv NULL ,
|
||||
the length of the string is stored in the memory location to which it
|
||||
points.
|
||||
.It Fa lineno
|
||||
If not
|
||||
.Dv NULL ,
|
||||
the value of the memory location to which is pointed to, is incremented
|
||||
by the number of lines actually read from the file.
|
||||
.It Fa delim
|
||||
Contains the escape, continuation, and comment characters.
|
||||
If a character is
|
||||
.Dv NUL
|
||||
then processing for that character is disabled.
|
||||
If
|
||||
.Dv NULL ,
|
||||
all characters default to values specified below.
|
||||
The contents of
|
||||
.Fa delim
|
||||
is as follows:
|
||||
.Bl -tag -width "delim[0]"
|
||||
.It Fa delim[0]
|
||||
The escape character, which defaults to
|
||||
.Cm \e ,
|
||||
is used to remove any special meaning from the next character.
|
||||
.It Fa delim[1]
|
||||
The continuation character, which defaults to
|
||||
.Cm \e ,
|
||||
is used to indicate that the next line should be concatenated with the
|
||||
current one if this character is the last character on the current line
|
||||
and is not escaped.
|
||||
.It Fa delim[2]
|
||||
The comment character, which defaults to
|
||||
.Cm # ,
|
||||
if not escaped indicates the beginning of a comment that extends until the
|
||||
end of the current line.
|
||||
.El
|
||||
.It Fa flags
|
||||
If non-zero, alter the operation of
|
||||
.Fn fparseln .
|
||||
The various flags, which may be
|
||||
.Em or Ns -ed
|
||||
together, are:
|
||||
.Bl -tag -width "FPARSELN_UNESCCOMM"
|
||||
.It Dv FPARSELN_UNESCCOMM
|
||||
Remove escape preceding an escaped comment.
|
||||
.It Dv FPARSELN_UNESCCONT
|
||||
Remove escape preceding an escaped continuation.
|
||||
.It Dv FPARSELN_UNESCESC
|
||||
Remove escape preceding an escaped escape.
|
||||
.It Dv FPARSELN_UNESCREST
|
||||
Remove escape preceding any other character.
|
||||
.It Dv FPARSELN_UNESCALL
|
||||
All of the above.
|
||||
.El
|
||||
.Pp
|
||||
.El
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion a pointer to the parsed line is returned;
|
||||
otherwise,
|
||||
.Dv NULL
|
||||
is returned.
|
||||
.Pp
|
||||
The
|
||||
.Fn fparseln
|
||||
function uses internally
|
||||
.Xr fgetln 3 ,
|
||||
so all error conditions that apply to
|
||||
.Xr fgetln 3 ,
|
||||
apply to
|
||||
.Fn fparseln .
|
||||
In addition
|
||||
.Fn fparseln
|
||||
may set
|
||||
.Va errno
|
||||
to
|
||||
.Bq Er ENOMEM
|
||||
and return
|
||||
.Dv NULL
|
||||
if it runs out of memory.
|
||||
.Sh SEE ALSO
|
||||
.Xr fgetln 3
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn fparseln
|
||||
function first appeared in
|
||||
.Nx 1.4 .
|
||||
@@ -0,0 +1,249 @@
|
||||
/* $NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997 Christos Zoulas. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(fparseln,_fparseln)
|
||||
#endif
|
||||
|
||||
#if ! HAVE_FPARSELN || BROKEN_FPARSELN
|
||||
|
||||
#ifndef HAVE_NBTOOL_CONFIG_H
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#else
|
||||
#define FLOCKFILE(fp)
|
||||
#define FUNLOCKFILE(fp)
|
||||
#endif
|
||||
|
||||
#if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
|
||||
#define __fgetln(f, l) __fgetstr(f, l, '\n')
|
||||
#else
|
||||
#define __fgetln(f, l) fgetln(f, l)
|
||||
#endif
|
||||
|
||||
static int isescaped(const char *, const char *, int);
|
||||
|
||||
/* isescaped():
|
||||
* Return true if the character in *p that belongs to a string
|
||||
* that starts in *sp, is escaped by the escape character esc.
|
||||
*/
|
||||
static int
|
||||
isescaped(const char *sp, const char *p, int esc)
|
||||
{
|
||||
const char *cp;
|
||||
size_t ne;
|
||||
|
||||
_DIAGASSERT(sp != NULL);
|
||||
_DIAGASSERT(p != NULL);
|
||||
|
||||
/* No escape character */
|
||||
if (esc == '\0')
|
||||
return 0;
|
||||
|
||||
/* Count the number of escape characters that precede ours */
|
||||
for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
|
||||
continue;
|
||||
|
||||
/* Return true if odd number of escape characters */
|
||||
return (ne & 1) != 0;
|
||||
}
|
||||
|
||||
|
||||
/* fparseln():
|
||||
* Read a line from a file parsing continuations ending in \
|
||||
* and eliminating trailing newlines, or comments starting with
|
||||
* the comment char.
|
||||
*/
|
||||
char *
|
||||
fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
|
||||
{
|
||||
static const char dstr[3] = { '\\', '\\', '#' };
|
||||
|
||||
size_t s, len;
|
||||
char *buf;
|
||||
char *ptr, *cp;
|
||||
int cnt;
|
||||
char esc, con, nl, com;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
len = 0;
|
||||
buf = NULL;
|
||||
cnt = 1;
|
||||
|
||||
if (str == NULL)
|
||||
str = dstr;
|
||||
|
||||
esc = str[0];
|
||||
con = str[1];
|
||||
com = str[2];
|
||||
/*
|
||||
* XXX: it would be cool to be able to specify the newline character,
|
||||
* but unfortunately, fgetln does not let us
|
||||
*/
|
||||
nl = '\n';
|
||||
|
||||
FLOCKFILE(fp);
|
||||
|
||||
while (cnt) {
|
||||
cnt = 0;
|
||||
|
||||
if (lineno)
|
||||
(*lineno)++;
|
||||
|
||||
if ((ptr = __fgetln(fp, &s)) == NULL)
|
||||
break;
|
||||
|
||||
if (s && com) { /* Check and eliminate comments */
|
||||
for (cp = ptr; cp < ptr + s; cp++)
|
||||
if (*cp == com && !isescaped(ptr, cp, esc)) {
|
||||
s = cp - ptr;
|
||||
cnt = s == 0 && buf == NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (s && nl) { /* Check and eliminate newlines */
|
||||
cp = &ptr[s - 1];
|
||||
|
||||
if (*cp == nl)
|
||||
s--; /* forget newline */
|
||||
}
|
||||
|
||||
if (s && con) { /* Check and eliminate continuations */
|
||||
cp = &ptr[s - 1];
|
||||
|
||||
if (*cp == con && !isescaped(ptr, cp, esc)) {
|
||||
s--; /* forget continuation char */
|
||||
cnt = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (s == 0) {
|
||||
/*
|
||||
* nothing to add, skip realloc except in case
|
||||
* we need a minimal buf to return an empty line
|
||||
*/
|
||||
if (cnt || buf != NULL)
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((cp = realloc(buf, len + s + 1)) == NULL) {
|
||||
FUNLOCKFILE(fp);
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
buf = cp;
|
||||
|
||||
(void) memcpy(buf + len, ptr, s);
|
||||
len += s;
|
||||
buf[len] = '\0';
|
||||
}
|
||||
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
|
||||
strchr(buf, esc) != NULL) {
|
||||
ptr = cp = buf;
|
||||
while (cp[0] != '\0') {
|
||||
int skipesc;
|
||||
|
||||
while (cp[0] != '\0' && cp[0] != esc)
|
||||
*ptr++ = *cp++;
|
||||
if (cp[0] == '\0' || cp[1] == '\0')
|
||||
break;
|
||||
|
||||
skipesc = 0;
|
||||
if (cp[1] == com)
|
||||
skipesc += (flags & FPARSELN_UNESCCOMM);
|
||||
if (cp[1] == con)
|
||||
skipesc += (flags & FPARSELN_UNESCCONT);
|
||||
if (cp[1] == esc)
|
||||
skipesc += (flags & FPARSELN_UNESCESC);
|
||||
if (cp[1] != com && cp[1] != con && cp[1] != esc)
|
||||
skipesc = (flags & FPARSELN_UNESCREST);
|
||||
|
||||
if (skipesc)
|
||||
cp++;
|
||||
else
|
||||
*ptr++ = *cp++;
|
||||
*ptr++ = *cp++;
|
||||
}
|
||||
*ptr = '\0';
|
||||
len = strlen(buf);
|
||||
}
|
||||
|
||||
if (size)
|
||||
*size = len;
|
||||
return buf;
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
int main(int, char **);
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *ptr;
|
||||
size_t size, line;
|
||||
|
||||
line = 0;
|
||||
while ((ptr = fparseln(stdin, &size, &line, NULL,
|
||||
FPARSELN_UNESCALL)) != NULL)
|
||||
printf("line %d (%d) |%s|\n", line, size, ptr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
# This is a test
|
||||
line 1
|
||||
line 2 \
|
||||
line 3 # Comment
|
||||
line 4 \# Not comment \\\\
|
||||
|
||||
# And a comment \
|
||||
line 5 \\\
|
||||
line 6
|
||||
|
||||
*/
|
||||
|
||||
#endif /* TEST */
|
||||
#endif /* ! HAVE_FPARSELN || BROKEN_FPARSELN */
|
||||
@@ -0,0 +1,62 @@
|
||||
/* $NetBSD: fprintf.c,v 1.11 2003/08/07 16:43:24 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fprintf.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fprintf.c,v 1.11 2003/08/07 16:43:24 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
fprintf(FILE *fp, const char *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(fmt != NULL);
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vfprintf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/* $NetBSD: fpurge.c,v 1.13 2003/08/07 16:43:24 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fpurge.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fpurge.c,v 1.13 2003/08/07 16:43:24 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* fpurge: like fflush, but without writing anything: leave the
|
||||
* given FILE's buffer empty.
|
||||
*/
|
||||
int
|
||||
fpurge(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
if (fp->_flags == 0) {
|
||||
errno = EBADF;
|
||||
return (EOF);
|
||||
}
|
||||
FLOCKFILE(fp);
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
WCIO_FREE(fp);
|
||||
fp->_p = fp->_bf._base;
|
||||
fp->_r = 0;
|
||||
fp->_w = fp->_flags & (__SLBF|__SNBF) ? 0 : fp->_bf._size;
|
||||
FUNLOCKFILE(fp);
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/* $NetBSD: fputc.c,v 1.12 2003/08/07 16:43:24 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fputc.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fputc.c,v 1.12 2003/08/07 16:43:24 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fputc(c, fp)
|
||||
int c;
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sputc(c, fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
.\" $NetBSD: fputs.3,v 1.11 2003/08/07 16:43:24 agc Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fputs.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd June 4, 1993
|
||||
.Dt FPUTS 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fputs ,
|
||||
.Nm puts
|
||||
.Nd output a line to a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn fputs "const char * restrict str" "FILE * restrict stream"
|
||||
.Ft int
|
||||
.Fn puts "const char *str"
|
||||
.Sh DESCRIPTION
|
||||
The function
|
||||
.Fn fputs
|
||||
writes the string pointed to by
|
||||
.Fa str
|
||||
to the stream pointed to by
|
||||
.Fa stream .
|
||||
.\" The terminating
|
||||
.\" .Dv NUL
|
||||
.\" character is not written.
|
||||
.Pp
|
||||
The function
|
||||
.Fn puts
|
||||
writes the string
|
||||
.Fa str ,
|
||||
and a terminating newline character,
|
||||
to the stream
|
||||
.Em stdout .
|
||||
.Sh RETURN VALUES
|
||||
The
|
||||
.Fn fputs
|
||||
function
|
||||
returns 0 on success and
|
||||
.Dv EOF
|
||||
on error;
|
||||
.Fn puts
|
||||
returns a nonnegative integer on success and
|
||||
.Dv EOF
|
||||
on error.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The
|
||||
.Fa stream
|
||||
supplied
|
||||
is not a writable stream.
|
||||
.El
|
||||
.Pp
|
||||
The functions
|
||||
.Fn fputs
|
||||
and
|
||||
.Fn puts
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr write 2 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr putc 3 ,
|
||||
.Xr stdio 3
|
||||
.Sh STANDARDS
|
||||
The functions
|
||||
.Fn fputs
|
||||
and
|
||||
.Fn puts
|
||||
conform to
|
||||
.St -ansiC .
|
||||
@@ -0,0 +1,79 @@
|
||||
/* $NetBSD: fputs.c,v 1.14 2005/06/22 19:45:22 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fputs.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fputs.c,v 1.14 2005/06/22 19:45:22 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
/*
|
||||
* Write the given string to the given file.
|
||||
*/
|
||||
int
|
||||
fputs(s, fp)
|
||||
const char *s;
|
||||
FILE *fp;
|
||||
{
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(s != NULL);
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
if (s == NULL)
|
||||
s = "(null)";
|
||||
|
||||
iov.iov_base = __UNCONST(s);
|
||||
iov.iov_len = uio.uio_resid = strlen(s);
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
FLOCKFILE(fp);
|
||||
_SET_ORIENTATION(fp, -1);
|
||||
r = __sfvwrite(fp, &uio);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/* $NetBSD: fputwc.c,v 1.4 2005/06/12 05:21:27 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fputwc.c,v 1.4 2005/06/12 05:21:27 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
wint_t
|
||||
__fputwc_unlock(wchar_t wc, FILE *fp)
|
||||
{
|
||||
struct wchar_io_data *wcio;
|
||||
mbstate_t *st;
|
||||
size_t size;
|
||||
char buf[MB_LEN_MAX];
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
/* LINTED we don't play with buf */
|
||||
iov.iov_base = (void *)buf;
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
|
||||
_SET_ORIENTATION(fp, 1);
|
||||
wcio = WCIO_GET(fp);
|
||||
if (wcio == 0) {
|
||||
errno = ENOMEM;
|
||||
return WEOF;
|
||||
}
|
||||
|
||||
wcio->wcio_ungetwc_inbuf = 0;
|
||||
st = &wcio->wcio_mbstate_out;
|
||||
|
||||
size = wcrtomb(buf, wc, st);
|
||||
if (size == (size_t)-1) {
|
||||
errno = EILSEQ;
|
||||
return WEOF;
|
||||
}
|
||||
|
||||
_DIAGASSERT(size != 0);
|
||||
|
||||
uio.uio_resid = iov.iov_len = size;
|
||||
if (__sfvwrite(fp, &uio)) {
|
||||
return WEOF;
|
||||
}
|
||||
|
||||
return (wint_t)wc;
|
||||
}
|
||||
|
||||
wint_t
|
||||
fputwc(wchar_t wc, FILE *fp)
|
||||
{
|
||||
wint_t r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __fputwc_unlock(wc, fp);
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
return (r);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
.\" $NetBSD: fputws.3,v 1.3 2010/12/16 17:42:27 wiz Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fputs.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.\" Original version ID:
|
||||
.\" FreeBSD: src/lib/libc/stdio/fputs.3,v 1.8 2001/10/01 16:08:59 ru Exp
|
||||
.\" FreeBSD: src/lib/libc/stdio/fputws.c,v 1.4 2002/09/20 13:25:40 tjr Exp
|
||||
.\"
|
||||
.Dd August 6, 2002
|
||||
.Dt FPUTWS 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fputws
|
||||
.Nd output a line of wide characters to a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.In wchar.h
|
||||
.Ft int
|
||||
.Fn fputws "const wchar_t * restrict ws" "FILE * restrict fp"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fputws
|
||||
function writes the wide-character string pointed to by
|
||||
.Fa ws
|
||||
to the stream pointed to by
|
||||
.Fa fp .
|
||||
.Sh RETURN VALUES
|
||||
The
|
||||
.Fn fputws
|
||||
function
|
||||
returns 0 on success and \-1 on error.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The
|
||||
.Fa fp
|
||||
argument supplied
|
||||
is not a writable stream.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn fputws
|
||||
function may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routine
|
||||
.Xr write 2 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fputs 3 ,
|
||||
.Xr putwc 3 ,
|
||||
.Xr stdio 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fputws
|
||||
function conforms to
|
||||
.St -p1003.1-2001 .
|
||||
@@ -0,0 +1,65 @@
|
||||
/* $NetBSD: fputws.c,v 1.1 2003/03/07 07:11:37 tshiozak Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 Tim J. Robbins.
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* Original version ID:
|
||||
* FreeBSD: src/lib/libc/stdio/fputws.c,v 1.4 2002/09/20 13:25:40 tjr Exp
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fputws.c,v 1.1 2003/03/07 07:11:37 tshiozak Exp $");
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fputws(ws, fp)
|
||||
const wchar_t * __restrict ws;
|
||||
FILE * __restrict fp;
|
||||
{
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(ws != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
_SET_ORIENTATION(fp, 1);
|
||||
|
||||
while (*ws != '\0') {
|
||||
if (__fputwc_unlock(*ws++, fp) == WEOF) {
|
||||
FUNLOCKFILE(fp);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
.\" $NetBSD: fread.3,v 1.14 2009/01/11 02:46:29 christos Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fread.3 8.2 (Berkeley) 3/8/94
|
||||
.\"
|
||||
.Dd December 19, 2008
|
||||
.Dt FREAD 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fread ,
|
||||
.Nm fwrite
|
||||
.Nd binary stream input/output
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft size_t
|
||||
.Fn fread "void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream"
|
||||
.Ft size_t
|
||||
.Fn fwrite "const void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream"
|
||||
.Sh DESCRIPTION
|
||||
The function
|
||||
.Fn fread
|
||||
reads
|
||||
.Fa nmemb
|
||||
objects, each
|
||||
.Fa size
|
||||
bytes long, from the stream pointed to by
|
||||
.Fa stream ,
|
||||
storing them at the location given by
|
||||
.Fa ptr .
|
||||
.Pp
|
||||
The function
|
||||
.Fn fwrite
|
||||
writes
|
||||
.Fa nmemb
|
||||
objects, each
|
||||
.Fa size
|
||||
bytes long, to the stream pointed to by
|
||||
.Fa stream ,
|
||||
obtaining them from the location given by
|
||||
.Fa ptr .
|
||||
.Pp
|
||||
Mixing
|
||||
.Fn fread
|
||||
and
|
||||
.Fn fwrite
|
||||
calls without setting the file position explicitly using
|
||||
.Fn fsetpos
|
||||
between read and write or write and read operations will lead to unexpected
|
||||
results because of buffering the file pointer not being set to the
|
||||
expected position after each operation completes.
|
||||
This behavior is allowed by ANSI C for efficiency and it will not be changed.
|
||||
.Sh RETURN VALUES
|
||||
The functions
|
||||
.Fn fread
|
||||
and
|
||||
.Fn fwrite
|
||||
advance the file position indicator for the stream
|
||||
by the number of bytes read or written.
|
||||
They return the number of objects read or written.
|
||||
If
|
||||
.Fa size
|
||||
or
|
||||
.Fa nmemb
|
||||
is 0, the functions return 0 and the state of
|
||||
.Fa stream
|
||||
remains unchanged.
|
||||
If an error occurs, or the end-of-file is reached,
|
||||
the return value is a short object count (or zero).
|
||||
.Pp
|
||||
The function
|
||||
.Fn fread
|
||||
does not distinguish between end-of-file and error, and callers
|
||||
must use
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
to determine which occurred.
|
||||
The function
|
||||
.Fn fwrite
|
||||
returns a value less than
|
||||
.Fa nmemb
|
||||
only if a write error has occurred.
|
||||
.Sh SEE ALSO
|
||||
.Xr read 2 ,
|
||||
.Xr write 2
|
||||
.Sh STANDARDS
|
||||
The functions
|
||||
.Fn fread
|
||||
and
|
||||
.Fn fwrite
|
||||
conform to
|
||||
.St -ansiC .
|
||||
@@ -0,0 +1,95 @@
|
||||
/* $NetBSD: fread.c,v 1.20 2009/10/25 20:44:13 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fread.c 8.2 (Berkeley) 12/11/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fread.c,v 1.20 2009/10/25 20:44:13 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
size_t
|
||||
fread(buf, size, count, fp)
|
||||
void *buf;
|
||||
size_t size, count;
|
||||
FILE *fp;
|
||||
{
|
||||
size_t resid;
|
||||
char *p;
|
||||
int r;
|
||||
size_t total;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
/*
|
||||
* The ANSI standard requires a return value of 0 for a count
|
||||
* or a size of 0. Whilst ANSI imposes no such requirements on
|
||||
* fwrite, the SUSv2 does.
|
||||
*/
|
||||
if ((resid = count * size) == 0)
|
||||
return (0);
|
||||
|
||||
_DIAGASSERT(buf != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
if (fp->_r < 0)
|
||||
fp->_r = 0;
|
||||
total = resid;
|
||||
p = buf;
|
||||
while (resid > (size_t)(r = fp->_r)) {
|
||||
(void)memcpy(p, 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 */
|
||||
FUNLOCKFILE(fp);
|
||||
return ((total - resid) / size);
|
||||
}
|
||||
}
|
||||
(void)memcpy(p, fp->_p, resid);
|
||||
fp->_r -= resid;
|
||||
fp->_p += resid;
|
||||
FUNLOCKFILE(fp);
|
||||
return (count);
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
/* $NetBSD: freopen.c,v 1.16 2010/01/11 20:39:29 joerg Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: freopen.c,v 1.16 2010/01/11 20:39:29 joerg Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include <limits.h>
|
||||
#include "reentrant.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(file, mode, fp)
|
||||
const char *file, *mode;
|
||||
FILE *fp;
|
||||
{
|
||||
int f;
|
||||
int flags, isopen, oflags, sverrno, wantfd;
|
||||
|
||||
_DIAGASSERT(file != NULL);
|
||||
_DIAGASSERT(mode != NULL);
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
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 = __sfileno(fp)) == -1 && isopen) {
|
||||
(void) (*fp->_close)(fp->_cookie);
|
||||
isopen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get a new descriptor to refer to the new file. */
|
||||
f = open(file, oflags, DEFFILEMODE);
|
||||
if (f < 0 && isopen) {
|
||||
/* If out of fd's close the old one and try again. */
|
||||
if (errno == ENFILE || errno == EMFILE) {
|
||||
(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 && f != wantfd)
|
||||
(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);
|
||||
WCIO_FREE(fp);
|
||||
_UB(fp)._size = 0;
|
||||
FREELB(fp);
|
||||
|
||||
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 (oflags & O_NONBLOCK) {
|
||||
struct stat st;
|
||||
if (fstat(f, &st) == -1) {
|
||||
sverrno = errno;
|
||||
(void)close(f);
|
||||
errno = sverrno;
|
||||
return (NULL);
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
(void)close(f);
|
||||
errno = EFTYPE;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* File descriptors are a full int, but _file is only a short.
|
||||
* If we get a valid file descriptor that is greater or equal to
|
||||
* USHRT_MAX, then the fd will get sign-extended into an
|
||||
* invalid file descriptor. Handle this case by failing the
|
||||
* open. (We treat the short as unsigned, and special-case -1).
|
||||
*/
|
||||
if (f >= USHRT_MAX) {
|
||||
(void)close(f);
|
||||
errno = EMFILE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fp->_flags = flags;
|
||||
fp->_file = f;
|
||||
fp->_cookie = fp;
|
||||
fp->_read = __sread;
|
||||
fp->_write = __swrite;
|
||||
fp->_seek = __sseek;
|
||||
fp->_close = __sclose;
|
||||
|
||||
/*
|
||||
* When reopening 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);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/* $NetBSD: fscanf.c,v 1.12 2003/08/07 16:43:25 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fscanf.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fscanf.c,v 1.12 2003/08/07 16:43:25 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fscanf(FILE *fp, char const *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = __svfscanf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
.\" $NetBSD: fseek.3,v 1.25 2010/10/22 21:29:31 christos Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)fseek.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd October 22, 2010
|
||||
.Dt FSEEK 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetpos ,
|
||||
.Nm fseek ,
|
||||
.Nm fseeko ,
|
||||
.Nm fsetpos ,
|
||||
.Nm ftell ,
|
||||
.Nm ftello ,
|
||||
.Nm rewind
|
||||
.Nd reposition a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn fseek "FILE *stream" "long int offset" "int whence"
|
||||
.Ft int
|
||||
.Fn fseeko "FILE *stream" "off_t offset" "int whence"
|
||||
.Ft long int
|
||||
.Fn ftell "FILE *stream"
|
||||
.Ft off_t
|
||||
.Fn ftello "FILE *stream"
|
||||
.Ft void
|
||||
.Fn rewind "FILE *stream"
|
||||
.Ft int
|
||||
.Fn fgetpos "FILE * restrict stream" "fpos_t * restrict pos"
|
||||
.Ft int
|
||||
.Fn fsetpos "FILE * restrict stream" "const fpos_t * restrict pos"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fseek
|
||||
function sets the file position indicator for the stream pointed
|
||||
to by
|
||||
.Fa stream .
|
||||
The new position, measured in bytes, is obtained by adding
|
||||
.Fa offset
|
||||
bytes to the position specified by
|
||||
.Fa whence .
|
||||
If
|
||||
.Fa whence
|
||||
is set to
|
||||
.Dv SEEK_SET ,
|
||||
.Dv SEEK_CUR ,
|
||||
or
|
||||
.Dv SEEK_END ,
|
||||
the offset is relative to the
|
||||
start of the file, the current position indicator, or end-of-file,
|
||||
respectively.
|
||||
A successful call to the
|
||||
.Fn fseek
|
||||
function clears the end-of-file indicator for the stream and undoes
|
||||
any effects of the
|
||||
.Xr ungetc 3
|
||||
function on the same stream.
|
||||
.Pp
|
||||
The
|
||||
.Fn fseeko
|
||||
function is identical to the
|
||||
.Fn fseek
|
||||
function except that the
|
||||
.Fa offset
|
||||
argument is of type
|
||||
.Fa off_t .
|
||||
.Pp
|
||||
The
|
||||
.Fn ftell
|
||||
function
|
||||
obtains the current value of the file position indicator for the
|
||||
stream pointed to by
|
||||
.Fa stream .
|
||||
.Pp
|
||||
The
|
||||
.Fn ftello
|
||||
function is identical to the
|
||||
.Fn ftell
|
||||
function except that the return value is of type
|
||||
.Fa off_t .
|
||||
.Pp
|
||||
The
|
||||
.Fn rewind
|
||||
function sets the file position indicator for the stream pointed
|
||||
to by
|
||||
.Fa stream
|
||||
to the beginning of the file.
|
||||
It is equivalent to:
|
||||
.Pp
|
||||
.Dl (void)fseek(stream, 0L, SEEK_SET)
|
||||
.Pp
|
||||
except that the error indicator for the stream is also cleared
|
||||
(see
|
||||
.Xr clearerr 3 ) .
|
||||
.Pp
|
||||
In this implementation, the
|
||||
.Fn fgetpos
|
||||
and
|
||||
.Fn fsetpos
|
||||
functions
|
||||
are alternative interfaces equivalent to
|
||||
.Fn ftell ,
|
||||
.Fn ftello ,
|
||||
.Fn fseek ,
|
||||
and
|
||||
.Fn fseeko
|
||||
(with whence set to
|
||||
.Dv SEEK_SET ) ,
|
||||
setting and storing the current value of
|
||||
the file offset into or from the object referenced by
|
||||
.Fa pos .
|
||||
In others implementations, an
|
||||
.Dq Fa fpos_t
|
||||
object may be a complex object
|
||||
and these routines may be the only way to portably reposition a text stream.
|
||||
.Sh RETURN VALUES
|
||||
The
|
||||
.Fn rewind
|
||||
function
|
||||
returns no value.
|
||||
Upon successful completion,
|
||||
.Fn fgetpos ,
|
||||
.Fn fseek ,
|
||||
.Fn fseeko ,
|
||||
and
|
||||
.Fn fsetpos
|
||||
return 0.
|
||||
The functions
|
||||
.Fn ftell
|
||||
and
|
||||
.Fn ftello
|
||||
return the current offset.
|
||||
Otherwise,
|
||||
.Fn fseek ,
|
||||
.Fn fseeko ,
|
||||
.Fn ftell ,
|
||||
and
|
||||
.Fn ftello
|
||||
return \-1 while
|
||||
.Fn fgetpos
|
||||
and
|
||||
.Fn fsetpos
|
||||
return a nonzero value.
|
||||
On error all functions the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
Since the
|
||||
.Fn rewind
|
||||
function does not return an error code, applications need to clear
|
||||
.Va errno
|
||||
before calling it in order to detect errors.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EBADF
|
||||
The
|
||||
.Fa stream
|
||||
specified
|
||||
is not a seekable stream.
|
||||
.It Bq Er EINVAL
|
||||
The
|
||||
.Fa whence
|
||||
argument to
|
||||
.Fn fseek
|
||||
was not
|
||||
.Dv SEEK_SET ,
|
||||
.Dv SEEK_END ,
|
||||
or
|
||||
.Dv SEEK_CUR .
|
||||
.It Bq Er EOVERFLOW
|
||||
For
|
||||
.Fn ftell ,
|
||||
the current file offset cannot be represented correctly in an object of type
|
||||
.Fa long .
|
||||
.El
|
||||
.Pp
|
||||
The function
|
||||
.Fn fgetpos ,
|
||||
.Fn fseek ,
|
||||
.Fn fseeko ,
|
||||
.Fn fsetpos ,
|
||||
.Fn ftell ,
|
||||
.Fn ftello ,
|
||||
and
|
||||
.Fn rewind
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr fflush 3 ,
|
||||
.Xr fstat 2 ,
|
||||
.Xr lseek 2 ,
|
||||
and
|
||||
.Xr malloc 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr lseek 2
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fgetpos ,
|
||||
.Fn fsetpos ,
|
||||
.Fn fseek ,
|
||||
.Fn ftell ,
|
||||
and
|
||||
.Fn rewind
|
||||
functions
|
||||
conform to
|
||||
.St -ansiC .
|
||||
The
|
||||
.Fn fseeko
|
||||
and
|
||||
.Fn ftello
|
||||
functions conform to
|
||||
.St -xsh5 .
|
||||
.Sh BUGS
|
||||
The
|
||||
.Fn fgetpos
|
||||
and
|
||||
.Fn fsetpos
|
||||
functions don't store/set shift states of the stream in this implementation.
|
||||
@@ -0,0 +1,63 @@
|
||||
/* $NetBSD: fseek.c,v 1.23 2008/04/29 06:53:01 martin Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2005 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by David Laight.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fseek.c,v 1.23 2008/04/29 06:53:01 martin Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Seek the given file to the given offset.
|
||||
* Zero extend the offset if SEEK_SET to allow access to 4GB files
|
||||
*/
|
||||
int
|
||||
fseek(FILE *fp, long l_offset, int whence)
|
||||
{
|
||||
off_t offset;
|
||||
|
||||
if (whence == SEEK_SET)
|
||||
offset = (unsigned long)l_offset;
|
||||
else
|
||||
offset = l_offset;
|
||||
return fseeko(fp, offset, whence);
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
/* $NetBSD: fseeko.c,v 1.8 2009/01/31 00:08:05 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fseeko.c,v 1.8 2009/01/31 00:08:05 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(fseeko, _fseeko)
|
||||
#endif
|
||||
|
||||
#define POS_ERR (-(fpos_t)1)
|
||||
|
||||
/*
|
||||
* Seek the given file to the given offset.
|
||||
* `Whence' must be one of the three SEEK_* macros.
|
||||
*/
|
||||
int
|
||||
fseeko(FILE *fp, off_t offset, int whence)
|
||||
{
|
||||
fpos_t (*seekfn)(void *, fpos_t, int);
|
||||
fpos_t target, curoff;
|
||||
size_t n;
|
||||
struct stat st;
|
||||
int havepos;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
/* make sure stdio is set up */
|
||||
if (!__sdidinit)
|
||||
__sinit();
|
||||
|
||||
FLOCKFILE(fp);
|
||||
|
||||
/*
|
||||
* Have to be able to seek.
|
||||
*/
|
||||
if ((seekfn = fp->_seek) == NULL) {
|
||||
errno = ESPIPE; /* historic practice */
|
||||
FUNLOCKFILE(fp);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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).
|
||||
*/
|
||||
__sflush(fp); /* may adjust seek offset on append stream */
|
||||
if (fp->_flags & __SOFF)
|
||||
curoff = fp->_offset;
|
||||
else {
|
||||
curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
||||
if (curoff == POS_ERR) {
|
||||
FUNLOCKFILE(fp);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
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;
|
||||
FUNLOCKFILE(fp);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 ||
|
||||
__sfileno(fp) == -1 || fstat(__sfileno(fp), &st) ||
|
||||
!S_ISREG(st.st_mode)) {
|
||||
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(__sfileno(fp), &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 < (fpos_t)(curoff + n)) {
|
||||
int o = (int)(target - curoff);
|
||||
|
||||
fp->_p = fp->_bf._base + o;
|
||||
fp->_r = n - o;
|
||||
if (HASUB(fp))
|
||||
FREEUB(fp);
|
||||
fp->_flags &= ~__SEOF;
|
||||
FUNLOCKFILE(fp);
|
||||
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 = (int)(target - curoff);
|
||||
if (n) {
|
||||
if (__srefill(fp) || (size_t)fp->_r < n)
|
||||
goto dumb;
|
||||
fp->_p += n;
|
||||
fp->_r -= n;
|
||||
}
|
||||
FUNLOCKFILE(fp);
|
||||
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) {
|
||||
FUNLOCKFILE(fp);
|
||||
return (-1);
|
||||
}
|
||||
/* 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;
|
||||
FUNLOCKFILE(fp);
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/* $NetBSD: fsetpos.c,v 1.10 2003/08/07 16:43:25 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fsetpos.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fsetpos.c,v 1.10 2003/08/07 16:43:25 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
* fsetpos: like fseek.
|
||||
*/
|
||||
int
|
||||
fsetpos(iop, pos)
|
||||
FILE *iop;
|
||||
const fpos_t *pos;
|
||||
{
|
||||
_DIAGASSERT(iop != NULL);
|
||||
_DIAGASSERT(pos != NULL);
|
||||
|
||||
return (fseeko(iop, (off_t)*pos, SEEK_SET));
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/* $NetBSD: ftell.c,v 1.17 2010/10/24 17:44:32 tron Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)ftell.c 8.2 (Berkeley) 5/4/95";
|
||||
#else
|
||||
__RCSID("$NetBSD: ftell.c,v 1.17 2010/10/24 17:44:32 tron Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* ftell: return current offset.
|
||||
*/
|
||||
long
|
||||
ftell(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
fpos_t pos;
|
||||
|
||||
|
||||
FLOCKFILE(fp);
|
||||
|
||||
if (fp->_seek == NULL) {
|
||||
FUNLOCKFILE(fp);
|
||||
errno = ESPIPE; /* historic practice */
|
||||
return -1L;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find offset of underlying I/O object, then
|
||||
* adjust for buffered bytes.
|
||||
*/
|
||||
__sflush(fp); /* may adjust seek offset on append stream */
|
||||
if (fp->_flags & __SOFF)
|
||||
pos = fp->_offset;
|
||||
else {
|
||||
pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
||||
if (pos == -1L) {
|
||||
FUNLOCKFILE(fp);
|
||||
return (long)(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;
|
||||
}
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
if (__fpos_overflow(pos)) {
|
||||
errno = EOVERFLOW;
|
||||
return -1L;
|
||||
}
|
||||
|
||||
return (long)(pos);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/* $NetBSD: ftello.c,v 1.4 2003/08/07 16:43:26 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: ftello.c,v 1.4 2003/08/07 16:43:26 agc Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(ftello, _ftello)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* ftell: return current offset.
|
||||
*/
|
||||
off_t
|
||||
ftello(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
fpos_t pos;
|
||||
|
||||
|
||||
FLOCKFILE(fp);
|
||||
|
||||
if (fp->_seek == NULL) {
|
||||
FUNLOCKFILE(fp);
|
||||
errno = ESPIPE; /* historic practice */
|
||||
return ((off_t)-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find offset of underlying I/O object, then
|
||||
* adjust for buffered bytes.
|
||||
*/
|
||||
__sflush(fp); /* may adjust seek offset on append stream */
|
||||
if (fp->_flags & __SOFF)
|
||||
pos = fp->_offset;
|
||||
else {
|
||||
pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR);
|
||||
if (pos == (fpos_t)-1) {
|
||||
FUNLOCKFILE(fp);
|
||||
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;
|
||||
}
|
||||
FUNLOCKFILE(fp);
|
||||
return (pos);
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
.\" $NetBSD: funopen.3,v 1.15 2010/03/22 19:30:54 joerg Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 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. 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.
|
||||
.\"
|
||||
.\" @(#)funopen.3 8.1 (Berkeley) 6/9/93
|
||||
.\"
|
||||
.Dd June 9, 1993
|
||||
.Dt FUNOPEN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm funopen ,
|
||||
.Nm fropen ,
|
||||
.Nm fwopen
|
||||
.Nd open a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft FILE *
|
||||
.Fn funopen "void *cookie" "int (*readfn)(void *, char *, int)" "int (*writefn)(void *, const char *, int)" "fpos_t (*seekfn)(void *, fpos_t, int)" "int (*closefn)(void *)"
|
||||
.Ft FILE *
|
||||
.Fn fropen "void *cookie" "int (*readfn)(void *, char *, int)"
|
||||
.Ft FILE *
|
||||
.Fn fwopen "void *cookie" "int (*writefn)(void *, const char *, int)"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn funopen
|
||||
function
|
||||
associates a stream with up to four
|
||||
.Dq Tn I/O No functions .
|
||||
Either
|
||||
.Fa readfn
|
||||
or
|
||||
.Fa writefn
|
||||
must be specified;
|
||||
the others can be given as an appropriately-typed
|
||||
.Dv NULL
|
||||
pointer.
|
||||
These
|
||||
.Tn I/O
|
||||
functions will be used to read, write, seek and
|
||||
close the new stream.
|
||||
.Pp
|
||||
In general, omitting a function means that any attempt to perform the
|
||||
associated operation on the resulting stream will fail.
|
||||
If the close function is omitted, closing the stream will flush
|
||||
any buffered output and then succeed.
|
||||
.Pp
|
||||
The calling conventions of
|
||||
.Fa readfn ,
|
||||
.Fa writefn ,
|
||||
.Fa seekfn
|
||||
and
|
||||
.Fa closefn
|
||||
must match those, respectively, of
|
||||
.Xr read 2 ,
|
||||
.Xr write 2 ,
|
||||
.Xr lseek 2 ,
|
||||
and
|
||||
.Xr close 2 ;
|
||||
except that they are passed the
|
||||
.Fa cookie
|
||||
argument specified to
|
||||
.Fn funopen
|
||||
in place of the traditional file descriptor argument,
|
||||
and
|
||||
.Fa seekfn
|
||||
uses
|
||||
.Li fpos_t
|
||||
instead of
|
||||
.Li off_t .
|
||||
.Pp
|
||||
Read and write
|
||||
.Tn I/O
|
||||
functions are allowed to change the underlying buffer
|
||||
on fully buffered or line buffered streams by calling
|
||||
.Xr setvbuf 3 .
|
||||
They are also not required to completely fill or empty the buffer.
|
||||
They are not, however, allowed to change streams from unbuffered to buffered
|
||||
or to change the state of the line buffering flag.
|
||||
They must also be prepared to have read or write calls occur on buffers other
|
||||
than the one most recently specified.
|
||||
.Pp
|
||||
All user
|
||||
.Tn I/O
|
||||
functions can report an error by returning \-1.
|
||||
Additionally, all of the functions should set the external variable
|
||||
.Va errno
|
||||
appropriately if an error occurs.
|
||||
.Pp
|
||||
An error on
|
||||
.Fn closefn
|
||||
does not keep the stream open.
|
||||
.Pp
|
||||
As a convenience, the include file
|
||||
.In stdio.h
|
||||
defines the macros
|
||||
.Fn fropen
|
||||
and
|
||||
.Fn fwopen
|
||||
as calls to
|
||||
.Fn funopen
|
||||
with only a read or write function specified.
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion,
|
||||
.Fn funopen
|
||||
returns a
|
||||
.Dv FILE
|
||||
pointer.
|
||||
Otherwise,
|
||||
.Dv NULL
|
||||
is returned and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er EINVAL
|
||||
The
|
||||
.Fn funopen
|
||||
function
|
||||
was called without either a read or write function.
|
||||
The
|
||||
.Fn funopen
|
||||
function
|
||||
may also fail and set
|
||||
.Va errno
|
||||
for any of the errors
|
||||
specified for the routine
|
||||
.Xr malloc 3 .
|
||||
.El
|
||||
.Sh SEE ALSO
|
||||
.Xr fcntl 2 ,
|
||||
.Xr open 2 ,
|
||||
.Xr fclose 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr fseek 3 ,
|
||||
.Xr setbuf 3
|
||||
.Sh HISTORY
|
||||
The
|
||||
.Fn funopen
|
||||
functions first appeared in
|
||||
.Bx 4.4 .
|
||||
.Sh BUGS
|
||||
The
|
||||
.Fn funopen
|
||||
function
|
||||
may not be portable to systems other than
|
||||
.Bx .
|
||||
@@ -0,0 +1,82 @@
|
||||
/* $NetBSD: funopen.c,v 1.10 2005/11/29 03:12:00 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)funopen.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: funopen.c,v 1.10 2005/11/29 03:12:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
FILE *
|
||||
funopen(cookie, readfn, writefn, seekfn, closefn)
|
||||
const void *cookie;
|
||||
int (*readfn) __P((void *, char *, int));
|
||||
int (*writefn) __P((void *, const char *, int));
|
||||
fpos_t (*seekfn) __P((void *, fpos_t, int));
|
||||
int (*closefn) __P((void *));
|
||||
{
|
||||
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 = __UNCONST(cookie);
|
||||
fp->_read = readfn;
|
||||
fp->_write = writefn;
|
||||
fp->_seek = seekfn;
|
||||
fp->_close = closefn;
|
||||
return (fp);
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
/* $NetBSD: fvwrite.c,v 1.21 2009/10/25 20:44:13 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fvwrite.c,v 1.21 2009/10/25 20:44:13 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#include "fvwrite.h"
|
||||
|
||||
/*
|
||||
* 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(fp, uio)
|
||||
FILE *fp;
|
||||
struct __suio *uio;
|
||||
{
|
||||
int len;
|
||||
char *p;
|
||||
struct __siov *iov;
|
||||
int w, s;
|
||||
char *nl;
|
||||
int nlknown, nldist;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(uio != NULL);
|
||||
|
||||
if ((int)uio->uio_resid < 0) {
|
||||
errno = EINVAL;
|
||||
return (EOF);
|
||||
}
|
||||
if ((len = uio->uio_resid) == 0)
|
||||
return (0);
|
||||
/* make sure we can write */
|
||||
if (cantwrite(fp)) {
|
||||
errno = EBADF;
|
||||
return (EOF);
|
||||
}
|
||||
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define COPY(n) (void)memcpy(fp->_p, 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(;);
|
||||
w = (*fp->_write)(fp->_cookie, p,
|
||||
(int)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(;);
|
||||
if ((fp->_flags & (__SALC | __SSTR)) ==
|
||||
(__SALC | __SSTR) && fp->_w < len) {
|
||||
int blen = fp->_p - fp->_bf._base;
|
||||
unsigned char *_base;
|
||||
int _size;
|
||||
|
||||
/* Allocate space exponentially. */
|
||||
_size = fp->_bf._size;
|
||||
do {
|
||||
_size = (_size << 1) + 1;
|
||||
} while (_size < blen + len);
|
||||
_base = realloc(fp->_bf._base,
|
||||
(size_t)(_size + 1));
|
||||
if (_base == NULL)
|
||||
goto err;
|
||||
fp->_w += _size - fp->_bf._size;
|
||||
fp->_bf._base = _base;
|
||||
fp->_bf._size = _size;
|
||||
fp->_p = _base + blen;
|
||||
}
|
||||
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 */
|
||||
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(p, '\n', (size_t)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)) {
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* $NetBSD: fvwrite.h,v 1.7 2003/08/07 16:43:26 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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;
|
||||
};
|
||||
|
||||
extern int __sfvwrite(FILE *, struct __suio *);
|
||||
@@ -0,0 +1,67 @@
|
||||
/* $NetBSD: fwalk.c,v 1.11 2003/08/07 16:43:26 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fwalk.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fwalk.c,v 1.11 2003/08/07 16:43:26 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#include "glue.h"
|
||||
|
||||
int
|
||||
_fwalk(function)
|
||||
int (*function) __P((FILE *));
|
||||
{
|
||||
FILE *fp;
|
||||
int n, ret;
|
||||
struct glue *g;
|
||||
|
||||
_DIAGASSERT(function != NULL);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
.\" $NetBSD: fwide.3,v 1.6 2003/09/08 17:54:32 wiz Exp $
|
||||
.\"
|
||||
.\" Copyright (c)2001 Citrus Project,
|
||||
.\" All rights reserved.
|
||||
.\"
|
||||
.\" 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.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
.\"
|
||||
.\" $Citrus: xpg4dl/FreeBSD/lib/libc/stdio/fwide.3,v 1.2 2001/12/07 04:47:08 yamt Exp $
|
||||
.\"
|
||||
.Dd October 24, 2001
|
||||
.Dt FWIDE 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fwide
|
||||
.Nd get/set orientation of a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.In wchar.h
|
||||
.Ft int
|
||||
.Fn fwide "FILE *stream" "int mode"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fwide
|
||||
function
|
||||
determines the orientation of the stream pointed at by
|
||||
.Fa stream .
|
||||
.Pp
|
||||
If the orientation of
|
||||
.Fa stream
|
||||
has already been determined,
|
||||
.Fn fwide
|
||||
leaves it unchanged.
|
||||
Otherwise,
|
||||
.Fn fwide
|
||||
sets the orientation of
|
||||
.Fa stream
|
||||
according to
|
||||
.Fa mode .
|
||||
.Pp
|
||||
If
|
||||
.Fa mode
|
||||
is less than zero,
|
||||
.Fa stream
|
||||
is set to byte-oriented.
|
||||
If it is greater than zero,
|
||||
.Fa stream
|
||||
is set to wide-oriented.
|
||||
Otherwise,
|
||||
.Fa mode
|
||||
is zero, and
|
||||
.Fa stream
|
||||
is unchanged.
|
||||
.Sh RETURN VALUES
|
||||
.Fn fwide
|
||||
returns a value according to orientation after the call of
|
||||
.Fn fwide ;
|
||||
a value less than zero if byte-oriented, a value greater than zero
|
||||
if wide-oriented, and zero if the stream has no orientation.
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fgetc 3 ,
|
||||
.Xr fgetwc 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr fputc 3 ,
|
||||
.Xr fputwc 3 ,
|
||||
.Xr freopen 3 ,
|
||||
.Xr stdio 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fwide
|
||||
function
|
||||
conforms to
|
||||
.St -isoC-99 .
|
||||
@@ -0,0 +1,72 @@
|
||||
/* $NetBSD: fwide.c,v 1.3 2005/06/12 05:21:27 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: fwide.c,v 1.3 2005/06/12 05:21:27 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
fwide(FILE *fp, int mode)
|
||||
{
|
||||
struct wchar_io_data *wcio;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
/*
|
||||
* this implementation use only -1, 0, 1
|
||||
* for mode value.
|
||||
* (we don't need to do this, but
|
||||
* this can make things simpler.)
|
||||
*/
|
||||
if (mode > 0)
|
||||
mode = 1;
|
||||
else if (mode < 0)
|
||||
mode = -1;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
wcio = WCIO_GET(fp);
|
||||
if (!wcio)
|
||||
return 0; /* XXX */
|
||||
|
||||
if (wcio->wcio_mode == 0 && mode != 0)
|
||||
wcio->wcio_mode = mode;
|
||||
else
|
||||
mode = wcio->wcio_mode;
|
||||
FUNLOCKFILE(fp);
|
||||
|
||||
return mode;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/* $NetBSD: fwprintf.c,v 1.1 2005/05/14 23:51:02 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 Tim J. Robbins
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
__FBSDID("$FreeBSD: src/lib/libc/stdio/fwprintf.c,v 1.1 2002/09/21 13:00:30 tjr Exp $");
|
||||
#else
|
||||
__RCSID("$NetBSD: fwprintf.c,v 1.1 2005/05/14 23:51:02 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int
|
||||
fwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vfwprintf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return (ret);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/* $NetBSD: fwrite.c,v 1.16 2005/11/29 03:12:00 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)fwrite.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: fwrite.c,v 1.16 2005/11/29 03:12:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.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(buf, size, count, fp)
|
||||
const void *buf;
|
||||
size_t size, count;
|
||||
FILE *fp;
|
||||
{
|
||||
size_t n;
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
/*
|
||||
* SUSv2 requires a return value of 0 for a count or a size of 0.
|
||||
*/
|
||||
if ((n = count * size) == 0)
|
||||
return (0);
|
||||
_DIAGASSERT(buf != NULL);
|
||||
|
||||
iov.iov_base = __UNCONST(buf);
|
||||
uio.uio_resid = iov.iov_len = n;
|
||||
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.
|
||||
*/
|
||||
FLOCKFILE(fp);
|
||||
if (__sfvwrite(fp, &uio) != 0)
|
||||
count = ((n - uio.uio_resid) / size);
|
||||
FUNLOCKFILE(fp);
|
||||
return (count);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/* $NetBSD: fwscanf.c,v 1.1 2005/05/14 23:51:02 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002 Tim J. Robbins
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
__FBSDID("$FreeBSD: src/lib/libc/stdio/fwscanf.c,v 1.1 2002/09/23 12:40:06 tjr Exp $");
|
||||
#else
|
||||
__RCSID("$NetBSD: fwscanf.c,v 1.1 2005/05/14 23:51:02 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
int
|
||||
fwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int r;
|
||||
|
||||
va_start(ap, fmt);
|
||||
r = vfwscanf(fp, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return (r);
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
.\" $NetBSD: getc.3,v 1.12 2003/08/07 16:43:26 agc Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)getc.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd April 25, 2001
|
||||
.Dt GETC 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetc ,
|
||||
.Nm getc ,
|
||||
.Nm getchar ,
|
||||
.Nm getc_unlocked ,
|
||||
.Nm getchar_unlocked ,
|
||||
.Nm getw
|
||||
.Nd get next character or word from input stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn fgetc "FILE *stream"
|
||||
.Ft int
|
||||
.Fn getc "FILE *stream"
|
||||
.Ft int
|
||||
.Fn getchar
|
||||
.Ft int
|
||||
.Fn getc_unlocked "FILE *stream"
|
||||
.Ft int
|
||||
.Fn getchar_unlocked
|
||||
.Ft int
|
||||
.Fn getw "FILE *stream"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fgetc
|
||||
function
|
||||
obtains the next input character (if present) from the stream pointed at by
|
||||
.Fa stream ,
|
||||
or the next character pushed back on the stream via
|
||||
.Xr ungetc 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn getc
|
||||
function
|
||||
acts essentially identically to
|
||||
.Fn fgetc ,
|
||||
but is a macro that expands in-line.
|
||||
.Pp
|
||||
The
|
||||
.Fn getchar
|
||||
function
|
||||
is equivalent to:
|
||||
getc with the argument stdin.
|
||||
.Pp
|
||||
The
|
||||
.Fn getc_unlocked
|
||||
and
|
||||
.Fn getchar_unlocked
|
||||
functions provide functionality identical to that of
|
||||
.Fn getc
|
||||
and
|
||||
.Fn getchar ,
|
||||
respectively, but do not perform implicit locking of the streams they
|
||||
operate on.
|
||||
In multi-threaded programs they may be used
|
||||
.Em only
|
||||
within a scope in which the stream
|
||||
has been successfully locked by the calling thread using either
|
||||
.Xr flockfile 3
|
||||
or
|
||||
.Xr ftrylockfile 3 ,
|
||||
and may later be released using
|
||||
.Xr funlockfile 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn getw
|
||||
function
|
||||
obtains the next
|
||||
.Em int
|
||||
(if present)
|
||||
from the stream pointed at by
|
||||
.Fa stream .
|
||||
.Sh RETURN VALUES
|
||||
If successful, these routines return the next requested object
|
||||
from the
|
||||
.Fa stream .
|
||||
If the stream is at end-of-file or a read error occurs,
|
||||
the routines return
|
||||
.Dv EOF .
|
||||
The routines
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
must be used to distinguish between end-of-file and error.
|
||||
If an error occurs, the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
The end-of-file condition is remembered, even on a terminal, and all
|
||||
subsequent attempts to read will return
|
||||
.Dv EOF
|
||||
until the condition is cleared with
|
||||
.Xr clearerr 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr flockfile 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr fread 3 ,
|
||||
.Xr ftrylockfile 3 ,
|
||||
.Xr funlockfile 3 ,
|
||||
.Xr putc 3 ,
|
||||
.Xr ungetc 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fgetc ,
|
||||
.Fn getc
|
||||
and
|
||||
.Fn getchar
|
||||
functions
|
||||
conform to
|
||||
.St -ansiC .
|
||||
The
|
||||
.Fn getc_unlocked
|
||||
and
|
||||
.Fn getchar_unlocked
|
||||
functions conform to
|
||||
.St -p1003.1-96 .
|
||||
.Sh BUGS
|
||||
Since
|
||||
.Dv EOF
|
||||
is a valid integer value,
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
must be used to check for failure after calling
|
||||
.Fn getw .
|
||||
The size and byte order of an
|
||||
.Em int
|
||||
varies from one machine to another, and
|
||||
.Fn getw
|
||||
is not recommended for portable applications.
|
||||
@@ -0,0 +1,78 @@
|
||||
/* $NetBSD: getc.c,v 1.11 2003/08/07 16:43:26 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)getc.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: getc.c,v 1.11 2003/08/07 16:43:26 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro getc.
|
||||
*/
|
||||
#undef getc
|
||||
#undef getc_unlocked
|
||||
|
||||
int
|
||||
getc(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sgetc(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
getc_unlocked(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
return (__sgetc(fp));
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/* $NetBSD: getchar.c,v 1.9 2003/08/07 16:43:27 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)getchar.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: getchar.c,v 1.9 2003/08/07 16:43:27 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro getchar.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#undef getchar
|
||||
#undef getchar_unlocked
|
||||
|
||||
int
|
||||
getchar()
|
||||
{
|
||||
FILE *fp = stdin;
|
||||
int r;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sgetc(fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
getchar_unlocked()
|
||||
{
|
||||
return (__sgetc(stdin));
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
.\" $NetBSD: getdelim.3,v 1.8 2010/06/30 13:38:10 jruoho Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 2009 The NetBSD Foundation, Inc.
|
||||
.\" All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to The NetBSD Foundation
|
||||
.\" by Roy Marples.
|
||||
.\"
|
||||
.\" 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.
|
||||
.\"
|
||||
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||
.\"
|
||||
.Dd June 30, 2010
|
||||
.Dt GETDELIM 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm getdelim ,
|
||||
.Nm getline
|
||||
.Nd read a delimited record from a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft ssize_t
|
||||
.Fn getdelim "char ** restrict lineptr" "size_t * restrict n" "int delimiter" "FILE * restrict stream"
|
||||
.Ft ssize_t
|
||||
.Fn getline "char ** restrict lineptr" "size_t * restrict n" "FILE * restrict stream"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn getdelim
|
||||
function reads from the
|
||||
.Fa stream
|
||||
until it encounters a character matching
|
||||
.Fa delimiter ,
|
||||
storing the input in
|
||||
.Fa *lineptr .
|
||||
The buffer is
|
||||
.Dv NUL Ns No -terminated
|
||||
and includes the delimiter.
|
||||
The
|
||||
.Fa delimiter
|
||||
character must be representable as an unsigned char.
|
||||
.Pp
|
||||
If
|
||||
.Fa *n
|
||||
is non-zero, then
|
||||
.Fa *lineptr
|
||||
must be pre-allocated to at least
|
||||
.Fa *n
|
||||
bytes.
|
||||
The buffer should be allocated dynamically;
|
||||
it must be possible to
|
||||
.Xr free 3
|
||||
.Fa *lineptr .
|
||||
.Fn getdelim
|
||||
ensures that
|
||||
.Fa *lineptr
|
||||
is large enough to hold the input, updating
|
||||
.Fa *n
|
||||
to reflect the new size.
|
||||
.Pp
|
||||
The
|
||||
.Fn getline
|
||||
function is equivalent to
|
||||
.Fn getdelim
|
||||
with
|
||||
.Fa delimiter
|
||||
set to the newline character.
|
||||
.Sh RETURN VALUES
|
||||
The
|
||||
.Fn getdelim
|
||||
and
|
||||
.Fn getline
|
||||
functions return the number of characters read, including the delimiter.
|
||||
If no characters were read and the stream is at end-of-file, the functions
|
||||
return \-1.
|
||||
If an error occurs, the functions return \-1 and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
.Pp
|
||||
The functions do not distinguish between end-of-file and error,
|
||||
and callers must use
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
to determine which occurred.
|
||||
.Sh EXAMPLES
|
||||
The following code fragment reads lines from a file and writes them to
|
||||
standard output.
|
||||
.Bd -literal -offset indent
|
||||
char *line = NULL;
|
||||
size_t linesize = 0;
|
||||
ssize_t linelen;
|
||||
while ((linelen = getline(\*[Am]line, \*[Am]linesize, fp)) != -1)
|
||||
fwrite(line, linelen, 1, stdout);
|
||||
|
||||
if (ferror(fp))
|
||||
perror("getline");
|
||||
.Ed
|
||||
.Sh ERRORS
|
||||
.Bl -tag -width [EOVERFLOW]
|
||||
.It Bq Er EINVAL
|
||||
.Fa *lineptr
|
||||
or
|
||||
.Fa *n
|
||||
is a
|
||||
.Dv NULL
|
||||
pointer.
|
||||
.It Bq Er EOVERFLOW
|
||||
More than SSIZE_MAX characters were read without encountering the delimiter.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn getdelim
|
||||
and
|
||||
.Fn getline
|
||||
functions may also fail and set
|
||||
.Va errno
|
||||
for any of the errors specified in the routines
|
||||
.Xr fflush 3 ,
|
||||
.Xr malloc 3 ,
|
||||
.Xr read 2 ,
|
||||
.Xr stat 2 ,
|
||||
or
|
||||
.Xr realloc 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fgets 3 ,
|
||||
.Xr fopen 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn getdelim
|
||||
and
|
||||
.Fn getline
|
||||
functions conform to
|
||||
.St -p1003.1-2008 .
|
||||
@@ -0,0 +1,154 @@
|
||||
/* $NetBSD: getdelim.c,v 1.12 2009/12/07 21:31:43 roy Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Roy Marples.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: getdelim.c,v 1.12 2009/12/07 21:31:43 roy Exp $");
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <sys/param.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(getdelim, _getdelim)
|
||||
#endif
|
||||
|
||||
/* Minimum buffer size we create.
|
||||
* This should allow config files to fit into our power of 2 buffer growth
|
||||
* without the need for a realloc. */
|
||||
#define MINBUF 128
|
||||
|
||||
ssize_t
|
||||
__getdelim(char **__restrict buf, size_t *__restrict buflen,
|
||||
int sep, FILE *__restrict fp)
|
||||
{
|
||||
unsigned char *p;
|
||||
size_t len, newlen, off;
|
||||
char *newb;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
if (buf == NULL || buflen == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If buf is NULL, we have to assume a size of zero */
|
||||
if (*buf == NULL)
|
||||
*buflen = 0;
|
||||
|
||||
_SET_ORIENTATION(fp, -1);
|
||||
off = 0;
|
||||
do {
|
||||
/* If the input buffer is empty, refill it */
|
||||
if (fp->_r <= 0 && __srefill(fp)) {
|
||||
if (__sferror(fp))
|
||||
goto error;
|
||||
/* No error, so EOF. */
|
||||
break;
|
||||
}
|
||||
|
||||
/* Scan through looking for the separator */
|
||||
p = memchr(fp->_p, sep, (size_t)fp->_r);
|
||||
if (p == NULL)
|
||||
len = fp->_r;
|
||||
else
|
||||
len = (p - fp->_p) + 1;
|
||||
|
||||
newlen = off + len;
|
||||
/* Ensure we can handle it */
|
||||
if (newlen < off || newlen > SSIZE_MAX) {
|
||||
errno = EOVERFLOW;
|
||||
goto error;
|
||||
}
|
||||
newlen++; /* reserve space for the NULL terminator */
|
||||
if (newlen > *buflen) {
|
||||
if (newlen < MINBUF)
|
||||
newlen = MINBUF;
|
||||
if (!powerof2(newlen)) {
|
||||
/* Grow the buffer to the next power of 2 */
|
||||
newlen--;
|
||||
newlen |= newlen >> 1;
|
||||
newlen |= newlen >> 2;
|
||||
newlen |= newlen >> 4;
|
||||
newlen |= newlen >> 8;
|
||||
newlen |= newlen >> 16;
|
||||
#if SIZE_T_MAX > 0xffffffffU
|
||||
newlen |= newlen >> 32;
|
||||
#endif
|
||||
newlen++;
|
||||
}
|
||||
|
||||
newb = realloc(*buf, newlen);
|
||||
if (newb == NULL)
|
||||
goto error;
|
||||
*buf = newb;
|
||||
*buflen = newlen;
|
||||
}
|
||||
|
||||
(void)memcpy((*buf + off), fp->_p, len);
|
||||
/* Safe, len is never greater than what fp->_r can fit. */
|
||||
fp->_r -= (int)len;
|
||||
fp->_p += (int)len;
|
||||
off += len;
|
||||
} while (p == NULL);
|
||||
|
||||
/* POSIX demands we return -1 on EOF. */
|
||||
if (off == 0)
|
||||
return -1;
|
||||
|
||||
if (*buf != NULL)
|
||||
*(*buf + off) = '\0';
|
||||
return off;
|
||||
|
||||
error:
|
||||
fp->_flags |= __SERR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t
|
||||
getdelim(char **__restrict buf, size_t *__restrict buflen,
|
||||
int sep, FILE *__restrict fp)
|
||||
{
|
||||
ssize_t n;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
n = __getdelim(buf, buflen, sep, fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return n;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/* $NetBSD: getline.c,v 1.3 2009/12/02 08:46:33 roy Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009 The NetBSD Foundation, Inc.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Roy Marples.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: getline.c,v 1.3 2009/12/02 08:46:33 roy Exp $");
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(getline, _getline)
|
||||
#endif
|
||||
|
||||
ssize_t
|
||||
getline(char **__restrict buf, size_t *__restrict buflen, FILE *__restrict fp)
|
||||
{
|
||||
return getdelim(buf, buflen, '\n', fp);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/* $NetBSD: gets.c,v 1.16 2007/06/03 17:39:26 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)gets.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: gets.c,v 1.16 2007/06/03 17:39:26 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#ifdef _FORTIFY_SOURCE
|
||||
#undef gets
|
||||
#endif
|
||||
|
||||
__warn_references(gets, "warning: this program uses gets(), which is unsafe.")
|
||||
|
||||
char *
|
||||
gets(buf)
|
||||
char *buf;
|
||||
{
|
||||
int c;
|
||||
char *s;
|
||||
|
||||
_DIAGASSERT(buf != NULL);
|
||||
|
||||
FLOCKFILE(stdin);
|
||||
for (s = buf; (c = getchar_unlocked()) != '\n'; ) {
|
||||
if (c == EOF) {
|
||||
if (s == buf) {
|
||||
FUNLOCKFILE(stdin);
|
||||
return (NULL);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
*s++ = c;
|
||||
}
|
||||
}
|
||||
*s = 0;
|
||||
FUNLOCKFILE(stdin);
|
||||
return (buf);
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
/* $NetBSD: gettemp.c,v 1.14 2008/10/20 10:28:38 apb Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#if HAVE_NBTOOL_CONFIG_H
|
||||
#include "nbtool_config.h"
|
||||
#endif
|
||||
|
||||
#if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: gettemp.c,v 1.14 2008/10/20 10:28:38 apb Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if HAVE_NBTOOL_CONFIG_H
|
||||
#define GETTEMP __nbcompat_gettemp
|
||||
#else
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#define GETTEMP __gettemp
|
||||
#endif
|
||||
|
||||
int
|
||||
GETTEMP(path, doopen, domkdir)
|
||||
char *path;
|
||||
int *doopen;
|
||||
int domkdir;
|
||||
{
|
||||
char *start, *trv;
|
||||
struct stat sbuf;
|
||||
u_int pid;
|
||||
|
||||
/* To guarantee multiple calls generate unique names even if
|
||||
the file is not created. 676 different possibilities with 7
|
||||
or more X's, 26 with 6 or less. */
|
||||
static char xtra[2] = "aa";
|
||||
int xcnt = 0;
|
||||
|
||||
_DIAGASSERT(path != NULL);
|
||||
/* doopen may be NULL */
|
||||
|
||||
pid = getpid();
|
||||
|
||||
/* Move to end of path and count trailing X's. */
|
||||
for (trv = path; *trv; ++trv)
|
||||
if (*trv == 'X')
|
||||
xcnt++;
|
||||
else
|
||||
xcnt = 0;
|
||||
|
||||
/* Use at least one from xtra. Use 2 if more than 6 X's. */
|
||||
if (*(trv - 1) == 'X')
|
||||
*--trv = xtra[0];
|
||||
if (xcnt > 6 && *(trv - 1) == 'X')
|
||||
*--trv = xtra[1];
|
||||
|
||||
/* Set remaining X's to pid digits with 0's to the left. */
|
||||
while (*--trv == 'X') {
|
||||
*trv = (pid % 10) + '0';
|
||||
pid /= 10;
|
||||
}
|
||||
|
||||
/* update xtra for next call. */
|
||||
if (xtra[0] != 'z')
|
||||
xtra[0]++;
|
||||
else {
|
||||
xtra[0] = 'a';
|
||||
if (xtra[1] != 'z')
|
||||
xtra[1]++;
|
||||
else
|
||||
xtra[1] = 'a';
|
||||
}
|
||||
|
||||
/*
|
||||
* check the target directory; if you have six X's and it
|
||||
* doesn't exist this runs for a *very* long time.
|
||||
*/
|
||||
for (start = trv + 1;; --trv) {
|
||||
if (trv <= path)
|
||||
break;
|
||||
if (*trv == '/') {
|
||||
*trv = '\0';
|
||||
if (stat(path, &sbuf))
|
||||
return (0);
|
||||
if (!S_ISDIR(sbuf.st_mode)) {
|
||||
errno = ENOTDIR;
|
||||
return (0);
|
||||
}
|
||||
*trv = '/';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (doopen) {
|
||||
if ((*doopen =
|
||||
open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)
|
||||
return (1);
|
||||
if (errno != EEXIST)
|
||||
return (0);
|
||||
} else if (domkdir) {
|
||||
if (mkdir(path, 0700) >= 0)
|
||||
return (1);
|
||||
if (errno != EEXIST)
|
||||
return (0);
|
||||
} else if (lstat(path, &sbuf))
|
||||
return (errno == ENOENT ? 1 : 0);
|
||||
|
||||
/* tricky little algorithm for backward compatibility */
|
||||
for (trv = start;;) {
|
||||
if (!*trv)
|
||||
return (0);
|
||||
if (*trv == 'z')
|
||||
*trv++ = 'a';
|
||||
else {
|
||||
if (isdigit((unsigned char)*trv))
|
||||
*trv = 'a';
|
||||
else
|
||||
++*trv;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*NOTREACHED*/
|
||||
}
|
||||
|
||||
#endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP */
|
||||
@@ -0,0 +1,57 @@
|
||||
/* $NetBSD: getw.c,v 1.9 2003/08/07 16:43:27 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)getw.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: getw.c,v 1.9 2003/08/07 16:43:27 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
getw(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
int x;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
return (fread((void *)&x, sizeof(x), 1, fp) == 1 ? x : EOF);
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
.\" $NetBSD: getwc.3,v 1.9 2010/12/16 17:42:27 wiz Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)getc.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd May 6, 2010
|
||||
.Dt GETWC 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetwc ,
|
||||
.Nm getwc ,
|
||||
.Nm getwchar ,
|
||||
.Nd get next wide character from input stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.In wchar.h
|
||||
.Ft wint_t
|
||||
.Fn fgetwc "FILE *stream"
|
||||
.Ft wint_t
|
||||
.Fn getwc "FILE *stream"
|
||||
.Ft wint_t
|
||||
.Fn getwchar
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fgetwc
|
||||
function
|
||||
obtains the next input wide character (if present) from the stream pointed at by
|
||||
.Fa stream ,
|
||||
or the next character pushed back on the stream via
|
||||
.Xr ungetwc 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn getwc
|
||||
function
|
||||
acts essentially identically to
|
||||
.Fn fgetwc ,
|
||||
but is a macro that expands in-line.
|
||||
.Pp
|
||||
The
|
||||
.Fn getwchar
|
||||
function
|
||||
is equivalent to
|
||||
.Fn getwc
|
||||
with the argument stdin.
|
||||
.Sh RETURN VALUES
|
||||
If successful, these routines return the next wide character
|
||||
from the
|
||||
.Fa stream .
|
||||
If the stream is at end-of-file or a read error occurs,
|
||||
the routines return
|
||||
.Dv WEOF .
|
||||
The routines
|
||||
.Xr feof 3
|
||||
and
|
||||
.Xr ferror 3
|
||||
must be used to distinguish between end-of-file and error.
|
||||
If an error occurs, the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
The end-of-file condition is remembered, even on a terminal, and all
|
||||
subsequent attempts to read will return
|
||||
.Dv WEOF
|
||||
until the condition is cleared with
|
||||
.Xr clearerr 3 .
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr fread 3 ,
|
||||
.Xr putwc 3 ,
|
||||
.Xr stdio 3 ,
|
||||
.Xr ungetwc 3
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn fgetwc ,
|
||||
.Fn getwc
|
||||
and
|
||||
.Fn getwchar
|
||||
functions
|
||||
conform to
|
||||
.St -isoC-99
|
||||
and
|
||||
.St -p1003.1-2001 .
|
||||
@@ -0,0 +1,49 @@
|
||||
/* $NetBSD: getwc.c,v 1.3 2005/06/12 05:21:27 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: getwc.c,v 1.3 2005/06/12 05:21:27 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro getwc.
|
||||
*/
|
||||
#undef getwc
|
||||
|
||||
wint_t
|
||||
getwc(FILE *fp)
|
||||
{
|
||||
|
||||
return fgetwc(fp);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/* $NetBSD: getwchar.c,v 1.3 2005/06/12 05:21:27 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: getwchar.c,v 1.3 2005/06/12 05:21:27 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro getwchar.
|
||||
*/
|
||||
#undef getwchar
|
||||
|
||||
wint_t
|
||||
getwchar()
|
||||
{
|
||||
|
||||
return fgetwc(stdin);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/* $NetBSD: glue.h,v 1.5 2003/08/07 16:43:27 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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;
|
||||
@@ -0,0 +1,128 @@
|
||||
/* $NetBSD: local.h,v 1.29 2010/10/24 17:44:32 tron Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.3 (Berkeley) 7/3/94
|
||||
*/
|
||||
|
||||
#include "wcio.h"
|
||||
#include "fileext.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
* Information local to this implementation of stdio,
|
||||
* in particular, macros and private variables.
|
||||
*/
|
||||
|
||||
extern int __sflush __P((FILE *));
|
||||
extern FILE *__sfp __P((void));
|
||||
extern void __sfpinit __P((FILE *));
|
||||
extern int __srefill __P((FILE *));
|
||||
extern int __sread __P((void *, char *, int));
|
||||
extern int __swrite __P((void *, char const *, int));
|
||||
extern fpos_t __sseek __P((void *, fpos_t, int));
|
||||
extern int __sclose __P((void *));
|
||||
extern void __sinit __P((void));
|
||||
extern void _cleanup __P((void));
|
||||
extern void (*__cleanup) __P((void));
|
||||
extern void __smakebuf __P((FILE *));
|
||||
extern int __swhatbuf __P((FILE *, size_t *, int *));
|
||||
extern int _fwalk __P((int (*)(FILE *)));
|
||||
extern char *_mktemp __P((char *));
|
||||
extern int __swsetup __P((FILE *));
|
||||
extern int __sflags __P((const char *, int *));
|
||||
extern int __svfscanf __P((FILE * __restrict, const char * __restrict,
|
||||
_BSD_VA_LIST_))
|
||||
__attribute__((__format__(__scanf__, 2, 0)));
|
||||
extern int __svfscanf_unlocked __P((FILE * __restrict, const char * __restrict,
|
||||
_BSD_VA_LIST_))
|
||||
__attribute__((__format__(__scanf__, 2, 0)));
|
||||
extern int __vfprintf_unlocked __P((FILE * __restrict, const char * __restrict,
|
||||
_BSD_VA_LIST_));
|
||||
|
||||
|
||||
extern int __sdidinit;
|
||||
|
||||
extern int __gettemp __P((char *, int *, int));
|
||||
|
||||
extern wint_t __fgetwc_unlock __P((FILE *));
|
||||
extern wint_t __fputwc_unlock __P((wchar_t, FILE *));
|
||||
|
||||
extern ssize_t __getdelim(char **__restrict, size_t *__restrict, int,
|
||||
FILE *__restrict);
|
||||
extern char *__fgetstr __P((FILE * __restrict, size_t * __restrict, int));
|
||||
extern int __vfwprintf_unlocked __P((FILE *, const wchar_t *,
|
||||
_BSD_VA_LIST_));
|
||||
extern int __vfwscanf_unlocked __P((FILE * __restrict,
|
||||
const wchar_t * __restrict, _BSD_VA_LIST_));
|
||||
|
||||
/*
|
||||
* 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) (_UB(fp)._base != NULL)
|
||||
#define FREEUB(fp) { \
|
||||
if (_UB(fp)._base != (fp)->_ubuf) \
|
||||
free((char *)_UB(fp)._base); \
|
||||
_UB(fp)._base = NULL; \
|
||||
}
|
||||
|
||||
/*
|
||||
* test for an fgetln() buffer.
|
||||
*/
|
||||
#define FREELB(fp) { \
|
||||
free(_EXT(fp)->_fgetstr_buf); \
|
||||
_EXT(fp)->_fgetstr_buf = NULL; \
|
||||
_EXT(fp)->_fgetstr_len = 0; \
|
||||
}
|
||||
|
||||
extern void __flockfile_internal __P((FILE *, int));
|
||||
extern void __funlockfile_internal __P((FILE *, int));
|
||||
|
||||
/*
|
||||
* Detect if the current file position fits in a long int.
|
||||
*/
|
||||
|
||||
static __inline bool
|
||||
__fpos_overflow(fpos_t pos)
|
||||
{
|
||||
return (pos < LONG_MIN) || (pos > LONG_MAX);
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/* $NetBSD: makebuf.c,v 1.15 2008/03/13 15:40:00 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)makebuf.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: makebuf.c,v 1.15 2008/03/13 15:40:00 christos Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "reentrant.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(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
void *p;
|
||||
int flags;
|
||||
size_t size;
|
||||
int couldbetty;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
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(__sfileno(fp)))
|
||||
flags |= __SLBF;
|
||||
fp->_flags |= flags;
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal routine to determine `proper' buffering for a file.
|
||||
*/
|
||||
int
|
||||
__swhatbuf(fp, bufsize, couldbetty)
|
||||
FILE *fp;
|
||||
size_t *bufsize;
|
||||
int *couldbetty;
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
_DIAGASSERT(bufsize != NULL);
|
||||
_DIAGASSERT(couldbetty != NULL);
|
||||
|
||||
if (__sfileno(fp) == -1 || fstat(__sfileno(fp), &st) < 0) {
|
||||
*couldbetty = 0;
|
||||
*bufsize = BUFSIZ;
|
||||
return (__SNPT);
|
||||
}
|
||||
|
||||
/* could be a tty iff it is a character device */
|
||||
*couldbetty = S_ISCHR(st.st_mode);
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/* $NetBSD: mkdtemp.c,v 1.10 2008/10/20 10:28:38 apb Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#if HAVE_NBTOOL_CONFIG_H
|
||||
#include "nbtool_config.h"
|
||||
#endif
|
||||
|
||||
#if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKDTEMP
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: mkdtemp.c,v 1.10 2008/10/20 10:28:38 apb Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#if HAVE_NBTOOL_CONFIG_H
|
||||
#define GETTEMP __nbcompat_gettemp
|
||||
#else
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#define GETTEMP __gettemp
|
||||
#endif
|
||||
|
||||
char *
|
||||
mkdtemp(path)
|
||||
char *path;
|
||||
{
|
||||
_DIAGASSERT(path != NULL);
|
||||
|
||||
return (GETTEMP(path, (int *)NULL, 1) ? path : (char *)NULL);
|
||||
}
|
||||
|
||||
#endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKDTEMP */
|
||||
@@ -0,0 +1,77 @@
|
||||
/* $NetBSD: mkstemp.c,v 1.10 2008/10/20 10:28:38 apb Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#if HAVE_NBTOOL_CONFIG_H
|
||||
#include "nbtool_config.h"
|
||||
#endif
|
||||
|
||||
#if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: mkstemp.c,v 1.10 2008/10/20 10:28:38 apb Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
|
||||
#if HAVE_NBTOOL_CONFIG_H
|
||||
#define GETTEMP __nbcompat_gettemp
|
||||
#else
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
#define GETTEMP __gettemp
|
||||
#endif
|
||||
|
||||
#ifdef __weak_alias
|
||||
__weak_alias(mkstemp,_mkstemp)
|
||||
#endif
|
||||
|
||||
int
|
||||
mkstemp(path)
|
||||
char *path;
|
||||
{
|
||||
int fd;
|
||||
|
||||
_DIAGASSERT(path != NULL);
|
||||
|
||||
return (GETTEMP(path, &fd, 0) ? fd : -1);
|
||||
}
|
||||
|
||||
#endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP */
|
||||
@@ -0,0 +1,329 @@
|
||||
.\" $NetBSD: mktemp.3,v 1.28 2010/05/14 03:22:49 joerg Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1989, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd April 29, 2010
|
||||
.Dt MKTEMP 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm mktemp ,
|
||||
.Nm mkstemp ,
|
||||
.Nm mkdtemp
|
||||
.Nd make unique temporary file or directory name
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdlib.h
|
||||
.Ft char *
|
||||
.Fn mktemp "char *template"
|
||||
.Ft int
|
||||
.Fn mkstemp "char *template"
|
||||
.Ft char *
|
||||
.Fn mkdtemp "char *template"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn mktemp
|
||||
function
|
||||
takes the given file name template and overwrites a portion of it
|
||||
to create a file name.
|
||||
This file name is unique and suitable for use
|
||||
by the application.
|
||||
The template may be any file name with some number of
|
||||
.So Li X
|
||||
.Sc Ns s
|
||||
appended
|
||||
to it, for example
|
||||
.Pa /tmp/temp.XXXXXX .
|
||||
The trailing
|
||||
.So Li X
|
||||
.Sc Ns s
|
||||
are replaced with the current process number and/or a
|
||||
unique letter combination.
|
||||
The number of unique file names
|
||||
.Fn mktemp
|
||||
can return depends on the number of
|
||||
.So Li X
|
||||
.Sc Ns s
|
||||
provided.
|
||||
Although the
|
||||
.Nx
|
||||
implementation of the functions will accept any number of trailing
|
||||
.So Li X
|
||||
.Sc Ns s ,
|
||||
for portability reasons one should use only six.
|
||||
Using six
|
||||
.So Li X
|
||||
.Sc Ns s
|
||||
will result in
|
||||
.Fn mktemp
|
||||
testing roughly 26 ** 6 (308915776) combinations.
|
||||
.Pp
|
||||
The
|
||||
.Fn mkstemp
|
||||
function
|
||||
makes the same replacement to the template and creates the template file,
|
||||
mode 0600, returning a file descriptor opened for reading and writing.
|
||||
This avoids the race between testing for a file's existence and opening it
|
||||
for use.
|
||||
.Pp
|
||||
The
|
||||
.Fn mkdtemp
|
||||
function
|
||||
is similar to
|
||||
.Fn mkstemp ,
|
||||
but it creates a mode 0700 directory instead and returns the path.
|
||||
.Pp
|
||||
Please note that the permissions of the file or directory being created are
|
||||
subject to the restrictions imposed by the
|
||||
.Xr umask 2
|
||||
system call.
|
||||
It may thus happen that the created file is unreadable and/or unwritable.
|
||||
.Sh RETURN VALUES
|
||||
The
|
||||
.Fn mktemp
|
||||
and
|
||||
.Fn mkdtemp
|
||||
functions
|
||||
return a pointer to the template on success and
|
||||
.Dv NULL
|
||||
on failure.
|
||||
The
|
||||
.Fn mkstemp
|
||||
function
|
||||
returns \-1 if no suitable file could be created.
|
||||
If either call fails an error code is placed in the global variable
|
||||
.Va errno .
|
||||
.Sh EXAMPLES
|
||||
Quite often a programmer will want to replace a use of
|
||||
.Fn mktemp
|
||||
with
|
||||
.Fn mkstemp ,
|
||||
usually to avoid the problems described above.
|
||||
Doing this correctly requires a good understanding of the code in question.
|
||||
.Pp
|
||||
For instance, code of this form:
|
||||
.Bd -literal -offset indent
|
||||
char sfn[15] = "";
|
||||
FILE *sfp;
|
||||
|
||||
strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
|
||||
if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
|
||||
fprintf(stderr, "%s: %s\en", sfn, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (sfp);
|
||||
.Ed
|
||||
.Pp
|
||||
should be rewritten like this:
|
||||
.Bd -literal -offset indent
|
||||
char sfn[15] = "";
|
||||
FILE *sfp;
|
||||
int fd = -1;
|
||||
|
||||
strlcpy(sfn, "/tmp/ed.XXXXXX", sizeof sfn);
|
||||
if ((fd = mkstemp(sfn)) == -1 ||
|
||||
(sfp = fdopen(fd, "w+")) == NULL) {
|
||||
if (fd != -1) {
|
||||
unlink(sfn);
|
||||
close(fd);
|
||||
}
|
||||
fprintf(stderr, "%s: %s\en", sfn, strerror(errno));
|
||||
return (NULL);
|
||||
}
|
||||
return (sfp);
|
||||
.Ed
|
||||
.Pp
|
||||
Often one will find code which uses
|
||||
.Fn mktemp
|
||||
very early on, perhaps to globally initialize the template nicely, but the
|
||||
code which calls
|
||||
.Xr open 2
|
||||
or
|
||||
.Xr fopen 3
|
||||
on that filename will occur much later.
|
||||
(In almost all cases, the use of
|
||||
.Xr fopen 3
|
||||
will mean that the flags
|
||||
.Dv O_CREAT
|
||||
|
|
||||
.Dv O_EXCL
|
||||
are not given to
|
||||
.Xr open 2 ,
|
||||
and thus a symbolic link race becomes possible, hence making
|
||||
necessary the use of
|
||||
.Xr fdopen 3
|
||||
as seen above).
|
||||
Furthermore, one must be careful about code which opens, closes, and then
|
||||
re-opens the file in question.
|
||||
Finally, one must ensure that upon error the temporary file is
|
||||
removed correctly.
|
||||
.Pp
|
||||
There are also cases where modifying the code to use
|
||||
.Fn mktemp ,
|
||||
in concert with
|
||||
.Xr open 2
|
||||
using the flags
|
||||
.Dv O_CREAT
|
||||
|
|
||||
.Dv O_EXCL ,
|
||||
is better, as long as the code retries a new template if
|
||||
.Xr open 2
|
||||
fails with an
|
||||
.Va errno
|
||||
of
|
||||
.Er EEXIST .
|
||||
.Sh ERRORS
|
||||
The
|
||||
.Fn mktemp ,
|
||||
.Fn mkstemp
|
||||
and
|
||||
.Fn mkdtemp
|
||||
functions
|
||||
may set
|
||||
.Va errno
|
||||
to one of the following values:
|
||||
.Bl -tag -width Er
|
||||
.It Bq Er ENOTDIR
|
||||
The pathname portion of the template is not an existing directory.
|
||||
.El
|
||||
.Pp
|
||||
The
|
||||
.Fn mktemp ,
|
||||
.Fn mkstemp
|
||||
and
|
||||
.Fn mkdtemp
|
||||
functions
|
||||
may also set
|
||||
.Va errno
|
||||
to any value specified by the
|
||||
.Xr stat 2
|
||||
function.
|
||||
.Pp
|
||||
The
|
||||
.Fn mkstemp
|
||||
function
|
||||
may also set
|
||||
.Va errno
|
||||
to any value specified by the
|
||||
.Xr open 2
|
||||
function.
|
||||
.Pp
|
||||
The
|
||||
.Fn mkdtemp
|
||||
function
|
||||
may also set
|
||||
.Va errno
|
||||
to any value specified by the
|
||||
.Xr mkdir 2
|
||||
function.
|
||||
.Sh SEE ALSO
|
||||
.Xr chmod 2 ,
|
||||
.Xr getpid 2 ,
|
||||
.Xr open 2 ,
|
||||
.Xr stat 2 ,
|
||||
.Xr umask 2
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn mktemp
|
||||
conforms to
|
||||
.St -p1003.1-2001 .
|
||||
It was however removed from the specification in the
|
||||
.St -p1003.1-2008
|
||||
revision.
|
||||
The
|
||||
.Fn mkstemp
|
||||
and
|
||||
.Fn mkdtemp
|
||||
functions conform to
|
||||
.St -p1003.1-2004
|
||||
and
|
||||
.St -p1003.1-2008 ,
|
||||
respectively.
|
||||
.Sh HISTORY
|
||||
A
|
||||
.Fn mktemp
|
||||
function appeared in
|
||||
.At v7 .
|
||||
.Pp
|
||||
The
|
||||
.Fn mkstemp
|
||||
function appeared in
|
||||
.Bx 4.4 .
|
||||
.Pp
|
||||
The
|
||||
.Fn mkdtemp
|
||||
function appeared in
|
||||
.Nx 1.4 .
|
||||
.Sh BUGS
|
||||
For
|
||||
.Fn mktemp
|
||||
there is an obvious race between file name selection and file
|
||||
creation and deletion: the program is typically written to call
|
||||
.Xr tmpnam 3 ,
|
||||
.Xr tempnam 3 ,
|
||||
or
|
||||
.Fn mktemp .
|
||||
Subsequently, the program calls
|
||||
.Xr open 2
|
||||
or
|
||||
.Xr fopen 3
|
||||
and erroneously opens a file (or symbolic link, fifo or other
|
||||
device) that the attacker has created in the expected file location.
|
||||
Hence
|
||||
.Fn mkstemp
|
||||
is recommended, since it atomically creates the file.
|
||||
An attacker can guess the filenames produced by
|
||||
.Fn mktemp .
|
||||
Whenever it is possible,
|
||||
.Fn mkstemp
|
||||
or
|
||||
.Fn mkdtemp
|
||||
should be used instead.
|
||||
.Pp
|
||||
For this reason,
|
||||
.Xr ld 1
|
||||
will output a warning message whenever it links code that uses
|
||||
.Fn mktemp .
|
||||
.Pp
|
||||
The
|
||||
.Fn mkdtemp
|
||||
function is nonstandard and should not be used if portability is required.
|
||||
.Sh SECURITY CONSIDERATIONS
|
||||
The use of
|
||||
.Fn mktemp
|
||||
should generally be avoided, as a hostile process can exploit a race
|
||||
condition in the time between the generation of a temporary filename by
|
||||
.Fn mktemp
|
||||
and the invoker's use of the temporary name.
|
||||
A link-time warning will be issued advising the use of
|
||||
.Fn mkstemp
|
||||
or
|
||||
.Fn mkdtemp
|
||||
instead.
|
||||
@@ -0,0 +1,70 @@
|
||||
/* $NetBSD: mktemp.c,v 1.19 2003/08/07 16:43:28 agc Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1987, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: mktemp.c,v 1.19 2003/08/07 16:43:28 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
char *
|
||||
_mktemp(path)
|
||||
char *path;
|
||||
{
|
||||
|
||||
_DIAGASSERT(path != NULL);
|
||||
|
||||
return (__gettemp(path, (int *)NULL, 0) ? path : (char *)NULL);
|
||||
}
|
||||
|
||||
__warn_references(mktemp,
|
||||
"warning: mktemp() possibly used unsafely, use mkstemp() or mkdtemp()")
|
||||
|
||||
char *
|
||||
mktemp(path)
|
||||
char *path;
|
||||
{
|
||||
|
||||
_DIAGASSERT(path != NULL);
|
||||
|
||||
return (__gettemp(path, (int *)NULL, 0) ? path : (char *)NULL);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/* $NetBSD: perror.c,v 1.24 2006/01/26 11:13:42 kleink Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1988, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)perror.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: perror.c,v 1.24 2006/01/26 11:13:42 kleink Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include "namespace.h"
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "extern.h"
|
||||
|
||||
/*
|
||||
* Since perror() is not allowed to change the contents of strerror()'s
|
||||
* static buffer, both functions supply their own buffers to strerror_r().
|
||||
*/
|
||||
|
||||
void
|
||||
perror(const char *s)
|
||||
{
|
||||
const char *separator;
|
||||
char buf[NL_TEXTMAX];
|
||||
|
||||
if (s == NULL)
|
||||
s = "";
|
||||
if (*s == '\0')
|
||||
separator = "";
|
||||
else
|
||||
separator = ": ";
|
||||
|
||||
(void)strerror_r(errno, buf, sizeof(buf));
|
||||
(void)fprintf(stderr, "%s%s%s\n", s, separator, buf);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,59 @@
|
||||
/* $NetBSD: printf.c,v 1.11 2003/08/07 16:43:29 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)printf.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: printf.c,v 1.11 2003/08/07 16:43:29 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
printf(char const *fmt, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
ret = vfprintf(stdout, fmt, ap);
|
||||
va_end(ap);
|
||||
return (ret);
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
.\" $NetBSD: putc.3,v 1.12 2010/05/06 09:01:34 jruoho Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)putc.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd May 6, 2010
|
||||
.Dt PUTC 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fputc ,
|
||||
.Nm putc ,
|
||||
.Nm putchar ,
|
||||
.Nm putc_unlocked ,
|
||||
.Nm putchar_unlocked ,
|
||||
.Nm putw
|
||||
.Nd output a character or word to a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn fputc "int c" "FILE *stream"
|
||||
.Ft int
|
||||
.Fn putc "int c" "FILE *stream"
|
||||
.Ft int
|
||||
.Fn putchar "int c"
|
||||
.Ft int
|
||||
.Fn putc_unlocked "int c" "FILE *stream"
|
||||
.Ft int
|
||||
.Fn putchar_unlocked "int c"
|
||||
.Ft int
|
||||
.Fn putw "int w" "FILE *stream"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fputc
|
||||
function
|
||||
writes the character
|
||||
.Fa c
|
||||
(converted to an ``unsigned char'')
|
||||
to the output stream pointed to by
|
||||
.Fa stream .
|
||||
.Pp
|
||||
.Fn putc
|
||||
acts essentially identically to
|
||||
.Fn fputc ,
|
||||
but is a macro that expands in-line.
|
||||
It may evaluate
|
||||
.Fa stream
|
||||
more than once, so arguments given to
|
||||
.Fn putc
|
||||
should not be expressions with potential side effects.
|
||||
.Pp
|
||||
.Fn putchar
|
||||
is identical to
|
||||
.Fn putc
|
||||
with an output stream of
|
||||
.Em stdout .
|
||||
.Pp
|
||||
The
|
||||
.Fn putc_unlocked
|
||||
and
|
||||
.Fn putchar_unlocked
|
||||
functions provide functionality identical to that of
|
||||
.Fn putc
|
||||
and
|
||||
.Fn putchar ,
|
||||
respectively, but do not perform implicit locking of the streams they
|
||||
operate on.
|
||||
In multi-threaded programs they may be used
|
||||
.Em only
|
||||
within a scope in which the stream
|
||||
has been successfully locked by the calling thread using either
|
||||
.Xr flockfile 3
|
||||
or
|
||||
.Xr ftrylockfile 3 ,
|
||||
and may later be released using
|
||||
.Xr funlockfile 3 .
|
||||
.Pp
|
||||
The
|
||||
.Fn putw
|
||||
function
|
||||
writes the specified
|
||||
.Em int
|
||||
to the named output
|
||||
.Fa stream .
|
||||
.Sh RETURN VALUES
|
||||
The functions,
|
||||
.Fn fputc ,
|
||||
.Fn putc
|
||||
and
|
||||
.Fn putchar
|
||||
return the character written.
|
||||
If an error occurs, the value
|
||||
.Dv EOF
|
||||
is returned.
|
||||
The
|
||||
.Fn putw
|
||||
function
|
||||
returns 0 on success;
|
||||
.Dv EOF
|
||||
is returned if
|
||||
a write error occurs,
|
||||
or if an attempt is made to write a read-only stream.
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr getc 3 ,
|
||||
.Xr stdio 3
|
||||
.Sh STANDARDS
|
||||
The functions
|
||||
.Fn fputc ,
|
||||
.Fn putc ,
|
||||
and
|
||||
.Fn putchar ,
|
||||
conform to
|
||||
.St -ansiC .
|
||||
The functions
|
||||
.Fn putc_unlocked
|
||||
and
|
||||
.Fn putchar_unlocked
|
||||
conform to
|
||||
.St -p1003.1-96 .
|
||||
.Sh HISTORY
|
||||
The functions
|
||||
.Fn putc ,
|
||||
.Fn putchar ,
|
||||
and
|
||||
.Fn putw
|
||||
first appeared in
|
||||
.At v6 .
|
||||
The function
|
||||
.Fn fputc
|
||||
appeared in
|
||||
.At v7 .
|
||||
.Sh BUGS
|
||||
The size and byte order of an
|
||||
.Em int
|
||||
varies from one machine to another, and
|
||||
.Fn putw
|
||||
is not recommended for portable applications.
|
||||
@@ -0,0 +1,80 @@
|
||||
/* $NetBSD: putc.c,v 1.11 2003/08/07 16:43:29 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)putc.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: putc.c,v 1.11 2003/08/07 16:43:29 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putc.
|
||||
*/
|
||||
#undef putc
|
||||
#undef putc_unlocked
|
||||
|
||||
int
|
||||
putc(c, fp)
|
||||
int c;
|
||||
FILE *fp;
|
||||
{
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sputc(c, fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
putc_unlocked(c, fp)
|
||||
int c;
|
||||
FILE *fp;
|
||||
{
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
return (__sputc(c, fp));
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/* $NetBSD: putchar.c,v 1.9 2003/08/07 16:43:29 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)putchar.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: putchar.c,v 1.9 2003/08/07 16:43:29 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#undef putchar
|
||||
#undef putchar_unlocked
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putchar
|
||||
*/
|
||||
int
|
||||
putchar(c)
|
||||
int c;
|
||||
{
|
||||
FILE *fp = stdout;
|
||||
int r;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
r = __sputc(c, fp);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
putchar_unlocked(c)
|
||||
int c;
|
||||
{
|
||||
return (__sputc(c, stdout));
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/* $NetBSD: puts.c,v 1.15 2006/03/17 02:25:23 chris Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)puts.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: puts.c,v 1.15 2006/03/17 02:25:23 chris Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "fvwrite.h"
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
/*
|
||||
* Write the given string to stdout, appending a newline.
|
||||
*/
|
||||
int
|
||||
puts(s)
|
||||
char const *s;
|
||||
{
|
||||
size_t c;
|
||||
struct __suio uio;
|
||||
struct __siov iov[2];
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(s != NULL);
|
||||
|
||||
if (s == NULL)
|
||||
s = "(null)";
|
||||
|
||||
c = strlen(s);
|
||||
|
||||
iov[0].iov_base = __UNCONST(s);
|
||||
iov[0].iov_len = c;
|
||||
iov[1].iov_base = __UNCONST("\n");
|
||||
iov[1].iov_len = 1;
|
||||
uio.uio_resid = c + 1;
|
||||
uio.uio_iov = &iov[0];
|
||||
uio.uio_iovcnt = 2;
|
||||
FLOCKFILE(stdout);
|
||||
r = __sfvwrite(stdout, &uio);
|
||||
FUNLOCKFILE(stdout);
|
||||
return (r ? EOF : '\n');
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/* $NetBSD: putw.c,v 1.11 2003/08/07 16:43:29 agc Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)putw.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: putw.c,v 1.11 2003/08/07 16:43:29 agc Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include "fvwrite.h"
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
int
|
||||
putw(w, fp)
|
||||
int w;
|
||||
FILE *fp;
|
||||
{
|
||||
struct __suio uio;
|
||||
struct __siov iov;
|
||||
int r;
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
iov.iov_base = &w;
|
||||
iov.iov_len = uio.uio_resid = sizeof(w);
|
||||
uio.uio_iov = &iov;
|
||||
uio.uio_iovcnt = 1;
|
||||
FLOCKFILE(fp);
|
||||
r = __sfvwrite(fp, &uio);
|
||||
FUNLOCKFILE(fp);
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
.\" $NetBSD: putwc.3,v 1.9 2010/12/16 17:42:27 wiz Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)putc.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd October 20, 2001
|
||||
.Dt PUTWC 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fputwc ,
|
||||
.Nm putwc ,
|
||||
.Nm putwchar ,
|
||||
.Nd output a wide character to a stream
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.In wchar.h
|
||||
.Ft wint_t
|
||||
.Fn fputwc "wchar_t wc" "FILE *stream"
|
||||
.Ft wint_t
|
||||
.Fn putwc "wchar_t wc" "FILE *stream"
|
||||
.Ft wint_t
|
||||
.Fn putwchar "wchar_t wc"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn fputwc
|
||||
function
|
||||
writes the wide character
|
||||
.Fa wc
|
||||
to the output stream pointed to by
|
||||
.Fa stream .
|
||||
.Pp
|
||||
.Fn putwc
|
||||
acts essentially identically to
|
||||
.Fn fputwc ,
|
||||
but is a macro that expands in-line.
|
||||
It may evaluate
|
||||
.Fa stream
|
||||
more than once, so arguments given to
|
||||
.Fn putwc
|
||||
should not be expressions with potential side effects.
|
||||
.Pp
|
||||
.Fn putwchar
|
||||
is identical to
|
||||
.Fn putwc
|
||||
with an output stream of
|
||||
.Em stdout .
|
||||
.Sh RETURN VALUES
|
||||
The functions
|
||||
.Fn fputwc ,
|
||||
.Fn putwc ,
|
||||
and
|
||||
.Fn putwchar
|
||||
return the wide character written.
|
||||
If an error occurs, the value
|
||||
.Dv WEOF
|
||||
is returned.
|
||||
.Sh SEE ALSO
|
||||
.Xr ferror 3 ,
|
||||
.Xr fopen 3 ,
|
||||
.Xr getwc 3 ,
|
||||
.Xr stdio 3
|
||||
.Sh STANDARDS
|
||||
The functions
|
||||
.Fn fputwc ,
|
||||
.Fn putwc ,
|
||||
and
|
||||
.Fn putwchar ,
|
||||
conform to
|
||||
.St -isoC-99 .
|
||||
@@ -0,0 +1,49 @@
|
||||
/* $NetBSD: putwc.c,v 1.4 2005/06/12 05:21:27 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: putwc.c,v 1.4 2005/06/12 05:21:27 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putwc.
|
||||
*/
|
||||
#undef putwc
|
||||
|
||||
wint_t
|
||||
putwc(wchar_t wc, FILE *fp)
|
||||
{
|
||||
|
||||
return fputwc(wc, fp);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/* $NetBSD: putwchar.c,v 1.4 2005/06/12 05:21:27 lukem Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c)2001 Citrus Project,
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*
|
||||
* $Citrus$
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: putwchar.c,v 1.4 2005/06/12 05:21:27 lukem Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
/*
|
||||
* A subroutine version of the macro putwchar.
|
||||
*/
|
||||
#undef putwchar
|
||||
|
||||
wint_t
|
||||
putwchar(wchar_t wc)
|
||||
{
|
||||
|
||||
return fputwc(wc, stdout);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/* $NetBSD: refill.c,v 1.14 2010/09/10 10:29:23 drochner Exp $ */
|
||||
|
||||
/*-
|
||||
* 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. 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
#if 0
|
||||
static char sccsid[] = "@(#)refill.c 8.1 (Berkeley) 6/4/93";
|
||||
#else
|
||||
__RCSID("$NetBSD: refill.c,v 1.14 2010/09/10 10:29:23 drochner Exp $");
|
||||
#endif
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "reentrant.h"
|
||||
#include "local.h"
|
||||
|
||||
#ifdef _REENTRANT
|
||||
extern rwlock_t __sfp_lock;
|
||||
#endif
|
||||
|
||||
static int lflush __P((FILE *));
|
||||
|
||||
static int
|
||||
lflush(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
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(fp)
|
||||
FILE *fp;
|
||||
{
|
||||
|
||||
_DIAGASSERT(fp != NULL);
|
||||
|
||||
/* 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;
|
||||
fp->_flags |= __SERR;
|
||||
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)) {
|
||||
rwlock_rdlock(&__sfp_lock);
|
||||
(void) _fwalk(lflush);
|
||||
rwlock_unlock(&__sfp_lock);
|
||||
}
|
||||
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);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
.\" $NetBSD: remove.3,v 1.12 2003/08/07 16:43:30 agc Exp $
|
||||
.\"
|
||||
.\" Copyright (c) 1990, 1991, 1993
|
||||
.\" The Regents of the University of California. All rights reserved.
|
||||
.\"
|
||||
.\" This code is derived from software contributed to Berkeley by
|
||||
.\" Chris Torek and the American National Standards Committee X3,
|
||||
.\" on Information Processing Systems.
|
||||
.\"
|
||||
.\" 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. 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.
|
||||
.\"
|
||||
.\" @(#)remove.3 8.1 (Berkeley) 6/4/93
|
||||
.\"
|
||||
.Dd June 4, 1993
|
||||
.Dt REMOVE 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm remove
|
||||
.Nd remove directory entry
|
||||
.Sh LIBRARY
|
||||
.Lb libc
|
||||
.Sh SYNOPSIS
|
||||
.In stdio.h
|
||||
.Ft int
|
||||
.Fn remove "const char *path"
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Fn remove
|
||||
function removes the file or directory specified by
|
||||
.Fa path .
|
||||
.Pp
|
||||
If
|
||||
.Fa path
|
||||
specifies a directory,
|
||||
.Fn remove "path"
|
||||
is the equivalent of
|
||||
.Fn rmdir "path" .
|
||||
Otherwise, it is the equivalent of
|
||||
.Fn unlink "path" .
|
||||
.Sh RETURN VALUES
|
||||
Upon successful completion,
|
||||
.Fn remove
|
||||
returns 0.
|
||||
Otherwise, \-1 is returned and the global variable
|
||||
.Va errno
|
||||
is set to indicate the error.
|
||||
.Sh ERRORS
|
||||
The
|
||||
.Fn remove
|
||||
function
|
||||
may fail and set
|
||||
.Va errno
|
||||
for any of the errors specified for the routines
|
||||
.Xr rmdir 2
|
||||
or
|
||||
.Xr unlink 2 .
|
||||
.Sh SEE ALSO
|
||||
.Xr rmdir 2 ,
|
||||
.Xr unlink 2 ,
|
||||
.Xr symlink 7
|
||||
.Sh STANDARDS
|
||||
The
|
||||
.Fn remove
|
||||
function conforms to
|
||||
.St -ansiC .
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user