aboutsummaryrefslogtreecommitdiff
path: root/src/args.c
blob: 6a516da1870d57d79ace588ccc97ca22ce302bdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "args.h"
int args_optind = 1;

int
args_getopt(int argc, char *argv[], const char *optstring, char **args_optarg)
{
    char option;
    if (args_optind >= argc) return -1;

    if (argv[args_optind][0] != '-'
        || strlen(argv[args_optind]) > 2
        || !strcmp(argv[args_optind], "-")) {

        *args_optarg = argv[args_optind];
        args_optind++;
        return 0;
    }

    int i;
    option = argv[args_optind][1];
    for (i = 0; i < strlen(optstring); i++) {
        if (optstring[i + 1] == ':' && optstring[i] == option) {
            *args_optarg = argv[++args_optind];
            args_optind++;
            return option;
        } else if (optstring[i + 1] != ':' && optstring[i] == option) {
            args_optind++;
            return option;
        }
    }
    args_optarg = 0;
    args_optind++;
    return -2;
}
Feel free to download, copy and edit any repo