xref: /dragonfly/contrib/libarchive/cat/bsdcat.c (revision e95abc47)
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 static struct archive *a;
46 static struct archive_entry *ae;
47 static const char *bsdcat_current_path;
48 static int exit_status = 0;
49 
50 
51 void
usage(FILE * stream,int eval)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
version(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
bsdcat_next(void)71 bsdcat_next(void)
72 {
73 	if (a != NULL) {
74 		if (archive_read_close(a) != ARCHIVE_OK)
75 			bsdcat_print_error();
76 		archive_read_free(a);
77 	}
78 
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);
83 }
84 
85 void
bsdcat_print_error(void)86 bsdcat_print_error(void)
87 {
88 	lafe_warnc(0, "%s: %s",
89 	    bsdcat_current_path, archive_error_string(a));
90 	exit_status = 1;
91 }
92 
93 void
bsdcat_read_to_stdout(const char * filename)94 bsdcat_read_to_stdout(const char* filename)
95 {
96 	int r;
97 
98 	if (archive_read_open_filename(a, filename, BYTES_PER_BLOCK)
99 	    != ARCHIVE_OK)
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 */
106 		;
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);
112 	a = NULL;
113 }
114 
115 int
main(int argc,char ** argv)116 main(int argc, char **argv)
117 {
118 	struct bsdcat *bsdcat, bsdcat_storage;
119 	int c;
120 
121 	bsdcat = &bsdcat_storage;
122 	memset(bsdcat, 0, sizeof(*bsdcat));
123 
124 	lafe_setprogname(*argv, "bsdcat");
125 
126 	bsdcat->argv = argv;
127 	bsdcat->argc = argc;
128 
129 	while ((c = bsdcat_getopt(bsdcat)) != -1) {
130 		switch (c) {
131 		case 'h':
132 			usage(stdout, 0);
133 			break;
134 		case OPTION_VERSION:
135 			version();
136 			break;
137 		default:
138 			usage(stderr, 1);
139 		}
140 	}
141 
142 	bsdcat_next();
143 	if (*bsdcat->argv == NULL) {
144 		bsdcat_current_path = "<stdin>";
145 		bsdcat_read_to_stdout(NULL);
146 	} else {
147 		while (*bsdcat->argv) {
148 			bsdcat_current_path = *bsdcat->argv++;
149 			bsdcat_read_to_stdout(bsdcat_current_path);
150 			bsdcat_next();
151 		}
152 		archive_read_free(a); /* Help valgrind & friends */
153 	}
154 
155 	exit(exit_status);
156 }
157