source: libcaca/trunk/src/mygetopt.c @ 2105

Last change on this file since 2105 was 1905, checked in by Jean-Yves Lamoureux, 13 years ago
  • WTF is a typedef missing ?
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
29typedef unsigned int            uintptr_t;
30#endif
31
32#include <stdio.h>
33#include <string.h>
34
35#include "mygetopt.h"
36
37int   myoptind = 1;
38char *myoptarg = NULL;
39
40/* XXX: this getopt_long implementation should not be trusted for other
41 * applications without any serious peer reviewing. It “just works” with
42 * zzuf but may fail miserably in other programs. */
43int mygetopt(int argc, char * const _argv[], const char *optstring,
44             const struct myoption *longopts, int *longindex)
45{
46    char **argv = (char **)(uintptr_t)_argv;
47    char *flag;
48    int i;
49
50    if(myoptind >= argc)
51        return -1;
52
53    flag = argv[myoptind];
54
55    if(flag[0] == '-' && flag[1] != '-')
56    {
57        char *tmp;
58        int ret = flag[1];
59
60        if(ret == '\0')
61            return -1;
62
63        tmp = strchr(optstring, ret);
64        if(!tmp || ret == ':')
65            return '?';
66
67        myoptind++;
68        if(tmp[1] == ':')
69        {
70            if(flag[2] != '\0')
71                myoptarg = tmp + 2;
72            else
73                myoptarg = argv[myoptind++];
74            return ret;
75        }
76
77        if(flag[2] != '\0')
78        {
79            flag[1] = '-';
80            myoptind--;
81            argv[myoptind]++;
82        }
83
84        return ret;
85    }
86
87    if(flag[0] == '-' && flag[1] == '-')
88    {
89        if(flag[2] == '\0')
90            return -1;
91
92        for(i = 0; longopts[i].name; i++)
93        {
94            size_t l = strlen(longopts[i].name);
95
96            if(strncmp(flag + 2, longopts[i].name, l))
97                continue;
98
99            switch(flag[2 + l])
100            {
101            case '=':
102                if(!longopts[i].has_arg)
103                    goto bad_opt;
104                if(longindex)
105                    *longindex = i;
106                myoptind++;
107                myoptarg = flag + 2 + l + 1;
108                return longopts[i].val;
109            case '\0':
110                if(longindex)
111                    *longindex = i;
112                myoptind++;
113                if(longopts[i].has_arg)
114                    myoptarg = argv[myoptind++];
115                return longopts[i].val;
116            default:
117                break;
118            }
119        }
120    bad_opt:
121        fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
122        return '?';
123    }
124
125    return -1;
126}
127
Note: See TracBrowser for help on using the repository browser.