/* WARNING: assumes machine is little endian */ /* arx: extract pdp-11 ar archive */ #include #include #define min(a, b) ((a) < (b) ? (a) : (b)) struct head { char name[8]; int time; uchar uid; uchar mode; ushort size; }; void usage(void) { fprint(2, "%s: ar-file\n", argv0); exits("usage"); } void x(int in, char *name, int sz) { char nam[9]; uchar buf[512]; int out; int n; memcpy(nam, name, 8); nam[8] = '\0'; out = create(nam, OWRITE, 0666); if(out < 0) sysfatal("cannot create %s", nam); while(sz > 0){ n = read(in, buf, min(512, sz)); write(out, buf, n); sz -= n; } close(out); } void main(int argc, char *argv[]) { ushort magic; struct head h; int fd; argv0 = argv[0]; if(argc < 2) usage(); fd = open(argv[1], OREAD); if(fd < 0) sysfatal("cannot open %s", argv[1]); read(fd, &magic, 2); if(magic != 0177555) sysfatal("wrong magic: %o", magic); while(read(fd, &h, 16) == 16){ print("%.8s %d\n", h.name, h.size); x(fd, h.name, h.size); } close(fd); exits(nil); }