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