lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [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 source tree. |
| 8 | */ |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <errno.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #define FAST_FUNC |
| 18 | |
| 19 | ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) |
| 20 | { |
| 21 | ssize_t n; |
| 22 | |
| 23 | for (;;) { |
| 24 | n = read(fd, buf, count); |
| 25 | if (n >= 0 || errno != EINTR) |
| 26 | break; |
| 27 | /* Some callers set errno=0, are upset when they see EINTR. |
| 28 | * Returning EINTR is wrong since we retry read(), |
| 29 | * the "error" was transient. |
| 30 | */ |
| 31 | errno = 0; |
| 32 | /* repeat the read() */ |
| 33 | } |
| 34 | |
| 35 | return n; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Read all of the supplied buffer from a file. |
| 40 | * This does multiple reads as necessary. |
| 41 | * Returns the amount read, or -1 on an error. |
| 42 | * A short read is returned on an end of file. |
| 43 | */ |
| 44 | ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) |
| 45 | { |
| 46 | ssize_t cc; |
| 47 | ssize_t total; |
| 48 | |
| 49 | total = 0; |
| 50 | |
| 51 | while (len) { |
| 52 | cc = safe_read(fd, buf, len); |
| 53 | |
| 54 | if (cc < 0) { |
| 55 | if (total) { |
| 56 | /* we already have some! */ |
| 57 | /* user can do another read to know the error code */ |
| 58 | return total; |
| 59 | } |
| 60 | return cc; /* read() returns -1 on failure. */ |
| 61 | } |
| 62 | if (cc == 0) |
| 63 | break; |
| 64 | buf = ((char *)buf) + cc; |
| 65 | total += cc; |
| 66 | len -= cc; |
| 67 | } |
| 68 | |
| 69 | return total; |
| 70 | } |
| 71 | |
| 72 | ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) |
| 73 | { |
| 74 | /*int e;*/ |
| 75 | size = full_read(fd, buf, size); |
| 76 | /*e = errno;*/ |
| 77 | close(fd); |
| 78 | /*errno = e;*/ |
| 79 | return size; |
| 80 | } |
| 81 | |
| 82 | ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) |
| 83 | { |
| 84 | int fd = open(filename, O_RDONLY); |
| 85 | if (fd < 0) |
| 86 | return fd; |
| 87 | return read_close(fd, buf, size); |
| 88 | } |
| 89 | |
| 90 | ssize_t FAST_FUNC safe_write(int fd, const void *buf, size_t count) |
| 91 | { |
| 92 | ssize_t n; |
| 93 | |
| 94 | for (;;) { |
| 95 | n = write(fd, buf, count); |
| 96 | if (n >= 0 || errno != EINTR) |
| 97 | break; |
| 98 | /* Some callers set errno=0, are upset when they see EINTR. |
| 99 | * Returning EINTR is wrong since we retry write(), |
| 100 | * the "error" was transient. |
| 101 | */ |
| 102 | errno = 0; |
| 103 | /* repeat the write() */ |
| 104 | } |
| 105 | |
| 106 | return n; |
| 107 | } |
| 108 | |
| 109 | ssize_t FAST_FUNC full_write(int fd, const void *buf, size_t len) |
| 110 | { |
| 111 | ssize_t cc; |
| 112 | ssize_t total; |
| 113 | |
| 114 | total = 0; |
| 115 | |
| 116 | while (len) { |
| 117 | cc = safe_write(fd, buf, len); |
| 118 | |
| 119 | if (cc < 0) { |
| 120 | if (total) { |
| 121 | /* we already wrote some! */ |
| 122 | /* user can do another write to know the error code */ |
| 123 | return total; |
| 124 | } |
| 125 | return cc; /* write() returns -1 on failure. */ |
| 126 | } |
| 127 | |
| 128 | total += cc; |
| 129 | buf = ((const char *)buf) + cc; |
| 130 | len -= cc; |
| 131 | } |
| 132 | |
| 133 | return total; |
| 134 | } |