1135642Scognet/* $NetBSD: dumptar.c,v 1.1 2004/06/16 14:28:21 christos Exp $ */ 2135642Scognet 3139735Simp/*- 4135642Scognet * Copyright (c) 2004 The NetBSD Foundation, Inc. 5135642Scognet * All rights reserved. 6135642Scognet * 7135642Scognet * This code is derived from software contributed to The NetBSD Foundation 8135642Scognet * by Christos Zoulas. 9135642Scognet * 10135642Scognet * Redistribution and use in source and binary forms, with or without 11135642Scognet * modification, are permitted provided that the following conditions 12135642Scognet * are met: 13135642Scognet * 1. Redistributions of source code must retain the above copyright 14135642Scognet * notice, this list of conditions and the following disclaimer. 15135642Scognet * 2. Redistributions in binary form must reproduce the above copyright 16135642Scognet * notice, this list of conditions and the following disclaimer in the 17135642Scognet * documentation and/or other materials provided with the distribution. 18135642Scognet * 19135642Scognet * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20135642Scognet * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21135642Scognet * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22135642Scognet * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23135642Scognet * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24135642Scognet * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25135642Scognet * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26135642Scognet * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27135642Scognet * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28135642Scognet * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29135642Scognet * POSSIBILITY OF SUCH DAMAGE. 30135642Scognet */ 31135642Scognet 32135642Scognet#include <stdio.h> 33135642Scognet#include <err.h> 34135642Scognet#include <fcntl.h> 35135642Scognet#include <sys/stat.h> 36135642Scognet#include <sys/mman.h> 37135642Scognet 38135642Scognet#include "tar.h" 39135642Scognet 40175982Sraj#define ussum(a) 1 41175982Sraj 42175982Srajstatic char * 43175982Srajbuf(const char *p, size_t s) 44175982Sraj{ 45226443Scognet static char buf[1024]; 46188540Scognet (void)snprintf(buf, sizeof(buf), "%s", p); 47188540Scognet buf[s] = '\0'; 48266311Sian return buf; 49226443Scognet} 50249582Sgabor 51226443Scognetint 52175982Srajintarg(const char *p, size_t s) 53239268Sgonzo{ 54239268Sgonzo char *ep, *b = buf(p, s); 55239268Sgonzo int r = (int)strtol(p, &ep, 8); 56239268Sgonzo return r; 57239268Sgonzo} 58239268Sgonzo 59239268Sgonzostatic int 60239268Sgonzousdump(void *p) 61175982Sraj{ 62175982Sraj HD_USTAR *t = p; 63175982Sraj int size = intarg(t->size, sizeof(t->size)); 64239268Sgonzo size = ((size + 511) / 512) * 512 + 512; 65175982Sraj 66175982Sraj (void)fprintf(stdout, "*****\n"); 67234337Sandrew#define PR(a) \ 68175982Sraj (void)fprintf(stdout, #a "=%s\n", buf(t->a, sizeof(t->a))); 69135642Scognet#define IPR(a) \ 70135642Scognet (void)fprintf(stdout, #a "=%d\n", intarg(t->a, sizeof(t->a))); 71135642Scognet#define OPR(a) \ 72135642Scognet (void)fprintf(stdout, #a "=%o\n", intarg(t->a, sizeof(t->a))); 73135642Scognet PR(name); 74135642Scognet OPR(mode); 75135642Scognet IPR(uid); 76135642Scognet IPR(gid); 77135642Scognet IPR(size); 78135642Scognet OPR(mtime); 79135642Scognet OPR(chksum); 80135642Scognet (void)fprintf(stdout, "typeflag=%c\n", t->typeflag); 81135642Scognet PR(linkname); 82142519Scognet PR(magic); 83142519Scognet PR(version); 84135642Scognet PR(uname); 85135642Scognet PR(gname); 86135642Scognet OPR(devmajor); 87135642Scognet OPR(devminor); 88135642Scognet PR(prefix); 89135642Scognet return size; 90135642Scognet} 91135642Scognet 92135642Scognetint 93135642Scognetmain(int argc, char *argv[]) 94142519Scognet{ 95135642Scognet int fd; 96135642Scognet struct stat st; 97135642Scognet char *p, *ep; 98234337Sandrew 99175982Sraj if (argc != 2) { 100175982Sraj (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname()); 101135642Scognet return 1; 102 } 103 104 if ((fd = open(argv[1], O_RDONLY)) == -1) 105 err(1, "Cannot open `%s'", argv[1]); 106 107 if (fstat(fd, &st) == -1) 108 err(1, "Cannot fstat `%s'", argv[1]); 109 110 if ((p = mmap(NULL, (size_t)st.st_size, PROT_READ, 111 MAP_FILE|MAP_PRIVATE, fd, (off_t)0)) == MAP_FAILED) 112 err(1, "Cannot mmap `%s'", argv[1]); 113 (void)close(fd); 114 115 ep = (char *)p + (size_t)st.st_size; 116 117 for (; p < ep + sizeof(HD_USTAR);) { 118 if (ussum(p)) 119 p += usdump(p); 120 } 121 return 0; 122} 123