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());
73 a = archive_read_new();
74 archive_read_support_filter_all(a);
75 archive_read_support_format_empty(a);
76 archive_read_support_format_raw(a);
80 bsdcat_print_error(void)
82 lafe_warnc(0, "%s: %s",
83 bsdcat_current_path, archive_error_string(a));
88 bsdcat_read_to_stdout(const char* filename)
92 if (archive_read_open_filename(a, filename, BYTES_PER_BLOCK)
95 else if (r = archive_read_next_header(a, &ae),
96 r != ARCHIVE_OK && r != ARCHIVE_EOF)
98 else if (r == ARCHIVE_EOF)
99 /* for empty payloads don't try and read data */
101 else if (archive_read_data_into_fd(a, 1) != ARCHIVE_OK)
102 bsdcat_print_error();
103 if (archive_read_free(a) != ARCHIVE_OK)
104 bsdcat_print_error();
108 main(int argc, char **argv)
110 struct bsdcat *bsdcat, bsdcat_storage;
113 bsdcat = &bsdcat_storage;
114 memset(bsdcat, 0, sizeof(*bsdcat));
116 lafe_setprogname(*argv, "bsdcat");
121 while ((c = bsdcat_getopt(bsdcat)) != -1) {
135 if (*bsdcat->argv == NULL) {
136 bsdcat_current_path = "<stdin>";
137 bsdcat_read_to_stdout(NULL);
139 while (*bsdcat->argv) {
140 bsdcat_current_path = *bsdcat->argv++;
141 bsdcat_read_to_stdout(bsdcat_current_path);
146 archive_read_free(a);