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