comparison lib/getopt.c @ 399:c41f22e4dae3

sync from FSF
author Jim Meyering <jim@meyering.net>
date Mon, 20 Mar 1995 15:09:56 +0000
parents db1c109d898d
children 3021132524bb
comparison
equal deleted inserted replaced
398:f016816dffa3 399:c41f22e4dae3
18 18
19 You should have received a copy of the GNU General Public License 19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software 20 along with this program; if not, write to the Free Software
21 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ 21 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 22
23 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24 Ditto for AIX 3.2 and <stdlib.h>. */
25 #ifndef _NO_PROTO
26 #define _NO_PROTO
27 #endif
28
23 #ifdef HAVE_CONFIG_H 29 #ifdef HAVE_CONFIG_H
24 #include <config.h> 30 #include <config.h>
25 #endif 31 #endif
26 32
27 #if !defined (__STDC__) || !__STDC__ 33 #if !defined (__STDC__) || !__STDC__
30 #ifndef const 36 #ifndef const
31 #define const 37 #define const
32 #endif 38 #endif
33 #endif 39 #endif
34 40
35 #define getopt _sys_getopt
36 #include <stdio.h> 41 #include <stdio.h>
37 #undef getopt
38 42
39 /* Comment out all this code if we are using the GNU C Library, and are not 43 /* Comment out all this code if we are using the GNU C Library, and are not
40 actually compiling the library itself. This code is part of the GNU C 44 actually compiling the library itself. This code is part of the GNU C
41 Library, but also included in many other GNU distributions. Compiling 45 Library, but also included in many other GNU distributions. Compiling
42 and linking in this code is a waste when using the GNU C library 46 and linking in this code is a waste when using the GNU C library
44 program understand `configure --with-gnu-libc' and omit the object files, 48 program understand `configure --with-gnu-libc' and omit the object files,
45 it is simpler to just do this in the source for each such file. */ 49 it is simpler to just do this in the source for each such file. */
46 50
47 #if defined (_LIBC) || !defined (__GNU_LIBRARY__) 51 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
48 52
49 #define getopt _sys_getopt 53
54 /* This needs to come after some library #include
55 to get __GNU_LIBRARY__ defined. */
56 #ifdef __GNU_LIBRARY__
57 /* Don't include stdlib.h for non-GNU C libraries because some of them
58 contain conflicting prototypes for getopt. */
50 #include <stdlib.h> 59 #include <stdlib.h>
51 #undef getopt 60 #endif /* GNU C library. */
61
62 /* This is for other GNU distributions with internationalized messages.
63 The GNU C Library itself does not yet support such messages. */
64 #if HAVE_LIBINTL_H
65 # include <libintl.h>
66 #else
67 # define gettext(msgid) (msgid)
68 #endif
52 69
53 /* This version of `getopt' appears to the caller like standard Unix `getopt' 70 /* This version of `getopt' appears to the caller like standard Unix `getopt'
54 but it behaves differently for the user, since it allows the user 71 but it behaves differently for the user, since it allows the user
55 to intersperse the options with the other arguments. 72 to intersperse the options with the other arguments.
56 73
144 } ordering; 161 } ordering;
145 162
146 /* Value of POSIXLY_CORRECT environment variable. */ 163 /* Value of POSIXLY_CORRECT environment variable. */
147 static char *posixly_correct; 164 static char *posixly_correct;
148 165
149 #if defined (__GNU_LIBRARY__) || defined (__sgi) 166 #ifdef __GNU_LIBRARY__
150 /* We want to avoid inclusion of string.h with non-GNU libraries 167 /* We want to avoid inclusion of string.h with non-GNU libraries
151 because there are many ways it can cause trouble. 168 because there are many ways it can cause trouble.
152 On some systems, it contains special magic macros that don't work 169 On some systems, it contains special magic macros that don't work
153 in GCC. */ 170 in GCC. */
154 #include <string.h> 171 #include <string.h>
466 int indfound; 483 int indfound;
467 int option_index; 484 int option_index;
468 485
469 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 486 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
470 /* Do nothing. */ ; 487 /* Do nothing. */ ;
471
472 #ifdef lint
473 /* Suppress `used before initialized' warning. */
474 indfound = 0;
475 #endif
476 488
477 /* Test all long options for either exact match 489 /* Test all long options for either exact match
478 or abbreviated matches. */ 490 or abbreviated matches. */
479 for (p = longopts, option_index = 0; p->name; p++, option_index++) 491 for (p = longopts, option_index = 0; p->name; p++, option_index++)
480 if (!strncmp (p->name, nextchar, nameend - nextchar)) 492 if (!strncmp (p->name, nextchar, nameend - nextchar))
499 } 511 }
500 512
501 if (ambig && !exact) 513 if (ambig && !exact)
502 { 514 {
503 if (opterr) 515 if (opterr)
504 fprintf (stderr, "%s: option `%s' is ambiguous\n", 516 fprintf (stderr, gettext ("%s: option `%s' is ambiguous\n"),
505 argv[0], argv[optind]); 517 argv[0], argv[optind]);
506 nextchar += strlen (nextchar); 518 nextchar += strlen (nextchar);
507 optind++; 519 optind++;
508 return '?'; 520 return '?';
509 } 521 }
519 if (pfound->has_arg) 531 if (pfound->has_arg)
520 optarg = nameend + 1; 532 optarg = nameend + 1;
521 else 533 else
522 { 534 {
523 if (opterr) 535 if (opterr)
524 { 536 if (argv[optind - 1][1] == '-')
525 if (argv[optind - 1][1] == '-') 537 /* --option */
526 /* --option */ 538 fprintf (stderr,
527 fprintf (stderr, 539 gettext ("%s: option `--%s' doesn't allow an argument\n"),
528 "%s: option `--%s' doesn't allow an argument\n", 540 argv[0], pfound->name);
529 argv[0], pfound->name); 541 else
530 else 542 /* +option or -option */
531 /* +option or -option */ 543 fprintf (stderr,
532 fprintf (stderr, 544 gettext ("%s: option `%c%s' doesn't allow an argument\n"),
533 "%s: option `%c%s' doesn't allow an argument\n", 545 argv[0], argv[optind - 1][0], pfound->name);
534 argv[0], argv[optind - 1][0], pfound->name); 546
535 }
536 nextchar += strlen (nextchar); 547 nextchar += strlen (nextchar);
537 return '?'; 548 return '?';
538 } 549 }
539 } 550 }
540 else if (pfound->has_arg == 1) 551 else if (pfound->has_arg == 1)
542 if (optind < argc) 553 if (optind < argc)
543 optarg = argv[optind++]; 554 optarg = argv[optind++];
544 else 555 else
545 { 556 {
546 if (opterr) 557 if (opterr)
547 fprintf (stderr, "%s: option `%s' requires an argument\n", 558 fprintf (stderr,
548 argv[0], argv[optind - 1]); 559 gettext ("%s: option `%s' requires an argument\n"),
560 argv[0], argv[optind - 1]);
549 nextchar += strlen (nextchar); 561 nextchar += strlen (nextchar);
550 return optstring[0] == ':' ? ':' : '?'; 562 return optstring[0] == ':' ? ':' : '?';
551 } 563 }
552 } 564 }
553 nextchar += strlen (nextchar); 565 nextchar += strlen (nextchar);
570 { 582 {
571 if (opterr) 583 if (opterr)
572 { 584 {
573 if (argv[optind][1] == '-') 585 if (argv[optind][1] == '-')
574 /* --option */ 586 /* --option */
575 fprintf (stderr, "%s: unrecognized option `--%s'\n", 587 fprintf (stderr, gettext ("%s: unrecognized option `--%s'\n"),
576 argv[0], nextchar); 588 argv[0], nextchar);
577 else 589 else
578 /* +option or -option */ 590 /* +option or -option */
579 fprintf (stderr, "%s: unrecognized option `%c%s'\n", 591 fprintf (stderr, gettext ("%s: unrecognized option `%c%s'\n"),
580 argv[0], argv[optind][0], nextchar); 592 argv[0], argv[optind][0], nextchar);
581 } 593 }
582 nextchar = (char *) ""; 594 nextchar = (char *) "";
583 optind++; 595 optind++;
584 return '?'; 596 return '?';
599 { 611 {
600 if (opterr) 612 if (opterr)
601 { 613 {
602 if (posixly_correct) 614 if (posixly_correct)
603 /* 1003.2 specifies the format of this message. */ 615 /* 1003.2 specifies the format of this message. */
604 fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c); 616 fprintf (stderr, gettext ("%s: illegal option -- %c\n"),
617 argv[0], c);
605 else 618 else
606 fprintf (stderr, "%s: invalid option -- %c\n", argv[0], c); 619 fprintf (stderr, gettext ("%s: invalid option -- %c\n"),
620 argv[0], c);
607 } 621 }
608 optopt = c; 622 optopt = c;
609 return '?'; 623 return '?';
610 } 624 }
611 if (temp[1] == ':') 625 if (temp[1] == ':')
635 else if (optind == argc) 649 else if (optind == argc)
636 { 650 {
637 if (opterr) 651 if (opterr)
638 { 652 {
639 /* 1003.2 specifies the format of this message. */ 653 /* 1003.2 specifies the format of this message. */
640 fprintf (stderr, "%s: option requires an argument -- %c\n", 654 fprintf (stderr,
641 argv[0], c); 655 gettext ("%s: option requires an argument -- %c\n"),
656 argv[0], c);
642 } 657 }
643 optopt = c; 658 optopt = c;
644 if (optstring[0] == ':') 659 if (optstring[0] == ':')
645 c = ':'; 660 c = ':';
646 else 661 else