Add our READMEs.
[dragonfly/vkernel-mp.git] / contrib / libarchive-2.1 / libarchive / archive_util.c
blob63bd5ae9087da3d999fdd36e1456f6b9a059e212
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_util.c,v 1.13 2007/03/03 07:37:36 kientzle Exp $");
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
39 #include "archive.h"
40 #include "archive_private.h"
42 int
43 archive_api_feature(void)
45 return (ARCHIVE_API_FEATURE);
48 int
49 archive_api_version(void)
51 return (ARCHIVE_API_VERSION);
54 const char *
55 archive_version(void)
57 return (PACKAGE_NAME " " PACKAGE_VERSION);
60 int
61 archive_errno(struct archive *a)
63 return (a->archive_error_number);
66 const char *
67 archive_error_string(struct archive *a)
70 if (a->error != NULL && *a->error != '\0')
71 return (a->error);
72 else
73 return ("(Empty error message)");
77 int
78 archive_format(struct archive *a)
80 return (a->archive_format);
83 const char *
84 archive_format_name(struct archive *a)
86 return (a->archive_format_name);
90 int
91 archive_compression(struct archive *a)
93 return (a->compression_code);
96 const char *
97 archive_compression_name(struct archive *a)
99 return (a->compression_name);
104 * Return a count of the number of compressed bytes processed.
106 int64_t
107 archive_position_compressed(struct archive *a)
109 return (a->raw_position);
113 * Return a count of the number of uncompressed bytes processed.
115 int64_t
116 archive_position_uncompressed(struct archive *a)
118 return (a->file_position);
121 void
122 archive_clear_error(struct archive *a)
124 archive_string_empty(&a->error_string);
125 a->error = NULL;
128 void
129 archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
131 va_list ap;
132 #ifdef HAVE_STRERROR_R
133 char errbuff[512];
134 #endif
135 char *errp;
137 a->archive_error_number = error_number;
138 if (fmt == NULL) {
139 a->error = NULL;
140 return;
143 va_start(ap, fmt);
144 archive_string_vsprintf(&(a->error_string), fmt, ap);
145 if (error_number > 0) {
146 archive_strcat(&(a->error_string), ": ");
147 #ifdef HAVE_STRERROR_R
148 #ifdef STRERROR_R_CHAR_P
149 errp = strerror_r(error_number, errbuff, sizeof(errbuff));
150 #else
151 strerror_r(error_number, errbuff, sizeof(errbuff));
152 errp = errbuff;
153 #endif
154 #else
155 /* Note: this is not threadsafe! */
156 errp = strerror(error_number);
157 #endif
158 archive_strcat(&(a->error_string), errp);
160 a->error = a->error_string.s;
161 va_end(ap);
164 void
165 archive_copy_error(struct archive *dest, struct archive *src)
167 dest->archive_error_number = src->archive_error_number;
169 archive_string_copy(&dest->error_string, &src->error_string);
170 dest->error = dest->error_string.s;
173 void
174 __archive_errx(int retvalue, const char *msg)
176 static const char *msg1 = "Fatal Internal Error in libarchive: ";
177 write(2, msg1, strlen(msg1));
178 write(2, msg, strlen(msg));
179 write(2, "\n", 1);
180 exit(retvalue);