source: neercs/trunk/src/mygetopt.c @ 2675

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