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

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