Add fgetwln(), a wide character version of fgetln().
This commit is contained in:
parent
66d56cb7b8
commit
9531ef0fc1
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=132242
@ -208,6 +208,7 @@ int wcwidth(wchar_t);
|
||||
#endif
|
||||
|
||||
#if __BSD_VISIBLE
|
||||
wchar_t *fgetwln(struct __sFILE * __restrict, size_t * __restrict);
|
||||
size_t wcslcat(wchar_t *, const wchar_t *, size_t);
|
||||
size_t wcslcpy(wchar_t *, const wchar_t *, size_t);
|
||||
#endif
|
||||
|
@ -5,7 +5,8 @@
|
||||
.PATH: ${.CURDIR}/stdio
|
||||
|
||||
SRCS+= _flock_stub.c asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c \
|
||||
fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetwc.c fgetws.c \
|
||||
fflush.c fgetc.c fgetln.c fgetpos.c fgets.c fgetwc.c fgetwln.c \
|
||||
fgetws.c \
|
||||
fileno.c findfp.c flags.c fopen.c fprintf.c fpurge.c fputc.c fputs.c \
|
||||
fputwc.c fputws.c fread.c freopen.c fscanf.c fseek.c fsetpos.c \
|
||||
ftell.c funopen.c fvwrite.c fwalk.c fwide.c fwprintf.c fwscanf.c \
|
||||
@ -22,7 +23,8 @@ SRCS+= _flock_stub.c asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c \
|
||||
vswprintf.c vswscanf.c vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c \
|
||||
wsetup.c
|
||||
|
||||
MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetws.3 flockfile.3 \
|
||||
MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fgetwln.3 fgetws.3 \
|
||||
flockfile.3 \
|
||||
fopen.3 fputs.3 \
|
||||
fputws.3 fread.3 fseek.3 funopen.3 fwide.3 getc.3 getwc.3 mktemp.3 \
|
||||
printf.3 putc.3 putwc.3 remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 \
|
||||
|
120
lib/libc/stdio/fgetwln.3
Normal file
120
lib/libc/stdio/fgetwln.3
Normal file
@ -0,0 +1,120 @@
|
||||
.\" 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$
|
||||
.\"
|
||||
.Dd July 16, 2004
|
||||
.Dt FGETWLN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm fgetln
|
||||
.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
|
68
lib/libc/stdio/fgetwln.c
Normal file
68
lib/libc/stdio/fgetwln.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*-
|
||||
* 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>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include "namespace.h"
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
#include "un-namespace.h"
|
||||
#include "libc_private.h"
|
||||
#include "local.h"
|
||||
|
||||
wchar_t *
|
||||
fgetwln(FILE * __restrict fp, size_t *lenp)
|
||||
{
|
||||
wint_t wc;
|
||||
size_t len;
|
||||
|
||||
FLOCKFILE(fp);
|
||||
ORIENT(fp, 1);
|
||||
|
||||
len = 0;
|
||||
while ((wc = __fgetwc(fp)) != WEOF) {
|
||||
#define GROW 512
|
||||
len++;
|
||||
if (len * sizeof(wchar_t) >= fp->_lb._size &&
|
||||
__slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user