From 851d501c2f8e9378417472aa8e083bddb6a59450 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Wed, 19 Apr 2023 16:33:12 -0400 Subject: main: Handle options Missing: -h, -V, and -? --- (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 1201330..94fed74 100644 --- a/src/main.c +++ b/src/main.c @@ -22,45 +22,130 @@ #include "opk.h" #include "ustar.h" +#include "config.h" + +#ifdef HAVE_GETOPT_LONG +#include +#else +#include +#endif + +const char *_optstring = "I:fchV"; +#ifdef HAVE_GETOPT_LONG +struct option _longopts[] = { + { + .name = "info", + .has_arg = 1, + .flag = NULL, + .val = 'I', + }, + { + .name = "control", + .has_arg = 0, + .flag = NULL, + .val = 'f', + }, + { + .name = "contents", + .has_arg = 0, + .flag = NULL, + .val = 'c', + }, + { + .name = "help", + .has_arg = 0, + .flag = NULL, + .val = 'h', + }, + { + .name = "version", + .has_arg = 0, + .flag = NULL, + .val = 'V', + }, +}; +#endif + int main(int argc, char *argv[]) { struct opkg_opk_ustar_seek_name *control_files; + int list_members; + int opt; struct opkg_opk_opk *opk; control_files = NULL; - if (opkg_opk_ustar_add_seek_name(&control_files, "control") != - OPKG_OPK_OK) { - goto error0; - } - if (opkg_opk_ustar_add_seek_name(&control_files, "md5sums") != - OPKG_OPK_OK) { - goto error0; + list_members = 0; +#ifdef HAVE_GETOPT_LONG + while ((opt = getopt_long(argc, argv, _optstring, _longopts, NULL)) + != -1) { +#else + while ((opt = getopt(argc, argv, _optstring)) != -1) { +#endif + switch(opt) { + case 'I': + if (opkg_opk_ustar_add_seek_name(&control_files, + optarg) != + OPKG_OPK_OK) { + goto error0; + } + break; + case 'f': + if (opkg_opk_ustar_add_seek_name(&control_files, + "control") != + OPKG_OPK_OK) { + goto error0; + } + break; + case 'c': + list_members = 1; + break; + case 'h': + /* TODO */ + break; + case 'V': + /* TODO */ + break; + default: + /* TODO */ + break; + } } + argc -= optind; + argv += optind; /* Initialize outer archive. */ - opk = opkg_opk_opk_init(argv[1]); + opk = opkg_opk_opk_init(argv[0]); if (opk == NULL) { goto error0; } /* Read control file. */ - if (opkg_opk_opk_read_control(opk, control_files) != OPKG_OPK_OK) { - goto error1; + if (control_files != NULL) { + if (opkg_opk_opk_read_control(opk, control_files) != + OPKG_OPK_OK) { + goto error1; + } } /* List data files. */ - if (opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) { - goto error1; + if (list_members == 1) { + if (opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) { + goto error1; + } } opkg_opk_opk_free(opk); - opkg_opk_ustar_free_seek_names(control_files); + if (control_files != NULL) { + opkg_opk_ustar_free_seek_names(control_files); + } return EXIT_SUCCESS; error1: opkg_opk_opk_free(opk); error0: - opkg_opk_ustar_free_seek_names(control_files); + if (control_files != NULL) { + opkg_opk_ustar_free_seek_names(control_files); + } return EXIT_FAILURE; } -- cgit v0.9.1