blob: 5152531487ea9de9a5ef7ceb4db21a933b481861 [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;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000035
Rob Landley90334532005-11-12 11:04:11 +000036 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 Landley90632d02006-02-13 19:40:43 +000047 if (total == size) status = 0;
Rob Landley21ccbb62005-11-04 01:20:46 +000048 } else if (xread < 0) {
49 bb_perror_msg(bb_msg_read_error);
50 break;
51 } else if (xread == 0) {
52 /* All done. */
53 status = 0;
54 break;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000055 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000056 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Rob Landley21ccbb62005-11-04 01:20:46 +000058out:
59 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000060
"Vladimir N. Oleynik"7600eec2006-01-31 12:12:15 +000061 return status ? status : (ssize_t)total;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000062}
63
64
65extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
66{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000067 if (size) {
Glenn L McGrath15c35122004-02-21 09:20:56 +000068 return(bb_full_fd_action(fd1, fd2, size));
Glenn L McGrathc3498f62003-11-24 23:50:07 +000069 }
70 return(0);
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000071}
72
73extern int bb_copyfd_eof(int fd1, int fd2)
74{
Glenn L McGrath15c35122004-02-21 09:20:56 +000075 return(bb_full_fd_action(fd1, fd2, 0));
Eric Andersen55f98722001-05-15 17:48:09 +000076}