1 /*
2 Copyright (c) 2002-2004 Tim J. Robbins.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 SUCH DAMAGE.
25  */
26 /*
27 FUNCTION
28 <<fwide>>---set and determine the orientation of a FILE stream
29 
30 INDEX
31 	fwide
32 INDEX
33 	_fwide_r
34 
35 SYNOPSIS
36 	#include <wchar.h>
37 	int fwide(FILE *<[fp]>, int <[mode]>);
38 
39 	int fwide( FILE *<[fp]>, int <[mode]>);
40 
41 DESCRIPTION
42 When <[mode]> is zero, the <<fwide>> function determines the current
43 orientation of <[fp]>. It returns a value > 0 if <[fp]> is
44 wide-character oriented, i.e. if wide character I/O is permitted but
45 char I/O is disallowed. It returns a value < 0 if <[fp]> is byte
46 oriented, i.e. if char I/O is permitted but wide character I/O is
47 disallowed. It returns zero if <[fp]> has no orientation yet; in
48 this case the next I/O operation might change the orientation (to byte
49 oriented if it is a char I/O operation, or to wide-character oriented
50 if it is a wide character I/O operation).
51 
52 Once a stream has an orientation, it cannot be changed and persists
53 until the stream is closed, unless the stream is re-opened with freopen,
54 which removes the orientation of the stream.
55 
56 When <[mode]> is non-zero, the <<fwide>> function first attempts to set
57 <[fp]>'s orientation (to wide-character oriented if <[mode]> > 0, or to
58 byte oriented if <[mode]> < 0). It then returns a value denoting the
59 current orientation, as above.
60 
61 RETURNS
62 The <<fwide>> function returns <[fp]>'s orientation, after possibly
63 changing it. A return value > 0 means wide-character oriented. A return
64 value < 0 means byte oriented. A return value of zero means undecided.
65 
66 PORTABILITY
67 C99, POSIX.1-2001.
68 
69 */
70 
71 #define _DEFAULT_SOURCE
72 #include <_ansi.h>
73 #include <wchar.h>
74 #include "local.h"
75 
76 int
fwide(FILE * fp,int mode)77 fwide (
78 	FILE *fp,
79 	int mode)
80 {
81   int ret;
82 
83   CHECK_INIT(ptr, fp);
84 
85   _newlib_flockfile_start (fp);
86   if (mode != 0) {
87     (void) ORIENT (fp, mode);
88   }
89   if (!(fp->_flags & __SORD))
90     ret = 0;
91   else
92     ret = (fp->_flags2 & __SWID) ? 1 : -1;
93   _newlib_flockfile_end (fp);
94   return ret;
95 }
96