first rev of some stupid code for reading/writing cpio archives
[rcpio.git] / reader.c
diff --git a/reader.c b/reader.c
new file mode 100644 (file)
index 0000000..5aae302
--- /dev/null
+++ b/reader.c
@@ -0,0 +1,140 @@
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <sys/mman.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <string.h>
+
+#include "cpio-fmt.h"
+
+static int extract_num(const char *num, int len)
+{
+    char buffer[len];
+
+    memcpy(buffer, num, len);
+    buffer[len] = '\0';
+
+    return strtoul(buffer, NULL, 16);
+}
+
+static void printf_header(const struct cpio_hdr *hdr)
+{
+   printf("   c_magic %6.6s        c_ino %8.8s      c_mode %8.8o       c_uid %8.8s      c_gid %8.8s\n",
+                       hdr->c_magic,
+                       hdr->c_ino,
+                       extract_num(hdr->c_mode, 8),
+                       hdr->c_uid,
+                       hdr->c_gid);
+
+    printf("   c_nlink %8.8s    c_mtime %8.8s  c_filesize %8.8s  c_namesize %8.8s c_checksum %8.8s\n",
+                       hdr->c_nlink,
+                       hdr->c_mtime,
+                       hdr->c_filesize,
+                       hdr->c_namesize,
+                       hdr->c_checksum);
+
+    printf("c_devmajor %8.8s c_devminor %8.8s c_rdevmajor %8.8s c_rdevminor %8.8s\n",
+                       hdr->c_devmajor,
+                       hdr->c_devminor,
+                       hdr->c_rdevmajor,
+                       hdr->c_rdevminor);
+}
+
+
+static void printf_stat(const struct stat *st)
+{
+    printf("st_ino %lx    st_mode %o     st_uid %x st_gid %x\n",
+           st->st_ino, st->st_mode, st->st_uid, st->st_gid);
+
+    printf("st_nlink %x\n", st->st_nlink);
+    printf("major %x minor %x\n", major(st->st_rdev), minor(st->st_rdev));
+#if 0
+    dev_t     st_dev;     /* ID of device containing file */
+    ino_t     st_ino;     /* inode number */
+    mode_t    st_mode;    /* protection */
+    nlink_t   st_nlink;   /* number of hard links */
+    uid_t     st_uid;     /* user ID of owner */
+    gid_t     st_gid;     /* group ID of owner */
+    dev_t     st_rdev;    /* device ID (if special file) */
+    off_t     st_size;    /* total size, in bytes */
+    blksize_t st_blksize; /* blocksize for filesystem I/O */
+    blkcnt_t  st_blocks;  /* number of blocks allocated */
+    time_t    st_atime;   /* time of last access */
+    time_t    st_mtime;   /* time of last modification */
+    time_t    st_ctime;   /* time of last status change */
+#endif
+}
+
+static void * print_record(void *ptr)
+{
+    struct stat st;
+    struct cpio_hdr *h = ptr;
+    int namesize, filesize;
+    char *str = (char *)(h + 1);
+
+    if (memcmp(CPIO_MAGIC, h->c_magic, sizeof(CPIO_MAGIC)-1) != 0) {
+       printf("bad magic\n");
+       exit(1);
+    }
+
+    namesize = extract_num(h->c_namesize, 8);
+    filesize = extract_num(h->c_filesize, 8);
+
+    printf("**** [%*.*s] ****************************************************\n", namesize-1, namesize-1, str);
+    printf_header(ptr);
+
+    printf("\n");
+    if (stat(str, &st) >= 0) {
+       printf_stat(&st);
+    }
+    printf("\n");
+
+    return ptr + pad(namesize + sizeof(struct cpio_hdr)) + pad(filesize);
+}
+
+static void print_files(void *ptr, int len)
+{
+    void *end = ptr + len;
+    while ((ptr = print_record(ptr))) {
+       if (ptr >= end)
+           break;
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    struct stat st;
+    void *ptr;
+    int fd;
+
+    if (argc != 2) {
+       printf("%s <file>\n", argv[0]);
+       exit(1);
+    }
+
+    fd = open(argv[1], O_RDONLY);
+    if (fd < 0) {
+       perror("open");
+       exit(1);
+    }
+
+    if (fstat(fd, &st) < 0) {
+       perror("stat");
+       exit(1);
+    }
+
+    ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+    if (ptr == MAP_FAILED) {
+       perror("mmap");
+       exit(1);
+    }
+    print_files(ptr, st.st_size);
+    munmap(ptr, st.st_size);
+    return 0;
+}