loader: remove shouting from ORB's variable name
[hvf.git] / build / mkarchive.c
blobb6d80be0869ff190838fdf7f7f2aa478de9dfe62
1 /*
2 * Copyright (c) 2011 Josef 'Jeff' Sipek
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <assert.h>
8 #include <libgen.h>
10 typedef unsigned char u8;
12 struct cpio_hdr {
13 u8 magic[6];
14 u8 dev[6];
15 u8 ino[6];
16 u8 mode[6];
17 u8 uid[6];
18 u8 gid[6];
19 u8 nlink[6];
20 u8 rdev[6];
21 u8 mtime[11];
22 u8 namesize[6];
23 u8 filesize[11];
24 u8 data[0];
27 struct buffer {
28 struct buffer *next;
29 int len;
30 u8 data[0];
33 static void storeoct(u8 *buf, int len, unsigned val)
35 len--;
37 while(val) {
38 buf[len] = '0' + (val % 8);
39 val /= 8;
40 len--;
44 static void mkhdr(struct cpio_hdr *hdr, char *fn, unsigned filesize)
46 memset(hdr, '0', sizeof(struct cpio_hdr));
48 hdr->magic[1] = '7';
49 hdr->magic[3] = '7';
50 hdr->magic[5] = '7';
52 hdr->nlink[5] = '1';
54 storeoct(hdr->namesize, 6, strlen(fn)+1);
55 storeoct(hdr->filesize, 11, filesize);
58 static unsigned __text(char *fn, int lrecl)
60 struct cpio_hdr hdr;
61 struct buffer *first, *last, *cur, *tmp;
62 FILE *f;
63 int l;
64 unsigned flen;
66 if (!lrecl) {
67 fprintf(stderr, "Error: zero lrecl is bogus\n");
68 exit(1);
71 f = fopen(fn, "rb");
72 if (!f) {
73 fprintf(stderr, "Error: could not open file '%s'\n",
74 fn);
75 exit(1);
78 flen = 0;
79 first = NULL;
80 last = NULL;
81 for(;;) {
82 cur = malloc(sizeof(struct buffer) + lrecl + 1);
83 if (!cur) {
84 fprintf(stderr, "Error: OOM\n");
85 exit(2);
88 if (!fgets((char*)cur->data, lrecl+1, f))
89 break;
91 l = strlen((char*)cur->data);
93 if (l && cur->data[l-1])
94 l--;
96 assert(l <= lrecl);
98 memset(cur->data+l, ' ', lrecl-l);
100 cur->next = NULL;
101 if (!first)
102 first = cur;
103 if (last)
104 last->next = cur;
105 last = cur;
107 flen += lrecl;
110 fclose(f);
112 mkhdr(&hdr, basename(fn), flen);
113 fwrite(&hdr, sizeof(hdr), 1, stdout);
114 fwrite(basename(fn), strlen(basename(fn))+1, 1, stdout);
116 for(cur=first; cur; cur=tmp) {
117 fwrite(cur->data, 1, lrecl, stdout);
118 tmp = cur->next;
119 free(cur);
122 return flen + strlen(basename(fn)) + 1 + sizeof(hdr);
125 #define BUF_LEN 4096
126 static unsigned __bin(char *fn)
128 struct cpio_hdr hdr;
129 struct buffer *first, *last, *cur, *tmp;
130 FILE *f;
131 unsigned flen;
132 size_t s;
134 f = fopen(fn, "rb");
135 if (!f) {
136 fprintf(stderr, "Error: could not open file '%s'\n",
137 fn);
138 exit(1);
141 flen = 0;
142 first = NULL;
143 last = NULL;
144 for(;;) {
145 cur = malloc(sizeof(struct buffer) + BUF_LEN);
146 if (!cur) {
147 fprintf(stderr, "Error: OOM\n");
148 exit(2);
151 memset(cur->data, 0, BUF_LEN);
152 if (!(s = fread(cur->data, 1, BUF_LEN, f)))
153 break;
155 cur->next = NULL;
156 cur->len = s;
157 if (!first)
158 first = cur;
159 if (last)
160 last->next = cur;
161 last = cur;
163 flen += s;
166 fclose(f);
168 mkhdr(&hdr, basename(fn), flen);
169 fwrite(&hdr, sizeof(hdr), 1, stdout);
170 fwrite(basename(fn), strlen(basename(fn))+1, 1, stdout);
172 for(cur=first; cur; cur=tmp) {
173 fwrite(cur->data, 1, cur->len, stdout);
174 tmp = cur->next;
175 free(cur);
178 return flen + strlen(basename(fn)) + 1 + sizeof(hdr);
181 static void mktail(unsigned len)
183 struct cpio_hdr hdr;
185 mkhdr(&hdr, "TRAILER!!!", 0);
186 fwrite(&hdr, sizeof(hdr), 1, stdout);
187 printf("TRAILER!!!");
189 while(len % 512) {
190 fwrite("", 1, 1, stdout); // write a nul char
191 len++;
195 int main(int argc, char **argv)
197 int lrecl;
198 char *fn;
199 int i;
200 unsigned len;
202 i=1;
203 len=0;
204 while((i+1) < argc) {
205 fn = argv[i];
206 i++;
208 if (!strcmp(argv[i], "text")) {
209 lrecl = atoi(argv[i+1]);
210 i += 2;
212 len += __text(fn, lrecl);
213 } else if (!strcmp(argv[i], "bin")) {
214 len += __bin(fn);
215 i++;
216 } else {
217 fprintf(stderr, "Error: cannot understand type '%s'\n",
218 argv[i]);
219 return 1;
223 mktail(len);
225 return 0;