blob: 3255e424a0009e55e33531c8a91cf03ba93cf7f1 [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 Vlasenko50f7f442007-04-11 23:20:53 +000017/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
Glenn L McGrathf0515da2003-11-22 02:13:41 +000018
Denis Vlasenko7039a662006-10-08 17:54:47 +000019static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000020{
Rob Landley21ccbb62005-11-04 01:20:46 +000021 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000022 off_t total = 0;
Denis Vlasenko50f7f442007-04-11 23:20:53 +000023 char buffer[BUFSIZ];
Eric Andersen55f98722001-05-15 17:48:09 +000024
Denis Vlasenko99912ca2007-04-10 15:43:37 +000025 if (src_fd < 0)
26 goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000027
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000028 if (!size) {
29 size = BUFSIZ;
30 status = 1; /* copy until eof */
31 }
Rob Landley90334532005-11-12 11:04:11 +000032
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000033 while (1) {
34 ssize_t rd;
35
36 rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size);
37
Denis Vlasenko714701c2006-12-22 00:21:07 +000038 if (!rd) { /* eof - all done */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000039 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 Landley53437472006-07-16 08:14:35 +000049 if (wr < rd) {
Rob Landley21ccbb62005-11-04 01:20:46 +000050 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000052 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000053 }
54 total += rd;
Denis Vlasenko714701c2006-12-22 00:21:07 +000055 if (status < 0) { /* if we aren't copying till EOF... */
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000056 size -= rd;
57 if (!size) {
Denis Vlasenko714701c2006-12-22 00:21:07 +000058 /* 'size' bytes copied - all done */
Denis Vlasenkof7996f32007-01-11 17:20:00 +000059 status = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000060 break;
61 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000062 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000063 }
Denis Vlasenko714701c2006-12-22 00:21:07 +000064 out:
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000065 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000066}
67
68
Denis Vlasenko714701c2006-12-22 00:21:07 +000069#if 0
70void 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 Vlasenko335b63d2007-04-10 21:38:30 +000075 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +000076}
77#endif
78
Denis Vlasenko7039a662006-10-08 17:54:47 +000079off_t bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000080{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000081 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +000082 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +000083 }
Denis Vlasenko6231e872006-09-17 15:45:48 +000084 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000085}
86
Denis Vlasenko714701c2006-12-22 00:21:07 +000087void 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 Vlasenko335b63d2007-04-10 21:38:30 +000095 xfunc_die();
Denis Vlasenko714701c2006-12-22 00:21:07 +000096}
97
Denis Vlasenko7039a662006-10-08 17:54:47 +000098off_t bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000099{
Denis Vlasenko6231e872006-09-17 15:45:48 +0000100 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +0000101}