xref: /freebsd/contrib/libarchive/cat/bsdcat.c (revision 6a414569b3517371c5c628abc9e02d87493e9678)
1cdf63a70SMartin Matuska /*-
2cdf63a70SMartin Matuska  * Copyright (c) 2011-2014, Mike Kazantsev
3cdf63a70SMartin Matuska  * All rights reserved.
4cdf63a70SMartin Matuska  *
5cdf63a70SMartin Matuska  * Redistribution and use in source and binary forms, with or without
6cdf63a70SMartin Matuska  * modification, are permitted provided that the following conditions
7cdf63a70SMartin Matuska  * are met:
8cdf63a70SMartin Matuska  * 1. Redistributions of source code must retain the above copyright
9cdf63a70SMartin Matuska  *    notice, this list of conditions and the following disclaimer.
10cdf63a70SMartin Matuska  * 2. Redistributions in binary form must reproduce the above copyright
11cdf63a70SMartin Matuska  *    notice, this list of conditions and the following disclaimer in the
12cdf63a70SMartin Matuska  *    documentation and/or other materials provided with the distribution.
13cdf63a70SMartin Matuska  *
14cdf63a70SMartin Matuska  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15cdf63a70SMartin Matuska  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16cdf63a70SMartin Matuska  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17cdf63a70SMartin Matuska  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18cdf63a70SMartin Matuska  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19cdf63a70SMartin Matuska  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20cdf63a70SMartin Matuska  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21cdf63a70SMartin Matuska  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22cdf63a70SMartin Matuska  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23cdf63a70SMartin Matuska  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24cdf63a70SMartin Matuska  */
25cdf63a70SMartin Matuska 
26cdf63a70SMartin Matuska #include "bsdcat_platform.h"
27cdf63a70SMartin Matuska __FBSDID("$FreeBSD$");
28cdf63a70SMartin Matuska 
29cdf63a70SMartin Matuska #include <stdio.h>
30cdf63a70SMartin Matuska #ifdef HAVE_STDLIB_H
31cdf63a70SMartin Matuska #include <stdlib.h>
32cdf63a70SMartin Matuska #endif
33cdf63a70SMartin Matuska #ifdef HAVE_UNISTD_H
34cdf63a70SMartin Matuska #include <unistd.h>
35cdf63a70SMartin Matuska #endif
36cdf63a70SMartin Matuska #ifdef HAVE_STRING_H
37cdf63a70SMartin Matuska #include <string.h>
38cdf63a70SMartin Matuska #endif
39cdf63a70SMartin Matuska 
40cdf63a70SMartin Matuska #include "bsdcat.h"
41cdf63a70SMartin Matuska #include "err.h"
42cdf63a70SMartin Matuska 
43cdf63a70SMartin Matuska #define	BYTES_PER_BLOCK	(20*512)
44cdf63a70SMartin Matuska 
45cdf63a70SMartin Matuska static struct archive *a;
46cdf63a70SMartin Matuska static struct archive_entry *ae;
47cdf63a70SMartin Matuska static const char *bsdcat_current_path;
48cdf63a70SMartin Matuska static int exit_status = 0;
49cdf63a70SMartin Matuska 
50cdf63a70SMartin Matuska 
51cdf63a70SMartin Matuska void
52cdf63a70SMartin Matuska usage(FILE *stream, int eval)
53cdf63a70SMartin Matuska {
54cdf63a70SMartin Matuska 	const char *p;
55cdf63a70SMartin Matuska 	p = lafe_getprogname();
56cdf63a70SMartin Matuska 	fprintf(stream,
57cdf63a70SMartin Matuska 	    "Usage: %s [-h] [--help] [--version] [--] [filenames...]\n", p);
58cdf63a70SMartin Matuska 	exit(eval);
59cdf63a70SMartin Matuska }
60cdf63a70SMartin Matuska 
61cdf63a70SMartin Matuska static void
62cdf63a70SMartin Matuska version(void)
63cdf63a70SMartin Matuska {
64cdf63a70SMartin Matuska 	printf("bsdcat %s - %s\n",
65cdf63a70SMartin Matuska 	    BSDCAT_VERSION_STRING,
66cdf63a70SMartin Matuska 	    archive_version_details());
67cdf63a70SMartin Matuska 	exit(0);
68cdf63a70SMartin Matuska }
69cdf63a70SMartin Matuska 
70cdf63a70SMartin Matuska void
71cdf63a70SMartin Matuska bsdcat_next(void)
72cdf63a70SMartin Matuska {
73cdf63a70SMartin Matuska 	a = archive_read_new();
74cdf63a70SMartin Matuska 	archive_read_support_filter_all(a);
75cdf63a70SMartin Matuska 	archive_read_support_format_empty(a);
76cdf63a70SMartin Matuska 	archive_read_support_format_raw(a);
77cdf63a70SMartin Matuska }
78cdf63a70SMartin Matuska 
79cdf63a70SMartin Matuska void
80cdf63a70SMartin Matuska bsdcat_print_error(void)
81cdf63a70SMartin Matuska {
82cdf63a70SMartin Matuska 	lafe_warnc(0, "%s: %s",
83cdf63a70SMartin Matuska 	    bsdcat_current_path, archive_error_string(a));
84cdf63a70SMartin Matuska 	exit_status = 1;
85cdf63a70SMartin Matuska }
86cdf63a70SMartin Matuska 
87cdf63a70SMartin Matuska void
88cdf63a70SMartin Matuska bsdcat_read_to_stdout(const char* filename)
89cdf63a70SMartin Matuska {
90cdf63a70SMartin Matuska 	int r;
91cdf63a70SMartin Matuska 
92cdf63a70SMartin Matuska 	if (archive_read_open_filename(a, filename, BYTES_PER_BLOCK)
93cdf63a70SMartin Matuska 	    != ARCHIVE_OK)
94cdf63a70SMartin Matuska 		bsdcat_print_error();
95cdf63a70SMartin Matuska 	else if (r = archive_read_next_header(a, &ae),
96cdf63a70SMartin Matuska 		 r != ARCHIVE_OK && r != ARCHIVE_EOF)
97cdf63a70SMartin Matuska 		bsdcat_print_error();
98cdf63a70SMartin Matuska 	else if (r == ARCHIVE_EOF)
99cdf63a70SMartin Matuska 		/* for empty payloads don't try and read data */
100cdf63a70SMartin Matuska 		;
101cdf63a70SMartin Matuska 	else if (archive_read_data_into_fd(a, 1) != ARCHIVE_OK)
102cdf63a70SMartin Matuska 		bsdcat_print_error();
103cdf63a70SMartin Matuska 	if (archive_read_free(a) != ARCHIVE_OK)
104cdf63a70SMartin Matuska 		bsdcat_print_error();
105cdf63a70SMartin Matuska }
106cdf63a70SMartin Matuska 
107cdf63a70SMartin Matuska int
108cdf63a70SMartin Matuska main(int argc, char **argv)
109cdf63a70SMartin Matuska {
110cdf63a70SMartin Matuska 	struct bsdcat *bsdcat, bsdcat_storage;
111cdf63a70SMartin Matuska 	int c;
112cdf63a70SMartin Matuska 
113cdf63a70SMartin Matuska 	bsdcat = &bsdcat_storage;
114cdf63a70SMartin Matuska 	memset(bsdcat, 0, sizeof(*bsdcat));
115cdf63a70SMartin Matuska 
116cdf63a70SMartin Matuska 	lafe_setprogname(*argv, "bsdcat");
117cdf63a70SMartin Matuska 
118cdf63a70SMartin Matuska 	bsdcat->argv = argv;
119cdf63a70SMartin Matuska 	bsdcat->argc = argc;
120cdf63a70SMartin Matuska 
121cdf63a70SMartin Matuska 	while ((c = bsdcat_getopt(bsdcat)) != -1) {
122cdf63a70SMartin Matuska 		switch (c) {
123cdf63a70SMartin Matuska 		case 'h':
124cdf63a70SMartin Matuska 			usage(stdout, 0);
125cdf63a70SMartin Matuska 			break;
126cdf63a70SMartin Matuska 		case OPTION_VERSION:
127cdf63a70SMartin Matuska 			version();
128cdf63a70SMartin Matuska 			break;
129cdf63a70SMartin Matuska 		default:
130cdf63a70SMartin Matuska 			usage(stderr, 1);
131cdf63a70SMartin Matuska 		}
132cdf63a70SMartin Matuska 	}
133cdf63a70SMartin Matuska 
134cdf63a70SMartin Matuska 	bsdcat_next();
135cdf63a70SMartin Matuska 	if (*bsdcat->argv == NULL) {
136cdf63a70SMartin Matuska 		bsdcat_current_path = "<stdin>";
137cdf63a70SMartin Matuska 		bsdcat_read_to_stdout(NULL);
138cdf63a70SMartin Matuska 	} else
139cdf63a70SMartin Matuska 		while (*bsdcat->argv) {
140cdf63a70SMartin Matuska 			bsdcat_current_path = *bsdcat->argv++;
141cdf63a70SMartin Matuska 			bsdcat_read_to_stdout(bsdcat_current_path);
142cdf63a70SMartin Matuska 			bsdcat_next();
143cdf63a70SMartin Matuska 		}
144cdf63a70SMartin Matuska 
145*6a414569SMartin Matuska 	if (a != NULL)
146*6a414569SMartin Matuska 		archive_read_free(a);
147*6a414569SMartin Matuska 
148cdf63a70SMartin Matuska 	exit(exit_status);
149cdf63a70SMartin Matuska }
150