diff options
Diffstat (limited to 'lib/libc/stdio')
34 files changed, 1389 insertions, 26 deletions
diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index 1c2639bd036..e02e032bfc7 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.12 2005/05/11 18:39:19 espie Exp $ +# $OpenBSD: Makefile.inc,v 1.13 2005/06/17 20:40:32 espie Exp $ # stdio sources .PATH: ${LIBCSRCDIR}/stdio @@ -14,11 +14,14 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c \ scanf.c setbuf.c setbuffer.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 __svfscanf.c + wbuf.c wsetup.c flockfile.c __svfscanf.c \ + fgetwc.c fgetws.c fputwc.c fputws.c fwide.c getwc.c getwchar.c \ + putwc.c putwchar.c ungetwc.c MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fopen.3 fputs.3 \ fread.3 fseek.3 funopen.3 getc.3 mktemp.3 perror.3 printf.3 putc.3 \ - remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 ungetc.3 + remove.3 scanf.3 setbuf.3 stdio.3 tmpnam.3 ungetc.3 \ + fgetws.3 fputws.3 fwide.3 getwc.3 putwc.3 ungetwc.3 MLINKS+=ferror.3 clearerr.3 ferror.3 feof.3 ferror.3 fileno.3 MLINKS+=fflush.3 fpurge.3 @@ -41,3 +44,6 @@ 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+=getwc.3 fgetwc.3 getwc.3 getwchar.3 +MLINKS+=putwc.3 fputwc.3 putwc.3 putwchar.3 diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c index 604ca412b4a..9ee69664382 100644 --- a/lib/libc/stdio/fclose.c +++ b/lib/libc/stdio/fclose.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: fclose.c,v 1.4 2004/09/28 18:12:43 otto Exp $"; +static char rcsid[] = "$OpenBSD: fclose.c,v 1.5 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <errno.h> @@ -48,6 +48,7 @@ fclose(FILE *fp) errno = EBADF; return (EOF); } + WCIO_FREE(fp); r = fp->_flags & __SWR ? __sflush(fp) : 0; if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) r = EOF; diff --git a/lib/libc/stdio/fgets.c b/lib/libc/stdio/fgets.c index fd7ff2b885b..9aef535baeb 100644 --- a/lib/libc/stdio/fgets.c +++ b/lib/libc/stdio/fgets.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: fgets.c,v 1.8 2005/05/28 04:34:05 millert Exp $"; +static char rcsid[] = "$OpenBSD: fgets.c,v 1.9 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -54,6 +54,7 @@ fgets(char *buf, int n, FILE *fp) if (n <= 0) /* sanity check */ return (NULL); + _SET_ORIENTATION(fp, -1); s = buf; n--; /* leave space for NUL */ while (n != 0) { diff --git a/lib/libc/stdio/fgetwc.c b/lib/libc/stdio/fgetwc.c new file mode 100644 index 00000000000..8da50877264 --- /dev/null +++ b/lib/libc/stdio/fgetwc.c @@ -0,0 +1,90 @@ +/* $OpenBSD: fgetwc.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: fgetwc.c,v 1.3 2003/03/07 07:11:36 tshiozak 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 <errno.h> +#include <stdio.h> +#include <wchar.h> +#include "local.h" + +wint_t +__fgetwc_unlock(FILE *fp) +{ + struct wchar_io_data *wcio; + mbstate_t *st; + wchar_t wc; + size_t size; + + _SET_ORIENTATION(fp, 1); + wcio = WCIO_GET(fp); + if (wcio == 0) { + errno = ENOMEM; + return WEOF; + } + + /* if there're ungetwc'ed wchars, use them */ + if (wcio->wcio_ungetwc_inbuf) { + wc = wcio->wcio_ungetwc_buf[--wcio->wcio_ungetwc_inbuf]; + + return wc; + } + + st = &wcio->wcio_mbstate_in; + + do { + char c; + int ch = __sgetc(fp); + + if (ch == EOF) { + return WEOF; + } + + c = ch; + size = mbrtowc(&wc, &c, 1, st); + if (size == (size_t)-1) { + errno = EILSEQ; + return WEOF; + } + } while (size == (size_t)-2); + + return wc; +} + +wint_t +fgetwc(FILE *fp) +{ + wint_t r; + + flockfile(fp); + r = __fgetwc_unlock(fp); + funlockfile(fp); + + return (r); +} diff --git a/lib/libc/stdio/fgetws.3 b/lib/libc/stdio/fgetws.3 new file mode 100644 index 00000000000..0162f9c5446 --- /dev/null +++ b/lib/libc/stdio/fgetws.3 @@ -0,0 +1,122 @@ +.\" $NetBSD: fgetws.3,v 1.2 2003/08/07 16:43:23 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. +.\" +.\" @(#)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 . diff --git a/lib/libc/stdio/fgetws.c b/lib/libc/stdio/fgetws.c new file mode 100644 index 00000000000..f70b43707fd --- /dev/null +++ b/lib/libc/stdio/fgetws.c @@ -0,0 +1,86 @@ +/* $OpenBSD: fgetws.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: fgetws.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/fgetws.c,v 1.4 2002/09/20 13:25:40 tjr Exp + * + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: fgetws.c,v 1.1 2005/06/17 20:40:32 espie Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <errno.h> +#include <stdio.h> +#include <wchar.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; + + flockfile(fp); + _SET_ORIENTATION(fp, 1); + + if (n <= 0) { + errno = EINVAL; + goto error; + } + + wsp = ws; + while (n-- > 1) { + if ((wc = __fgetwc_unlock(fp)) == WEOF && errno == EILSEQ) { + goto error; + } + if (wc == WEOF) { + 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); +} diff --git a/lib/libc/stdio/fileext.h b/lib/libc/stdio/fileext.h index 5619940c2bc..2d070430248 100644 --- a/lib/libc/stdio/fileext.h +++ b/lib/libc/stdio/fileext.h @@ -1,4 +1,4 @@ -/* $OpenBSD: fileext.h,v 1.1 2005/04/30 09:25:17 espie Exp $ */ +/* $OpenBSD: fileext.h,v 1.2 2005/06/17 20:40:32 espie Exp $ */ /* $NetBSD: fileext.h,v 1.5 2003/07/18 21:46:41 nathanw Exp $ */ /*- @@ -34,9 +34,21 @@ */ struct __sfileext { struct __sbuf _ub; /* ungetc buffer */ - /* further data */ + struct wchar_io_data _wcio; /* wide char io status */ }; #define _EXT(fp) ((struct __sfileext *)((fp)->_ext._base)) #define _UB(fp) _EXT(fp)->_ub -#define _FILEEXT_SETUP(f, fext) /* LINTED */(f)->_ext._base = (unsigned char *)(fext) + +#define _FILEEXT_INIT(fp) \ +do { \ + _UB(fp)._base = NULL; \ + _UB(fp)._size = 0; \ + WCIO_INIT(fp); \ +} while (0) + +#define _FILEEXT_SETUP(f, fext) \ +do { \ + (f)->_ext._base = (unsigned char *)(fext); \ + _FILEEXT_INIT(f); \ +} while (0) diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c index f4de6458146..442a5959e12 100644 --- a/lib/libc/stdio/findfp.c +++ b/lib/libc/stdio/findfp.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: findfp.c,v 1.6 2005/04/30 09:25:17 espie Exp $"; +static char rcsid[] = "$OpenBSD: findfp.c,v 1.7 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> @@ -125,10 +125,9 @@ found: 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; fp->_lb._base = NULL; /* no line buffer */ fp->_lb._size = 0; + _FILEEXT_INIT(fp); return (fp); } @@ -172,8 +171,9 @@ __sinit(void) { int i; - for (i = 0; i < FOPEN_MAX - 3; i++) + for (i = 0; i < FOPEN_MAX - 3; i++) { _FILEEXT_SETUP(usual+i, usualext+i); + } /* make sure we clean up on exit */ __atexit_register_cleanup(_cleanup); /* conservative */ __sdidinit = 1; diff --git a/lib/libc/stdio/fpurge.c b/lib/libc/stdio/fpurge.c index 1891bb9b491..e928c73b016 100644 --- a/lib/libc/stdio/fpurge.c +++ b/lib/libc/stdio/fpurge.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: fpurge.c,v 1.4 2004/09/28 18:12:44 otto Exp $"; +static char rcsid[] = "$OpenBSD: fpurge.c,v 1.5 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <errno.h> @@ -53,6 +53,7 @@ fpurge(FILE *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; diff --git a/lib/libc/stdio/fputs.c b/lib/libc/stdio/fputs.c index 99194a68ee6..ffb0df788ba 100644 --- a/lib/libc/stdio/fputs.c +++ b/lib/libc/stdio/fputs.c @@ -31,11 +31,12 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: fputs.c,v 1.5 2004/09/28 18:12:44 otto Exp $"; +static char rcsid[] = "$OpenBSD: fputs.c,v 1.6 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> #include <string.h> +#include "local.h" #include "fvwrite.h" /* @@ -51,5 +52,6 @@ fputs(const char *s, FILE *fp) iov.iov_len = uio.uio_resid = strlen(s); uio.uio_iov = &iov; uio.uio_iovcnt = 1; + _SET_ORIENTATION(fp, -1); return (__sfvwrite(fp, &uio)); } diff --git a/lib/libc/stdio/fputwc.c b/lib/libc/stdio/fputwc.c new file mode 100644 index 00000000000..4b54cfa4dd5 --- /dev/null +++ b/lib/libc/stdio/fputwc.c @@ -0,0 +1,88 @@ +/* $OpenBSD: fputwc.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: fputwc.c,v 1.3 2003/03/07 07:11:37 tshiozak 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 <errno.h> +#include <limits.h> +#include <stdio.h> +#include <wchar.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; + + /* 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; + } + + 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; + + flockfile(fp); + r = __fputwc_unlock(wc, fp); + funlockfile(fp); + + return (r); +} diff --git a/lib/libc/stdio/fputws.3 b/lib/libc/stdio/fputws.3 new file mode 100644 index 00000000000..b622bf468eb --- /dev/null +++ b/lib/libc/stdio/fputws.3 @@ -0,0 +1,89 @@ +.\" $NetBSD: fputws.3,v 1.2 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 +.\" +.\" 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 . diff --git a/lib/libc/stdio/fputws.c b/lib/libc/stdio/fputws.c new file mode 100644 index 00000000000..f6aa2366e2a --- /dev/null +++ b/lib/libc/stdio/fputws.c @@ -0,0 +1,60 @@ +/* $OpenBSD: fputws.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $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 + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char rcsid[] = "$OpenBSD: fputws.c,v 1.1 2005/06/17 20:40:32 espie Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include <errno.h> +#include <stdio.h> +#include <wchar.h> +#include "local.h" + +int +fputws(ws, fp) + const wchar_t * __restrict ws; + FILE * __restrict fp; +{ + flockfile(fp); + _SET_ORIENTATION(fp, 1); + + while (*ws != '\0') { + if (__fputwc_unlock(*ws++, fp) == WEOF) { + funlockfile(fp); + return (-1); + } + } + + funlockfile(fp); + + return (0); +} diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c index 0131001bea6..4667332b2d6 100644 --- a/lib/libc/stdio/freopen.c +++ b/lib/libc/stdio/freopen.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: freopen.c,v 1.7 2005/04/30 09:25:17 espie Exp $"; +static char rcsid[] = "$OpenBSD: freopen.c,v 1.8 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -116,6 +116,7 @@ freopen(const char *file, const char *mode, FILE *fp) if (HASUB(fp)) FREEUB(fp); _UB(fp)._size = 0; + WCIO_FREE(fp); if (HASLB(fp)) FREELB(fp); fp->_lb._size = 0; diff --git a/lib/libc/stdio/fwide.3 b/lib/libc/stdio/fwide.3 new file mode 100644 index 00000000000..62bc368c1fc --- /dev/null +++ b/lib/libc/stdio/fwide.3 @@ -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 . diff --git a/lib/libc/stdio/fwide.c b/lib/libc/stdio/fwide.c new file mode 100644 index 00000000000..6fd31b0e73e --- /dev/null +++ b/lib/libc/stdio/fwide.c @@ -0,0 +1,64 @@ +/* $OpenBSD: fwide.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: fwide.c,v 1.2 2003/01/18 11:29:54 thorpej 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 <stdio.h> +#include <wchar.h> +#include "local.h" + +int +fwide(FILE *fp, int mode) +{ + struct wchar_io_data *wcio; + + /* + * 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; +} diff --git a/lib/libc/stdio/getwc.3 b/lib/libc/stdio/getwc.3 new file mode 100644 index 00000000000..2632aa2ae62 --- /dev/null +++ b/lib/libc/stdio/getwc.3 @@ -0,0 +1,112 @@ +.\" $NetBSD: getwc.3,v 1.7 2003/09/08 17:54:32 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 October 24, 2001 +.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 . diff --git a/lib/libc/stdio/getwc.c b/lib/libc/stdio/getwc.c new file mode 100644 index 00000000000..e9bbb7c2421 --- /dev/null +++ b/lib/libc/stdio/getwc.c @@ -0,0 +1,45 @@ +/* $OpenBSD: getwc.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: getwc.c,v 1.2 2003/01/18 11:29:55 thorpej 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 <stdio.h> +#include <wchar.h> + +/* + * A subroutine version of the macro getwc. + */ +#undef getwc + +wint_t +getwc(FILE *fp) +{ + + return fgetwc(fp); +} diff --git a/lib/libc/stdio/getwchar.c b/lib/libc/stdio/getwchar.c new file mode 100644 index 00000000000..2a112ed8fbe --- /dev/null +++ b/lib/libc/stdio/getwchar.c @@ -0,0 +1,45 @@ +/* $OpenBSD: getwchar.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: getwchar.c,v 1.2 2003/01/18 11:29:55 thorpej 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 <stdio.h> +#include <wchar.h> + +/* + * A subroutine version of the macro getwchar. + */ +#undef getwchar + +wint_t +getwchar() +{ + + return fgetwc(stdin); +} diff --git a/lib/libc/stdio/local.h b/lib/libc/stdio/local.h index c2e912eae2b..ca3533ece1b 100644 --- a/lib/libc/stdio/local.h +++ b/lib/libc/stdio/local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: local.h,v 1.10 2005/04/30 09:25:17 espie Exp $ */ +/* $OpenBSD: local.h,v 1.11 2005/06/17 20:40:32 espie Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -37,6 +37,8 @@ * in particular, macros and private variables. */ +#include <wchar.h> +#include "wcio.h" #include "fileext.h" int __sflush(FILE *); diff --git a/lib/libc/stdio/putwc.3 b/lib/libc/stdio/putwc.3 new file mode 100644 index 00000000000..65d1d188727 --- /dev/null +++ b/lib/libc/stdio/putwc.3 @@ -0,0 +1,101 @@ +.\" $NetBSD: putwc.3,v 1.7 2003/09/08 17:54:32 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 . diff --git a/lib/libc/stdio/putwc.c b/lib/libc/stdio/putwc.c new file mode 100644 index 00000000000..8e2ff2dca45 --- /dev/null +++ b/lib/libc/stdio/putwc.c @@ -0,0 +1,45 @@ +/* $OpenBSD: putwc.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: putwc.c,v 1.3 2003/01/18 11:29:56 thorpej 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 <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); +} diff --git a/lib/libc/stdio/putwchar.c b/lib/libc/stdio/putwchar.c new file mode 100644 index 00000000000..940ec05ab59 --- /dev/null +++ b/lib/libc/stdio/putwchar.c @@ -0,0 +1,45 @@ +/* $OpenBSD: putwchar.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: putwchar.c,v 1.3 2003/01/18 11:29:56 thorpej 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 <stdio.h> +#include <wchar.h> + +/* + * A subroutine version of the macro putwchar. + */ +#undef putwchar + +wint_t +putwchar(wchar_t wc) +{ + + return fputwc(wc, stdout); +} diff --git a/lib/libc/stdio/rget.c b/lib/libc/stdio/rget.c index cc258ba6ca0..ecbb8b80c0b 100644 --- a/lib/libc/stdio/rget.c +++ b/lib/libc/stdio/rget.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: rget.c,v 1.5 2004/09/28 18:12:44 otto Exp $"; +static char rcsid[] = "$OpenBSD: rget.c,v 1.6 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -45,6 +45,7 @@ static char rcsid[] = "$OpenBSD: rget.c,v 1.5 2004/09/28 18:12:44 otto Exp $"; int __srget(FILE *fp) { + _SET_ORIENTATION(fp, -1); if (__srefill(fp) == 0) { fp->_r--; return (*fp->_p++); diff --git a/lib/libc/stdio/setvbuf.c b/lib/libc/stdio/setvbuf.c index 34ab32dcf67..3605e77e290 100644 --- a/lib/libc/stdio/setvbuf.c +++ b/lib/libc/stdio/setvbuf.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: setvbuf.c,v 1.6 2004/09/28 18:12:44 otto Exp $"; +static char rcsid[] = "$OpenBSD: setvbuf.c,v 1.7 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -68,6 +68,7 @@ setvbuf(FILE *fp, char *buf, int mode, size_t size) (void)__sflush(fp); if (HASUB(fp)) FREEUB(fp); + WCIO_FREE(fp); fp->_r = fp->_lbfsize = 0; flags = fp->_flags; if (flags & __SMBF) diff --git a/lib/libc/stdio/sscanf.c b/lib/libc/stdio/sscanf.c index e8fea3ee68c..4c1b8d83d78 100644 --- a/lib/libc/stdio/sscanf.c +++ b/lib/libc/stdio/sscanf.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: sscanf.c,v 1.10 2005/05/11 18:39:19 espie Exp $"; +static char rcsid[] = "$OpenBSD: sscanf.c,v 1.11 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -60,7 +60,6 @@ sscanf(const char *str, const char *fmt, ...) f._bf._base = f._p = (unsigned char *)str; f._bf._size = f._r = strlen(str); f._read = eofread; - _UB(&f)._base = NULL; f._lb._base = NULL; va_start(ap, fmt); ret = vfscanf(&f, fmt, ap); diff --git a/lib/libc/stdio/ungetc.c b/lib/libc/stdio/ungetc.c index dc16a8ad562..579ab2cd90d 100644 --- a/lib/libc/stdio/ungetc.c +++ b/lib/libc/stdio/ungetc.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: ungetc.c,v 1.7 2005/04/30 09:25:17 espie Exp $"; +static char rcsid[] = "$OpenBSD: ungetc.c,v 1.8 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -85,6 +85,7 @@ ungetc(int c, FILE *fp) return (EOF); if (!__sdidinit) __sinit(); + _SET_ORIENTATION(fp, -1); if ((fp->_flags & __SRD) == 0) { /* * Not already reading: no good unless reading-and-writing. diff --git a/lib/libc/stdio/ungetwc.3 b/lib/libc/stdio/ungetwc.3 new file mode 100644 index 00000000000..b3194656987 --- /dev/null +++ b/lib/libc/stdio/ungetwc.3 @@ -0,0 +1,95 @@ +.\" $NetBSD: ungetwc.3,v 1.7 2003/09/08 17:54:32 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. +.\" +.\" @(#)ungetc.3 8.1 (Berkeley) 6/4/93 +.\" +.Dd October 24, 2001 +.Dt UNGETWC 3 +.Os +.Sh NAME +.Nm ungetwc +.Nd un-get wide-character from input stream +.Sh LIBRARY +.Lb libc +.Sh SYNOPSIS +.In stdio.h +.In wchar.h +.Ft wint_t +.Fn ungetwc "wint_t wc" "FILE *stream" +.Sh DESCRIPTION +The +.Fn ungetwc +function pushes the wide-character +.Fa wc +(converted to an wchar_t) +back onto the input stream pointed to by +.Fa stream . +The pushed-backed wide-characters will be returned by subsequent reads on the +stream (in reverse order). +A successful intervening call, using the same stream, to one of the file +positioning functions +.Xr fseek 3 , +.Xr fsetpos 3 , +or +.Xr rewind 3 +will discard the pushed back wide-characters. +.Pp +One wide-character of push-back is guaranteed, +but as long as there is +sufficient memory, an effectively infinite amount of pushback is allowed. +.Pp +If a character is successfully pushed-back, +the end-of-file indicator for the stream is cleared. +.Sh RETURN VALUES +The +.Fn ungetwc +function +returns +the wide-character pushed-back after the conversion, or +.Dv WEOF +if the operation fails. +If the value of the argument +.Fa c +character equals +.Dv WEOF , +the operation will fail and the stream will remain unchanged. +.Sh SEE ALSO +.Xr fseek 3 , +.Xr getwc 3 +.Sh STANDARDS +The +.Fn ungetwc +function conforms to +.St -isoC-99 . +.Sh BUGS +The current implementation uses a fixed sized ungetwc-buffer. diff --git a/lib/libc/stdio/ungetwc.c b/lib/libc/stdio/ungetwc.c new file mode 100644 index 00000000000..8bd30168130 --- /dev/null +++ b/lib/libc/stdio/ungetwc.c @@ -0,0 +1,70 @@ +/* $OpenBSD: ungetwc.c,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: ungetwc.c,v 1.2 2003/01/18 11:29:59 thorpej 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 <errno.h> +#include <stdio.h> +#include <wchar.h> +#include "local.h" + +wint_t +ungetwc(wint_t wc, FILE *fp) +{ + struct wchar_io_data *wcio; + + if (wc == WEOF) + return WEOF; + + flockfile(fp); + _SET_ORIENTATION(fp, 1); + /* + * XXX since we have no way to transform a wchar string to + * a char string in reverse order, we can't use ungetc. + */ + /* XXX should we flush ungetc buffer? */ + + wcio = WCIO_GET(fp); + if (wcio == 0) { + funlockfile(fp); + errno = ENOMEM; /* XXX */ + return WEOF; + } + + if (wcio->wcio_ungetwc_inbuf >= WCIO_UNGETWC_BUFSIZE) { + funlockfile(fp); + return WEOF; + } + + wcio->wcio_ungetwc_buf[wcio->wcio_ungetwc_inbuf++] = wc; + __sclearerr(fp); + funlockfile(fp); + + return wc; +} diff --git a/lib/libc/stdio/vfprintf.c b/lib/libc/stdio/vfprintf.c index 98bd80096f0..c99c8336efc 100644 --- a/lib/libc/stdio/vfprintf.c +++ b/lib/libc/stdio/vfprintf.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: vfprintf.c,v 1.30 2005/06/08 22:08:30 espie Exp $"; +static char *rcsid = "$OpenBSD: vfprintf.c,v 1.31 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -292,6 +292,7 @@ vfprintf(FILE *fp, const char *fmt0, _BSD_VA_LIST_ ap) (((argtable != NULL) ? (void)(ap = argtable[nextarg]) : (void)0), \ nextarg++, va_arg(ap, type)) + _SET_ORIENTATION(fp, -1); /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ if (cantwrite(fp)) { errno = EBADF; diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c index 58a59145033..cf184c6ef3e 100644 --- a/lib/libc/stdio/vfscanf.c +++ b/lib/libc/stdio/vfscanf.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vfscanf.c,v 1.13 2005/05/28 04:34:05 millert Exp $"; +static char rcsid[] = "$OpenBSD: vfscanf.c,v 1.14 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -113,6 +113,8 @@ VFSCANF(FILE *fp, const char *fmt0, _BSD_VA_LIST_ ap) static short basefix[17] = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + _SET_ORIENTATION(fp, -1); + nassigned = 0; nread = 0; base = 0; /* XXX just to keep gcc happy */ diff --git a/lib/libc/stdio/vsscanf.c b/lib/libc/stdio/vsscanf.c index a1585c622a0..7b6040a26b4 100644 --- a/lib/libc/stdio/vsscanf.c +++ b/lib/libc/stdio/vsscanf.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: vsscanf.c,v 1.8 2005/05/11 18:39:19 espie Exp $"; +static char rcsid[] = "$OpenBSD: vsscanf.c,v 1.9 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -57,7 +57,6 @@ vsscanf(const char *str, const char *fmt, _BSD_VA_LIST_ ap) f._bf._base = f._p = (unsigned char *)str; f._bf._size = f._r = strlen(str); f._read = eofread; - _UB(&f)._base = NULL; f._lb._base = NULL; return (vfscanf(&f, fmt, ap)); } diff --git a/lib/libc/stdio/wbuf.c b/lib/libc/stdio/wbuf.c index e099b3d89c6..2f702668a92 100644 --- a/lib/libc/stdio/wbuf.c +++ b/lib/libc/stdio/wbuf.c @@ -31,7 +31,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: wbuf.c,v 1.7 2004/09/28 18:12:44 otto Exp $"; +static char rcsid[] = "$OpenBSD: wbuf.c,v 1.8 2005/06/17 20:40:32 espie Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -48,6 +48,7 @@ __swbuf(int c, FILE *fp) { int n; + _SET_ORIENTATION(fp, -1); /* * In case we cannot write, or longjmp takes us out early, * make sure _w is 0 (if fully- or un-buffered) or -_bf._size diff --git a/lib/libc/stdio/wcio.h b/lib/libc/stdio/wcio.h new file mode 100644 index 00000000000..1b41ec971d2 --- /dev/null +++ b/lib/libc/stdio/wcio.h @@ -0,0 +1,81 @@ +/* $OpenBSD: wcio.h,v 1.1 2005/06/17 20:40:32 espie Exp $ */ +/* $NetBSD: wcio.h,v 1.3 2003/01/18 11:30:00 thorpej 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$ + */ + +#ifndef _WCIO_H_ +#define _WCIO_H_ + +/* minimal requirement of SUSv2 */ +#define WCIO_UNGETWC_BUFSIZE 1 + +struct wchar_io_data { + mbstate_t wcio_mbstate_in; + mbstate_t wcio_mbstate_out; + + wchar_t wcio_ungetwc_buf[WCIO_UNGETWC_BUFSIZE]; + size_t wcio_ungetwc_inbuf; + + int wcio_mode; /* orientation */ +}; + +#define WCIO_GET(fp) \ + (_EXT(fp) ? &(_EXT(fp)->_wcio) : (struct wchar_io_data *)0) + +#define _SET_ORIENTATION(fp, mode) \ +do {\ + struct wchar_io_data *_wcio = WCIO_GET(fp); \ + if (_wcio && _wcio->wcio_mode == 0) \ + _wcio->wcio_mode = (mode);\ +} while (0) + +/* + * WCIO_FREE should be called by fclose + */ +#define WCIO_FREE(fp) \ +do {\ + struct wchar_io_data *_wcio = WCIO_GET(fp); \ + if (_wcio) { \ + _wcio->wcio_mode = 0;\ + _wcio->wcio_ungetwc_inbuf = 0;\ + } \ +} while (0) + +#define WCIO_FREEUB(fp) \ +do {\ + struct wchar_io_data *_wcio = WCIO_GET(fp); \ + if (_wcio) { \ + _wcio->wcio_ungetwc_inbuf = 0;\ + } \ +} while (0) + +#define WCIO_INIT(fp) \ + memset(WCIO_GET(fp), 0, sizeof(struct wchar_io_data)) + +#endif /*_WCIO_H_*/ |