xref: /freebsd/contrib/libarchive/cat/bsdcat.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1cdf63a70SMartin Matuska /*-
2*bd66c1b4SMartin Matuska  * SPDX-License-Identifier: BSD-2-Clause
3*bd66c1b4SMartin Matuska  *
4cdf63a70SMartin Matuska  * Copyright (c) 2011-2014, Mike Kazantsev
5cdf63a70SMartin Matuska  * All rights reserved.
6cdf63a70SMartin Matuska  */
7cdf63a70SMartin Matuska 
8cdf63a70SMartin Matuska #include "bsdcat_platform.h"
9cdf63a70SMartin Matuska 
10cdf63a70SMartin Matuska #include <stdio.h>
11cdf63a70SMartin Matuska #ifdef HAVE_STDLIB_H
12cdf63a70SMartin Matuska #include <stdlib.h>
13cdf63a70SMartin Matuska #endif
14cdf63a70SMartin Matuska #ifdef HAVE_UNISTD_H
15cdf63a70SMartin Matuska #include <unistd.h>
16cdf63a70SMartin Matuska #endif
17cdf63a70SMartin Matuska #ifdef HAVE_STRING_H
18cdf63a70SMartin Matuska #include <string.h>
19cdf63a70SMartin Matuska #endif
20cdf63a70SMartin Matuska 
21b9128a37SMartin Matuska #include <archive.h>
22b9128a37SMartin Matuska #include <archive_entry.h>
23b9128a37SMartin Matuska 
24cdf63a70SMartin Matuska #include "bsdcat.h"
25cdf63a70SMartin Matuska #include "err.h"
26cdf63a70SMartin Matuska 
27cdf63a70SMartin Matuska #define	BYTES_PER_BLOCK	(20*512)
28cdf63a70SMartin Matuska 
29cdf63a70SMartin Matuska static struct archive *a;
30cdf63a70SMartin Matuska static struct archive_entry *ae;
31cdf63a70SMartin Matuska static const char *bsdcat_current_path;
32cdf63a70SMartin Matuska static int exit_status = 0;
33cdf63a70SMartin Matuska 
34cdf63a70SMartin Matuska 
35b9128a37SMartin Matuska static __LA_NORETURN void
usage(FILE * stream,int eval)36cdf63a70SMartin Matuska usage(FILE *stream, int eval)
37cdf63a70SMartin Matuska {
38cdf63a70SMartin Matuska 	const char *p;
39cdf63a70SMartin Matuska 	p = lafe_getprogname();
40cdf63a70SMartin Matuska 	fprintf(stream,
41cdf63a70SMartin Matuska 	    "Usage: %s [-h] [--help] [--version] [--] [filenames...]\n", p);
42cdf63a70SMartin Matuska 	exit(eval);
43cdf63a70SMartin Matuska }
44cdf63a70SMartin Matuska 
45b9128a37SMartin Matuska static __LA_NORETURN void
version(void)46cdf63a70SMartin Matuska version(void)
47cdf63a70SMartin Matuska {
48cdf63a70SMartin Matuska 	printf("bsdcat %s - %s \n",
49cdf63a70SMartin Matuska 	    BSDCAT_VERSION_STRING,
50cdf63a70SMartin Matuska 	    archive_version_details());
51cdf63a70SMartin Matuska 	exit(0);
52cdf63a70SMartin Matuska }
53cdf63a70SMartin Matuska 
54b9128a37SMartin Matuska static void
bsdcat_print_error(void)55b9128a37SMartin Matuska bsdcat_print_error(void)
56b9128a37SMartin Matuska {
57b9128a37SMartin Matuska 	lafe_warnc(0, "%s: %s",
58b9128a37SMartin Matuska 	    bsdcat_current_path, archive_error_string(a));
59b9128a37SMartin Matuska 	exit_status = 1;
60b9128a37SMartin Matuska }
61b9128a37SMartin Matuska 
62b9128a37SMartin Matuska static void
bsdcat_next(void)63cdf63a70SMartin Matuska bsdcat_next(void)
64cdf63a70SMartin Matuska {
65a2a3407cSMartin Matuska 	if (a != NULL) {
66a2a3407cSMartin Matuska 		if (archive_read_close(a) != ARCHIVE_OK)
67a2a3407cSMartin Matuska 			bsdcat_print_error();
68a2a3407cSMartin Matuska 		archive_read_free(a);
69a2a3407cSMartin Matuska 	}
70a2a3407cSMartin Matuska 
71cdf63a70SMartin Matuska 	a = archive_read_new();
72cdf63a70SMartin Matuska 	archive_read_support_filter_all(a);
73cdf63a70SMartin Matuska 	archive_read_support_format_empty(a);
74cdf63a70SMartin Matuska 	archive_read_support_format_raw(a);
75cdf63a70SMartin Matuska }
76cdf63a70SMartin Matuska 
77b9128a37SMartin Matuska static void
bsdcat_read_to_stdout(const char * filename)78cdf63a70SMartin Matuska bsdcat_read_to_stdout(const char* filename)
79cdf63a70SMartin Matuska {
80cdf63a70SMartin Matuska 	int r;
81cdf63a70SMartin Matuska 
82cdf63a70SMartin Matuska 	if (archive_read_open_filename(a, filename, BYTES_PER_BLOCK)
83cdf63a70SMartin Matuska 	    != ARCHIVE_OK)
84cdf63a70SMartin Matuska 		bsdcat_print_error();
85cdf63a70SMartin Matuska 	else if (r = archive_read_next_header(a, &ae),
86cdf63a70SMartin Matuska 		 r != ARCHIVE_OK && r != ARCHIVE_EOF)
87cdf63a70SMartin Matuska 		bsdcat_print_error();
88cdf63a70SMartin Matuska 	else if (r == ARCHIVE_EOF)
89cdf63a70SMartin Matuska 		/* for empty payloads don't try and read data */
90cdf63a70SMartin Matuska 		;
91cdf63a70SMartin Matuska 	else if (archive_read_data_into_fd(a, 1) != ARCHIVE_OK)
92cdf63a70SMartin Matuska 		bsdcat_print_error();
93a2a3407cSMartin Matuska 	if (archive_read_close(a) != ARCHIVE_OK)
94cdf63a70SMartin Matuska 		bsdcat_print_error();
95a2a3407cSMartin Matuska 	archive_read_free(a);
96a2a3407cSMartin Matuska 	a = NULL;
97cdf63a70SMartin Matuska }
98cdf63a70SMartin Matuska 
99cdf63a70SMartin Matuska int
main(int argc,char ** argv)100cdf63a70SMartin Matuska main(int argc, char **argv)
101cdf63a70SMartin Matuska {
102cdf63a70SMartin Matuska 	struct bsdcat *bsdcat, bsdcat_storage;
103cdf63a70SMartin Matuska 	int c;
104cdf63a70SMartin Matuska 
105cdf63a70SMartin Matuska 	bsdcat = &bsdcat_storage;
106cdf63a70SMartin Matuska 	memset(bsdcat, 0, sizeof(*bsdcat));
107cdf63a70SMartin Matuska 
108cdf63a70SMartin Matuska 	lafe_setprogname(*argv, "bsdcat");
109cdf63a70SMartin Matuska 
110cdf63a70SMartin Matuska 	bsdcat->argv = argv;
111cdf63a70SMartin Matuska 	bsdcat->argc = argc;
112cdf63a70SMartin Matuska 
113cdf63a70SMartin Matuska 	while ((c = bsdcat_getopt(bsdcat)) != -1) {
114cdf63a70SMartin Matuska 		switch (c) {
115cdf63a70SMartin Matuska 		case 'h':
116cdf63a70SMartin Matuska 			usage(stdout, 0);
117b9128a37SMartin Matuska 			/* NOTREACHED */
118b9128a37SMartin Matuska 			/* Fallthrough */
119cdf63a70SMartin Matuska 		case OPTION_VERSION:
120cdf63a70SMartin Matuska 			version();
121b9128a37SMartin Matuska 			/* NOTREACHED */
122b9128a37SMartin Matuska 			/* Fallthrough */
123cdf63a70SMartin Matuska 		default:
124cdf63a70SMartin Matuska 			usage(stderr, 1);
125b9128a37SMartin Matuska 			/* Fallthrough */
126b9128a37SMartin Matuska 			/* NOTREACHED */
127cdf63a70SMartin Matuska 		}
128cdf63a70SMartin Matuska 	}
129cdf63a70SMartin Matuska 
130cdf63a70SMartin Matuska 	bsdcat_next();
131cdf63a70SMartin Matuska 	if (*bsdcat->argv == NULL) {
132cdf63a70SMartin Matuska 		bsdcat_current_path = "<stdin>";
133cdf63a70SMartin Matuska 		bsdcat_read_to_stdout(NULL);
134a2a3407cSMartin Matuska 	} else {
135cdf63a70SMartin Matuska 		while (*bsdcat->argv) {
136cdf63a70SMartin Matuska 			bsdcat_current_path = *bsdcat->argv++;
137cdf63a70SMartin Matuska 			bsdcat_read_to_stdout(bsdcat_current_path);
138cdf63a70SMartin Matuska 			bsdcat_next();
139cdf63a70SMartin Matuska 		}
140a2a3407cSMartin Matuska 		archive_read_free(a); /* Help valgrind & friends */
141a2a3407cSMartin Matuska 	}
1426a414569SMartin Matuska 
143cdf63a70SMartin Matuska 	exit(exit_status);
144cdf63a70SMartin Matuska }
145