blob: 723e26c424c4d9d68a9f6cfb9d75efd67e58abdb [file] [log] [blame]
Thomas Heijligend1e04572023-11-27 14:28:55 +00001#include <stdio.h>
Thomas Heijligenb00b3162023-11-29 10:02:31 +00002#include <stdbool.h>
3#include <getopt.h>
Thomas Heijligend1e04572023-11-27 14:28:55 +00004
Thomas Heijligen62268ee2023-11-27 15:10:41 +00005#include "blockdev.h"
Thomas Heijligen75d1ff32023-12-04 13:40:11 +00006#include "vfs.h"
Thomas Heijligenb00b3162023-11-29 10:02:31 +00007
Nico Huber9775da22024-01-23 16:23:47 +01008extern void adainit (void);
9extern void adafinal (void);
10
Thomas Heijligenb00b3162023-11-29 10:02:31 +000011struct program_options {
12 char *devname;
13};
14
15static int get_options(int argc, char* argv[], struct program_options* opt);
16
Thomas Heijligend1e04572023-11-27 14:28:55 +000017
18int main(int argc, char* argv[])
19{
Thomas Heijligenb00b3162023-11-29 10:02:31 +000020 struct program_options opt;
Nico Huber9775da22024-01-23 16:23:47 +010021 int ret = -1;
22
23 adainit();
Thomas Heijligenb00b3162023-11-29 10:02:31 +000024
25 if (get_options(argc, argv, &opt)) {
Nico Huber9775da22024-01-23 16:23:47 +010026 goto final;
Thomas Heijligend1e04572023-11-27 14:28:55 +000027 }
28
Thomas Heijligenb00b3162023-11-29 10:02:31 +000029
30 if (devopen(opt.devname, NULL) != 1) {
Nico Huber9775da22024-01-23 16:23:47 +010031 goto final;
Thomas Heijligend1e04572023-11-27 14:28:55 +000032 }
33
Nico Huber9775da22024-01-23 16:23:47 +010034 fs_tests();
Thomas Heijligend1e04572023-11-27 14:28:55 +000035
Thomas Heijligend1e04572023-11-27 14:28:55 +000036 devclose();
Nico Huber9775da22024-01-23 16:23:47 +010037 ret = 0;
38
39final:
40 adafinal();
41 return ret;
Thomas Heijligend1e04572023-11-27 14:28:55 +000042}
43
Thomas Heijligenb00b3162023-11-29 10:02:31 +000044
45
46static 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
53static 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}