1*88c3eadbSlukem /* $NetBSD: fwide.c,v 1.3 2005/06/12 05:21:27 lukem Exp $ */
217f3654aSyamt
317f3654aSyamt /*-
417f3654aSyamt * Copyright (c)2001 Citrus Project,
517f3654aSyamt * All rights reserved.
617f3654aSyamt *
717f3654aSyamt * Redistribution and use in source and binary forms, with or without
817f3654aSyamt * modification, are permitted provided that the following conditions
917f3654aSyamt * are met:
1017f3654aSyamt * 1. Redistributions of source code must retain the above copyright
1117f3654aSyamt * notice, this list of conditions and the following disclaimer.
1217f3654aSyamt * 2. Redistributions in binary form must reproduce the above copyright
1317f3654aSyamt * notice, this list of conditions and the following disclaimer in the
1417f3654aSyamt * documentation and/or other materials provided with the distribution.
1517f3654aSyamt *
1617f3654aSyamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1717f3654aSyamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1817f3654aSyamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1917f3654aSyamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2017f3654aSyamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2117f3654aSyamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2217f3654aSyamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2317f3654aSyamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2417f3654aSyamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2517f3654aSyamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2617f3654aSyamt * SUCH DAMAGE.
2717f3654aSyamt *
2817f3654aSyamt * $Citrus$
2917f3654aSyamt */
3017f3654aSyamt
31*88c3eadbSlukem #include <sys/cdefs.h>
32*88c3eadbSlukem #if defined(LIBC_SCCS) && !defined(lint)
33*88c3eadbSlukem __RCSID("$NetBSD: fwide.c,v 1.3 2005/06/12 05:21:27 lukem Exp $");
34*88c3eadbSlukem #endif /* LIBC_SCCS and not lint */
35*88c3eadbSlukem
3617f3654aSyamt #include <assert.h>
3717f3654aSyamt #include <stdio.h>
3817f3654aSyamt #include <wchar.h>
3917f3654aSyamt #include "reentrant.h"
403fdac2b8Sthorpej #include "local.h"
4117f3654aSyamt
4217f3654aSyamt int
fwide(FILE * fp,int mode)4317f3654aSyamt fwide(FILE *fp, int mode)
4417f3654aSyamt {
4517f3654aSyamt struct wchar_io_data *wcio;
4617f3654aSyamt
4717f3654aSyamt _DIAGASSERT(fp != NULL);
4817f3654aSyamt
4917f3654aSyamt /*
5017f3654aSyamt * this implementation use only -1, 0, 1
5117f3654aSyamt * for mode value.
5217f3654aSyamt * (we don't need to do this, but
5317f3654aSyamt * this can make things simpler.)
5417f3654aSyamt */
5517f3654aSyamt if (mode > 0)
5617f3654aSyamt mode = 1;
5717f3654aSyamt else if (mode < 0)
5817f3654aSyamt mode = -1;
5917f3654aSyamt
6017f3654aSyamt FLOCKFILE(fp);
6117f3654aSyamt wcio = WCIO_GET(fp);
6217f3654aSyamt if (!wcio)
6317f3654aSyamt return 0; /* XXX */
6417f3654aSyamt
6517f3654aSyamt if (wcio->wcio_mode == 0 && mode != 0)
6617f3654aSyamt wcio->wcio_mode = mode;
6717f3654aSyamt else
6817f3654aSyamt mode = wcio->wcio_mode;
6917f3654aSyamt FUNLOCKFILE(fp);
7017f3654aSyamt
7117f3654aSyamt return mode;
7217f3654aSyamt }
73