- add llist_free_one() and llist_free() to libbb; Add a bit of documentation.
[oweals/busybox.git] / libbb / copyfd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
6  *
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
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "busybox.h"
28 #include "libbb.h"
29
30
31 #if BUFSIZ < 4096
32 #undef BUFSIZ
33 #define BUFSIZ 4096
34 #endif
35
36
37 static size_t bb_full_fd_action(int src_fd, int dst_fd, const size_t size2)
38 {
39         int status;
40         size_t xread, wrote, total, size = size2;
41
42         if (src_fd < 0) {
43                 return -1;
44         }
45
46         if (size == 0) {
47                 /* If size is 0 copy until EOF */
48                 size = ULONG_MAX;
49         }
50
51         {
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 < (total + BUFSIZ))
60                                 xread = size - total;
61                         xread = bb_full_read(src_fd, buffer, xread);
62                         if (xread > 0) {
63                                 if (dst_fd < 0) {
64                                         /* A -1 dst_fd means we need to fake it... */
65                                         wrote = xread;
66                                 } else {
67                                         wrote = bb_full_write(dst_fd, buffer, xread);
68                                 }
69                                 if (wrote < xread) {
70                                         bb_perror_msg(bb_msg_write_error);
71                                         break;
72                                 }
73                                 total += wrote;
74                         } else if (xread < 0) {
75                                 bb_perror_msg(bb_msg_read_error);
76                                 break;
77                         } else if (xread == 0) {
78                                 /* All done. */
79                                 status = 0;
80                                 break;
81                         }
82                 }
83                 RELEASE_CONFIG_BUFFER(buffer);
84         }
85
86         if (status == 0 || total)
87                 return total;
88         /* Some sortof error occured */
89         return -1;
90 }
91
92
93 extern int bb_copyfd_size(int fd1, int fd2, const off_t size)
94 {
95         if (size) {
96                 return(bb_full_fd_action(fd1, fd2, size));
97         }
98         return(0);
99 }
100
101 extern int bb_copyfd_eof(int fd1, int fd2)
102 {
103         return(bb_full_fd_action(fd1, fd2, 0));
104 }