xref: /minix3/external/bsd/libarchive/dist/libarchive/archive_write_open_file.c (revision 543adbed3a3a783ed36434adafbc258b6bde442d)
1*543adbedSBen Gras /*-
2*543adbedSBen Gras  * Copyright (c) 2003-2007 Tim Kientzle
3*543adbedSBen Gras  * All rights reserved.
4*543adbedSBen Gras  *
5*543adbedSBen Gras  * Redistribution and use in source and binary forms, with or without
6*543adbedSBen Gras  * modification, are permitted provided that the following conditions
7*543adbedSBen Gras  * are met:
8*543adbedSBen Gras  * 1. Redistributions of source code must retain the above copyright
9*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer.
10*543adbedSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
11*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer in the
12*543adbedSBen Gras  *    documentation and/or other materials provided with the distribution.
13*543adbedSBen Gras  *
14*543adbedSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*543adbedSBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*543adbedSBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*543adbedSBen Gras  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*543adbedSBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*543adbedSBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*543adbedSBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*543adbedSBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*543adbedSBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*543adbedSBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*543adbedSBen Gras  */
25*543adbedSBen Gras 
26*543adbedSBen Gras #include "archive_platform.h"
27*543adbedSBen Gras __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_open_file.c,v 1.19 2007/01/09 08:05:56 kientzle Exp $");
28*543adbedSBen Gras 
29*543adbedSBen Gras #ifdef HAVE_SYS_STAT_H
30*543adbedSBen Gras #include <sys/stat.h>
31*543adbedSBen Gras #endif
32*543adbedSBen Gras #ifdef HAVE_ERRNO_H
33*543adbedSBen Gras #include <errno.h>
34*543adbedSBen Gras #endif
35*543adbedSBen Gras #ifdef HAVE_FCNTL_H
36*543adbedSBen Gras #include <fcntl.h>
37*543adbedSBen Gras #endif
38*543adbedSBen Gras #ifdef HAVE_STDLIB_H
39*543adbedSBen Gras #include <stdlib.h>
40*543adbedSBen Gras #endif
41*543adbedSBen Gras #ifdef HAVE_STRING_H
42*543adbedSBen Gras #include <string.h>
43*543adbedSBen Gras #endif
44*543adbedSBen Gras #ifdef HAVE_UNISTD_H
45*543adbedSBen Gras #include <unistd.h>
46*543adbedSBen Gras #endif
47*543adbedSBen Gras 
48*543adbedSBen Gras #include "archive.h"
49*543adbedSBen Gras 
50*543adbedSBen Gras struct write_FILE_data {
51*543adbedSBen Gras 	FILE		*f;
52*543adbedSBen Gras };
53*543adbedSBen Gras 
54*543adbedSBen Gras static int	file_close(struct archive *, void *);
55*543adbedSBen Gras static int	file_open(struct archive *, void *);
56*543adbedSBen Gras static ssize_t	file_write(struct archive *, void *, const void *buff, size_t);
57*543adbedSBen Gras 
58*543adbedSBen Gras int
archive_write_open_FILE(struct archive * a,FILE * f)59*543adbedSBen Gras archive_write_open_FILE(struct archive *a, FILE *f)
60*543adbedSBen Gras {
61*543adbedSBen Gras 	struct write_FILE_data *mine;
62*543adbedSBen Gras 
63*543adbedSBen Gras 	mine = (struct write_FILE_data *)malloc(sizeof(*mine));
64*543adbedSBen Gras 	if (mine == NULL) {
65*543adbedSBen Gras 		archive_set_error(a, ENOMEM, "No memory");
66*543adbedSBen Gras 		return (ARCHIVE_FATAL);
67*543adbedSBen Gras 	}
68*543adbedSBen Gras 	mine->f = f;
69*543adbedSBen Gras 	return (archive_write_open(a, mine,
70*543adbedSBen Gras 		    file_open, file_write, file_close));
71*543adbedSBen Gras }
72*543adbedSBen Gras 
73*543adbedSBen Gras static int
file_open(struct archive * a,void * client_data)74*543adbedSBen Gras file_open(struct archive *a, void *client_data)
75*543adbedSBen Gras {
76*543adbedSBen Gras 	(void)a; /* UNUSED */
77*543adbedSBen Gras 	(void)client_data; /* UNUSED */
78*543adbedSBen Gras 
79*543adbedSBen Gras 	return (ARCHIVE_OK);
80*543adbedSBen Gras }
81*543adbedSBen Gras 
82*543adbedSBen Gras static ssize_t
file_write(struct archive * a,void * client_data,const void * buff,size_t length)83*543adbedSBen Gras file_write(struct archive *a, void *client_data, const void *buff, size_t length)
84*543adbedSBen Gras {
85*543adbedSBen Gras 	struct write_FILE_data	*mine;
86*543adbedSBen Gras 	size_t	bytesWritten;
87*543adbedSBen Gras 
88*543adbedSBen Gras 	mine = client_data;
89*543adbedSBen Gras 	bytesWritten = fwrite(buff, 1, length, mine->f);
90*543adbedSBen Gras 	if (bytesWritten < length) {
91*543adbedSBen Gras 		archive_set_error(a, errno, "Write error");
92*543adbedSBen Gras 		return (-1);
93*543adbedSBen Gras 	}
94*543adbedSBen Gras 	return (bytesWritten);
95*543adbedSBen Gras }
96*543adbedSBen Gras 
97*543adbedSBen Gras static int
file_close(struct archive * a,void * client_data)98*543adbedSBen Gras file_close(struct archive *a, void *client_data)
99*543adbedSBen Gras {
100*543adbedSBen Gras 	struct write_FILE_data	*mine = client_data;
101*543adbedSBen Gras 
102*543adbedSBen Gras 	(void)a; /* UNUSED */
103*543adbedSBen Gras 	free(mine);
104*543adbedSBen Gras 	return (ARCHIVE_OK);
105*543adbedSBen Gras }
106