xref: /illumos-gate/usr/src/lib/libc/port/stdio/__extensions.c (revision a61ed2ce7a86a4d6428f2a83eb4739fae945447e)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "file64.h"
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <mtlib.h>
34 #include "stdiom.h"
35 #include <stdio_ext.h>
36 
37 /*
38  * Returns non-zero if the file is open readonly, or if the last operation
39  * on the stream was a read e.g. fread() or fgetc().  Otherwise returns 0.
40  */
41 int
42 __freading(FILE *stream)
43 {
44 	return (stream->_flag & _IOREAD);
45 }
46 
47 /*
48  * Returns non-zero if the file is open write-only or append-only, or if
49  * the last operation on the stream was a write e.g. fwrite() or fputc().
50  * Otherwise returns 0.
51  */
52 int
53 __fwriting(FILE *stream)
54 {
55 	return (stream->_flag & _IOWRT);
56 }
57 
58 /*
59  * Returns non-zero if it is possible to read from a stream.
60  */
61 int
62 __freadable(FILE *stream)
63 {
64 	return (stream->_flag & (_IOREAD|_IORW));
65 }
66 
67 /*
68  * Returns non-zero if it is possible to write on a stream.
69  */
70 int
71 __fwritable(FILE *stream)
72 {
73 	return (stream->_flag & (_IOWRT|_IORW));
74 }
75 
76 /*
77  * Returns non-zero if the stream is line buffered.
78  */
79 int
80 __flbf(FILE *stream)
81 {
82 	return (stream->_flag & _IOLBF);
83 }
84 
85 /*
86  * Discard any pending buffered I/O.
87  */
88 void
89 __fpurge(FILE *stream)
90 {
91 	rmutex_t *lk;
92 
93 	FLOCKFILE(lk, stream);
94 	if ((stream->_ptr = stream->_base) != NULL)
95 		stream->_cnt = 0;
96 	FUNLOCKFILE(lk);
97 }
98 
99 /*
100  * Return the amount of output pending on a stream (in bytes).
101  */
102 size_t
103 __fpending(FILE *stream)
104 {
105 	size_t amount;
106 	rmutex_t *lk;
107 
108 	FLOCKFILE(lk, stream);
109 	amount = stream->_ptr - stream->_base;
110 	FUNLOCKFILE(lk);
111 	return (amount);
112 }
113 
114 /*
115  * Returns the buffer size (in bytes) currently in use by the given stream.
116  */
117 size_t
118 __fbufsize(FILE *stream)
119 {
120 	size_t size;
121 	rmutex_t *lk;
122 
123 	FLOCKFILE(lk, stream);
124 	size = _bufend(stream) - stream->_base;
125 	FUNLOCKFILE(lk);
126 	return (size);
127 }
128