16 static int extract_num(const char *num, int len)
20 memcpy(buffer, num, len);
23 return strtoul(buffer, NULL, 16);
26 static void printf_header(const struct cpio_hdr *hdr)
28 printf(" c_magic %6.6s c_ino %8.8s c_mode %8.8o c_uid %8.8s c_gid %8.8s\n",
31 extract_num(hdr->c_mode, 8),
35 printf(" c_nlink %8.8s c_mtime %8.8s c_filesize %8.8s c_namesize %8.8s c_checksum %8.8s\n",
42 printf("c_devmajor %8.8s c_devminor %8.8s c_rdevmajor %8.8s c_rdevminor %8.8s\n",
50 static void printf_stat(const struct stat *st)
52 printf("st_ino %lx st_mode %o st_uid %x st_gid %x\n",
53 st->st_ino, st->st_mode, st->st_uid, st->st_gid);
55 printf("st_nlink %x\n", st->st_nlink);
56 printf("major %x minor %x\n", major(st->st_rdev), minor(st->st_rdev));
58 dev_t st_dev; /* ID of device containing file */
59 ino_t st_ino; /* inode number */
60 mode_t st_mode; /* protection */
61 nlink_t st_nlink; /* number of hard links */
62 uid_t st_uid; /* user ID of owner */
63 gid_t st_gid; /* group ID of owner */
64 dev_t st_rdev; /* device ID (if special file) */
65 off_t st_size; /* total size, in bytes */
66 blksize_t st_blksize; /* blocksize for filesystem I/O */
67 blkcnt_t st_blocks; /* number of blocks allocated */
68 time_t st_atime; /* time of last access */
69 time_t st_mtime; /* time of last modification */
70 time_t st_ctime; /* time of last status change */
74 static void * print_record(void *ptr)
77 struct cpio_hdr *h = ptr;
78 int namesize, filesize;
79 char *str = (char *)(h + 1);
81 if (memcmp(CPIO_MAGIC, h->c_magic, sizeof(CPIO_MAGIC)-1) != 0) {
82 printf("bad magic\n");
86 namesize = extract_num(h->c_namesize, 8);
87 filesize = extract_num(h->c_filesize, 8);
89 printf("**** [%*.*s] ****************************************************\n", namesize-1, namesize-1, str);
93 if (stat(str, &st) >= 0) {
98 return ptr + pad(namesize + sizeof(struct cpio_hdr)) + pad(filesize);
101 static void print_files(void *ptr, int len)
103 void *end = ptr + len;
104 while ((ptr = print_record(ptr))) {
110 int main(int argc, char *argv[])
117 printf("%s <file>\n", argv[0]);
121 fd = open(argv[1], O_RDONLY);
127 if (fstat(fd, &st) < 0) {
132 ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
133 if (ptr == MAP_FAILED) {
137 print_files(ptr, st.st_size);
138 munmap(ptr, st.st_size);