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 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 24 | 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] | 25 | { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 26 | int status = -1; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 27 | off_t total = 0; |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 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; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 32 | if (!size) { |
| 33 | size = BUFSIZ; |
| 34 | status = 1; /* copy until eof */ |
| 35 | } |
Rob Landley | 9033453 | 2005-11-12 11:04:11 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 37 | while (1) { |
| 38 | ssize_t rd; |
| 39 | |
| 40 | rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size); |
| 41 | |
| 42 | if (!rd) { /* eof - all done. */ |
| 43 | status = 0; |
| 44 | break; |
| 45 | } |
| 46 | if (rd < 0) { |
| 47 | bb_perror_msg(bb_msg_read_error); |
| 48 | break; |
| 49 | } |
| 50 | /* dst_fd == -1 is a fake, else... */ |
| 51 | if (dst_fd >= 0) { |
| 52 | ssize_t wr = full_write(dst_fd, buffer, rd); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 53 | if (wr < rd) { |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 54 | bb_perror_msg(bb_msg_write_error); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | break; |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 56 | } |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 57 | } |
| 58 | total += rd; |
| 59 | if (status < 0) { |
| 60 | size -= rd; |
| 61 | if (!size) { |
| 62 | status = 0; |
| 63 | break; |
| 64 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 65 | } |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 66 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 67 | |
Rob Landley | 21ccbb6 | 2005-11-04 01:20:46 +0000 | [diff] [blame] | 68 | out: |
| 69 | RELEASE_CONFIG_BUFFER(buffer); |
Glenn L McGrath | 25fe94f | 2002-12-13 08:20:44 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 2d27e4c | 2006-11-25 23:50:28 +0000 | [diff] [blame] | 71 | return status ? -1 : total; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 75 | 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] | 76 | { |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 77 | if (size) { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 78 | return bb_full_fd_action(fd1, fd2, size); |
Glenn L McGrath | c3498f6 | 2003-11-24 23:50:07 +0000 | [diff] [blame] | 79 | } |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 80 | return 0; |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 83 | off_t bb_copyfd_eof(int fd1, int fd2) |
Glenn L McGrath | 7ffe133 | 2003-11-21 22:24:57 +0000 | [diff] [blame] | 84 | { |
Denis Vlasenko | 6231e87 | 2006-09-17 15:45:48 +0000 | [diff] [blame] | 85 | return bb_full_fd_action(fd1, fd2, 0); |
Eric Andersen | 55f9872 | 2001-05-15 17:48:09 +0000 | [diff] [blame] | 86 | } |