diff options
-rw-r--r-- | include/wchar.h | 3 | ||||
-rw-r--r-- | lib/libc/shlib_version | 2 | ||||
-rw-r--r-- | lib/libc/stdio/Makefile.inc | 16 | ||||
-rw-r--r-- | lib/libc/stdio/fgetwln.3 | 117 | ||||
-rw-r--r-- | lib/libc/stdio/fgetwln.c | 81 |
5 files changed, 209 insertions, 10 deletions
diff --git a/include/wchar.h b/include/wchar.h index 0ba27368441..b2513e7e432 100644 --- a/include/wchar.h +++ b/include/wchar.h @@ -1,4 +1,4 @@ -/* $OpenBSD: wchar.h,v 1.26 2014/07/12 02:48:33 guenther Exp $ */ +/* $OpenBSD: wchar.h,v 1.27 2015/01/12 20:58:07 millert Exp $ */ /* $NetBSD: wchar.h,v 1.16 2003/03/07 07:11:35 tshiozak Exp $ */ /*- @@ -201,6 +201,7 @@ unsigned long long int wcstoull(const wchar_t * __restrict, wint_t ungetwc(wint_t, FILE *); wint_t fgetwc(FILE *); +wchar_t *fgetwln(FILE * __restrict, size_t * __restrict); wchar_t *fgetws(wchar_t * __restrict, int, FILE * __restrict) __attribute__ ((__bounded__(__wcstring__,1,2))); wint_t getwc(FILE *); diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version index 3592d7d91db..72264f2e9ce 100644 --- a/lib/libc/shlib_version +++ b/lib/libc/shlib_version @@ -1,4 +1,4 @@ major=78 -minor=0 +minor=1 # note: If changes were made to include/thread_private.h or if system # calls were added/changed then librthread/shlib_version also be updated. diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index 865419b360c..f000047f00a 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.27 2014/11/25 17:01:49 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.28 2015/01/12 20:58:07 millert Exp $ # stdio sources .PATH: ${LIBCSRCDIR}/stdio @@ -15,17 +15,17 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c \ setvbuf.c snprintf.c sprintf.c sscanf.c stdio.c tempnam.c tmpfile.c \ tmpnam.c ungetc.c vasprintf.c vfprintf.c vfscanf.c vprintf.c vscanf.c \ vsnprintf.c vsprintf.c vsscanf.c wbuf.c wsetup.c flockfile.c \ - fgetwc.c fgetws.c fputwc.c fputws.c fwide.c getwc.c getwchar.c \ - putwc.c putwchar.c ungetwc.c \ + fgetwc.c fgetwln.c fgetws.c fputwc.c fputws.c fwide.c getwc.c \ + getwchar.c putwc.c putwchar.c ungetwc.c \ fwprintf.c swprintf.c vfwprintf.c vswprintf.c vwprintf.c wprintf.c \ fwscanf.c swscanf.c vfwscanf.c vswscanf.c vwscanf.c wscanf.c \ getdelim.c getline.c dprintf.c vdprintf.c -MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fmemopen.3 fopen.3 fputs.3 \ - fread.3 fseek.3 funopen.3 getc.3 mktemp.3 open_memstream.3 perror.3 \ - printf.3 putc.3 remove.3 scanf.3 setbuf.3 setvbuf.3 stdio.3 tmpnam.3 \ - ungetc.3 fgetws.3 fputws.3 fwide.3 getwc.3 putwc.3 ungetwc.3 wprintf.3 \ - wscanf.3 getdelim.3 +MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fmemopen.3 \ + fopen.3 fputs.3 fread.3 fseek.3 funopen.3 getc.3 mktemp.3 \ + open_memstream.3 perror.3 printf.3 putc.3 remove.3 scanf.3 setbuf.3 \ + setvbuf.3 stdio.3 tmpnam.3 ungetc.3 fgetws.3 fputws.3 fwide.3 getwc.3 \ + putwc.3 ungetwc.3 wprintf.3 wscanf.3 getdelim.3 MLINKS+=ferror.3 clearerr.3 ferror.3 feof.3 ferror.3 fileno.3 MLINKS+=fflush.3 fpurge.3 diff --git a/lib/libc/stdio/fgetwln.3 b/lib/libc/stdio/fgetwln.3 new file mode 100644 index 00000000000..0d6e3ac34b3 --- /dev/null +++ b/lib/libc/stdio/fgetwln.3 @@ -0,0 +1,117 @@ +.\" $OpenBSD: fgetwln.3,v 1.1 2015/01/12 20:58:07 millert 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. +.\" +.Dd $Mdocdate: January 12 2015 $ +.Dt FGETWLN 3 +.Os +.Sh NAME +.Nm fgetwln +.Nd get a line of wide characters from a stream +.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 and is guaranteed to be greater than 0 upon successful completion. +(Note, however, that if the last line in the stream 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. +.Pp +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 [EBADF] +.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 malloc 3 , +.Xr mbrtowc 3 , +.Xr read 2 , +.Xr stat 2 , +or +.Xr realloc 3 . +.Sh SEE ALSO +.Xr ferror 3 , +.Xr fgetln 3 , +.Xr fgetws 3 , +.Xr fopen 3 +.Sh HISTORY +The +.Fn fgetwln +function first appeared in +.Ox 5.7 . diff --git a/lib/libc/stdio/fgetwln.c b/lib/libc/stdio/fgetwln.c new file mode 100644 index 00000000000..1b6c64c175a --- /dev/null +++ b/lib/libc/stdio/fgetwln.c @@ -0,0 +1,81 @@ +/* $OpenBSD: fgetwln.c,v 1.1 2015/01/12 20:58:07 millert 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 <stdio.h> +#include <stdlib.h> +#include <wchar.h> +#include "local.h" + +/* + * Expand the line buffer. Return -1 on error. + */ +static int +__slbexpand(FILE *fp, size_t newsize) +{ + void *p; + + if (fp->_lb._size / sizeof(wchar_t) >= newsize) + return 0; + if ((p = reallocarray(fp->_lb._base, newsize, sizeof(wchar_t))) == NULL) + return -1; + fp->_lb._base = p; + fp->_lb._size = newsize * sizeof(wchar_t); + 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 >= fp->_lb._size / sizeof(wchar_t) && + __slbexpand(fp, len + GROW)) + goto error; + *((wchar_t *)fp->_lb._base + len++) = wc; + if (wc == L'\n') + break; + } + if (len == 0) + goto error; + + FUNLOCKFILE(fp); + *lenp = len; + return (wchar_t *)fp->_lb._base; + +error: + FUNLOCKFILE(fp); + *lenp = 0; + return NULL; +} |