From cd534f500edd5c04fc8e68e72eae3264dfef34fd Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 16 Aug 2010 13:05:13 +0800 Subject: easytool: ad getopt option parser --- easytool/easytool.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/easytool/easytool.c b/easytool/easytool.c index 67157c8..75cec83 100644 --- a/easytool/easytool.c +++ b/easytool/easytool.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -125,20 +126,64 @@ static void print_help(void) { } +enum mode { + MODE_DUMP_MFACC, + MODE_DUMP, + MODE_RECHARGE, + MODE_PURCHASE, +}; + int main(int argc, char **argv) { + enum mode mode = MODE_DUMP; + int delta = 0; + int option_index = 0; struct stat st; printf(COPYRIGHT); - if (argc < 2) { + while (1) { + int c; + static struct option long_options[] = { + { "dump-access-bits", 0, 0, 'a' }, + { "alter-recharge", 1, 0, 'r' }, + { "alter-purchase", 1, 0, 'p' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "r:p:ha", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 'a': + mode = MODE_DUMP_MFACC; + break; + case 'r': + mode = MODE_RECHARGE; + delta = atoi(optarg); + break; + case 'p': + mode = MODE_PURCHASE; + delta = atoi(optarg); + break; + case 'h': + print_help(); + exit(0); + break; + } + }; + + if (argc <= optind) { fprintf(stderr, "ERROR: You must specify the file name of " "a mifare dump file (.mfd)\n"); print_help(); exit(2); } - global.fd = open(argv[1], O_RDONLY); + global.fd = open(argv[optind], O_RDONLY); if (global.fd < 0) { perror("Error opening the MFD file"); exit(1); @@ -155,8 +200,18 @@ int main(int argc, char **argv) exit(1); } - //dump_mfcl(global.mft); - dump_easycard(global.mft); + switch (mode) { + case MODE_DUMP_MFACC: + dump_mfcl(global.mft); + break; + case MODE_DUMP: + dump_easycard(global.mft); + break; + case MODE_RECHARGE: + break; + case MODE_PURCHASE: + break; + } munmap(global.mft, global.size); close(global.fd); -- cgit v1.2.3