Ignore:
Timestamp:
Feb 1, 2007, 11:33:07 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Reimplemented getopt_long. We use our replacement even if simple getopt is available on the operating system.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/zzuf.c

    r1729 r1733  
    2424#   include <inttypes.h>
    2525#endif
    26 #if defined HAVE_GETOPT_H
     26#if !defined HAVE_GETOPT_LONG
     27#   include "mygetopt.h"
     28#elif defined HAVE_GETOPT_H
    2729#   include <getopt.h>
    2830#endif
     
    6365#include "timer.h"
    6466
     67#if defined HAVE_GETOPT_LONG
     68#   define mygetopt getopt_long
     69#   define myoptind optind
     70#   define myoptarg optarg
     71#   define myoption option
     72#endif
     73
    6574#if !defined SIGKILL
    6675#   define SIGKILL 9
     
    94103#endif
    95104static void version(void);
    96 #if defined HAVE_GETOPT_H
    97105static void usage(void);
    98 #endif
    99106
    100107#if defined HAVE_WINDOWS_H
     
    125132#endif
    126133    int i;
    127 #if !defined HAVE_GETOPT_H
    128 #   define MOREINFO "Usage: %s message...\n"
    129     int optind = 1;
    130 #endif
    131134
    132135    _zz_opts_init(opts);
    133136
    134 #if defined HAVE_GETOPT_H
    135137    for(;;)
    136138    {
    137 #   if defined HAVE_REGEX_H
    138 #       define OPTSTR "Ab:B:cC:dD:E:f:F:iI:mM:nP:qr:R:s:ST:vxhV"
    139 #   else
    140 #       define OPTSTR "Ab:B:C:dD:f:F:imM:nP:qr:R:s:ST:vxhV"
    141 #   endif
    142 #   if defined HAVE_GETOPT_LONG
    143 #       define MOREINFO "Try `%s --help' for more information.\n"
     139#if defined HAVE_REGEX_H
     140#   define OPTSTR "Ab:B:cC:dD:E:f:F:iI:mM:nP:qr:R:s:ST:vxhV"
     141#else
     142#   define OPTSTR "Ab:B:C:dD:f:F:imM:nP:qr:R:s:ST:vxhV"
     143#endif
     144#define MOREINFO "Try `%s --help' for more information.\n"
    144145        int option_index = 0;
    145         static struct option long_options[] =
     146        static struct myoption long_options[] =
    146147        {
    147148            /* Long option, needs arg, flag, short option */
     
    180181            { NULL,          0, NULL,  0  }
    181182        };
    182         int c = getopt_long(argc, argv, OPTSTR, long_options, &option_index);
    183 #   else
    184 #       define MOREINFO "Try `%s -h' for more information.\n"
    185         int c = getopt(argc, argv, OPTSTR);
    186 #   endif
     183        int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index);
     184
    187185        if(c == -1)
    188186            break;
     
    194192            break;
    195193        case 'b': /* --bytes */
    196             opts->bytes = optarg;
     194            opts->bytes = myoptarg;
    197195            break;
    198196        case 'B': /* --max-bytes */
    199             opts->maxbytes = atoi(optarg);
     197            opts->maxbytes = atoi(myoptarg);
    200198            break;
    201199#if defined HAVE_REGEX_H
     
    205203#endif
    206204        case 'C': /* --max-crashes */
    207             opts->maxcrashes = atoi(optarg);
     205            opts->maxcrashes = atoi(myoptarg);
    208206            if(opts->maxcrashes <= 0)
    209207                opts->maxcrashes = 0;
     
    213211            break;
    214212        case 'D': /* --delay */
    215             opts->delay = (int64_t)(atof(optarg) * 1000000.0);
     213            opts->delay = (int64_t)(atof(myoptarg) * 1000000.0);
    216214            break;
    217215#if defined HAVE_REGEX_H
    218216        case 'E': /* --exclude */
    219             exclude = merge_regex(exclude, optarg);
     217            exclude = merge_regex(exclude, myoptarg);
    220218            if(!exclude)
    221219            {
    222                 printf("%s: invalid regex -- `%s'\n", argv[0], optarg);
     220                fprintf(stderr, "%s: invalid regex -- `%s'\n",
     221                        argv[0], myoptarg);
    223222                _zz_opts_fini(opts);
    224223                return EXIT_FAILURE;
     
    227226#endif
    228227        case 'f': /* --fuzzing */
    229             opts->fuzzing = optarg;
     228            opts->fuzzing = myoptarg;
    230229            break;
    231230        case 'F': /* --max-forks */
    232             opts->maxchild = atoi(optarg) > 1 ? atoi(optarg) : 1;
     231            opts->maxchild = atoi(myoptarg) > 1 ? atoi(myoptarg) : 1;
    233232            break;
    234233        case 'i': /* --stdin */
     
    237236#if defined HAVE_REGEX_H
    238237        case 'I': /* --include */
    239             include = merge_regex(include, optarg);
     238            include = merge_regex(include, myoptarg);
    240239            if(!include)
    241240            {
    242                 printf("%s: invalid regex -- `%s'\n", argv[0], optarg);
     241                fprintf(stderr, "%s: invalid regex -- `%s'\n",
     242                        argv[0], myoptarg);
    243243                _zz_opts_fini(opts);
    244244                return EXIT_FAILURE;
     
    252252        case 'M': /* --max-memory */
    253253            setenv("ZZUF_MEMORY", "1", 1);
    254             opts->maxmem = atoi(optarg);
     254            opts->maxmem = atoi(myoptarg);
    255255            break;
    256256#endif
     
    259259            break;
    260260        case 'P': /* --protect */
    261             opts->protect = optarg;
     261            opts->protect = myoptarg;
    262262            break;
    263263        case 'q': /* --quiet */
     
    265265            break;
    266266        case 'r': /* --ratio */
    267             tmp = strchr(optarg, ':');
    268             opts->minratio = atof(optarg);
     267            tmp = strchr(myoptarg, ':');
     268            opts->minratio = atof(myoptarg);
    269269            opts->maxratio = tmp ? atof(tmp + 1) : opts->minratio;
    270270            break;
    271271        case 'R': /* --refuse */
    272             opts->refuse = optarg;
     272            opts->refuse = myoptarg;
    273273            break;
    274274        case 's': /* --seed */
    275             tmp = strchr(optarg, ':');
    276             opts->seed = atol(optarg);
     275            tmp = strchr(myoptarg, ':');
     276            opts->seed = atol(myoptarg);
    277277            opts->endseed = tmp ? (uint32_t)atoi(tmp + 1) : opts->seed + 1;
    278278            break;
     
    281281            break;
    282282        case 'T': /* --max-time */
    283             opts->maxtime = (int64_t)(atof(optarg) * 1000000.0);
     283            opts->maxtime = (int64_t)(atof(myoptarg) * 1000000.0);
    284284            break;
    285285        case 'x': /* --check-exit */
     
    298298            return 0;
    299299        default:
    300             printf("%s: invalid option -- %c\n", argv[0], c);
     300            fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
    301301            printf(MOREINFO, argv[0]);
    302302            _zz_opts_fini(opts);
     
    304304        }
    305305    }
    306 #endif
    307306
    308307    _zz_setratio(opts->minratio, opts->maxratio);
     
    310309
    311310    /* If asked to read from the standard input */
    312     if(optind >= argc)
     311    if(myoptind >= argc)
    313312    {
    314313        if(opts->endseed != opts->seed + 1)
    315314        {
    316             printf("%s: seed ranges are incompatible with stdin fuzzing\n",
    317                    argv[0]);
     315            fprintf(stderr, "%s: seed ranges are incompatible with "
     316                            "stdin fuzzing\n", argv[0]);
    318317            printf(MOREINFO, argv[0]);
    319318            _zz_opts_fini(opts);
     
    333332        int dashdash = 0;
    334333
    335         for(i = optind + 1; i < argc; i++)
     334        for(i = myoptind + 1; i < argc; i++)
    336335        {
    337336            if(dashdash)
     
    367366    /* Create new argv */
    368367    opts->oldargv = argv;
    369     opts->newargv = malloc((argc - optind + 1) * sizeof(char *));
    370     memcpy(opts->newargv, argv + optind, (argc - optind) * sizeof(char *));
    371     opts->newargv[argc - optind] = (char *)NULL;
     368    opts->newargv = malloc((argc - myoptind + 1) * sizeof(char *));
     369    memcpy(opts->newargv, argv + myoptind, (argc - myoptind) * sizeof(char *));
     370    opts->newargv[argc - myoptind] = (char *)NULL;
    372371
    373372    /* Main loop */
     
    10511050}
    10521051
    1053 #if defined HAVE_GETOPT_H
    10541052static void usage(void)
    10551053{
     
    10661064    printf("              [PROGRAM [--] [ARGS]...]\n");
    10671065#endif
    1068 #   if defined HAVE_GETOPT_LONG
    10691066    printf("       zzuf -h | --help\n");
    10701067    printf("       zzuf -V | --version\n");
    1071 #   else
    1072     printf("       zzuf -h\n");
    1073     printf("       zzuf -V\n");
    1074 #   endif
    10751068    printf("Run PROGRAM with optional arguments ARGS and fuzz its input.\n");
    10761069    printf("\n");
    10771070    printf("Mandatory arguments to long options are mandatory for short options too.\n");
    1078 #   if defined HAVE_GETOPT_LONG
    10791071    printf("  -A, --autoinc             increment seed each time a new file is opened\n");
    10801072    printf("  -b, --bytes <ranges>      only fuzz bytes at offsets within <ranges>\n");
     
    11131105    printf("  -h, --help                display this help and exit\n");
    11141106    printf("  -V, --version             output version information and exit\n");
    1115 #   else
    1116     printf("  -A               increment seed each time a new file is opened\n");
    1117     printf("  -b <ranges>      only fuzz bytes at offsets within <ranges>\n");
    1118     printf("  -B <n>           kill children that output more than <n> bytes\n");
    1119 #if defined HAVE_REGEX_H
    1120     printf("  -c               only fuzz files specified in the command line\n");
    1121 #endif
    1122     printf("  -C <n>           stop after <n> children have crashed (default 1)\n");
    1123     printf("  -d               print debug messages\n");
    1124     printf("  -D               delay between forks\n");
    1125 #if defined HAVE_REGEX_H
    1126     printf("  -E <regex>       do not fuzz files matching <regex>\n");
    1127 #endif
    1128     printf("  -f <mode>        use fuzzing mode <mode>\n");
    1129     printf("  -F <n>           number of concurrent forks (default 1)\n");
    1130     printf("  -i               fuzz standard input\n");
    1131 #if defined HAVE_REGEX_H
    1132     printf("  -I <regex>       only fuzz files matching <regex>\n");
    1133 #endif
    1134     printf("  -m               compute the output's MD5 hash\n");
    1135 #if defined HAVE_SETRLIMIT
    1136     printf("  -M               maximum child virtual memory size in MB\n");
    1137 #endif
    1138     printf("  -n               fuzz network input\n");
    1139     printf("  -P <list>        protect bytes and characters in <list>\n");
    1140     printf("  -q               do not print the fuzzed application's messages\n");
    1141     printf("  -r <ratio>       bit fuzzing ratio (default %g)\n", DEFAULT_RATIO);
    1142     printf("     <start:stop>  specify a ratio range\n");
    1143     printf("  -R <list>        refuse bytes and characters in <list>\n");
    1144     printf("  -s <seed>        random seed (default %i)\n", DEFAULT_SEED);
    1145     printf("     <start:stop>  specify a seed range\n");
    1146     printf("  -S               prevent children from diverting crashing signals\n");
    1147     printf("  -T <n>           kill children that run for more than <n> seconds\n");
    1148     printf("  -v               print information during the run\n");
    1149     printf("  -x               report processes that exit with a non-zero status\n");
    1150     printf("  -h               display this help and exit\n");
    1151     printf("  -V               output version information and exit\n");
    1152 #   endif
    11531107    printf("\n");
    11541108    printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n");
    11551109}
    1156 #endif
    1157 
     1110
Note: See TracChangeset for help on using the changeset viewer.