Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 8 | */ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 10 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 11 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 12 | { |
| 13 | ssize_t n; |
| 14 | |
Martin Lewis | dd46861 | 2019-09-15 18:13:28 +0200 | [diff] [blame] | 15 | for (;;) { |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 16 | n = read(fd, buf, count); |
Martin Lewis | dd46861 | 2019-09-15 18:13:28 +0200 | [diff] [blame] | 17 | if (n >= 0 || errno != EINTR) |
| 18 | break; |
| 19 | /* Some callers set errno=0, are upset when they see EINTR. |
| 20 | * Returning EINTR is wrong since we retry read(), |
| 21 | * the "error" was transient. |
| 22 | */ |
| 23 | errno = 0; |
| 24 | /* repeat the read() */ |
| 25 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 26 | |
| 27 | return n; |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Read all of the supplied buffer from a file. |
| 32 | * This does multiple reads as necessary. |
| 33 | * Returns the amount read, or -1 on an error. |
| 34 | * A short read is returned on an end of file. |
| 35 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 36 | ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 37 | { |
| 38 | ssize_t cc; |
| 39 | ssize_t total; |
| 40 | |
| 41 | total = 0; |
| 42 | |
| 43 | while (len) { |
| 44 | cc = safe_read(fd, buf, len); |
| 45 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 46 | if (cc < 0) { |
| 47 | if (total) { |
| 48 | /* we already have some! */ |
| 49 | /* user can do another read to know the error code */ |
| 50 | return total; |
| 51 | } |
| 52 | return cc; /* read() returns -1 on failure. */ |
| 53 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 54 | if (cc == 0) |
| 55 | break; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 56 | buf = ((char *)buf) + cc; |
| 57 | total += cc; |
| 58 | len -= cc; |
| 59 | } |
| 60 | |
| 61 | return total; |
| 62 | } |
| 63 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 64 | ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 65 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 66 | /*int e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 67 | size = full_read(fd, buf, size); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 68 | /*e = errno;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 69 | close(fd); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 70 | /*errno = e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 71 | return size; |
| 72 | } |
| 73 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 74 | ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 75 | { |
| 76 | int fd = open(filename, O_RDONLY); |
| 77 | if (fd < 0) |
| 78 | return fd; |
| 79 | return read_close(fd, buf, size); |
| 80 | } |