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 | * |
| 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 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 | |
| 15 | do { |
| 16 | n = read(fd, buf, count); |
| 17 | } while (n < 0 && errno == EINTR); |
| 18 | |
| 19 | return n; |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | * Read all of the supplied buffer from a file. |
| 24 | * This does multiple reads as necessary. |
| 25 | * Returns the amount read, or -1 on an error. |
| 26 | * A short read is returned on an end of file. |
| 27 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 28 | 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] | 29 | { |
| 30 | ssize_t cc; |
| 31 | ssize_t total; |
| 32 | |
| 33 | total = 0; |
| 34 | |
| 35 | while (len) { |
| 36 | cc = safe_read(fd, buf, len); |
| 37 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 38 | if (cc < 0) { |
| 39 | if (total) { |
| 40 | /* we already have some! */ |
| 41 | /* user can do another read to know the error code */ |
| 42 | return total; |
| 43 | } |
| 44 | return cc; /* read() returns -1 on failure. */ |
| 45 | } |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 46 | if (cc == 0) |
| 47 | break; |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 48 | buf = ((char *)buf) + cc; |
| 49 | total += cc; |
| 50 | len -= cc; |
| 51 | } |
| 52 | |
| 53 | return total; |
| 54 | } |
| 55 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 56 | 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] | 57 | { |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 58 | /*int e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 59 | size = full_read(fd, buf, size); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 60 | /*e = errno;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 61 | close(fd); |
Denis Vlasenko | 98ebab8 | 2007-06-30 14:47:41 +0000 | [diff] [blame] | 62 | /*errno = e;*/ |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 63 | return size; |
| 64 | } |
| 65 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 66 | 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] | 67 | { |
| 68 | int fd = open(filename, O_RDONLY); |
| 69 | if (fd < 0) |
| 70 | return fd; |
| 71 | return read_close(fd, buf, size); |
| 72 | } |