source: libpipi/trunk/genethumb/mygetopt.c @ 2694

Last change on this file since 2694 was 2546, checked in by Sam Hocevar, 15 years ago
  • mygetopt.c: fix a parsing issue with short options.
File size: 3.0 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: mygetopt.c 271 2007-02-02 11:58:06Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  mygetopt.c: getopt_long reimplementation
17 */
18
19#include "config.h"
20
21#if defined HAVE_STDINT_H
22#   include <stdint.h>
23#elif defined HAVE_INTTYPES_H
24#   include <inttypes.h>
25#endif
26
27/* DOS / Kernel driver */
28#ifndef __intptr_t_defined
29#ifndef _UINTPTR_T
30typedef unsigned int            uintptr_t;
31#endif
32#endif
33
34#include <stdio.h>
35#include <string.h>
36
37#include "mygetopt.h"
38
39int   myoptind = 1;
40char *myoptarg = NULL;
41
42/* XXX: this getopt_long implementation should not be trusted for other
43 * applications without any serious peer reviewing. It “just works” with
44 * zzuf but may fail miserably in other programs. */
45int mygetopt(int argc, char * const _argv[], const char *optstring,
46             const struct myoption *longopts, int *longindex)
47{
48    char **argv = (char **)(uintptr_t)_argv;
49    char *flag;
50    int i;
51
52    if(myoptind >= argc)
53        return -1;
54
55    flag = argv[myoptind];
56
57    if(flag[0] == '-' && flag[1] != '-')
58    {
59        char *tmp;
60        int ret = flag[1];
61
62        if(ret == '\0')
63            return -1;
64
65        tmp = strchr(optstring, ret);
66        if(!tmp || ret == ':')
67            return '?';
68
69        myoptind++;
70        if(tmp[1] == ':')
71        {
72            if(flag[2] != '\0')
73                myoptarg = flag + 2;
74            else
75                myoptarg = argv[myoptind++];
76            return ret;
77        }
78
79        if(flag[2] != '\0')
80        {
81            flag[1] = '-';
82            myoptind--;
83            argv[myoptind]++;
84        }
85
86        return ret;
87    }
88
89    if(flag[0] == '-' && flag[1] == '-')
90    {
91        if(flag[2] == '\0')
92            return -1;
93
94        for(i = 0; longopts[i].name; i++)
95        {
96            size_t l = strlen(longopts[i].name);
97
98            if(strncmp(flag + 2, longopts[i].name, l))
99                continue;
100
101            switch(flag[2 + l])
102            {
103            case '=':
104                if(!longopts[i].has_arg)
105                    goto bad_opt;
106                if(longindex)
107                    *longindex = i;
108                myoptind++;
109                myoptarg = flag + 2 + l + 1;
110                return longopts[i].val;
111            case '\0':
112                if(longindex)
113                    *longindex = i;
114                myoptind++;
115                if(longopts[i].has_arg)
116                    myoptarg = argv[myoptind++];
117                return longopts[i].val;
118            default:
119                break;
120            }
121        }
122    bad_opt:
123        fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
124        return '?';
125    }
126
127    return -1;
128}
129
Note: See TracBrowser for help on using the repository browser.