annotate toys/pending/cpio.c @ 1121:977f0a4dc562 draft

Support -F, and ignore -u since that's what we do anyway. (Really, checking the original file date is the Right Thing, but I haven't written it yet.)
author Isaac Dunham <ibid.ag@gmail.com>
date Sat, 16 Nov 2013 10:37:49 -0600
parents 46d83f3c7546
children 581c35250cff
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
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
9
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
10 USE_CPIO(NEWTOY(cpio, "H:iotuF:", 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
11
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
12 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
13 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
14 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
15 help
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
16 usage: cpio { -iu | -o | -t } [-H FMT] [-F 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
17
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
18 copy files into and out of an archive
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
19 -i extract from archive into file system (stdin is an archive)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
20 -o create archive (stdin is a list of files, stdout is an archive)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
21 -t list files (stdin is an archive, stdout is a list of files)
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
22 -u always overwrite files (current default)
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
23 -H FMT write archive in specified format:
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
24 newc SVR4 new character format (default)
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
25 -F ARCHIVE read from or write to 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
26 */
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
27 #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
28 #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
29
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
30 GLOBALS(
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
31 char * 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
32 char * fmt;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
33 )
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
34
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
35 /* Iterate through a list of files, read from stdin.
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
36 * No users need rw.
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
37 */
1098
46d83f3c7546 Here's a revised cpio.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1087
diff changeset
38 void loopfiles_stdin(void (*function)(int fd, char *name, struct stat st))
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
39 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
40 int fd;
1098
46d83f3c7546 Here's a revised cpio.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1087
diff changeset
41 struct stat st;
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 char *name = toybuf;
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 while (name != NULL){
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
45 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
46 name = fgets(toybuf, sizeof(toybuf) - 1, stdin);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
47
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
48 if (name != NULL) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
49 if (toybuf[strlen(name) - 1] == '\n' ) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
50 toybuf[strlen(name) - 1 ] = '\0';
1098
46d83f3c7546 Here's a revised cpio.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1087
diff changeset
51 if (lstat(name, &st) == -1) 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
52 fd = open(name, O_RDONLY);
1098
46d83f3c7546 Here's a revised cpio.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1087
diff changeset
53 if (fd > 0 || !S_ISREG(st.st_mode)) {
46d83f3c7546 Here's a revised cpio.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1087
diff changeset
54 function(fd, name, st);
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
55 close(fd);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
56 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
57 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
58 }
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 }
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 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
64 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
65 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
66 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 };
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
79
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
80 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
81 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
82 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
83 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
84 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
85 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
86
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
87 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
88 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
89 || 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
90 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
91 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
92 "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
93 "%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
94 "%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
95 (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
96 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
97 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
98 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
99 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
100 write(1, name, nlen);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
101 if ((nlen + 2) % 4) write(1, &n, 4 - ((nlen + 2) % 4));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
102 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
103 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
104 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
105 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
106 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
107 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
108 } 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
109 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
110 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
111 if (out > 0) write(1, toybuf, out);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
112 if (errno || out < sizeof(toybuf)) break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
113 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
114 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
115 if (buf.st_size % 4) write(1, &n, 4 - (buf.st_size % 4));
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
116 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
117
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
118 //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
119 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
120 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
121 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
122
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
123 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
124 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
125 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
126 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
127 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 break;
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 'A':
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 'B':
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 'C':
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 'D':
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 'E':
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 'F':
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
145 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
146 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
147 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
148 i++;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
149 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
150 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
151 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
152
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
153 /* Read one cpio record.
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
154 * Returns 0 for last record,
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
155 * 1 for "continue".
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
156 */
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
157 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
158 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
159 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
160 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
161 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
162 struct newc_header hdr;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
163 char *name;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
164 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
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 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
167 nsize = htou(hdr.c_namesize);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
168 name = xmalloc(nsize);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
169 xreadall(fd, name, nsize);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
170 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
171 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
172 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
173 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
174 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
175 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
176 if (how & 1) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
177 if (S_ISDIR(mode)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
178 ofd = mkdir(name, 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 } else if (S_ISLNK(mode)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187 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
188 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
189 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
190 } else {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
191 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
192 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
193 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
194 } 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
195 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
196 ofd = mknod(name, mode, dev);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
197 } else {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
198 ofd = creat(name, mode);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
199 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
200 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
201 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
202 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
203 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
204 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
215 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
216 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
217 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
218
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
219 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
220 {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
221 for(;;) {
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 (!read_cpio_member(fd, how)) return;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
223 }
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 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
227 {
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
228 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
229 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
230 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
231 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
232 } 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
233 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
234 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
235 }
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
236 }
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
237
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
238 switch (toys.optflags & (FLAG_i | FLAG_o | FLAG_t)) {
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
239 case FLAG_o:
1098
46d83f3c7546 Here's a revised cpio.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1087
diff changeset
240 loopfiles_stdin(write_cpio_member);
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
241 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
242 "000000000000000000000000000000000000000B00000000TRAILER!!!\0\0\0", 124);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
243 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
244 case FLAG_i:
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
245 read_cpio_archive(0, 1);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
246 case FLAG_t:
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
247 case (FLAG_t | FLAG_i):
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
248 read_cpio_archive(0, 2);
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
249 break;
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
250 default:
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
251 error_exit("must use one of -iot");
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
252 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
253 }