1 | /* |
1 | /* |
2 | * getopt.c - Enhanced implementation of BSD getopt(1) |
2 | * getopt.c - Enhanced implementation of BSD getopt(1) |
3 | * Copyright (c) 1997-2005 Frodo Looijaard <frodo@frodo.looijaard.name> |
3 | * Copyright (c) 1997-2014 Frodo Looijaard <frodo@frodo.looijaard.name> |
4 | * |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
… | |
… | |
37 | * <misiek@pld.org.pl>) |
37 | * <misiek@pld.org.pl>) |
38 | * Version 1.1.4: Mon Nov 7 2005 |
38 | * Version 1.1.4: Mon Nov 7 2005 |
39 | * Fixed a few type's in the manpage |
39 | * Fixed a few type's in the manpage |
40 | * Version 1.1.5: Sun Aug 12 2012 |
40 | * Version 1.1.5: Sun Aug 12 2012 |
41 | * Sync with util-linux-2.21, fixed build problems, many new translations |
41 | * Sync with util-linux-2.21, fixed build problems, many new translations |
|
|
42 | * Version 1.1.6: ??? 2014 |
|
|
43 | * Sync with util-linux git 20141120, detect ambiguous long options |
42 | */ |
44 | */ |
43 | |
45 | |
44 | /* Exit codes: |
46 | /* Exit codes: |
45 | * 0) No errors, successful operation. |
47 | * 0) No errors, successful operation. |
46 | * 1) getopt(3) returned an error. |
48 | * 1) getopt(3) returned an error. |
… | |
… | |
71 | |
73 | |
72 | /* NON_OPT is the code that is returned when a non-option is found in '+' |
74 | /* NON_OPT is the code that is returned when a non-option is found in '+' |
73 | * mode */ |
75 | * mode */ |
74 | #define NON_OPT 1 |
76 | #define NON_OPT 1 |
75 | /* LONG_OPT is the code that is returned when a long option is found. */ |
77 | /* LONG_OPT is the code that is returned when a long option is found. */ |
76 | #define LONG_OPT 2 |
78 | #define LONG_OPT 0 |
77 | |
79 | |
78 | /* The shells recognized. */ |
80 | /* The shells recognized. */ |
79 | typedef enum { BASH, TCSH } shell_t; |
81 | typedef enum { BASH, TCSH } shell_t; |
80 | |
82 | |
81 | |
83 | |
… | |
… | |
238 | |
240 | |
239 | /* Register a long option. The contents of name is copied. */ |
241 | /* Register a long option. The contents of name is copied. */ |
240 | static void add_longopt(const char *name, int has_arg) |
242 | static void add_longopt(const char *name, int has_arg) |
241 | { |
243 | { |
242 | char *tmp; |
244 | char *tmp; |
|
|
245 | static int flag; |
|
|
246 | |
243 | if (!name) { |
247 | if (!name) { |
244 | /* init */ |
248 | /* init */ |
245 | free(long_options); |
249 | free(long_options); |
246 | long_options = NULL; |
250 | long_options = NULL; |
247 | long_options_length = 0; |
251 | long_options_length = 0; |
… | |
… | |
261 | long_options[long_options_nr].val = 0; |
265 | long_options[long_options_nr].val = 0; |
262 | |
266 | |
263 | if (long_options_nr && name) { |
267 | if (long_options_nr && name) { |
264 | /* Not for init! */ |
268 | /* Not for init! */ |
265 | long_options[long_options_nr - 1].has_arg = has_arg; |
269 | long_options[long_options_nr - 1].has_arg = has_arg; |
266 | long_options[long_options_nr - 1].flag = NULL; |
270 | long_options[long_options_nr - 1].flag = &flag; |
267 | long_options[long_options_nr - 1].val = LONG_OPT; |
271 | long_options[long_options_nr - 1].val = long_options_nr; |
268 | tmp = xmalloc(strlen(name) + 1); |
272 | tmp = xmalloc(strlen(name) + 1); |
269 | strcpy(tmp, name); |
273 | strcpy(tmp, name); |
270 | long_options[long_options_nr - 1].name = tmp; |
274 | long_options[long_options_nr - 1].name = tmp; |
271 | } |
275 | } |
272 | long_options_nr++; |
276 | long_options_nr++; |