From 8e3c9a05e23f9664c5f3f66b47a3e1220248263f Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 3 Jan 2005 16:32:07 +0000 Subject: [PATCH] * use getopt. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/pwntcha/trunk@388 92316355-f0b4-4df1-b90c-862c8a59935f --- src/main.c | 87 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index b2204cb..6c2449d 100644 --- a/src/main.c +++ b/src/main.c @@ -11,36 +11,93 @@ #include #include +#include #include "config.h" #include "common.h" int main(int argc, char *argv[]) { - struct image *img; - char *result; + char *mode = "auto"; - if(argc != 2) + int c; + int digit_optind = 0; + + for(;;) { - fprintf(stderr, "usage: %s \n", argv[0]); - return -1; + int this_option_optind = optind ? optind : 1; + int option_index = 0; + static struct option long_options[] = + { + { "mode", 1, 0, 'm' }, + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'v' }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "hm:v", long_options, &option_index); + if(c == -1) + break; + + switch(c) + { + case 'h': /* --help */ + printf("Usage: %s [OPTION]... FILE...\n", argv[0]); + printf(" -m, --mode force operating mode\n"); + printf(" -h, --help display this help and exit\n"); + printf(" -v, --version output version information and exit\n"); + return 0; + case 'm': /* --mode */ + mode = optarg; + break; + case 'v': /* --version */ + printf("pwntcha (CAPTCHA decoder) %s\n", VERSION); + printf("Written by Sam Hocevar.\n"); + printf("\n"); + printf("Copyright (C) 2004-2005 Sam Hocevar \n"); + printf("This is free software; see the source for copying conditions. There is NO\n"); + printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"); + return 0; + case '?': + break; + default: + printf("%s: invalid option -- %i\n", argv[0], c); + printf("Try `%s --help' for more information.\n", argv[0]); + return 1; + } } - img = image_load(argv[1]); - if(!img) + if(optind >= argc) { - fprintf(stderr, "cannot load %s\n", argv[1]); - return -1; + printf("%s: too few arguments\n", argv[0]); + printf("Try `%s --help' for more information.\n", argv[0]); + return 1; } - result = decode_slashdot(img); - if(!result) + for(; optind < argc; optind++) { - fprintf(stderr, "sorry, decoding failed\n"); - return -1; - } + char *input = argv[optind], *result; + struct image *img; - printf("%s\n", result); + img = image_load(argv[optind]); + if(!img) + { + fprintf(stderr, "%s: cannot load %s\n", argv[0], input); + printf("\n"); + continue; + } + + result = decode_slashdot(img); + if(!result) + { + fprintf(stderr, "%s: sorry, decoding failed\n", argv[0]); + printf("\n"); + continue; + } + + printf("%s\n", result); + free(result); + } return 0; }