Nico Huber | bc6d631 | 2024-01-28 13:58:55 +0100 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <getopt.h> |
| 4 | #include <ctype.h> |
| 5 | |
| 6 | #include "blockdev.h" |
| 7 | #include "vfs.h" |
| 8 | |
| 9 | extern void adainit (void); |
| 10 | extern void adafinal (void); |
| 11 | |
| 12 | struct program_options { |
| 13 | char *devname; |
| 14 | }; |
| 15 | |
| 16 | static int get_options(int argc, char* argv[], struct program_options* opt); |
| 17 | static void cat(int argc, char *argv[]); |
| 18 | static void cat_file(const char *path); |
| 19 | |
| 20 | int main(int argc, char* argv[]) |
| 21 | { |
| 22 | struct program_options opt = { 0, }; |
| 23 | int ret = -1; |
| 24 | |
| 25 | adainit(); |
| 26 | |
| 27 | if (get_options(argc, argv, &opt)) { |
| 28 | goto final; |
| 29 | } |
| 30 | |
| 31 | if (devopen(opt.devname, NULL) != 1) { |
| 32 | goto final; |
| 33 | } |
| 34 | |
| 35 | cat(argc - optind, argv + optind); |
| 36 | |
| 37 | devclose(); |
| 38 | ret = 0; |
| 39 | |
| 40 | final: |
| 41 | adafinal(); |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | static void fprint_help(FILE* to, char * pname) |
| 46 | { |
| 47 | fprintf(to, "Usage: %s\n" |
| 48 | " -d | --dev <path> \t File to use as block device\n" |
| 49 | , pname); |
| 50 | } |
| 51 | |
| 52 | static int get_options(int argc, char* argv[], struct program_options *opt){ |
| 53 | const struct option long_options[] = { |
| 54 | {"dev", required_argument, NULL, 'd'}, |
| 55 | {NULL, 0, NULL, 0}, |
| 56 | }; |
| 57 | |
| 58 | int c, option_index = 0; |
| 59 | while ((c = getopt_long(argc, argv, "d:", long_options, &option_index)) != -1) { |
| 60 | switch (c) { |
| 61 | case 'd': |
| 62 | opt->devname = optarg; |
| 63 | break; |
| 64 | default: |
| 65 | fprint_help(stderr, argv[0]); |
| 66 | return -1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (argc == 1) { |
| 71 | fprintf(stderr, "%s needs arguments.\n", argv[0]); |
| 72 | fprint_help(stderr, argv[0]); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | if (opt->devname == NULL) { |
| 77 | fprintf(stderr, "Argument \"--dev/-d\" missing.\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static void cat(int argc, char *argv[]) |
| 84 | { |
| 85 | if (!mount_fs(NULL)) { |
| 86 | fprintf(stderr, "ERROR: Failed to mount FS.\n"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | for (; argc; --argc, ++argv) |
| 91 | cat_file(*argv); |
| 92 | } |
| 93 | |
| 94 | static void cat_file(const char *path) |
| 95 | { |
| 96 | if (!file_open(path)) { |
| 97 | fprintf(stderr, "ERROR: Failed to open `%s'.\n", path); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | unsigned char motd[1024]; |
| 102 | int read; |
| 103 | |
| 104 | do { |
| 105 | read = file_read(motd, sizeof(motd)); |
| 106 | if (read > 0) |
| 107 | fwrite(motd, 1, read, stdout); |
| 108 | } while (read == sizeof(motd)); |
| 109 | fflush(stdout); |
| 110 | file_close(); |
| 111 | |
| 112 | if (read < 0) { |
| 113 | fprintf(stderr, "ERROR: Failed to read from `%s'.\n", path); |
| 114 | return; |
| 115 | } |
| 116 | } |