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

Last change on this file since 2821 was 2821, checked in by Sam Hocevar, 11 years ago

Starting refactoring to get rid of libcucul. The initial reason for the
split is rendered moot by the plugin system: when enabled, binaries do
not link directly with libX11 or libGL. I hope this is a step towards
more consisteny and clarity.

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