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 | |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 16 | |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 17 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 18 | #if BUFSIZ < 4096 |
| 19 | #undef BUFSIZ |
| 20 | #define BUFSIZ 4096 |
| 21 | #endif |
| 22 | |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 23 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 24 | 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] | 25 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 26 | int status = -1; |
| 27 | size_t total = 0; |
| 28 | RESERVE_CONFIG_BUFFER(buffer,BUFSIZ); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 29 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 30 | if (src_fd < 0) goto out; |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 31 | while (!size || total < size) |
Eric Andersen | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 32 | { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 33 | ssize_t wr, rd; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 34 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 35 | rd = safe_read(src_fd, buffer, |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 36 | (!size || size - total > BUFSIZ) ? BUFSIZ : size - total); |
| 37 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 38 | if (rd > 0) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 39 | /* A -1 dst_fd means we need to fake it... */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 40 | wr = (dst_fd < 0) ? rd : full_write(dst_fd, buffer, rd); |
| 41 | if (wr < rd) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 42 | bb_perror_msg(bb_msg_write_error); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | break; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 44 | } |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 45 | total += wr; |
Rob Landley | 90632d0 | 2006-02-13 19:40:43 +0000 | [diff] [blame] | 46 | if (total == size) status = 0; |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 47 | } else if (rd < 0) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 48 | bb_perror_msg(bb_msg_read_error); |
| 49 | break; |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 50 | } else if (rd == 0) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 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 | |
"Vladimir N. Oleynik" | 7600eec | 2006-01-31 12:12:15 +0000 | [diff] [blame] | 60 | return status ? status : (ssize_t)total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 64 | int bb_copyfd_size(int fd1, int fd2, const off_t size) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 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 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 72 | int bb_copyfd_eof(int fd1, int fd2) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 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 | } |