dumptar.c revision 1.1
1/*	$NetBSD: dumptar.c,v 1.1 2004/06/16 14:28:21 christos Exp $	*/
2
3/*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <stdio.h>
40#include <err.h>
41#include <fcntl.h>
42#include <sys/stat.h>
43#include <sys/mman.h>
44
45#include "tar.h"
46
47#define ussum(a) 1
48
49static char *
50buf(const char *p, size_t s)
51{
52	static char buf[1024];
53	(void)snprintf(buf, sizeof(buf), "%s", p);
54	buf[s] = '\0';
55	return buf;
56}
57
58int
59intarg(const char *p, size_t s)
60{
61	char *ep, *b = buf(p, s);
62	int r = (int)strtol(p, &ep, 8);
63	return r;
64}
65
66static int
67usdump(void *p)
68{
69	HD_USTAR *t = p;
70	int size = intarg(t->size, sizeof(t->size));
71	size = ((size + 511) / 512) * 512 + 512;
72
73	(void)fprintf(stdout, "*****\n");
74#define PR(a) \
75	(void)fprintf(stdout, #a "=%s\n", buf(t->a, sizeof(t->a)));
76#define IPR(a) \
77	(void)fprintf(stdout, #a "=%d\n", intarg(t->a, sizeof(t->a)));
78#define OPR(a) \
79	(void)fprintf(stdout, #a "=%o\n", intarg(t->a, sizeof(t->a)));
80	PR(name);
81	OPR(mode);
82	IPR(uid);
83	IPR(gid);
84	IPR(size);
85	OPR(mtime);
86	OPR(chksum);
87	(void)fprintf(stdout, "typeflag=%c\n", t->typeflag);
88	PR(linkname);
89	PR(magic);
90	PR(version);
91	PR(uname);
92	PR(gname);
93	OPR(devmajor);
94	OPR(devminor);
95	PR(prefix);
96	return size;
97}
98
99int
100main(int argc, char *argv[])
101{
102	int fd;
103	struct stat st;
104	char *p, *ep;
105
106	if (argc != 2) {
107		(void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
108		return 1;
109	}
110
111	if ((fd = open(argv[1], O_RDONLY)) == -1)
112		err(1, "Cannot open `%s'", argv[1]);
113
114	if (fstat(fd, &st) == -1)
115		err(1, "Cannot fstat `%s'", argv[1]);
116
117	if ((p = mmap(NULL, (size_t)st.st_size, PROT_READ,
118	    MAP_FILE|MAP_PRIVATE, fd, (off_t)0)) == MAP_FAILED)
119		err(1, "Cannot mmap `%s'", argv[1]);
120	(void)close(fd);
121
122	ep = (char *)p + (size_t)st.st_size;
123
124	for (; p < ep + sizeof(HD_USTAR);) {
125		if (ussum(p))
126			p += usdump(p);
127	}
128	return 0;
129}
130