diff options
author | Rob Landley <rob@landley.net> | 2006-07-16 08:14:35 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2006-07-16 08:14:35 +0000 |
commit | 534374755d618c9c36c9940c82756241c4b25a67 (patch) | |
tree | fac906b4fa40a68c53cecf20215a7a25b3b1cab6 /libbb/xfuncs.c | |
parent | afb94ecf2bb6c53ce2a381d6ce45a426243c76d9 (diff) | |
download | busybox-w32-534374755d618c9c36c9940c82756241c4b25a67.tar.gz busybox-w32-534374755d618c9c36c9940c82756241c4b25a67.tar.bz2 busybox-w32-534374755d618c9c36c9940c82756241c4b25a67.zip |
Cleaup read() and write() variants, plus a couple of new functions like
xlseek and fdlength() for the new mkswap.
Diffstat (limited to 'libbb/xfuncs.c')
-rw-r--r-- | libbb/xfuncs.c | 85 |
1 files changed, 69 insertions, 16 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index d843414f9..8562a4fcb 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -120,40 +120,55 @@ int bb_xopen3(const char *pathname, int flags, int mode) | |||
120 | #endif | 120 | #endif |
121 | 121 | ||
122 | #ifdef L_xread | 122 | #ifdef L_xread |
123 | ssize_t bb_xread(int fd, void *buf, size_t count) | 123 | |
124 | // Die with an error message if we can't read the entire buffer. | ||
125 | |||
126 | void xread(int fd, void *buf, size_t count) | ||
124 | { | 127 | { |
125 | ssize_t size; | 128 | while (count) { |
129 | ssize_t size; | ||
126 | 130 | ||
127 | size = read(fd, buf, count); | 131 | if ((size = safe_read(fd, buf, count)) < 1) |
128 | if (size < 0) { | 132 | bb_error_msg_and_die("Short read"); |
129 | bb_perror_msg_and_die(bb_msg_read_error); | 133 | count -= size; |
134 | buf = ((char *) buf) + size; | ||
130 | } | 135 | } |
131 | return(size); | ||
132 | } | 136 | } |
133 | #endif | 137 | #endif |
134 | 138 | ||
135 | #ifdef L_xread_all | 139 | #ifdef L_xwrite |
136 | void bb_xread_all(int fd, void *buf, size_t count) | ||
137 | { | ||
138 | ssize_t size; | ||
139 | 140 | ||
141 | // Die with an error message if we can't write the entire buffer. | ||
142 | |||
143 | void xwrite(int fd, void *buf, size_t count) | ||
144 | { | ||
140 | while (count) { | 145 | while (count) { |
141 | if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */ | 146 | ssize_t size; |
142 | bb_error_msg_and_die("Short read"); | 147 | |
143 | } | 148 | if ((size = safe_write(fd, buf, count)) < 1) |
149 | bb_error_msg_and_die("Short write"); | ||
144 | count -= size; | 150 | count -= size; |
145 | buf = ((char *) buf) + size; | 151 | buf = ((char *) buf) + size; |
146 | } | 152 | } |
147 | return; | 153 | } |
154 | #endif | ||
155 | |||
156 | #ifdef L_xlseek | ||
157 | |||
158 | // Die if we can't lseek to the right spot. | ||
159 | |||
160 | void xlseek(int fd, off_t offset, int whence) | ||
161 | { | ||
162 | if (whence != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek"); | ||
148 | } | 163 | } |
149 | #endif | 164 | #endif |
150 | 165 | ||
151 | #ifdef L_xread_char | 166 | #ifdef L_xread_char |
152 | unsigned char bb_xread_char(int fd) | 167 | unsigned char xread_char(int fd) |
153 | { | 168 | { |
154 | char tmp; | 169 | char tmp; |
155 | 170 | ||
156 | bb_xread_all(fd, &tmp, 1); | 171 | xread(fd, &tmp, 1); |
157 | 172 | ||
158 | return(tmp); | 173 | return(tmp); |
159 | } | 174 | } |
@@ -294,3 +309,41 @@ void xsetuid(uid_t uid) | |||
294 | if (setuid(uid)) bb_error_msg_and_die("setuid"); | 309 | if (setuid(uid)) bb_error_msg_and_die("setuid"); |
295 | } | 310 | } |
296 | #endif | 311 | #endif |
312 | |||
313 | #ifdef L_fdlength | ||
314 | off_t fdlength(int fd) | ||
315 | { | ||
316 | off_t bottom = 0, top = 0, pos; | ||
317 | long size; | ||
318 | |||
319 | // If the ioctl works for this, return it. | ||
320 | |||
321 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; | ||
322 | |||
323 | // If not, do a binary search for the last location we can read. | ||
324 | |||
325 | do { | ||
326 | char temp; | ||
327 | |||
328 | pos = bottom + (top - bottom) / 2;; | ||
329 | |||
330 | // If we can read from the current location, it's bigger. | ||
331 | |||
332 | if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { | ||
333 | if (bottom == top) bottom = top = (top+1) * 2; | ||
334 | else bottom = pos; | ||
335 | |||
336 | // If we can't, it's smaller. | ||
337 | |||
338 | } else { | ||
339 | if (bottom == top) { | ||
340 | if (!top) return 0; | ||
341 | bottom = top/2; | ||
342 | } | ||
343 | else top = pos; | ||
344 | } | ||
345 | } while (bottom + 1 != top); | ||
346 | |||
347 | return pos + 1; | ||
348 | } | ||
349 | #endif | ||