annotate toys/pending/cpio.c @ 1221:8451065c5a19 draft

Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
author Rob Landley <rob@landley.net>
date Thu, 13 Mar 2014 19:42:42 -0500
parents 10d4dd6621cb
children bb9d19136eeb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
1 /* cpio.c - a basic cpio
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
2 *
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
3 * Written 2013 AD by Isaac Dunham; this code is placed under the
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
4 * same license as toybox or as CC0, at your option.
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
5 *
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
7 *
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
8 * http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
1220
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
9 * (Yes, that's SUSv2, the newer standards removed it around the time RPM
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
10 * and initramfs started heavily using this archive format. Go figure.)
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
11
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
12 USE_CPIO(NEWTOY(cpio, "H:di|o|t|uF:[!iot][!dot][!uot]", TOYFLAG_BIN))
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
13
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
14 config CPIO
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
15 bool "cpio"
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
16 default n
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
17 help
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
18 usage: cpio {-o|-t|-i[du]} [-H FMT] [-F ARCHIVE]
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
19
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
20 copy files into and out of a "newc" format cpio archive
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
21
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
22 Actions:
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
23 -o create archive (stdin is a list of files, stdout is an archive)
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
24 -t list files (stdin is an archive, stdout is a list of files)
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
25 -i extract from archive into file system (stdin is an archive)
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
26
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
27 Extract options:
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
28 -d create leading directories when extracting an archive
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
29 -u always overwrite files (default)
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
30
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
31 Other options:
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
32 -H FMT archive format (ignored, only newc supported)
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
33 -F ARCHIVE read from or write to ARCHIVE file
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
34 */
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
35
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
36 #define FOR_cpio
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
37 #include "toys.h"
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
38
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
39 GLOBALS(
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
40 char *archive;
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
41 char *fmt;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
42 )
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
43
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
44 struct newc_header {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
45 char c_magic[6];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
46 char c_ino[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
47 char c_mode[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
48 char c_uid[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
49 char c_gid[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
50 char c_nlink[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
51 char c_mtime[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
52 char c_filesize[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
53 char c_devmajor[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
54 char c_devminor[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
55 char c_rdevmajor[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
56 char c_rdevminor[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
57 char c_namesize[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
58 char c_check[8];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
59 };
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
60
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
61 void write_cpio_member(int fd, char *name, struct stat buf)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
62 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
63 char ahdr[sizeof(struct newc_header) + 1];
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
64 struct newc_header *hdr = (struct newc_header *)ahdr;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
65 size_t out = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
66 unsigned int n = 0x00000000, nlen = strlen(name) + 1;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
67
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
68 memset(hdr, '0', sizeof(struct newc_header));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
69 if (S_ISDIR(buf.st_mode) || S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
70 || S_ISFIFO(buf.st_mode) || S_ISSOCK(buf.st_mode))
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
71 buf.st_size = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
72 snprintf((char *)(hdr), sizeof(struct newc_header)+1,
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
73 "070701%08X%08X" "%08X%08X"
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
74 "%08X%08X%08X"
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
75 "%08X%08X" "%08X%08X%08X00000000",
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
76 (unsigned int)(buf.st_ino), buf.st_mode, buf.st_uid, buf.st_gid,
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
77 buf.st_nlink, (uint32_t)(buf.st_mtime), (uint32_t)(buf.st_size),
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
78 major(buf.st_dev), minor(buf.st_dev),
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
79 major(buf.st_rdev), minor(buf.st_rdev), nlen);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
80 write(1, hdr, sizeof(struct newc_header));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
81 write(1, name, nlen);
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
82 if ((nlen + 2) & 3) write(1, &n, 4 - ((nlen + 2) & 3));
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
83 if (S_ISLNK(buf.st_mode)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
84 ssize_t llen = readlink(name, toybuf, sizeof(toybuf) - 1);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
85 if (llen > 0) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
86 toybuf[llen] = '\0';
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
87 write(1, toybuf, buf.st_size);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
88 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
89 } else if (buf.st_size) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
90 for (; (lseek(fd, 0, SEEK_CUR) < (uint32_t)(buf.st_size));) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
91 out = read(fd, toybuf, sizeof(toybuf));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
92 if (out > 0) write(1, toybuf, out);
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
93 if (errno || out < sizeof(toybuf)) break;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
94 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
95 }
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
96 if (buf.st_size & 3) write(1, &n, 4 - (buf.st_size & 3));
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
97 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
98
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
99 /* Iterate through a list of files, read from stdin.
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
100 * No users need rw.
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
101 */
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
102 void loopfiles_stdin(void)
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
103 {
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
104 int fd;
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
105 struct stat st;
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
106 char *name = toybuf;
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
107
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
108 while (name) {
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
109 memset(toybuf, 0, sizeof(toybuf));
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
110 name = fgets(toybuf, sizeof(toybuf) - 1, stdin);
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
111
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
112 if (name) {
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
113 if (toybuf[strlen(name) - 1] == '\n' ) {
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
114 toybuf[strlen(name) - 1 ] = '\0';
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
115 if (lstat(name, &st) == -1) verror_msg(name, errno, NULL);
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
116 if (errno) continue;
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
117 fd = open(name, O_RDONLY);
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
118 if (fd > 0 || !S_ISREG(st.st_mode)) {
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
119 write_cpio_member(fd, name, st);
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
120 close(fd);
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
121 }
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
122 errno = 0;
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
123 }
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
124 }
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
125 }
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
126 }
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
127
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
128 //convert hex to uint; mostly to allow using bits of non-terminated strings
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
129 unsigned int htou(char * hex)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
130 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
131 unsigned int ret = 0, i = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
132
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
133 for (;(i < 8 && hex[i]);) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
134 ret *= 16;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
135 switch(hex[i]) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
136 case '0':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
137 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
138 case '1':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
139 case '2':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
140 case '3':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
141 case '4':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
142 case '5':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
143 case '6':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
144 case '7':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
145 case '8':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
146 case '9':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
147 ret += hex[i] - '1' + 1;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
148 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
149 case 'A':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
150 case 'B':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
151 case 'C':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
152 case 'D':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
153 case 'E':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
154 case 'F':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
155 ret += hex[i] - 'A' + 10;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
156 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
157 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
158 i++;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
159 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
160 return ret;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
161 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
162
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
163 // Read one cpio record. Returns 0 for last record, 1 for "continue".
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
164 int read_cpio_member(int fd, int how)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
165 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
166 uint32_t nsize, fsize;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
167 mode_t mode = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
168 int pad, ofd = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
169 struct newc_header hdr;
1220
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
170 char *name, *lastdir;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
171 dev_t dev = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
172
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
173 xreadall(fd, &hdr, sizeof(struct newc_header));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
174 nsize = htou(hdr.c_namesize);
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
175 xreadall(fd, name = xmalloc(nsize), nsize);
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
176 if (!strcmp("TRAILER!!!", name)) return 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
177 fsize = htou(hdr.c_filesize);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
178 mode += htou(hdr.c_mode);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
179 pad = 4 - ((nsize + 2) % 4); // 2 == sizeof(struct newc_header) % 4
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
180 if (pad < 4) xreadall(fd, toybuf, pad);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
181 pad = 4 - (fsize % 4);
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
182
1220
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
183 if ((toys.optflags&FLAG_d) && (lastdir = strrchr(name, '/')))
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
184 if (mkpathat(AT_FDCWD, name, 0, 2)) perror_msg("mkpath '%s'", name);
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
185
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
186 if (how & 1) {
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
187 if (S_ISDIR(mode)) ofd = mkdir(name, mode);
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
188 else if (S_ISLNK(mode)) {
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
189 memset(toybuf, 0, sizeof(toybuf));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
190 if (fsize < sizeof(toybuf)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
191 pad = readall(fd, toybuf, fsize);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
192 if (pad < fsize) error_exit("short archive");
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
193 pad = 4 - (fsize % 4);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
194 fsize = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
195 if (symlink(toybuf, name)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
196 perror_msg("could not write link %s", name);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
197 toys.exitval |= 1;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
198 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
199 } else {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
200 perror_msg("link too long: %s", name);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
201 toys.exitval |= 1;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
202 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
203 } else if (S_ISBLK(mode)||S_ISCHR(mode)||S_ISFIFO(mode)||S_ISSOCK(mode)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
204 dev = makedev(htou(hdr.c_rdevmajor),htou(hdr.c_rdevminor));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
205 ofd = mknod(name, mode, dev);
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
206 } else ofd = creat(name, mode);
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
207 if (ofd == -1) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
208 error_msg("could not create %s", name);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
209 toys.exitval |= 1;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
210 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
211 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
212 errno = 0;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
213 if (how & 2) puts(name);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
214 while (fsize) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
215 int i;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
216 memset(toybuf, 0, sizeof(toybuf));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
217 i = readall(fd, toybuf, (fsize>sizeof(toybuf)) ? sizeof(toybuf) : fsize);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
218 if (i < 1) error_exit("archive too short");
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
219 if (ofd > 0) writeall(ofd, toybuf, i);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
220 fsize -= i;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
221 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
222 if (pad < 4) xreadall(fd, toybuf, pad);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
223 return 1;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
224 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
225
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
226 void read_cpio_archive(int fd, int how)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
227 {
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
228 for(;;) if (!read_cpio_member(fd, how)) return;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
229 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
230
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
231 void cpio_main(void)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
232 {
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
233 if (TT.archive) {
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
234 if (toys.optflags & (FLAG_i|FLAG_t)) {
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
235 xclose(0);
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
236 xopen(TT.archive, O_RDONLY);
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
237 } else if (toys.optflags & FLAG_o) {
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
238 xclose(1);
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
239 xcreate(TT.archive, O_CREAT|O_WRONLY|O_TRUNC, 0755);
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
240 }
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
241 }
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
242
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
243 if (toys.optflags & FLAG_t) read_cpio_archive(0, 2);
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
244 else if (toys.optflags & FLAG_i) read_cpio_archive(0, 1);
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
245 else if (toys.optflags & FLAG_o) {
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
246 loopfiles_stdin();
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
247 write(1, "07070100000000000000000000000000000000000000010000000000000000"
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
248 "000000000000000000000000000000000000000B00000000TRAILER!!!\0\0\0", 124);
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
249 } else error_exit("must use one of -iot");
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
250 }