xref: /dragonfly/contrib/libarchive/cat/bsdcat.c (revision 62dc643e)
1 /*-
2  * Copyright (c) 2011-2014, Mike Kazantsev
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  * 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.
13  *
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.
24  */
25 
26 #include "bsdcat_platform.h"
27 __FBSDID("$FreeBSD$");
28 
29 #include <stdio.h>
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 
40 #include "bsdcat.h"
41 #include "err.h"
42 
43 #define	BYTES_PER_BLOCK	(20*512)
44 
45 struct archive *a;
46 struct archive_entry *ae;
47 char *bsdcat_current_path;
48 int exit_status = 0;
49 
50 
51 void
52 usage(FILE *stream, int eval)
53 {
54 	const char *p;
55 	p = lafe_getprogname();
56 	fprintf(stream,
57 	    "Usage: %s [-h] [--help] [--version] [--] [filenames...]\n", p);
58 	exit(eval);
59 }
60 
61 static void
62 version(void)
63 {
64 	printf("bsdcat %s - %s\n",
65 	    BSDCAT_VERSION_STRING,
66 	    archive_version_details());
67 	exit(0);
68 }
69 
70 void
71 bsdcat_next()
72 {
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);
77 }
78 
79 void
80 bsdcat_print_error(void)
81 {
82 	lafe_warnc(0, "%s: %s",
83 	    bsdcat_current_path, archive_error_string(a));
84 	exit_status = 1;
85 }
86 
87 void
88 bsdcat_read_to_stdout(char* filename)
89 {
90 	int r;
91 
92 	if (archive_read_open_filename(a, filename, BYTES_PER_BLOCK)
93 	    != ARCHIVE_OK)
94 		bsdcat_print_error();
95 	else if (r = archive_read_next_header(a, &ae),
96 		 r != ARCHIVE_OK && r != ARCHIVE_EOF)
97 		bsdcat_print_error();
98 	else if (r == ARCHIVE_EOF)
99 		/* for empty payloads don't try and read data */
100 		;
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();
105 }
106 
107 int
108 main(int argc, char **argv)
109 {
110 	struct bsdcat *bsdcat, bsdcat_storage;
111 	int c;
112 
113 	bsdcat = &bsdcat_storage;
114 	memset(bsdcat, 0, sizeof(*bsdcat));
115 
116 	lafe_setprogname(*argv, "bsdcat");
117 
118 	bsdcat->argv = argv;
119 	bsdcat->argc = argc;
120 
121 	while ((c = bsdcat_getopt(bsdcat)) != -1) {
122 		switch (c) {
123 		case 'h':
124 			usage(stdout, 0);
125 			break;
126 		case OPTION_VERSION:
127 			version();
128 			break;
129 		default:
130 			usage(stderr, 1);
131 		}
132 	}
133 
134 	bsdcat_next();
135 	if (*bsdcat->argv == NULL) {
136 		bsdcat_current_path = "<stdin>";
137 		bsdcat_read_to_stdout(NULL);
138 	} else
139 		while (*bsdcat->argv) {
140 			bsdcat_current_path = *bsdcat->argv++;
141 			bsdcat_read_to_stdout(bsdcat_current_path);
142 			bsdcat_next();
143 		}
144 
145 	exit(exit_status);
146 }
147