1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 ssize_t safe_read(int fd, void *buf, size_t count)
17 n = read(fd, buf, count);
18 } while (n < 0 && errno == EINTR);
23 /* Suppose that you are a shell. You start child processes.
24 * They work and eventually exit. You want to get user input.
25 * You read stdin. But what happens if last child switched
26 * its stdin into O_NONBLOCK mode?
28 * *** SURPRISE! It will affect the parent too! ***
29 * *** BIG SURPRISE! It stays even after child exits! ***
31 * This is a design bug in UNIX API.
32 * fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK);
33 * will set nonblocking mode not only on _your_ stdin, but
34 * also on stdin of your parent, etc.
38 * fcntl(fd2, F_SETFL, fcntl(fd2, F_GETFL, 0) | O_NONBLOCK);
39 * sets both fd1 and fd2 to O_NONBLOCK. This includes cases
40 * where duping is done implicitly by fork() etc.
43 * fcntl(fd2, F_SETFD, fcntl(fd2, F_GETFD, 0) | O_NONBLOCK);
44 * (note SETFD, not SETFL!) but such thing doesn't exist.
46 * Alternatively, we need nonblocking_read(fd, ...) which doesn't
47 * require O_NONBLOCK dance at all. Actually, it exists:
48 * n = recv(fd, buf, len, MSG_DONTWAIT);
50 * Enables non-blocking operation; if the operation
51 * would block, EAGAIN is returned."
52 * but recv() works only for sockets!
54 * So far I don't see any good solution, I can only propose
55 * that affected readers should be careful and use this routine,
56 * which detects EAGAIN and uses poll() to wait on the fd.
57 * Thankfully, poll() doesn't care about O_NONBLOCK flag.
59 ssize_t nonblock_safe_read(int fd, void *buf, size_t count)
65 n = safe_read(fd, buf, count);
66 if (n >= 0 || errno != EAGAIN)
68 /* fd is in O_NONBLOCK mode. Wait using poll and repeat */
70 pfd[0].events = POLLIN;
71 safe_poll(pfd, 1, -1);
76 * Read all of the supplied buffer from a file.
77 * This does multiple reads as necessary.
78 * Returns the amount read, or -1 on an error.
79 * A short read is returned on an end of file.
81 ssize_t full_read(int fd, void *buf, size_t len)
89 cc = safe_read(fd, buf, len);
93 /* we already have some! */
94 /* user can do another read to know the error code */
97 return cc; /* read() returns -1 on failure. */
101 buf = ((char *)buf) + cc;
109 // Die with an error message if we can't read the entire buffer.
110 void xread(int fd, void *buf, size_t count)
113 ssize_t size = full_read(fd, buf, count);
115 bb_error_msg_and_die("short read");
119 // Die with an error message if we can't read one character.
120 unsigned char xread_char(int fd)
127 // Read one line a-la fgets. Works only on seekable streams
128 char *reads(int fd, char *buffer, size_t size)
134 size = full_read(fd, buffer, size-1);
135 if ((ssize_t)size <= 0)
139 p = strchr(buffer, '\n');
143 // avoid incorrect (unsigned) widening
144 offset = (off_t)(p - buffer) - (off_t)size;
145 // set fd position right after '\n'
146 if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
152 // Reads one line a-la fgets (but doesn't save terminating '\n').
153 // Reads byte-by-byte. Useful when it is important to not read ahead.
154 // Bytes are appended to pfx (which must be malloced, or NULL).
155 char *xmalloc_reads(int fd, char *buf, size_t *maxsz_p)
158 size_t sz = buf ? strlen(buf) : 0;
159 size_t maxsz = maxsz_p ? *maxsz_p : MAXINT(size_t);
165 buf = xrealloc(buf, sz + 128);
169 /* nonblock_safe_read() because we are used by e.g. shells */
170 if (nonblock_safe_read(fd, p, 1) != 1) { /* EOF/error */
171 if (p == buf) { /* we read nothing */
185 return xrealloc(buf, p - buf);
188 ssize_t read_close(int fd, void *buf, size_t size)
191 size = full_read(fd, buf, size);
198 ssize_t open_read_close(const char *filename, void *buf, size_t size)
200 int fd = open(filename, O_RDONLY);
203 return read_close(fd, buf, size);
206 // Read (potentially big) files in one go. File size is estimated by
208 void *xmalloc_open_read_close(const char *filename, size_t *sizep)
211 size_t size = sizep ? *sizep : INT_MAX;
215 fd = xopen(filename, O_RDONLY);
216 /* /proc/N/stat files report len 0 here */
217 /* In order to make such files readable, we add small const */
218 len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */
219 xlseek(fd, 0, SEEK_SET);
222 buf = xmalloc(size + 1);
223 size = read_close(fd, buf, size);
224 if ((ssize_t)size < 0)
225 bb_perror_msg_and_die("'%s'", filename);
226 xrealloc(buf, size + 1);