blob: e9300a97d53ce3ba414bcf8e6a18a5ff31172153 [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
Rob Landley21ccbb62005-11-04 01:20:46 +000024static ssize_t bb_full_fd_action(int src_fd, int dst_fd, size_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000025{
Rob Landley21ccbb62005-11-04 01:20:46 +000026 int status = -1;
27 size_t total = 0;
28 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;
Denis Vlasenko6231e872006-09-17 15:45:48 +000031 while (!size || total < size) {
Rob Landley53437472006-07-16 08:14:35 +000032 ssize_t wr, rd;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033
Rob Landley53437472006-07-16 08:14:35 +000034 rd = safe_read(src_fd, buffer,
Rob Landley90334532005-11-12 11:04:11 +000035 (!size || size - total > BUFSIZ) ? BUFSIZ : size - total);
36
Rob Landley53437472006-07-16 08:14:35 +000037 if (rd > 0) {
Rob Landley21ccbb62005-11-04 01:20:46 +000038 /* A -1 dst_fd means we need to fake it... */
Rob Landley53437472006-07-16 08:14:35 +000039 wr = (dst_fd < 0) ? rd : full_write(dst_fd, buffer, rd);
40 if (wr < rd) {
Rob Landley21ccbb62005-11-04 01:20:46 +000041 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000043 }
Rob Landley53437472006-07-16 08:14:35 +000044 total += wr;
Rob Landley90632d02006-02-13 19:40:43 +000045 if (total == size) status = 0;
Rob Landley53437472006-07-16 08:14:35 +000046 } else if (rd < 0) {
Rob Landley21ccbb62005-11-04 01:20:46 +000047 bb_perror_msg(bb_msg_read_error);
48 break;
Rob Landley53437472006-07-16 08:14:35 +000049 } else if (rd == 0) {
Rob Landley21ccbb62005-11-04 01:20:46 +000050 /* All done. */
51 status = 0;
52 break;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000053 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000054 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000055
Rob Landley21ccbb62005-11-04 01:20:46 +000056out:
57 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000058
"Vladimir N. Oleynik"7600eec2006-01-31 12:12:15 +000059 return status ? status : (ssize_t)total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000060}
61
62
Rob Landleydfba7412006-03-06 20:47:33 +000063int bb_copyfd_size(int fd1, int fd2, const off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000064{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000065 if (size) {
Denis Vlasenko6231e872006-09-17 15:45:48 +000066 return bb_full_fd_action(fd1, fd2, size);
Glenn L McGrathc3498f62003-11-24 23:50:07 +000067 }
Denis Vlasenko6231e872006-09-17 15:45:48 +000068 return 0;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000069}
70
Rob Landleydfba7412006-03-06 20:47:33 +000071int bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000072{
Denis Vlasenko6231e872006-09-17 15:45:48 +000073 return bb_full_fd_action(fd1, fd2, 0);
Eric Andersen55f98722001-05-15 17:48:09 +000074}