aboutsummaryrefslogtreecommitdiff
path: root/archival/libunarchive/copy_file_chunk_fd.c
diff options
context:
space:
mode:
Diffstat (limited to 'archival/libunarchive/copy_file_chunk_fd.c')
-rw-r--r--archival/libunarchive/copy_file_chunk_fd.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/archival/libunarchive/copy_file_chunk_fd.c b/archival/libunarchive/copy_file_chunk_fd.c
new file mode 100644
index 000000000..fb513e6d5
--- /dev/null
+++ b/archival/libunarchive/copy_file_chunk_fd.c
@@ -0,0 +1,33 @@
+#include <unistd.h>
+#include <sys/types.h>
+#include "libbb.h"
+
+/* Copy CHUNKSIZE bytes (or untill EOF if chunksize == -1)
+ * from SRC_FILE to DST_FILE. */
+extern int copy_file_chunk_fd(int src_fd, int dst_fd, off_t chunksize)
+{
+ size_t nread, size;
+ char buffer[BUFSIZ];
+
+ while (chunksize != 0) {
+ if (chunksize > BUFSIZ) {
+ size = BUFSIZ;
+ } else {
+ size = chunksize;
+ }
+ nread = xread(src_fd, buffer, size);
+ if (nread == 0) {
+ return 1;
+ }
+
+ if (write (dst_fd, buffer, nread) != nread) {
+ error_msg_and_die ("Short write");
+ }
+
+ if (chunksize != -1) {
+ chunksize -= nread;
+ }
+ }
+
+ return 0;
+}