diff --git a/kpack_c.c b/kpack_c.c index ea2ab49..f1076e8 100644 --- a/kpack_c.c +++ b/kpack_c.c @@ -1,4 +1,9 @@ #include +#include +#include +#include +#include +#include /* TODO @@ -8,7 +13,67 @@ kpack [--nologo / -n] [--kernel / -k] [--unpack / -u] [] */ +static const char* str_usage = "Usage: %s [--nologo / -n] [--kernel / -k] [--unpack / -u] []\n"; + int main(int argc, char *argv[]) { - printf("Abobaa!"); + bool nologo = false; + bool kernel = false; + bool unpack = false; + char *infile = NULL; + char *outfile = NULL; + int opt; + static struct option long_options[] = { + {"nologo", no_argument, NULL, 'n'}, + {"kernel", no_argument, NULL, 'k'}, + {"unpack", no_argument, NULL, 'u'}, + {0, 0, 0, 0} // must be zeros here + }; + while ((opt = getopt_long(argc, argv, "nku", long_options, NULL)) != -1) { + switch (opt) { + case 'n': + nologo = true; + break; + case 'k': + kernel = true; + break; + case 'u': + unpack = true; + break; + default: // '?' + fprintf(stderr, str_usage, argv[0]); + return 1; + } + } + // Check for required infile + if (optind >= argc) { + fprintf(stderr, "Error: Input file is required.\n"); + fprintf(stderr, str_usage, argv[0]); + return 1; + } + infile = argv[optind]; + + // Optional outfile + if (optind + 1 < argc) { + outfile = argv[optind + 1]; + optind++; + } + + // printf("argc = %d, optind = %d\n", argc - 1, optind); + if (argc - 1 > optind) { + fprintf(stderr, "Error: too many arguments\n"); + fprintf(stderr, str_usage, argv[0]); + return 1; + } + + // for debug: print parsed options and args + printf("nologo: %s\n", nologo ? "true" : "false"); + printf("kernel: %s\n", kernel ? "true" : "false"); + printf("unpack: %s\n", unpack ? "true" : "false"); + printf("infile: %s\n", infile); + printf("outfile: %s\n", outfile ? outfile : "Not specified"); + + // TODO logic here + + return 0; } \ No newline at end of file