xref: /dragonfly/lib/libc/stdio/getc.3 (revision 9f3fc534)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)getc.3	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD: src/lib/libc/stdio/getc.3,v 1.21 2007/01/09 00:28:06 imp Exp $
34.\" $DragonFly: src/lib/libc/stdio/getc.3,v 1.3 2005/09/25 20:49:55 asmodai Exp $
35.\"
36.Dd March 22, 2009
37.Dt GETC 3
38.Os
39.Sh NAME
40.Nm fgetc ,
41.Nm getc ,
42.Nm getc_unlocked ,
43.Nm getchar ,
44.Nm getchar_unlocked ,
45.Nm getw
46.Nd get next character or word from input stream
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In stdio.h
51.Ft int
52.Fn fgetc "FILE *stream"
53.Ft int
54.Fn getc "FILE *stream"
55.Ft int
56.Fn getc_unlocked "FILE *stream"
57.Ft int
58.Fn getchar void
59.Ft int
60.Fn getchar_unlocked "void"
61.Ft int
62.Fn getw "FILE *stream"
63.Sh DESCRIPTION
64The
65.Fn fgetc
66function
67obtains the next input character (if present) from the stream pointed at by
68.Fa stream ,
69or the next character pushed back on the stream via
70.Xr ungetc 3 .
71.Pp
72The
73.Fn getc
74function
75acts essentially identically to
76.Fn fgetc ,
77but is a macro that expands in-line.
78.Pp
79The
80.Fn getchar
81function
82is equivalent to
83.Fn getc stdin .
84.Pp
85The
86.Fn getw
87function
88obtains the next
89.Vt int
90(if present)
91from the stream pointed at by
92.Fa stream .
93.Pp
94The
95.Fn getc_unlocked
96and
97.Fn getchar_unlocked
98functions are equivalent to
99.Fn getc
100and
101.Fn getchar
102respectively,
103except that the caller is responsible for locking the stream
104with
105.Xr flockfile 3
106before calling them.
107These functions may be used to avoid the overhead of locking the stream
108for each character, and to avoid input being dispersed among multiple
109threads reading from the same stream.
110.Sh RETURN VALUES
111If successful, these routines return the next requested object
112from the
113.Fa stream .
114Character values are returned as an
115.Vt "unsigned char"
116converted to an
117.Vt int .
118If the stream is at end-of-file or a read error occurs,
119the routines return
120.Dv EOF .
121The routines
122.Xr feof 3
123and
124.Xr ferror 3
125must be used to distinguish between end-of-file and error.
126If an error occurs, the global variable
127.Va errno
128is set to indicate the error.
129The end-of-file condition is remembered, even on a terminal, and all
130subsequent attempts to read will return
131.Dv EOF
132until the condition is cleared with
133.Xr clearerr 3 .
134.Sh SEE ALSO
135.Xr ferror 3 ,
136.Xr flockfile 3 ,
137.Xr fopen 3 ,
138.Xr fread 3 ,
139.Xr getwc 3 ,
140.Xr putc 3 ,
141.Xr ungetc 3
142.Sh STANDARDS
143The
144.Fn fgetc ,
145.Fn getc
146and
147.Fn getchar
148functions
149conform to
150.St -isoC .
151The
152.Fn getc_unlocked
153and
154.Fn getchar_unlocked
155functions conform to
156.St -p1003.1-2001 .
157.Sh BUGS
158Since
159.Dv EOF
160is a valid integer value,
161.Xr feof 3
162and
163.Xr ferror 3
164must be used to check for failure after calling
165.Fn getw .
166The size and byte order of an
167.Vt int
168varies from one machine to another, and
169.Fn getw
170is not recommended for portable applications.
171.Pp
172