OSDN Git Service

(split) LDP: Update original to v3.37.
[linuxjm/LDP_man-pages.git] / original / man3 / gets.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Wed Jul 28 11:12:07 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified Fri Sep  8 15:48:13 1995 by Andries Brouwer (aeb@cwi.nl)
25 .TH GETS 3  2012-01-18 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 fgetc, fgets, getc, getchar, gets, ungetc \- input of characters and strings
28 .SH SYNOPSIS
29 .nf
30 .B #include <stdio.h>
31 .sp
32 .BI "int fgetc(FILE *" stream );
33
34 .BI "char *fgets(char *" "s" ", int " "size" ", FILE *" "stream" );
35
36 .BI "int getc(FILE *" stream );
37
38 .B "int getchar(void);"
39
40 .BI "char *gets(char *" "s" );
41
42 .BI "int ungetc(int " c ", FILE *" stream );
43 .fi
44 .SH DESCRIPTION
45 .BR fgetc ()
46 reads the next character from
47 .I stream
48 and returns it as an
49 .I unsigned char
50 cast to an
51 .IR int ,
52 or
53 .B EOF
54 on end of file or error.
55 .PP
56 .BR getc ()
57 is equivalent to
58 .BR fgetc ()
59 except that it may be implemented as a macro which evaluates
60 .I stream
61 more than once.
62 .PP
63 .BR getchar ()
64 is equivalent to
65 .BI "getc(" stdin ) \fR.
66 .PP
67 .BR gets ()
68 reads a line from
69 .I stdin
70 into the buffer pointed to by
71 .I s
72 until either a terminating newline or
73 .BR EOF ,
74 which it replaces with a null byte (\(aq\e0\(aq).
75 No check for buffer overrun is performed (see BUGS below).
76 .PP
77 .BR fgets ()
78 reads in at most one less than
79 .I size
80 characters from
81 .I stream
82 and stores them into the buffer pointed to by
83 .IR s .
84 Reading stops after an
85 .B EOF
86 or a newline.
87 If a newline is read, it is stored into the buffer.
88 A terminating null byte (\(aq\e0\(aq)
89 is stored after the last character in the buffer.
90 .PP
91 .BR ungetc ()
92 pushes
93 .I c
94 back to
95 .IR stream ,
96 cast to
97 .IR "unsigned char" ,
98 where it is available for subsequent read operations.
99 Pushed-back characters
100 will be returned in reverse order; only one pushback is guaranteed.
101 .PP
102 Calls to the functions described here can be mixed with each other and with
103 calls to other input functions from the
104 .I stdio
105 library for the same input stream.
106 .PP
107 For nonlocking counterparts, see
108 .BR unlocked_stdio (3).
109 .SH "RETURN VALUE"
110 .BR fgetc (),
111 .BR getc ()
112 and
113 .BR getchar ()
114 return the character read as an
115 .I unsigned char
116 cast to an
117 .I int
118 or
119 .B EOF
120 on end of file or error.
121 .PP
122 .BR gets ()
123 and
124 .BR fgets ()
125 return
126 .I s
127 on success, and NULL
128 on error or when end of file occurs while no characters have been read.
129 .PP
130 .BR ungetc ()
131 returns
132 .I c
133 on success, or
134 .B EOF
135 on error.
136 .SH "CONFORMING TO"
137 C89, C99, POSIX.1-2001.
138
139 LSB deprecates
140 .BR gets ().
141 POSIX.1-2008 marks
142 .BR gets ()
143 obsolescent.
144 ISO C11 removes the specification of
145 .BR gets ()
146 from the C language, and since version 2.16,
147 glibc header files don't expose the function declaration if the
148 .B _ISOC11_SOURCE
149 feature test macro is defined.
150 .SH BUGS
151 Never use
152 .BR gets ().
153 Because it is impossible to tell without knowing the data in advance how many
154 characters
155 .BR gets ()
156 will read, and because
157 .BR gets ()
158 will continue to store characters past the end of the buffer,
159 it is extremely dangerous to use.
160 It has been used to break computer security.
161 Use
162 .BR fgets ()
163 instead.
164 .PP
165 It is not advisable to mix calls to input functions from the
166 .I stdio
167 library with low-level calls to
168 .BR read (2)
169 for the file descriptor associated with the input stream; the results
170 will be undefined and very probably not what you want.
171 .SH "SEE ALSO"
172 .BR read (2),
173 .BR write (2),
174 .BR ferror (3),
175 .BR fgetwc (3),
176 .BR fgetws (3),
177 .BR fopen (3),
178 .BR fread (3),
179 .BR fseek (3),
180 .BR getline (3),
181 .BR getwchar (3),
182 .BR puts (3),
183 .BR scanf (3),
184 .BR ungetwc (3),
185 .BR unlocked_stdio (3),
186 .BR feature_test_macros (7)