/[public]/getopt/trunk/getopt.c
ViewVC logotype

Diff of /getopt/trunk/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 325 Revision 363
31 * Version 1.0.5: Tue Jun 22 1999 31 * Version 1.0.5: Tue Jun 22 1999
32 * Make -u option work (not present) 32 * Make -u option work (not present)
33 * Version 1.0.6: Tue Jun 27 2000 33 * Version 1.0.6: Tue Jun 27 2000
34 * No important changes 34 * No important changes
35 * Version 1.1.0: Tue Jun 30 2000 35 * Version 1.1.0: Tue Jun 30 2000
36 * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz 36 * Added NLS support (partly written by Arkadiusz Miśkiewicz
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
41 * Sync with util-linux-2.21, fixed build problems, many new translations
40 */ 42 */
41 43
42/* Exit codes: 44/* Exit codes:
43 * 0) No errors, succesful operation. 45 * 0) No errors, succesful operation.
44 * 1) getopt(3) returned an error. 46 * 1) getopt(3) returned an error.
61#include <getopt.h> 63#include <getopt.h>
62#else 64#else
63#include "getopt.h" 65#include "getopt.h"
64#endif 66#endif
65 67
68#include "util-linux-compat.h"
66#include "nls.h" 69#include "nls.h"
67#include "xalloc.h" 70#include "xalloc.h"
68 71
69/* NON_OPT is the code that is returned when a non-option is found in '+' 72/* NON_OPT is the code that is returned when a non-option is found in '+'
70 * mode */ 73 * mode */
195 if (opt == LONG_OPT) { 198 if (opt == LONG_OPT) {
196 printf(" --%s", longopts[longindex].name); 199 printf(" --%s", longopts[longindex].name);
197 if (longopts[longindex].has_arg) 200 if (longopts[longindex].has_arg)
198 printf(" %s", normalize(optarg ? optarg : "")); 201 printf(" %s", normalize(optarg ? optarg : ""));
199 } else if (opt == NON_OPT) 202 } else if (opt == NON_OPT)
200 printf(" %s", normalize(optarg)); 203 printf(" %s", normalize(optarg ? optarg : ""));
201 else { 204 else {
202 printf(" -%c", opt); 205 printf(" -%c", opt);
203 charptr = strchr(optstr, opt); 206 charptr = strchr(optstr, opt);
204 if (charptr != NULL && *++charptr == ':') 207 if (charptr != NULL && *++charptr == ':')
205 printf(" %s", normalize(optarg ? optarg : "")); 208 printf(" %s", normalize(optarg ? optarg : ""));
256 long_options[long_options_nr].name = NULL; 259 long_options[long_options_nr].name = NULL;
257 long_options[long_options_nr].has_arg = 0; 260 long_options[long_options_nr].has_arg = 0;
258 long_options[long_options_nr].flag = NULL; 261 long_options[long_options_nr].flag = NULL;
259 long_options[long_options_nr].val = 0; 262 long_options[long_options_nr].val = 0;
260 263
261 if (long_options_nr) { 264 if (long_options_nr && name) {
262 /* Not for init! */ 265 /* Not for init! */
263 long_options[long_options_nr - 1].has_arg = has_arg; 266 long_options[long_options_nr - 1].has_arg = has_arg;
264 long_options[long_options_nr - 1].flag = NULL; 267 long_options[long_options_nr - 1].flag = NULL;
265 long_options[long_options_nr - 1].val = LONG_OPT; 268 long_options[long_options_nr - 1].val = LONG_OPT;
266 tmp = xmalloc(strlen(name) + 1); 269 tmp = xmalloc(strlen(name) + 1);
316 ("unknown shell after -s or --shell argument")); 319 ("unknown shell after -s or --shell argument"));
317} 320}
318 321
319static void __attribute__ ((__noreturn__)) print_help(void) 322static void __attribute__ ((__noreturn__)) print_help(void)
320{ 323{
321 fputs(_("Usage: getopt optstring parameters\n"), stderr); 324 fputs(USAGE_HEADER, stderr);
322 fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); 325 fprintf(stderr, _(
323 fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); 326 " %1$s optstring parameters\n"
324 fputs(_(" parameters\n"), stderr); 327 " %1$s [options] [--] optstring parameters\n"
325 fputs(_("\nOptions:\n"), stderr); 328 " %1$s [options] -o|--options optstring [options] [--] parameters\n"),
329 program_invocation_short_name);
330
331 fputs(USAGE_OPTIONS, stderr);
326 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); 332 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr);
327 fputs(_(" -h, --help This small usage guide\n"), stderr);
328 fputs(_(" -l, --longoptions <longopts> Long options to be recognized\n"), stderr); 333 fputs(_(" -l, --longoptions <longopts> Long options to be recognized\n"), stderr);
329 fputs(_(" -n, --name <progname> The name under which errors are reported\n"), stderr); 334 fputs(_(" -n, --name <progname> The name under which errors are reported\n"), stderr);
330 fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr); 335 fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr);
331 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); 336 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr);
332 fputs(_(" -Q, --quiet-output No normal output\n"), stderr); 337 fputs(_(" -Q, --quiet-output No normal output\n"), stderr);
333 fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr); 338 fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr);
334 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); 339 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr);
335 fputs(_(" -u, --unquote Do not quote the output\n"), stderr); 340 fputs(_(" -u, --unquoted Do not quote the output\n"), stderr);
336 fputs(_(" -V, --version Output version information\n"), stderr); 341 fputs(USAGE_SEPARATOR, stderr);
337 fputc('\n', stderr); 342 fputs(USAGE_HELP, stderr);
338 343 fputs(USAGE_VERSION, stderr);
344 fprintf(stderr, USAGE_MAN_TAIL("getopt(1)"));
339 exit(PARAMETER_EXIT_CODE); 345 exit(PARAMETER_EXIT_CODE);
340} 346}
341 347
342int main(int argc, char *argv[]) 348int main(int argc, char *argv[])
343{ 349{
428 return TEST_EXIT_CODE; 434 return TEST_EXIT_CODE;
429 case 'u': 435 case 'u':
430 quote = 0; 436 quote = 0;
431 break; 437 break;
432 case 'V': 438 case 'V':
433 printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version); 439 printf(UTIL_LINUX_VERSION);
434 return EXIT_SUCCESS; 440 return EXIT_SUCCESS;
435 case '?': 441 case '?':
436 case ':': 442 case ':':
437 parse_error(NULL); 443 parse_error(NULL);
438 default: 444 default:

Legend:
Removed from v.325  
changed lines
  Added in v.363

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26