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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 28 | #include "libbb.h" |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 29 | |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 30 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | #if BUFSIZ < 4096 |
| 32 | #undef BUFSIZ |
| 33 | #define BUFSIZ 4096 |
| 34 | #endif |
| 35 | |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 37 | static size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size2) |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 38 | { |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 39 | int status; |
| 40 | size_t xread, wrote, total, size = size2; |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 41 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 42 | if ((dst_fd < 0) || (src_fd < 0)) { |
| 43 | return -1; |
| 44 | } |
Matt Kraai | d6ef074 | 2001-05-18 14:14:55 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 46 | if (size == 0) { |
| 47 | /* If size is 0 copy until EOF */ |
| 48 | size = ULONG_MAX; |
| 49 | } |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 50 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 51 | { |
| 52 | RESERVE_CONFIG_BUFFER(buffer,BUFSIZ); |
| 53 | total = 0; |
| 54 | wrote = 0; |
| 55 | status = -1; |
| 56 | while (total < size) |
| 57 | { |
| 58 | xread = BUFSIZ; |
| 59 | if (size < (wrote + BUFSIZ)) |
| 60 | xread = size - wrote; |
| 61 | xread = bb_full_read(src_fd, buffer, xread); |
| 62 | if (xread > 0) { |
| 63 | wrote = bb_full_write(dst_fd, buffer, xread); |
| 64 | if (wrote < xread) { |
| 65 | bb_perror_msg(bb_msg_write_error); |
| 66 | break; |
| 67 | } |
| 68 | total += wrote; |
| 69 | } else if (xread < 0) { |
| 70 | bb_perror_msg(bb_msg_read_error); |
| 71 | break; |
| 72 | } else if (xread == 0) { |
| 73 | /* All done. */ |
| 74 | status = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | break; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 76 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 77 | } |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 78 | RELEASE_CONFIG_BUFFER(buffer); |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 79 | } |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 80 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame^] | 81 | if (status == 0 || wrote) |
| 82 | return wrote; |
| 83 | /* Some sortof error occured */ |
| 84 | return -1; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | extern int bb_copyfd_size(int fd1, int fd2, const off_t size) |
| 89 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 90 | if (size) { |
Glenn L McGrath | 15c3512 | 2004-02-21 09:20:56 +0000 | [diff] [blame] | 91 | return(bb_full_fd_action(fd1, fd2, size)); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 92 | } |
| 93 | return(0); |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | extern int bb_copyfd_eof(int fd1, int fd2) |
| 97 | { |
Glenn L McGrath | 15c3512 | 2004-02-21 09:20:56 +0000 | [diff] [blame] | 98 | return(bb_full_fd_action(fd1, fd2, 0)); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 99 | } |