xref: /illumos-gate/usr/src/lib/libc/port/stdio/__extensions.c (revision ce7a304d880a0cf033d8b45bfaf65dfdec0d3faf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "lint.h"
28 #include "file64.h"
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <mtlib.h>
32 #include "stdiom.h"
33 #include <stdio_ext.h>
34 
35 /*
36  * Returns non-zero if the file is open readonly, or if the last operation
37  * on the stream was a read e.g. fread() or fgetc().  Otherwise returns 0.
38  */
39 int
40 __freading(FILE *stream)
41 {
42 	return (stream->_flag & _IOREAD);
43 }
44 
45 /*
46  * Returns non-zero if the file is open write-only or append-only, or if
47  * the last operation on the stream was a write e.g. fwrite() or fputc().
48  * Otherwise returns 0.
49  */
50 int
51 __fwriting(FILE *stream)
52 {
53 	return (stream->_flag & _IOWRT);
54 }
55 
56 /*
57  * Returns non-zero if it is possible to read from a stream.
58  */
59 int
60 __freadable(FILE *stream)
61 {
62 	return (stream->_flag & (_IOREAD|_IORW));
63 }
64 
65 /*
66  * Returns non-zero if it is possible to write on a stream.
67  */
68 int
69 __fwritable(FILE *stream)
70 {
71 	return (stream->_flag & (_IOWRT|_IORW));
72 }
73 
74 /*
75  * Returns non-zero if the stream is line buffered.
76  */
77 int
78 __flbf(FILE *stream)
79 {
80 	return (stream->_flag & _IOLBF);
81 }
82 
83 /*
84  * Discard any pending buffered I/O.
85  */
86 void
87 __fpurge(FILE *stream)
88 {
89 	rmutex_t *lk;
90 
91 	FLOCKFILE(lk, stream);
92 	if ((stream->_ptr = stream->_base) != NULL)
93 		stream->_cnt = 0;
94 	FUNLOCKFILE(lk);
95 }
96 
97 /*
98  * Return the amount of output pending on a stream (in bytes).
99  */
100 size_t
101 __fpending(FILE *stream)
102 {
103 	size_t amount;
104 	rmutex_t *lk;
105 
106 	FLOCKFILE(lk, stream);
107 	amount = stream->_ptr - stream->_base;
108 	FUNLOCKFILE(lk);
109 	return (amount);
110 }
111 
112 /*
113  * Returns the buffer size (in bytes) currently in use by the given stream.
114  */
115 size_t
116 __fbufsize(FILE *stream)
117 {
118 	size_t size;
119 	rmutex_t *lk;
120 
121 	FLOCKFILE(lk, stream);
122 	size = _bufend(stream) - stream->_base;
123 	FUNLOCKFILE(lk);
124 	return (size);
125 }
126