OSDN Git Service

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