Import libarchive-2.4.17. See NEWS for details.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_util.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_util.c,v 1.16 2007/12/30 04:58:21 kientzle Exp $");
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38
39 #include "archive.h"
40 #include "archive_private.h"
41
42 int
43 archive_api_feature(void)
44 {
45         return (ARCHIVE_API_FEATURE);
46 }
47
48 int
49 archive_api_version(void)
50 {
51         return (ARCHIVE_API_VERSION);
52 }
53
54 int
55 archive_version_stamp(void)
56 {
57         return (ARCHIVE_VERSION_STAMP);
58 }
59
60 const char *
61 archive_version(void)
62 {
63         return (ARCHIVE_LIBRARY_VERSION);
64 }
65
66 int
67 archive_errno(struct archive *a)
68 {
69         return (a->archive_error_number);
70 }
71
72 const char *
73 archive_error_string(struct archive *a)
74 {
75
76         if (a->error != NULL  &&  *a->error != '\0')
77                 return (a->error);
78         else
79                 return ("(Empty error message)");
80 }
81
82
83 int
84 archive_format(struct archive *a)
85 {
86         return (a->archive_format);
87 }
88
89 const char *
90 archive_format_name(struct archive *a)
91 {
92         return (a->archive_format_name);
93 }
94
95
96 int
97 archive_compression(struct archive *a)
98 {
99         return (a->compression_code);
100 }
101
102 const char *
103 archive_compression_name(struct archive *a)
104 {
105         return (a->compression_name);
106 }
107
108
109 /*
110  * Return a count of the number of compressed bytes processed.
111  */
112 int64_t
113 archive_position_compressed(struct archive *a)
114 {
115         return (a->raw_position);
116 }
117
118 /*
119  * Return a count of the number of uncompressed bytes processed.
120  */
121 int64_t
122 archive_position_uncompressed(struct archive *a)
123 {
124         return (a->file_position);
125 }
126
127 void
128 archive_clear_error(struct archive *a)
129 {
130         archive_string_empty(&a->error_string);
131         a->error = NULL;
132 }
133
134 void
135 archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
136 {
137         va_list ap;
138 #ifdef HAVE_STRERROR_R
139         char errbuff[512];
140 #endif
141         char *errp;
142
143         a->archive_error_number = error_number;
144         if (fmt == NULL) {
145                 a->error = NULL;
146                 return;
147         }
148
149         va_start(ap, fmt);
150         archive_string_vsprintf(&(a->error_string), fmt, ap);
151         if (error_number > 0) {
152                 archive_strcat(&(a->error_string), ": ");
153 #ifdef HAVE_STRERROR_R
154 #ifdef STRERROR_R_CHAR_P
155                 errp = strerror_r(error_number, errbuff, sizeof(errbuff));
156 #else
157                 strerror_r(error_number, errbuff, sizeof(errbuff));
158                 errp = errbuff;
159 #endif
160 #else
161                 /* Note: this is not threadsafe! */
162                 errp = strerror(error_number);
163 #endif
164                 archive_strcat(&(a->error_string), errp);
165         }
166         a->error = a->error_string.s;
167         va_end(ap);
168 }
169
170 void
171 archive_copy_error(struct archive *dest, struct archive *src)
172 {
173         dest->archive_error_number = src->archive_error_number;
174
175         archive_string_copy(&dest->error_string, &src->error_string);
176         dest->error = dest->error_string.s;
177 }
178
179 void
180 __archive_errx(int retvalue, const char *msg)
181 {
182         static const char *msg1 = "Fatal Internal Error in libarchive: ";
183         write(2, msg1, strlen(msg1));
184         write(2, msg, strlen(msg));
185         write(2, "\n", 1);
186         exit(retvalue);
187 }