2 * Copyright (c) 2011-2014, Mike Kazantsev
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 "bsdcat_platform.h"
27 __FBSDID("$FreeBSD$");
43 #define BYTES_PER_BLOCK (20*512)
45 static struct archive *a;
46 static struct archive_entry *ae;
47 static const char *bsdcat_current_path;
48 static int exit_status = 0;
52 usage(FILE *stream, int eval)
55 p = lafe_getprogname();
57 "Usage: %s [-h] [--help] [--version] [--] [filenames...]\n", p);
64 printf("bsdcat %s - %s \n",
65 BSDCAT_VERSION_STRING,
66 archive_version_details());
74 if (archive_read_close(a) != ARCHIVE_OK)
79 a = archive_read_new();
80 archive_read_support_filter_all(a);
81 archive_read_support_format_empty(a);
82 archive_read_support_format_raw(a);
86 bsdcat_print_error(void)
88 lafe_warnc(0, "%s: %s",
89 bsdcat_current_path, archive_error_string(a));
94 bsdcat_read_to_stdout(const char* filename)
98 if (archive_read_open_filename(a, filename, BYTES_PER_BLOCK)
100 bsdcat_print_error();
101 else if (r = archive_read_next_header(a, &ae),
102 r != ARCHIVE_OK && r != ARCHIVE_EOF)
103 bsdcat_print_error();
104 else if (r == ARCHIVE_EOF)
105 /* for empty payloads don't try and read data */
107 else if (archive_read_data_into_fd(a, 1) != ARCHIVE_OK)
108 bsdcat_print_error();
109 if (archive_read_close(a) != ARCHIVE_OK)
110 bsdcat_print_error();
111 archive_read_free(a);
116 main(int argc, char **argv)
118 struct bsdcat *bsdcat, bsdcat_storage;
121 bsdcat = &bsdcat_storage;
122 memset(bsdcat, 0, sizeof(*bsdcat));
124 lafe_setprogname(*argv, "bsdcat");
129 while ((c = bsdcat_getopt(bsdcat)) != -1) {
143 if (*bsdcat->argv == NULL) {
144 bsdcat_current_path = "<stdin>";
145 bsdcat_read_to_stdout(NULL);
147 while (*bsdcat->argv) {
148 bsdcat_current_path = *bsdcat->argv++;
149 bsdcat_read_to_stdout(bsdcat_current_path);
152 archive_read_free(a); /* Help valgrind & friends */