Module Name: src Committed By: roy Date: Mon Jul 13 22:19:25 UTC 2009
Modified Files: src/distrib/sets/lists/comp: mi src/include: stdio.h src/lib/libc/stdio: Makefile.inc stdio.3 Added Files: src/lib/libc/stdio: getdelim.3 getdelim.c getline.c Log Message: Add implementations for getdelim(3) and getline(3). To generate a diff of this commit: cvs rdiff -u -r1.1277 -r1.1278 src/distrib/sets/lists/comp/mi cvs rdiff -u -r1.73 -r1.74 src/include/stdio.h cvs rdiff -u -r1.35 -r1.36 src/lib/libc/stdio/Makefile.inc cvs rdiff -u -r0 -r1.1 src/lib/libc/stdio/getdelim.3 \ src/lib/libc/stdio/getdelim.c src/lib/libc/stdio/getline.c cvs rdiff -u -r1.20 -r1.21 src/lib/libc/stdio/stdio.3 Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/comp/mi diff -u src/distrib/sets/lists/comp/mi:1.1277 src/distrib/sets/lists/comp/mi:1.1278 --- src/distrib/sets/lists/comp/mi:1.1277 Wed Jul 8 21:23:52 2009 +++ src/distrib/sets/lists/comp/mi Mon Jul 13 22:19:24 2009 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1277 2009/07/08 21:23:52 tls Exp $ +# $NetBSD: mi,v 1.1278 2009/07/13 22:19:24 roy Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -5557,6 +5557,7 @@ ./usr/share/man/cat3/getcwd.0 comp-c-catman .cat ./usr/share/man/cat3/getdate.0 comp-c-catman .cat ./usr/share/man/cat3/getdate_err.0 comp-c-catman .cat +./usr/share/man/cat3/getdelim.0 comp-c-catman .cat ./usr/share/man/cat3/getdevmajor.0 comp-c-catman .cat ./usr/share/man/cat3/getdirentries.0 comp-c-catman .cat ./usr/share/man/cat3/getdiskbyname.0 comp-c-catman .cat @@ -5586,6 +5587,7 @@ ./usr/share/man/cat3/getlabeloffset.0 comp-c-catman .cat ./usr/share/man/cat3/getlabelsector.0 comp-c-catman .cat ./usr/share/man/cat3/getlastlogx.0 comp-c-catman .cat +./usr/share/man/cat3/getline.0 comp-c-catman .cat ./usr/share/man/cat3/getloadavg.0 comp-c-catman .cat ./usr/share/man/cat3/getmaxpartitions.0 comp-c-catman .cat ./usr/share/man/cat3/getmaxx.0 comp-c-catman .cat @@ -16459,6 +16461,7 @@ ./usr/share/man/man3/getcwd.3 comp-c-man .man ./usr/share/man/man3/getdate.3 comp-c-man .man ./usr/share/man/man3/getdate_err.3 comp-c-man .man +./usr/share/man/man3/getdelim.3 comp-c-man .man ./usr/share/man/man3/getdevmajor.3 comp-c-man .man ./usr/share/man/man3/getdirentries.3 comp-c-man .man ./usr/share/man/man3/getdiskbyname.3 comp-c-man .man @@ -16488,6 +16491,7 @@ ./usr/share/man/man3/getlabeloffset.3 comp-c-man .man ./usr/share/man/man3/getlabelsector.3 comp-c-man .man ./usr/share/man/man3/getlastlogx.3 comp-c-man .man +./usr/share/man/man3/getline.3 comp-c-man .man ./usr/share/man/man3/getloadavg.3 comp-c-man .man ./usr/share/man/man3/getmaxpartitions.3 comp-c-man .man ./usr/share/man/man3/getmaxx.3 comp-c-man .man Index: src/include/stdio.h diff -u src/include/stdio.h:1.73 src/include/stdio.h:1.74 --- src/include/stdio.h:1.73 Sun Sep 21 16:59:46 2008 +++ src/include/stdio.h Mon Jul 13 22:19:24 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: stdio.h,v 1.73 2008/09/21 16:59:46 christos Exp $ */ +/* $NetBSD: stdio.h,v 1.74 2009/07/13 22:19:24 roy Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -46,6 +46,10 @@ typedef _BSD_SIZE_T_ size_t; #undef _BSD_SIZE_T_ #endif +#ifdef _BSD_SSIZE_T_ +typedef _BSD_SSIZE_T_ ssize_t; +#undef _BSD_SSIZE_T_ +#endif #include <sys/null.h> @@ -236,6 +240,9 @@ size_t fwrite(const void * __restrict, size_t, size_t, FILE * __restrict); int getc(FILE *); int getchar(void); +ssize_t getdelim(char ** __restrict, size_t * __restrict, int, + FILE * __restrict); +ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict); void perror(const char *); int printf(const char * __restrict, ...) __attribute__((__format__(__printf__, 1, 2))); Index: src/lib/libc/stdio/Makefile.inc diff -u src/lib/libc/stdio/Makefile.inc:1.35 src/lib/libc/stdio/Makefile.inc:1.36 --- src/lib/libc/stdio/Makefile.inc:1.35 Sat Feb 3 00:28:56 2007 +++ src/lib/libc/stdio/Makefile.inc Mon Jul 13 22:19:25 2009 @@ -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 @@ 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 @@ 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+=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 \ Index: src/lib/libc/stdio/stdio.3 diff -u src/lib/libc/stdio/stdio.3:1.20 src/lib/libc/stdio/stdio.3:1.21 --- src/lib/libc/stdio/stdio.3:1.20 Sun Mar 15 09:26:07 2009 +++ src/lib/libc/stdio/stdio.3 Mon Jul 13 22:19:25 2009 @@ -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 @@ .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 Added files: Index: src/lib/libc/stdio/getdelim.3 diff -u /dev/null src/lib/libc/stdio/getdelim.3:1.1 --- /dev/null Mon Jul 13 22:19:25 2009 +++ src/lib/libc/stdio/getdelim.3 Mon Jul 13 22:19:25 2009 @@ -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 Index: src/lib/libc/stdio/getdelim.c diff -u /dev/null src/lib/libc/stdio/getdelim.c:1.1 --- /dev/null Mon Jul 13 22:19:25 2009 +++ src/lib/libc/stdio/getdelim.c Mon Jul 13 22:19:25 2009 @@ -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; +} Index: src/lib/libc/stdio/getline.c diff -u /dev/null src/lib/libc/stdio/getline.c:1.1 --- /dev/null Mon Jul 13 22:19:25 2009 +++ src/lib/libc/stdio/getline.c Mon Jul 13 22:19:25 2009 @@ -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); +}