blob: c6b886647aecc5d5209298b7874e08bd8d79e46d [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 Andersen55f98722001-05-15 17:48:09 +000010#include <errno.h>
Glenn L McGrathf0515da2003-11-22 02:13:41 +000011#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14
Eric Andersen451c0d22005-04-16 07:46:53 +000015#include "libbb.h"
Eric Andersen55f98722001-05-15 17:48:09 +000016
Glenn L McGrathf0515da2003-11-22 02:13:41 +000017
Manuel Novoa III cad53642003-03-19 09:13:01 +000018#if BUFSIZ < 4096
19#undef BUFSIZ
20#define BUFSIZ 4096
21#endif
22
Glenn L McGrathf0515da2003-11-22 02:13:41 +000023
Denis Vlasenko7039a662006-10-08 17:54:47 +000024static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000025{
Rob Landley21ccbb62005-11-04 01:20:46 +000026 int status = -1;
Denis Vlasenko7039a662006-10-08 17:54:47 +000027 off_t total = 0;
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000028 RESERVE_CONFIG_BUFFER(buffer, BUFSIZ);
Eric Andersen55f98722001-05-15 17:48:09 +000029
Rob Landley21ccbb62005-11-04 01:20:46 +000030 if (src_fd < 0) goto out;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000031
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000032 if (!size) {
33 size = BUFSIZ;
34 status = 1; /* copy until eof */
35 }
Rob Landley90334532005-11-12 11:04:11 +000036
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000037 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 Landley53437472006-07-16 08:14:35 +000053 if (wr < rd) {
Rob Landley21ccbb62005-11-04 01:20:46 +000054 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000056 }
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000057 }
58 total += rd;
59 if (status < 0) {
60 size -= rd;
61 if (!size) {
62 status = 0;
63 break;
64 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000065 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000066 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000067
Rob Landley21ccbb62005-11-04 01:20:46 +000068out:
69 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000070
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +000071 return status ? -1 : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000072}
73
74
Denis Vlasenko7039a662006-10-08 17:54:47 +000075off_t bb_copyfd_size(int fd1, int fd2, off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000076{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000077 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +000078 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +000079 }
Denis Vlasenko6231e872006-09-17 15:45:48 +000080 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000081}
82
Denis Vlasenko7039a662006-10-08 17:54:47 +000083off_t bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000084{
Denis Vlasenko6231e872006-09-17 15:45:48 +000085 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +000086}