Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 2 | #include <stdbool.h> |
| 3 | #include <getopt.h> |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 4 | |
Thomas Heijligen | 62268ee | 2023-11-27 15:10:41 +0000 | [diff] [blame] | 5 | #include "blockdev.h" |
Thomas Heijligen | 75d1ff3 | 2023-12-04 13:40:11 +0000 | [diff] [blame] | 6 | #include "vfs.h" |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 7 | |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame^] | 8 | extern void adainit (void); |
| 9 | extern void adafinal (void); |
| 10 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 11 | struct program_options { |
| 12 | char *devname; |
| 13 | }; |
| 14 | |
| 15 | static int get_options(int argc, char* argv[], struct program_options* opt); |
| 16 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 17 | |
| 18 | int main(int argc, char* argv[]) |
| 19 | { |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 20 | struct program_options opt; |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame^] | 21 | int ret = -1; |
| 22 | |
| 23 | adainit(); |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 24 | |
| 25 | if (get_options(argc, argv, &opt)) { |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame^] | 26 | goto final; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 29 | |
| 30 | if (devopen(opt.devname, NULL) != 1) { |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame^] | 31 | goto final; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame^] | 34 | fs_tests(); |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 35 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 36 | devclose(); |
Nico Huber | 9775da2 | 2024-01-23 16:23:47 +0100 | [diff] [blame^] | 37 | ret = 0; |
| 38 | |
| 39 | final: |
| 40 | adafinal(); |
| 41 | return ret; |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | static void fprint_help(FILE* to, char * pname) |
| 47 | { |
| 48 | fprintf(to, "Usage: %s\n" |
| 49 | " -d | --dev <path> \t File to use as block device\n" |
| 50 | , pname); |
| 51 | } |
| 52 | |
| 53 | static int get_options(int argc, char* argv[], struct program_options *opt){ |
| 54 | const struct option long_options[] = { |
| 55 | {"dev", required_argument, NULL, 'd'}, |
| 56 | {NULL, 0, NULL, 0}, |
| 57 | }; |
| 58 | |
| 59 | int c, option_index = 0; |
| 60 | while ((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1) { |
| 61 | switch (c) { |
| 62 | case 'd': |
| 63 | opt->devname = optarg; |
| 64 | break; |
| 65 | default: |
| 66 | fprint_help(stderr, argv[0]); |
| 67 | return -1; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (argc == 1) { |
| 72 | fprintf(stderr, "%s needs arguments.\n", argv[0]); |
| 73 | fprint_help(stderr, argv[0]); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | if (opt->devname == NULL) { |
| 78 | fprintf(stderr, "Argument \"--dev/-d\" missing.\n"); |
| 79 | return -1; |
| 80 | } |
| 81 | return 0; |
| 82 | } |