summaryrefslogtreecommitdiff
path: root/lib/libc/stdio
diff options
context:
space:
mode:
authorroy <roy@NetBSD.org>2009-07-13 22:19:24 +0000
committerroy <roy@NetBSD.org>2009-07-13 22:19:24 +0000
commitd4a3cf6a5aef5c137222dd3e8a87eddc29abc91d (patch)
tree97a247476cb40e5115c4736c6cbb48ad86e066cf /lib/libc/stdio
parent585ce9eda67a8cb4ff17fd3ad93ec619d6e8f4a8 (diff)
Add implementations for getdelim(3) and getline(3).
Diffstat (limited to 'lib/libc/stdio')
-rw-r--r--lib/libc/stdio/Makefile.inc13
-rw-r--r--lib/libc/stdio/getdelim.3118
-rw-r--r--lib/libc/stdio/getdelim.c133
-rw-r--r--lib/libc/stdio/getline.c39
-rw-r--r--lib/libc/stdio/stdio.38
5 files changed, 302 insertions, 9 deletions
diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc
index dbaf9dcbe24..72dad2cda86 100644
--- a/lib/libc/stdio/Makefile.inc
+++ b/lib/libc/stdio/Makefile.inc
@@ -1,5 +1,5 @@
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
-# $NetBSD: Makefile.inc,v 1.35 2007/02/03 00:28:56 christos Exp $
+# $NetBSD: Makefile.inc,v 1.36 2009/07/13 22:19:25 roy Exp $
# stdio sources
.PATH: ${.CURDIR}/stdio
@@ -12,10 +12,10 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.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 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 \
+ 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 vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c vprintf.c \
vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c vswscanf.c \
@@ -30,7 +30,7 @@ SRCS+= _fileno.c _fseeko.c _ftello.c
MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fgetws.3 \
flockfile.3 fopen.3 fparseln.3 fputs.3 fputws.3 fread.3 fseek.3 \
- funopen.3 fwide.3 getc.3 getwc.3 mktemp.3 printf.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
@@ -46,6 +46,7 @@ MLINKS+=fseek.3 fgetpos.3 fseek.3 fseeko.3 fseek.3 fsetpos.3 fseek.3 ftell.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 \
diff --git a/lib/libc/stdio/getdelim.3 b/lib/libc/stdio/getdelim.3
new file mode 100644
index 00000000000..2056b4769f7
--- /dev/null
+++ b/lib/libc/stdio/getdelim.3
@@ -0,0 +1,118 @@
+.\" $NetBSD: getdelim.3,v 1.1 2009/07/13 22:19:25 roy 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 July 12, 2009
+.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 the
+.Fa delimiter
+character, storing the input in
+.Fa *lineptr .
+The buffer is
+.Dv NULL
+terminated and includes the delimiter.
+.Pp
+If
+.Fa *n
+is non zero, then
+.Fa *lineptr
+must be pre-allocated to at least
+.Fa *n
+bytes.
+.Fn getdelim
+enures that
+.Fa *lineptr
+is large enough to hold the input, updating
+.Fa *n
+to reflect the new size.
+.Pp
+.Fa getline
+is equivalent to
+.Fa getdelim
+with the delimiter set to the newline character.
+.Sh RETURN VALUES
+The
+.Fn getdelim
+function return the number of characters read, including the delimiter.
+If an error occurs the function return -1 and the global variable
+.Va errno
+is set to indicate the error.
+.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_t characters were read without encountering the delimiter.
+.El
+.Pp
+The
+.Fn getdelim
+function 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
diff --git a/lib/libc/stdio/getdelim.c b/lib/libc/stdio/getdelim.c
new file mode 100644
index 00000000000..9fc6d6e37cc
--- /dev/null
+++ b/lib/libc/stdio/getdelim.c
@@ -0,0 +1,133 @@
+/* $NetBSD: getdelim.c,v 1.1 2009/07/13 22:19:25 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.1 2009/07/13 22:19:25 roy Exp $");
+
+#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"
+
+/* 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, off, newlen;
+ 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;
+
+ FLOCKFILE(fp);
+ _SET_ORIENTATION(fp, -1);
+ off = 0;
+ for (;;) {
+ /* If the input buffer is empty, refill it */
+ if (fp->_r <= 0 && __srefill(fp)) {
+ /* POSIX requires we return -1 on EOF */
+ if (off == 0 || __sferror(fp))
+ goto error;
+ break;
+ }
+
+ /* Scan through looking for the separator */
+ p = memchr(fp->_p, sep, fp->_r);
+ if (p == NULL)
+ len = fp->_r;
+ else
+ len = (p - fp->_p) + 1;
+
+ newlen = off + len + 1;
+ /* Ensure that the resultant buffer length fits in ssize_t */
+ if (newlen > (size_t)SSIZE_MAX + 1) {
+ errno = EOVERFLOW;
+ goto error;
+ }
+ 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((void *)(*buf + off), (void *)fp->_p, len);
+ fp->_r -= len;
+ fp->_p += len;
+ off += len;
+ if (p != NULL)
+ break;
+ }
+ FUNLOCKFILE(fp);
+ if (*buf != NULL)
+ *(*buf + off) = '\0';
+ return off;
+
+error:
+ FUNLOCKFILE(fp);
+ return -1;
+}
diff --git a/lib/libc/stdio/getline.c b/lib/libc/stdio/getline.c
new file mode 100644
index 00000000000..8e75fc4af88
--- /dev/null
+++ b/lib/libc/stdio/getline.c
@@ -0,0 +1,39 @@
+/* $NetBSD: getline.c,v 1.1 2009/07/13 22:19:25 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.1 2009/07/13 22:19:25 roy Exp $");
+
+#include <stdio.h>
+
+ssize_t
+getline(char **__restrict buf, size_t *__restrict buflen, FILE *__restrict fp)
+{
+ return getdelim(buf, buflen, '\n', fp);
+}
diff --git a/lib/libc/stdio/stdio.3 b/lib/libc/stdio/stdio.3
index 82a6313bb89..9d775bdba31 100644
--- a/lib/libc/stdio/stdio.3
+++ b/lib/libc/stdio/stdio.3
@@ -1,4 +1,4 @@
-.\" $NetBSD: stdio.3,v 1.20 2009/03/15 09:26:07 joerg Exp $
+.\" $NetBSD: stdio.3,v 1.21 2009/07/13 22:19:25 roy Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -29,7 +29,7 @@
.\"
.\" @(#)stdio.3 8.7 (Berkeley) 4/19/94
.\"
-.Dd January 28, 2003
+.Dd July 13, 2009
.Dt STDIO 3
.Os
.Sh NAME
@@ -271,12 +271,14 @@ library conforms to
.It fwide set/get orientation of a stream
.It fwopen open a stream
.It fwrite binary stream input/output
-.It getc get next character or word from input stream
+.It getc get next character or word from inputt stream
.It getc_unlocked get next character or word from input stream
.It Ta (no implicit locking)
.It getchar get next character or word from input stream
.It getchar_unlocked get next character or word from input stream
.It Ta (no implicit locking)
+.It getdelim get a delimited record from a stream
+.It getline get a line from a stream
.It gets get a line from a stream
.It getw get next character or word from input stream
.It getwc get next wide character from input stream