1*526d9427Schristos /* $NetBSD: fgetws.c,v 1.3 2012/03/15 18:22:30 christos Exp $ */
25abc4b4fStshiozak
35abc4b4fStshiozak /*-
45abc4b4fStshiozak * Copyright (c) 2002 Tim J. Robbins.
55abc4b4fStshiozak * All rights reserved.
65abc4b4fStshiozak *
75abc4b4fStshiozak * Redistribution and use in source and binary forms, with or without
85abc4b4fStshiozak * modification, are permitted provided that the following conditions
95abc4b4fStshiozak * are met:
105abc4b4fStshiozak * 1. Redistributions of source code must retain the above copyright
115abc4b4fStshiozak * notice, this list of conditions and the following disclaimer.
125abc4b4fStshiozak * 2. Redistributions in binary form must reproduce the above copyright
135abc4b4fStshiozak * notice, this list of conditions and the following disclaimer in the
145abc4b4fStshiozak * documentation and/or other materials provided with the distribution.
155abc4b4fStshiozak *
165abc4b4fStshiozak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
175abc4b4fStshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
185abc4b4fStshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
195abc4b4fStshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
205abc4b4fStshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
215abc4b4fStshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
225abc4b4fStshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
235abc4b4fStshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
245abc4b4fStshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
255abc4b4fStshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265abc4b4fStshiozak * SUCH DAMAGE.
275abc4b4fStshiozak *
285abc4b4fStshiozak * Original version ID:
295abc4b4fStshiozak * FreeBSD: src/lib/libc/stdio/fgetws.c,v 1.4 2002/09/20 13:25:40 tjr Exp
305abc4b4fStshiozak *
315abc4b4fStshiozak */
325abc4b4fStshiozak
335abc4b4fStshiozak #include <sys/cdefs.h>
345abc4b4fStshiozak #if defined(LIB_SCCS) && !defined(lint)
35*526d9427Schristos __RCSID("$NetBSD: fgetws.c,v 1.3 2012/03/15 18:22:30 christos Exp $");
365abc4b4fStshiozak #endif
375abc4b4fStshiozak
385abc4b4fStshiozak #include <assert.h>
395abc4b4fStshiozak #include <errno.h>
405abc4b4fStshiozak #include <stdio.h>
415abc4b4fStshiozak #include <wchar.h>
425abc4b4fStshiozak #include "reentrant.h"
435abc4b4fStshiozak #include "local.h"
445abc4b4fStshiozak
455abc4b4fStshiozak wchar_t *
fgetws(wchar_t * __restrict ws,int n,FILE * __restrict fp)46*526d9427Schristos fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)
475abc4b4fStshiozak {
485abc4b4fStshiozak wchar_t *wsp;
495abc4b4fStshiozak wint_t wc;
505abc4b4fStshiozak
515abc4b4fStshiozak _DIAGASSERT(fp != NULL);
525abc4b4fStshiozak _DIAGASSERT(ws != NULL);
535abc4b4fStshiozak
545abc4b4fStshiozak FLOCKFILE(fp);
555abc4b4fStshiozak _SET_ORIENTATION(fp, 1);
565abc4b4fStshiozak
575abc4b4fStshiozak if (n <= 0) {
585abc4b4fStshiozak errno = EINVAL;
595abc4b4fStshiozak goto error;
605abc4b4fStshiozak }
615abc4b4fStshiozak
625abc4b4fStshiozak wsp = ws;
635abc4b4fStshiozak while (n-- > 1) {
64c98a8494Stnozaki wc = __fgetwc_unlock(fp);
65c98a8494Stnozaki if (__sferror(fp) != 0)
665abc4b4fStshiozak goto error;
67c98a8494Stnozaki if (__sfeof(fp) != 0) {
685abc4b4fStshiozak if (wsp == ws) {
695abc4b4fStshiozak /* EOF/error, no characters read yet. */
705abc4b4fStshiozak goto error;
715abc4b4fStshiozak }
725abc4b4fStshiozak break;
735abc4b4fStshiozak }
745abc4b4fStshiozak *wsp++ = (wchar_t)wc;
755abc4b4fStshiozak if (wc == L'\n') {
765abc4b4fStshiozak break;
775abc4b4fStshiozak }
785abc4b4fStshiozak }
795abc4b4fStshiozak
805abc4b4fStshiozak *wsp++ = L'\0';
815abc4b4fStshiozak FUNLOCKFILE(fp);
825abc4b4fStshiozak
83*526d9427Schristos return ws;
845abc4b4fStshiozak
855abc4b4fStshiozak error:
865abc4b4fStshiozak FUNLOCKFILE(fp);
87*526d9427Schristos return NULL;
885abc4b4fStshiozak }
89