blob: e2c542e321c0951144aa7aa9037dbd7e6868d628 [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;
Rob Landley21ccbb62005-11-04 01:20:46 +000031 while (!size || total < size)
Eric Andersen451c0d22005-04-16 07:46:53 +000032 {
Rob Landley90334532005-11-12 11:04:11 +000033 ssize_t wrote, xread;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000034
Rob Landley90334532005-11-12 11:04:11 +000035 xread = safe_read(src_fd, buffer,
36 (!size || size - total > BUFSIZ) ? BUFSIZ : size - total);
37
Rob Landley21ccbb62005-11-04 01:20:46 +000038 if (xread > 0) {
39 /* A -1 dst_fd means we need to fake it... */
40 wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread);
41 if (wrote < xread) {
42 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000044 }
Rob Landley21ccbb62005-11-04 01:20:46 +000045 total += wrote;
Rob Landley90632d02006-02-13 19:40:43 +000046 if (total == size) status = 0;
Rob Landley21ccbb62005-11-04 01:20:46 +000047 } else if (xread < 0) {
48 bb_perror_msg(bb_msg_read_error);
49 break;
50 } else if (xread == 0) {
51 /* All done. */
52 status = 0;
53 break;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000054 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000055 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056
Rob Landley21ccbb62005-11-04 01:20:46 +000057out:
58 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000059
"Vladimir N. Oleynik"7600eec2006-01-31 12:12:15 +000060 return status ? status : (ssize_t)total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000061}
62
63
Rob Landleydfba7412006-03-06 20:47:33 +000064int bb_copyfd_size(int fd1, int fd2, const off_t size)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000065{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000066 if (size) {
Glenn L McGrath15c35122004-02-21 09:20:56 +000067 return(bb_full_fd_action(fd1, fd2, size));
Glenn L McGrathc3498f62003-11-24 23:50:07 +000068 }
69 return(0);
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000070}
71
Rob Landleydfba7412006-03-06 20:47:33 +000072int bb_copyfd_eof(int fd1, int fd2)
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000073{
Glenn L McGrath15c35122004-02-21 09:20:56 +000074 return(bb_full_fd_action(fd1, fd2, 0));
Eric Andersen55f98722001-05-15 17:48:09 +000075}