1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 /* Used by NOFORK applets (e.g. cat) - must be very careful
18 * when calling xfuncs, allocating memory, with signals, termios, etc... */
20 static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
24 RESERVE_CONFIG_BUFFER(buffer, BUFSIZ);
31 status = 1; /* copy until eof */
37 rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size);
39 if (!rd) { /* eof - all done */
44 bb_perror_msg(bb_msg_read_error);
47 /* dst_fd == -1 is a fake, else... */
49 ssize_t wr = full_write(dst_fd, buffer, rd);
51 bb_perror_msg(bb_msg_write_error);
56 if (status < 0) { /* if we aren't copying till EOF... */
59 /* 'size' bytes copied - all done */
66 RELEASE_CONFIG_BUFFER(buffer);
67 return status ? -1 : total;
72 void complain_copyfd_and_die(off_t sz)
75 bb_error_msg_and_die("short read");
76 /* if sz == -1, bb_copyfd_XX already complained */
81 off_t bb_copyfd_size(int fd1, int fd2, off_t size)
84 return bb_full_fd_action(fd1, fd2, size);
89 void bb_copyfd_exact_size(int fd1, int fd2, off_t size)
91 off_t sz = bb_copyfd_size(fd1, fd2, size);
95 bb_error_msg_and_die("short read");
96 /* if sz == -1, bb_copyfd_XX already complained */
100 off_t bb_copyfd_eof(int fd1, int fd2)
102 return bb_full_fd_action(fd1, fd2, 0);