blob: 0b850884b2fa77d6a629b3436bbcaacd63d80020 [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
Manuel Novoa III cad53642003-03-19 09:13:01 +000015#include "busybox.h"
Eric Andersen451c0d22005-04-16 07:46:53 +000016#include "libbb.h"
Eric Andersen55f98722001-05-15 17:48:09 +000017
Glenn L McGrathf0515da2003-11-22 02:13:41 +000018
Manuel Novoa III cad53642003-03-19 09:13:01 +000019#if BUFSIZ < 4096
20#undef BUFSIZ
21#define BUFSIZ 4096
22#endif
23
Glenn L McGrathf0515da2003-11-22 02:13:41 +000024
Rob Landley21ccbb62005-11-04 01:20:46 +000025static ssize_t bb_full_fd_action(int src_fd, int dst_fd, size_t size)
Eric Andersen55f98722001-05-15 17:48:09 +000026{
Rob Landley21ccbb62005-11-04 01:20:46 +000027 int status = -1;
28 size_t total = 0;
29 RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
Eric Andersen55f98722001-05-15 17:48:09 +000030
Rob Landley21ccbb62005-11-04 01:20:46 +000031 if (src_fd < 0) goto out;
Rob Landley21ccbb62005-11-04 01:20:46 +000032 while (!size || total < size)
Eric Andersen451c0d22005-04-16 07:46:53 +000033 {
Rob Landley90334532005-11-12 11:04:11 +000034 ssize_t wrote, xread;
35
36 xread = safe_read(src_fd, buffer,
37 (!size || size - total > BUFSIZ) ? BUFSIZ : size - total);
38
Rob Landley21ccbb62005-11-04 01:20:46 +000039 if (xread > 0) {
40 /* A -1 dst_fd means we need to fake it... */
41 wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread);
42 if (wrote < xread) {
43 bb_perror_msg(bb_msg_write_error);
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000045 }
Rob Landley21ccbb62005-11-04 01:20:46 +000046 total += wrote;
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 }
Rob Landley21ccbb62005-11-04 01:20:46 +000056
57out:
58 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000059
Rob Landley21ccbb62005-11-04 01:20:46 +000060 return status ? status : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000061}
62
63
64extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
65{
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
72extern int bb_copyfd_eof(int fd1, int fd2)
73{
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}