14297a3b0SGarrett D'Amore /*
2*45bed668SBryan Cantrill * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
34297a3b0SGarrett D'Amore * Copyright (c) 2002-2004 Tim J. Robbins.
44297a3b0SGarrett D'Amore * All rights reserved.
54297a3b0SGarrett D'Amore *
64297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without
74297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions
84297a3b0SGarrett D'Amore * are met:
94297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
104297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer.
114297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
124297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
134297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution.
144297a3b0SGarrett D'Amore *
154297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254297a3b0SGarrett D'Amore * SUCH DAMAGE.
264297a3b0SGarrett D'Amore */
274297a3b0SGarrett D'Amore
284297a3b0SGarrett D'Amore #include "lint.h"
294297a3b0SGarrett D'Amore #include "mse_int.h"
304297a3b0SGarrett D'Amore #include "file64.h"
314297a3b0SGarrett D'Amore #include <errno.h>
324297a3b0SGarrett D'Amore #include <stdio.h>
334297a3b0SGarrett D'Amore #include <string.h>
344297a3b0SGarrett D'Amore #include <wchar.h>
354297a3b0SGarrett D'Amore #include "mblocal.h"
364297a3b0SGarrett D'Amore #include "mse.h"
374297a3b0SGarrett D'Amore #include "stdiom.h"
384297a3b0SGarrett D'Amore #include "libc.h"
394297a3b0SGarrett D'Amore
404297a3b0SGarrett D'Amore /*
414297a3b0SGarrett D'Amore * FreeBSD implementation here included a full version that tried to be more
424297a3b0SGarrett D'Amore * efficient with memory strings. However, for simplicity's sake, we are
434297a3b0SGarrett D'Amore * going to just use fgetwc(). We also do the stream orientation thing for
444297a3b0SGarrett D'Amore * XPG5 if we need to.
454297a3b0SGarrett D'Amore */
464297a3b0SGarrett D'Amore
474297a3b0SGarrett D'Amore wchar_t *
_fgetws_impl(wchar_t * _RESTRICT_KYWD ws,int n,FILE * _RESTRICT_KYWD fp,int orient)484297a3b0SGarrett D'Amore _fgetws_impl(wchar_t *_RESTRICT_KYWD ws, int n, FILE *_RESTRICT_KYWD fp,
494297a3b0SGarrett D'Amore int orient)
504297a3b0SGarrett D'Amore {
514297a3b0SGarrett D'Amore wint_t wc;
524297a3b0SGarrett D'Amore wchar_t *wsp;
534297a3b0SGarrett D'Amore rmutex_t *lk;
544297a3b0SGarrett D'Amore
554297a3b0SGarrett D'Amore FLOCKFILE(lk, fp);
564297a3b0SGarrett D'Amore if (orient && GET_NO_MODE(fp))
574297a3b0SGarrett D'Amore _setorientation(fp, _WC_MODE);
584297a3b0SGarrett D'Amore
594297a3b0SGarrett D'Amore if (n <= 0) {
604297a3b0SGarrett D'Amore errno = EINVAL;
614297a3b0SGarrett D'Amore FUNLOCKFILE(lk);
624297a3b0SGarrett D'Amore return (NULL);
634297a3b0SGarrett D'Amore }
644297a3b0SGarrett D'Amore
654297a3b0SGarrett D'Amore wsp = ws;
66*45bed668SBryan Cantrill while (--n) {
674297a3b0SGarrett D'Amore wc = _fgetwc_unlocked(fp);
684297a3b0SGarrett D'Amore if (wc == EOF) {
694297a3b0SGarrett D'Amore /*
704297a3b0SGarrett D'Amore * This can happen because of an EOF on
714297a3b0SGarrett D'Amore * the stream, or because of a decoding error.
724297a3b0SGarrett D'Amore * Its up to the caller to check errno.
734297a3b0SGarrett D'Amore */
744297a3b0SGarrett D'Amore if (wsp == ws) {
754297a3b0SGarrett D'Amore /* EOF with no data read */
764297a3b0SGarrett D'Amore FUNLOCKFILE(lk);
774297a3b0SGarrett D'Amore return (NULL);
784297a3b0SGarrett D'Amore }
794297a3b0SGarrett D'Amore break;
804297a3b0SGarrett D'Amore }
814297a3b0SGarrett D'Amore *wsp++ = wc;
824297a3b0SGarrett D'Amore
834297a3b0SGarrett D'Amore if (wc == L'\n')
844297a3b0SGarrett D'Amore break;
854297a3b0SGarrett D'Amore }
864297a3b0SGarrett D'Amore *wsp = 0;
874297a3b0SGarrett D'Amore FUNLOCKFILE(lk);
884297a3b0SGarrett D'Amore return (ws);
894297a3b0SGarrett D'Amore }
904297a3b0SGarrett D'Amore
914297a3b0SGarrett D'Amore wchar_t *
fgetws(wchar_t * _RESTRICT_KYWD ws,int n,FILE * _RESTRICT_KYWD fp)924297a3b0SGarrett D'Amore fgetws(wchar_t *_RESTRICT_KYWD ws, int n, FILE *_RESTRICT_KYWD fp)
934297a3b0SGarrett D'Amore {
944297a3b0SGarrett D'Amore return (_fgetws_impl(ws, n, fp, 0));
954297a3b0SGarrett D'Amore }
964297a3b0SGarrett D'Amore
974297a3b0SGarrett D'Amore wchar_t *
__fgetws_xpg5(wchar_t * ws,int n,FILE * fp)984297a3b0SGarrett D'Amore __fgetws_xpg5(wchar_t *ws, int n, FILE *fp)
994297a3b0SGarrett D'Amore {
1004297a3b0SGarrett D'Amore return (_fgetws_impl(ws, n, fp, 1));
1014297a3b0SGarrett D'Amore }
1024297a3b0SGarrett D'Amore
1034297a3b0SGarrett D'Amore wchar_t *
getws(wchar_t * ws)1044297a3b0SGarrett D'Amore getws(wchar_t *ws)
1054297a3b0SGarrett D'Amore {
1064297a3b0SGarrett D'Amore wint_t wc;
1074297a3b0SGarrett D'Amore wchar_t *wsp;
1084297a3b0SGarrett D'Amore rmutex_t *lk;
1094297a3b0SGarrett D'Amore
1104297a3b0SGarrett D'Amore FLOCKFILE(lk, stdin);
1114297a3b0SGarrett D'Amore
1124297a3b0SGarrett D'Amore wsp = ws;
1134297a3b0SGarrett D'Amore for (;;) {
1144297a3b0SGarrett D'Amore wc = _fgetwc_unlocked(stdin);
1154297a3b0SGarrett D'Amore if (wc == EOF) {
1164297a3b0SGarrett D'Amore /*
1174297a3b0SGarrett D'Amore * This can happen because of an EOF on
1184297a3b0SGarrett D'Amore * the stream, or because of a decoding error.
1194297a3b0SGarrett D'Amore * Its up to the caller to check errno.
1204297a3b0SGarrett D'Amore */
1214297a3b0SGarrett D'Amore if (wsp == ws) {
1224297a3b0SGarrett D'Amore /* EOF with no data read */
1234297a3b0SGarrett D'Amore FUNLOCKFILE(lk);
1244297a3b0SGarrett D'Amore return (NULL);
1254297a3b0SGarrett D'Amore }
1264297a3b0SGarrett D'Amore break;
1274297a3b0SGarrett D'Amore }
1284297a3b0SGarrett D'Amore *wsp++ = wc;
1294297a3b0SGarrett D'Amore
1304297a3b0SGarrett D'Amore if (wc == L'\n')
1314297a3b0SGarrett D'Amore break;
1324297a3b0SGarrett D'Amore }
1334297a3b0SGarrett D'Amore *wsp = 0;
1344297a3b0SGarrett D'Amore FUNLOCKFILE(lk);
1354297a3b0SGarrett D'Amore return (ws);
1364297a3b0SGarrett D'Amore }
137