bump for release 1.0.29
[uclibc-ng.git] / libc / unistd / getopt_long-simple.c
blob47612dfe647a6040e6cfadd5059b9d7ab951fff9
1 /*
2 * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx>
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5 */
7 #include <stddef.h>
8 #include <getopt.h>
9 #include <stdio.h>
12 static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
14 if (optind >= argc || !argv[optind] || argv[optind][0] != '-') return -1;
15 if ((longonly && argv[optind][1]) ||
16 (argv[optind][1] == '-' && argv[optind][2]))
18 int i;
19 for (i=0; longopts[i].name; i++) {
20 const char *name = longopts[i].name;
21 char *opt = argv[optind]+2;
22 while (*name && *name++ == *opt++);
23 if (*name || (*opt && *opt != '=')) continue;
24 if (*opt == '=') {
25 if (!longopts[i].has_arg) continue;
26 optarg = opt+1;
27 } else {
28 if (longopts[i].has_arg == required_argument) {
29 if (!(optarg = argv[++optind]))
30 return ':';
31 } else optarg = NULL;
33 optind++;
34 if (idx) *idx = i;
35 if (longopts[i].flag) {
36 *longopts[i].flag = longopts[i].val;
37 return 0;
39 return longopts[i].val;
41 if (argv[optind][1] == '-') {
42 optind++;
43 return '?';
46 return getopt(argc, argv, optstring);
49 int getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
51 return __getopt_long(argc, argv, optstring, longopts, idx, 0);
54 int getopt_long_only(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx)
56 return __getopt_long(argc, argv, optstring, longopts, idx, 1);