blob: 5e4a66d29c87089781909a899a803123591ec5ed [file] [log] [blame]
swissChili8efa4922021-03-02 16:34:49 -08001#include <initrd/initrd.h>
2#include <stdio.h>
3#include <string.h>
4
swissChilib3abcd92021-03-02 20:00:44 -08005int file_size(FILE *file)
6{
7 fseek(file, 0, SEEK_END);
8 int size = ftell(file);
9 fseek(file, 0, SEEK_SET);
10 return size;
11}
12
swissChili8efa4922021-03-02 16:34:49 -080013int main(int argc, char **argv)
14{
15 if (argc == 1 || (argc > 1 && !strcmp(argv[1], "-h")))
16 {
swissChilib3abcd92021-03-02 20:00:44 -080017 fprintf(stderr, "%s <output> [input...]\n", argv[0]);
swissChili8efa4922021-03-02 16:34:49 -080018 return argc == 1;
19 }
20
swissChilib3abcd92021-03-02 20:00:44 -080021 FILE *out; /* output file */
22
23 if (!strcmp(argv[1], "-"))
24 out = stdin;
25 else
26 out = fopen(argv[1], "w");
27
28 if (!out)
29 {
30 fprintf(stderr, "Failed to open %s\n", argv[1]);
31 return 1;
32 }
33
34 /* Header */
35 int num_files = argc - 2;
36 struct initrd_global_header header = {
37 .magic = INITRD_MAGIC,
38 .num_files = num_files,
39 };
40
41 fwrite(&header, sizeof(header), 1, out);
42
43 for (int i = 0; i < num_files; i++)
44 {
45 FILE *in = fopen(argv[i + 2], "rb");
46 if (!in)
47 {
48 fprintf(stderr, "Failed to open %s\n", argv[i + 2]);
49 return 2;
50 }
51
52 struct initrd_file_header file = {
53 .size = file_size(in),
54 };
55
swissChilif46600c2021-03-03 12:35:33 -080056 strcpy(file.name, argv[i + 2]);
swissChilic2e62ed2021-03-10 17:04:18 -080057 fwrite(&file, sizeof(file), 1, out);
58// fprintf(stderr, "file name is %s, sizeof(file) = 0x%lx\n", file.name, sizeof(file));
swissChilib3abcd92021-03-02 20:00:44 -080059
60 char c;
61 while ((c = getc(in)) != EOF)
62 {
63 putc(c, out);
64 }
65 fclose(in);
66 }
67 fclose(out);
swissChili8efa4922021-03-02 16:34:49 -080068}