blob: e0596d5f6dde18ed503e6ef81205e196ae09b1ac [file] [log] [blame]
Eric Andersen55f98722001-05-15 17:48:09 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersen451c0d22005-04-16 07:46:53 +00005 * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
Eric Andersen55f98722001-05-15 17:48:09 +00006 *
Rob Landley21ccbb62005-11-04 01:20:46 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen55f98722001-05-15 17:48:09 +00008 */
9
Eric Andersen451c0d22005-04-16 07:46:53 +000010#include "libbb.h"
Eric Andersen55f98722001-05-15 17:48:09 +000011
Manuel Novoa III cad53642003-03-19 09:13:01 +000012#if BUFSIZ < 4096
13#undef BUFSIZ
14#define BUFSIZ 4096
15#endif
16
Denis Vlasenko99912ca2007-04-10 15:43:37 +000017/* Used by NOFORK applets (e.g. cat) - must be very careful
18 * when calling xfuncs, allocating memory, with signals, termios, etc... */
Glenn L McGrathf0515da2003-11-22 02:13:41 +000019
Denis Vlasenko7039a662006-10-08 17:54:47 +000020static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000021{
Rob Landley21ccbb62005-11-04 01:20:46 +000022 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000023 off_t total = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000024 RESERVE_CONFIG_BUFFER(buffer, BUFSIZ);
Eric Andersen55f98722001-05-15 17:48:09 +000025
Denis Vlasenko99912ca2007-04-10 15:43:37 +000026 if (src_fd < 0)
27 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000029 if (!size) {
30 size = BUFSIZ;
31 status = 1; /* copy until eof */
32 }
Rob Landley90334532005-11-12 11:04:11 +000033
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000034 while (1) {
35 ssize_t rd;
36
37 rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size);
38
Denis Vlasenko714701c2006-12-22 00:21:07 +000039 if (!rd) { /* eof - all done */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000040 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 Landley53437472006-07-16 08:14:35 +000050 if (wr < rd) {
Rob Landley21ccbb62005-11-04 01:20:46 +000051 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000053 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000054 }
55 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +000056 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000057 size -= rd;
58 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +000059 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +000060 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000061 break;
62 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000063 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000064 }
Denis Vlasenko714701c2006-12-22 00:21:07 +000065 out:
Rob Landley21ccbb62005-11-04 01:20:46 +000066 RELEASE_CONFIG_BUFFER(buffer);
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000067 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000068}
69
70
Denis Vlasenko714701c2006-12-22 00:21:07 +000071#if 0
72void 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 Vlasenko3f3aa2a2007-04-09 21:35:07 +000077 sleep_and_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +000078}
79#endif
80
Denis Vlasenko7039a662006-10-08 17:54:47 +000081off_t bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000082{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000083 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +000084 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +000085 }
Denis Vlasenko6231e872006-09-17 15:45:48 +000086 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000087}
88
Denis Vlasenko714701c2006-12-22 00:21:07 +000089void 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 Vlasenko3f3aa2a2007-04-09 21:35:07 +000097 sleep_and_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +000098}
99
Denis Vlasenko7039a662006-10-08 17:54:47 +0000100off_t bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +0000101{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000102 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000103}