5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 /* Getopt, like getopt(3). */
24 int silc_getopt(int argc, char **argv, const char *optstring, SilcGetOpt op)
28 SilcBool optional = FALSE, optional_found = FALSE;
34 if (op->opt_sp == 1) {
35 if (op->opt_index >= argc ||
36 argv[op->opt_index][0] != '-' || argv[op->opt_index][1] == '\0') {
38 } else if (strcmp(argv[op->opt_index], "--") == 0) {
43 op->opt_option = c = argv[op->opt_index][op->opt_sp];
45 if (c == ':' || (cp = strchr(optstring, c)) == NULL) {
47 fprintf(stderr, "%s: illegal option -- %c\n", argv[0], c);
48 if (argv[op->opt_index][++op->opt_sp] == '\0') {
56 /* Check for optional argument (::), must be written -oarg, not -o arg. */
57 if (strlen(cp) && *(cp + 1) == ':') {
59 if (argv[op->opt_index][op->opt_sp + 1] != '\0')
60 optional_found = TRUE;
63 if (argv[op->opt_index][op->opt_sp + 1] != '\0')
64 op->opt_arg = &argv[op->opt_index++][op->opt_sp + 1];
65 else if (++op->opt_index >= argc) {
66 if (!optional && !optional_found) {
68 fprintf(stderr, "%s: option requires an argument -- %c\n",
73 } else if (!optional || optional_found)
74 op->opt_arg = argv[op->opt_index++];
77 if (argv[op->opt_index][++op->opt_sp] == '\0') {