blob: c1962e3c649be36b982063fc44c86cb2c5636d68 [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;
Matt Kraaid6ef0742001-05-18 14:14:55 +000032
Rob Landley21ccbb62005-11-04 01:20:46 +000033 while (!size || total < size)
Eric Andersen451c0d22005-04-16 07:46:53 +000034 {
Rob Landley21ccbb62005-11-04 01:20:46 +000035 ssize_t wrote, xread = (size && size < BUFSIZ) ? size : BUFSIZ;
Rob Landley3fc4ad12005-11-04 01:54:15 +000036 xread = safe_read(src_fd, buffer, xread);
Rob Landley21ccbb62005-11-04 01:20:46 +000037 if (xread > 0) {
38 /* A -1 dst_fd means we need to fake it... */
39 wrote = (dst_fd < 0) ? xread : bb_full_write(dst_fd, buffer, xread);
40 if (wrote < xread) {
41 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 Landley21ccbb62005-11-04 01:20:46 +000044 total += wrote;
45 size -= wrote;
46 } else if (xread < 0) {
47 bb_perror_msg(bb_msg_read_error);
48 break;
49 } else if (xread == 0) {
50 /* 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 }
Rob Landley21ccbb62005-11-04 01:20:46 +000055
56out:
57 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000058
Rob Landley21ccbb62005-11-04 01:20:46 +000059 return status ? status : total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000060}
61
62
63extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
64{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000065 if (size) {
Glenn L McGrath15c35122004-02-21 09:20:56 +000066 return(bb_full_fd_action(fd1, fd2, size));
Glenn L McGrathc3498f62003-11-24 23:50:07 +000067 }
68 return(0);
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000069}
70
71extern int bb_copyfd_eof(int fd1, int fd2)
72{
Glenn L McGrath15c35122004-02-21 09:20:56 +000073 return(bb_full_fd_action(fd1, fd2, 0));
Eric Andersen55f98722001-05-15 17:48:09 +000074}