blob: 228b04b98edd468d030d7ca9af5c33d5adfa6e0c [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
56 strncpy(file.name, 64, argv[i + 2]);
57
58 char c;
59 while ((c = getc(in)) != EOF)
60 {
61 putc(c, out);
62 }
63 fclose(in);
64 }
65 fclose(out);
swissChili8efa4922021-03-02 16:34:49 -080066}