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 | |
| 8 | struct program_options { |
| 9 | char *devname; |
| 10 | }; |
| 11 | |
| 12 | static int get_options(int argc, char* argv[], struct program_options* opt); |
| 13 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 14 | |
| 15 | int main(int argc, char* argv[]) |
| 16 | { |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 17 | struct program_options opt; |
| 18 | |
| 19 | if (get_options(argc, argv, &opt)) { |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 20 | return -1; |
| 21 | } |
| 22 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 23 | |
| 24 | if (devopen(opt.devname, NULL) != 1) { |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 25 | return -1; |
| 26 | } |
| 27 | |
| 28 | // TODO |
| 29 | |
Thomas Heijligen | d1e0457 | 2023-11-27 14:28:55 +0000 | [diff] [blame] | 30 | devclose(); |
| 31 | return 0; |
| 32 | } |
| 33 | |
Thomas Heijligen | b00b316 | 2023-11-29 10:02:31 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | static void fprint_help(FILE* to, char * pname) |
| 37 | { |
| 38 | fprintf(to, "Usage: %s\n" |
| 39 | " -d | --dev <path> \t File to use as block device\n" |
| 40 | , pname); |
| 41 | } |
| 42 | |
| 43 | static int get_options(int argc, char* argv[], struct program_options *opt){ |
| 44 | const struct option long_options[] = { |
| 45 | {"dev", required_argument, NULL, 'd'}, |
| 46 | {NULL, 0, NULL, 0}, |
| 47 | }; |
| 48 | |
| 49 | int c, option_index = 0; |
| 50 | while ((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1) { |
| 51 | switch (c) { |
| 52 | case 'd': |
| 53 | opt->devname = optarg; |
| 54 | break; |
| 55 | default: |
| 56 | fprint_help(stderr, argv[0]); |
| 57 | return -1; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (argc == 1) { |
| 62 | fprintf(stderr, "%s needs arguments.\n", argv[0]); |
| 63 | fprint_help(stderr, argv[0]); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if (opt->devname == NULL) { |
| 68 | fprintf(stderr, "Argument \"--dev/-d\" missing.\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | return 0; |
| 72 | } |