Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/libbb/read.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/libbb/read.c
r1765 r2725 5 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 6 6 * 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 8 8 */ 9 10 9 #include "libbb.h" 11 10 12 ssize_t safe_read(int fd, void *buf, size_t count)11 ssize_t FAST_FUNC safe_read(int fd, void *buf, size_t count) 13 12 { 14 13 ssize_t n; … … 27 26 * A short read is returned on an end of file. 28 27 */ 29 ssize_t full_read(int fd, void *buf, size_t len)28 ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len) 30 29 { 31 30 ssize_t cc; … … 37 36 cc = safe_read(fd, buf, len); 38 37 39 if (cc < 0) 40 return cc; /* read() returns -1 on failure. */ 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 } 41 46 if (cc == 0) 42 47 break; … … 49 54 } 50 55 51 // Die with an error message if we can't read the entire buffer. 52 void xread(int fd, void *buf, size_t count) 53 { 54 if (count) { 55 ssize_t size = full_read(fd, buf, count); 56 if (size != count) 57 bb_error_msg_and_die("short read"); 58 } 59 } 60 61 // Die with an error message if we can't read one character. 62 unsigned char xread_char(int fd) 63 { 64 char tmp; 65 xread(fd, &tmp, 1); 66 return tmp; 67 } 68 69 // Read one line a-la fgets. Works only on seekable streams 70 char *reads(int fd, char *buffer, size_t size) 71 { 72 char *p; 73 74 if (size < 2) 75 return NULL; 76 size = full_read(fd, buffer, size-1); 77 if ((ssize_t)size <= 0) 78 return NULL; 79 80 buffer[size] = '\0'; 81 p = strchr(buffer, '\n'); 82 if (p) { 83 off_t offset; 84 *p++ = '\0'; 85 // avoid incorrect (unsigned) widening 86 offset = (off_t)(p-buffer) - (off_t)size; 87 // set fd position right after '\n' 88 if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) 89 return NULL; 90 } 91 return buffer; 92 } 93 94 // Read one line a-la fgets. Reads byte-by-byte. 95 // Useful when it is important to not read ahead. 96 char *xmalloc_reads(int fd, char *buf) 97 { 98 char *p; 99 int sz = buf ? strlen(buf) : 0; 100 101 goto jump_in; 102 while (1) { 103 if (p - buf == sz) { 104 jump_in: 105 buf = xrealloc(buf, sz + 128); 106 p = buf + sz; 107 sz += 128; 108 } 109 if (safe_read(fd, p, 1) != 1) { /* EOF/error */ 110 if (p == buf) { 111 /* we read nothing [and buf was NULL initially] */ 112 free(buf); 113 return NULL; 114 } 115 break; 116 } 117 if (*p == '\n') 118 break; 119 p++; 120 } 121 *p++ = '\0'; 122 return xrealloc(buf, p - buf); 123 } 124 125 ssize_t read_close(int fd, void *buf, size_t size) 56 ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) 126 57 { 127 58 /*int e;*/ … … 133 64 } 134 65 135 ssize_t open_read_close(const char *filename, void *buf, size_t size)66 ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) 136 67 { 137 68 int fd = open(filename, O_RDONLY); … … 140 71 return read_close(fd, buf, size); 141 72 } 142 143 // Read (potentially big) files in one go. File size is estimated by144 // lseek to end.145 void *xmalloc_open_read_close(const char *filename, size_t *sizep)146 {147 char *buf;148 size_t size = sizep ? *sizep : INT_MAX;149 int fd;150 off_t len;151 152 fd = xopen(filename, O_RDONLY);153 /* /proc/N/stat files report len 0 here */154 /* In order to make such files readable, we add small const */155 len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */156 xlseek(fd, 0, SEEK_SET);157 if (len < size)158 size = len;159 buf = xmalloc(size + 1);160 size = read_close(fd, buf, size);161 if ((ssize_t)size < 0)162 bb_perror_msg_and_die("'%s'", filename);163 xrealloc(buf, size + 1);164 buf[size] = '\0';165 if (sizep)166 *sizep = size;167 return buf;168 }
Note:
See TracChangeset
for help on using the changeset viewer.