OSDN Git Service

c166ef04af6a1b04130c99fda1f30d15b4069886
[linuxjm/LDP_man-pages.git] / original / man2 / read.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\"                               1993 Michael Haardt, Ian Jackson.
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Modified Sat Jul 24 00:06:00 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt
28 .\"   <michael@cantor.informatik.rwth-aachen.de>
29 .\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
31 .\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt
33 .\"   <michael@cantor.informatik.rwth-aachen.de>
34 .\"
35 .TH READ 2 2009-02-23 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 read \- read from a file descriptor
38 .SH SYNOPSIS
39 .nf
40 .B #include <unistd.h>
41 .sp
42 .BI "ssize_t read(int " fd ", void *" buf ", size_t " count );
43 .fi
44 .SH DESCRIPTION
45 .BR read ()
46 attempts to read up to
47 .I count
48 bytes from file descriptor
49 .I fd
50 into the buffer starting at
51 .IR buf .
52 .PP
53 If
54 .I count
55 is zero,
56 .BR read ()
57 returns zero and has no other results.
58 If
59 .I count
60 is greater than
61 .BR SSIZE_MAX ,
62 the result is unspecified.
63 .SH "RETURN VALUE"
64 On success, the number of bytes read is returned (zero indicates end of
65 file), and the file position is advanced by this number.
66 It is not an error if this number is smaller than the number of bytes
67 requested; this may happen for example because fewer bytes are actually
68 available right now (maybe because we were close to end-of-file, or
69 because we are reading from a pipe, or from a terminal), or because
70 .BR read ()
71 was interrupted by a signal.
72 On error, \-1 is returned, and
73 .I errno
74 is set appropriately.
75 In this case it is left unspecified whether
76 the file position (if any) changes.
77 .SH ERRORS
78 .TP
79 .B EAGAIN
80 The file descriptor
81 .I fd
82 refers to a file other than a socket and has been marked nonblocking
83 .RB ( O_NONBLOCK ),
84 and the read would block.
85 .TP
86 .BR EAGAIN " or " EWOULDBLOCK
87 .\" Actually EAGAIN on Linux
88 The file descriptor
89 .I fd
90 refers to a socket and has been marked nonblocking
91 .RB ( O_NONBLOCK ),
92 and the read would block.
93 POSIX.1-2001 allows either error to be returned for this case,
94 and does not require these constants to have the same value,
95 so a portable application should check for both possibilities.
96 .TP
97 .B EBADF
98 .I fd
99 is not a valid file descriptor or is not open for reading.
100 .TP
101 .B EFAULT
102 .I buf
103 is outside your accessible address space.
104 .TP
105 .B EINTR
106 The call was interrupted by a signal before any data was read; see
107 .BR signal (7).
108 .TP
109 .B EINVAL
110 .I fd
111 is attached to an object which is unsuitable for reading;
112 or the file was opened with the
113 .B O_DIRECT
114 flag, and either the address specified in
115 .IR buf ,
116 the value specified in
117 .IR count ,
118 or the current file offset is not suitably aligned.
119 .TP
120 .B EINVAL
121 .I fd
122 was created via a call to
123 .BR timerfd_create (2)
124 and the wrong size buffer was given to
125 .BR read ();
126 see
127 .BR timerfd_create (2)
128 for further information.
129 .TP
130 .B EIO
131 I/O error.
132 This will happen for example when the process is in a
133 background process group, tries to read from its controlling tty,
134 and either it is ignoring or blocking
135 .B SIGTTIN
136 or its process group
137 is orphaned.
138 It may also occur when there is a low-level I/O error
139 while reading from a disk or tape.
140 .TP
141 .B EISDIR
142 .I fd
143 refers to a directory.
144 .PP
145 Other errors may occur, depending on the object connected to
146 .IR fd .
147 POSIX allows a
148 .BR read ()
149 that is interrupted after reading some data
150 to return \-1 (with
151 .I errno
152 set to
153 .BR EINTR )
154 or to return the number of bytes already read.
155 .SH "CONFORMING TO"
156 SVr4, 4.3BSD, POSIX.1-2001.
157 .SH NOTES
158 On NFS file systems, reading small amounts of data will only update the
159 timestamp the first time, subsequent calls may not do so.
160 This is caused
161 by client side attribute caching, because most if not all NFS clients
162 leave st_atime (last file access time)
163 updates to the server and client side reads satisfied from the
164 client's cache will not cause st_atime updates on the server as there are no
165 server side reads.
166 Unix semantics can be obtained by disabling client
167 side attribute caching, but in most situations this will substantially
168 increase server load and decrease performance.
169 .PP
170 Many file systems and disks were considered to be fast enough that the
171 implementation of
172 .B O_NONBLOCK
173 was deemed unnecessary.
174 So,
175 .B O_NONBLOCK
176 may not be available on files
177 and/or disks.
178 .SH "SEE ALSO"
179 .BR close (2),
180 .BR fcntl (2),
181 .BR ioctl (2),
182 .BR lseek (2),
183 .BR open (2),
184 .BR pread (2),
185 .BR readdir (2),
186 .BR readlink (2),
187 .BR readv (2),
188 .BR select (2),
189 .BR write (2),
190 .BR fread (3)