/[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 323 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 : ""));
220 * we already sent a message, we just exit with a helpful hint. 223 * we already sent a message, we just exit with a helpful hint.
221 */ 224 */
222static void __attribute__ ((__noreturn__)) parse_error(const char *message) 225static void __attribute__ ((__noreturn__)) parse_error(const char *message)
223{ 226{
224 if (message) 227 if (message)
225 fprintf(stderr, "%s: %s\n", ,program_short_invocation_name, message); 228 fprintf(stderr, "%s: %s\n", program_invocation_short_name, message);
226 fprintf(stderr, _("Try `%s --help' for more information.\n"), 229 fprintf(stderr, _("Try `%s --help' for more information.\n"),
227 program_invocation_short_name); 230 program_invocation_short_name);
228 exit(PARAMETER_EXIT_CODE); 231 exit(PARAMETER_EXIT_CODE);
229} 232}
230 233
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{
360 {"alternative", no_argument, NULL, 'a'}, 366 {"alternative", no_argument, NULL, 'a'},
361 {"name", required_argument, NULL, 'n'}, 367 {"name", required_argument, NULL, 'n'},
362 {"version", no_argument, NULL, 'V'}, 368 {"version", no_argument, NULL, 'V'},
363 {NULL, 0, NULL, 0} 369 {NULL, 0, NULL, 0}
364 }; 370 };
365#if WITHOUT_GETTEXT
366#else
367 setlocale(LC_ALL, ""); 371 setlocale(LC_ALL, "");
368 bindtextdomain(PACKAGE, LOCALEDIR); 372 bindtextdomain(PACKAGE, LOCALEDIR);
369 textdomain(PACKAGE); 373 textdomain(PACKAGE);
370#endif
371 374
372 init_longopt(); 375 init_longopt();
373 getopt_long_fp = getopt_long; 376 getopt_long_fp = getopt_long;
374 377
375 if (getenv("GETOPT_COMPATIBLE")) 378 if (getenv("GETOPT_COMPATIBLE"))
431 return TEST_EXIT_CODE; 434 return TEST_EXIT_CODE;
432 case 'u': 435 case 'u':
433 quote = 0; 436 quote = 0;
434 break; 437 break;
435 case 'V': 438 case 'V':
436 printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version); 439 printf(UTIL_LINUX_VERSION);
437 return EXIT_SUCCESS; 440 return EXIT_SUCCESS;
438 case '?': 441 case '?':
439 case ':': 442 case ':':
440 parse_error(NULL); 443 parse_error(NULL);
441 default: 444 default:

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

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