blob: baf99df51949ca3da997bdb7d98c7f26022d47ab [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Eric Andersen55f98722001-05-15 17:48:09 +000022#include <errno.h>
Glenn L McGrathf0515da2003-11-22 02:13:41 +000023#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
Manuel Novoa III cad53642003-03-19 09:13:01 +000027#include "busybox.h"
Eric Andersen451c0d22005-04-16 07:46:53 +000028#include "libbb.h"
Eric Andersen55f98722001-05-15 17:48:09 +000029
Glenn L McGrathf0515da2003-11-22 02:13:41 +000030
Manuel Novoa III cad53642003-03-19 09:13:01 +000031#if BUFSIZ < 4096
32#undef BUFSIZ
33#define BUFSIZ 4096
34#endif
35
Glenn L McGrathf0515da2003-11-22 02:13:41 +000036
Eric Andersen451c0d22005-04-16 07:46:53 +000037static size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size2)
Eric Andersen55f98722001-05-15 17:48:09 +000038{
Eric Andersen451c0d22005-04-16 07:46:53 +000039 int status;
40 size_t xread, wrote, total, size = size2;
Eric Andersen55f98722001-05-15 17:48:09 +000041
Eric Andersen451c0d22005-04-16 07:46:53 +000042 if ((dst_fd < 0) || (src_fd < 0)) {
43 return -1;
44 }
Matt Kraaid6ef0742001-05-18 14:14:55 +000045
Eric Andersen451c0d22005-04-16 07:46:53 +000046 if (size == 0) {
47 /* If size is 0 copy until EOF */
48 size = ULONG_MAX;
49 }
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000050
Eric Andersen451c0d22005-04-16 07:46:53 +000051 {
52 RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
53 total = 0;
54 wrote = 0;
55 status = -1;
56 while (total < size)
57 {
58 xread = BUFSIZ;
59 if (size < (wrote + BUFSIZ))
60 xread = size - wrote;
61 xread = bb_full_read(src_fd, buffer, xread);
62 if (xread > 0) {
63 wrote = bb_full_write(dst_fd, buffer, xread);
64 if (wrote < xread) {
65 bb_perror_msg(bb_msg_write_error);
66 break;
67 }
68 total += wrote;
69 } else if (xread < 0) {
70 bb_perror_msg(bb_msg_read_error);
71 break;
72 } else if (xread == 0) {
73 /* All done. */
74 status = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 break;
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000076 }
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000077 }
Eric Andersen451c0d22005-04-16 07:46:53 +000078 RELEASE_CONFIG_BUFFER(buffer);
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000079 }
Glenn L McGrath25fe94f2002-12-13 08:20:44 +000080
Eric Andersen451c0d22005-04-16 07:46:53 +000081 if (status == 0 || wrote)
82 return wrote;
83 /* Some sortof error occured */
84 return -1;
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000085}
86
87
88extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
89{
Glenn L McGrathc3498f62003-11-24 23:50:07 +000090 if (size) {
Glenn L McGrath15c35122004-02-21 09:20:56 +000091 return(bb_full_fd_action(fd1, fd2, size));
Glenn L McGrathc3498f62003-11-24 23:50:07 +000092 }
93 return(0);
Glenn L McGrath7ffe1332003-11-21 22:24:57 +000094}
95
96extern int bb_copyfd_eof(int fd1, int fd2)
97{
Glenn L McGrath15c35122004-02-21 09:20:56 +000098 return(bb_full_fd_action(fd1, fd2, 0));
Eric Andersen55f98722001-05-15 17:48:09 +000099}