Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 10 | #include <errno.h> |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 17 | |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | #if BUFSIZ < 4096 |
| 20 | #undef BUFSIZ |
| 21 | #define BUFSIZ 4096 |
| 22 | #endif |
| 23 | |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 24 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 25 | static ssize_t bb_full_fd_action(int src_fd, int dst_fd, size_t size) |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 26 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 27 | int status = -1; |
| 28 | size_t total = 0; |
| 29 | RESERVE_CONFIG_BUFFER(buffer,BUFSIZ); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 30 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 31 | if (src_fd < 0) goto out; |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 32 | while (!size || total < size) |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 33 | { |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 34 | ssize_t wrote, xread; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame^] | 35 | |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 36 | xread = safe_read(src_fd, buffer, |
| 37 | (!size || size - total > BUFSIZ) ? BUFSIZ : size - total); |
| 38 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 39 | if (xread > 0) { |
| 40 | /* A -1 dst_fd means we need to fake it... */ |
| 41 | wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread); |
| 42 | if (wrote < xread) { |
| 43 | bb_perror_msg(bb_msg_write_error); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | break; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 45 | } |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 46 | total += wrote; |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 47 | } else if (xread < 0) { |
| 48 | bb_perror_msg(bb_msg_read_error); |
| 49 | break; |
| 50 | } else if (xread == 0) { |
| 51 | /* All done. */ |
| 52 | status = 0; |
| 53 | break; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 54 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 55 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame^] | 56 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 57 | out: |
| 58 | RELEASE_CONFIG_BUFFER(buffer); |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 59 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 60 | return status ? status : total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | extern int bb_copyfd_size(int fd1, int fd2, const off_t size) |
| 65 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 66 | if (size) { |
Glenn L McGrath | 15c3512 | 2004-02-21 09:20:56 +0000 | [diff] [blame] | 67 | return(bb_full_fd_action(fd1, fd2, size)); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 68 | } |
| 69 | return(0); |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | extern int bb_copyfd_eof(int fd1, int fd2) |
| 73 | { |
Glenn L McGrath | 15c3512 | 2004-02-21 09:20:56 +0000 | [diff] [blame] | 74 | return(bb_full_fd_action(fd1, fd2, 0)); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 75 | } |