swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 1 | #include <initrd/initrd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
swissChili | b3abcd9 | 2021-03-02 20:00:44 -0800 | [diff] [blame^] | 5 | int 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 | |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 13 | int main(int argc, char **argv) |
| 14 | { |
| 15 | if (argc == 1 || (argc > 1 && !strcmp(argv[1], "-h"))) |
| 16 | { |
swissChili | b3abcd9 | 2021-03-02 20:00:44 -0800 | [diff] [blame^] | 17 | fprintf(stderr, "%s <output> [input...]\n", argv[0]); |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 18 | return argc == 1; |
| 19 | } |
| 20 | |
swissChili | b3abcd9 | 2021-03-02 20:00:44 -0800 | [diff] [blame^] | 21 | 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); |
swissChili | 8efa492 | 2021-03-02 16:34:49 -0800 | [diff] [blame] | 66 | } |