/[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 322 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 */
115 free(BUFFER); 118 free(BUFFER);
116 119
117 if (!quote) { 120 if (!quote) {
118 /* Just copy arg */ 121 /* Just copy arg */
119 BUFFER = xmalloc(strlen(arg) + 1); 122 BUFFER = xmalloc(strlen(arg) + 1);
120
121 strcpy(BUFFER, arg); 123 strcpy(BUFFER, arg);
122 return BUFFER; 124 return BUFFER;
123 } 125 }
124 126
125 /* 127 /*
196 if (opt == LONG_OPT) { 198 if (opt == LONG_OPT) {
197 printf(" --%s", longopts[longindex].name); 199 printf(" --%s", longopts[longindex].name);
198 if (longopts[longindex].has_arg) 200 if (longopts[longindex].has_arg)
199 printf(" %s", normalize(optarg ? optarg : "")); 201 printf(" %s", normalize(optarg ? optarg : ""));
200 } else if (opt == NON_OPT) 202 } else if (opt == NON_OPT)
201 printf(" %s", normalize(optarg)); 203 printf(" %s", normalize(optarg ? optarg : ""));
202 else { 204 else {
203 printf(" -%c", opt); 205 printf(" -%c", opt);
204 charptr = strchr(optstr, opt); 206 charptr = strchr(optstr, opt);
205 if (charptr != NULL && *++charptr == ':') 207 if (charptr != NULL && *++charptr == ':')
206 printf(" %s", normalize(optarg ? optarg : "")); 208 printf(" %s", normalize(optarg ? optarg : ""));
221 * 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.
222 */ 224 */
223static void __attribute__ ((__noreturn__)) parse_error(const char *message) 225static void __attribute__ ((__noreturn__)) parse_error(const char *message)
224{ 226{
225 if (message) 227 if (message)
226 fprintf(stderr, "getopt: %s\n", message); 228 fprintf(stderr, "%s: %s\n", program_invocation_short_name, message);
227 fputs(_("Try `getopt --help' for more information.\n"), stderr); 229 fprintf(stderr, _("Try `%s --help' for more information.\n"),
230 program_invocation_short_name);
228 exit(PARAMETER_EXIT_CODE); 231 exit(PARAMETER_EXIT_CODE);
229} 232}
230 233
231static struct option *long_options = NULL; 234static struct option *long_options = NULL;
232static int long_options_length = 0; /* Length of array */ 235static int long_options_length = 0; /* Length of array */
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"
328 " %1$s [options] -o|--options optstring [options] [--] parameters\n"),
329 program_invocation_short_name);
330
331 fputs(USAGE_OPTIONS, stderr);
325 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); 332 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr);
326 fputs(_(" -h, --help This small usage guide\n"), stderr);
327 fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"), stderr); 333 fputs(_(" -l, --longoptions <longopts> Long options to be recognized\n"), stderr);
328 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);
329 fputs(_(" -o, --options=optstring Short options to be recognized\n"), stderr); 335 fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr);
330 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); 336 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr);
331 fputs(_(" -Q, --quiet-output No normal output\n"), stderr); 337 fputs(_(" -Q, --quiet-output No normal output\n"), stderr);
332 fputs(_(" -s, --shell=shell Set shell quoting conventions\n"), stderr); 338 fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr);
333 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); 339 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr);
334 fputs(_(" -u, --unqote Do not quote the output\n"), stderr); 340 fputs(_(" -u, --unquoted Do not quote the output\n"), stderr);
335 fputs(_(" -V, --version Output version information\n"), stderr); 341 fputs(USAGE_SEPARATOR, stderr);
342 fputs(USAGE_HELP, stderr);
343 fputs(USAGE_VERSION, stderr);
344 fprintf(stderr, USAGE_MAN_TAIL("getopt(1)"));
336 exit(PARAMETER_EXIT_CODE); 345 exit(PARAMETER_EXIT_CODE);
337} 346}
338 347
339int main(int argc, char *argv[]) 348int main(int argc, char *argv[])
340{ 349{
357 {"alternative", no_argument, NULL, 'a'}, 366 {"alternative", no_argument, NULL, 'a'},
358 {"name", required_argument, NULL, 'n'}, 367 {"name", required_argument, NULL, 'n'},
359 {"version", no_argument, NULL, 'V'}, 368 {"version", no_argument, NULL, 'V'},
360 {NULL, 0, NULL, 0} 369 {NULL, 0, NULL, 0}
361 }; 370 };
362#if WITHOUT_GETTEXT
363#else
364 setlocale(LC_ALL, ""); 371 setlocale(LC_ALL, "");
365 bindtextdomain(PACKAGE, LOCALEDIR); 372 bindtextdomain(PACKAGE, LOCALEDIR);
366 textdomain(PACKAGE); 373 textdomain(PACKAGE);
367#endif
368 374
369 init_longopt(); 375 init_longopt();
370 getopt_long_fp = getopt_long; 376 getopt_long_fp = getopt_long;
371 377
372 if (getenv("GETOPT_COMPATIBLE")) 378 if (getenv("GETOPT_COMPATIBLE"))
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(_("getopt (enhanced) 1.1.4\n")); 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.322  
changed lines
  Added in v.363

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