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