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 | 451c0d2 | 2005-04-16 07:46:53 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 11 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | #if BUFSIZ < 4096 |
| 13 | #undef BUFSIZ |
| 14 | #define BUFSIZ 4096 |
| 15 | #endif |
| 16 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 17 | /* Used by NOFORK applets (e.g. cat) - must not use xmalloc */ |
Glenn L McGrath | f0515da | 2003-11-22 02:13:41 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 19 | static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 20 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 21 | int status = -1; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 22 | off_t total = 0; |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 23 | char buffer[BUFSIZ]; |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 25 | if (src_fd < 0) |
| 26 | goto out; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 28 | if (!size) { |
| 29 | size = BUFSIZ; |
| 30 | status = 1; /* copy until eof */ |
| 31 | } |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 33 | while (1) { |
| 34 | ssize_t rd; |
| 35 | |
| 36 | rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size); |
| 37 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 38 | if (!rd) { /* eof - all done */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 39 | status = 0; |
| 40 | break; |
| 41 | } |
| 42 | if (rd < 0) { |
| 43 | bb_perror_msg(bb_msg_read_error); |
| 44 | break; |
| 45 | } |
| 46 | /* dst_fd == -1 is a fake, else... */ |
| 47 | if (dst_fd >= 0) { |
| 48 | ssize_t wr = full_write(dst_fd, buffer, rd); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 49 | if (wr < rd) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 50 | bb_perror_msg(bb_msg_write_error); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | break; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 52 | } |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 53 | } |
| 54 | total += rd; |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 55 | if (status < 0) { /* if we aren't copying till EOF... */ |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 56 | size -= rd; |
| 57 | if (!size) { |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 58 | /* 'size' bytes copied - all done */ |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 59 | status = 0; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 60 | break; |
| 61 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 62 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 63 | } |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 64 | out: |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 65 | return status ? -1 : total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 69 | #if 0 |
| 70 | void complain_copyfd_and_die(off_t sz) |
| 71 | { |
| 72 | if (sz != -1) |
| 73 | bb_error_msg_and_die("short read"); |
| 74 | /* if sz == -1, bb_copyfd_XX already complained */ |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 75 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 76 | } |
| 77 | #endif |
| 78 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 79 | off_t bb_copyfd_size(int fd1, int fd2, off_t size) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 80 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 81 | if (size) { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 82 | return bb_full_fd_action(fd1, fd2, size); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 83 | } |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 84 | return 0; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 87 | void bb_copyfd_exact_size(int fd1, int fd2, off_t size) |
| 88 | { |
| 89 | off_t sz = bb_copyfd_size(fd1, fd2, size); |
| 90 | if (sz == size) |
| 91 | return; |
| 92 | if (sz != -1) |
| 93 | bb_error_msg_and_die("short read"); |
| 94 | /* if sz == -1, bb_copyfd_XX already complained */ |
Denis Vlasenko | 335b63d | 2007-04-10 21:38:30 +0000 | [diff] [blame] | 95 | xfunc_die(); |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 98 | off_t bb_copyfd_eof(int fd1, int fd2) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 100 | return bb_full_fd_action(fd1, fd2, 0); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 101 | } |