1- /* An implementation of getopt() by Amrit Prem */
1+ /*---------------------------------------------------------------------------*
2+ * <RCS keywords>
3+ *
4+ * C++ Library
5+ *
6+ * Copyright 1992-1994, David Gottner
7+ *
8+ * All Rights Reserved
9+ *
10+ * Permission to use, copy, modify, and distribute this software and its
11+ * documentation for any purpose and without fee is hereby granted,
12+ * provided that the above copyright notice, this permission notice and
13+ * the following disclaimer notice appear unmodified in all copies.
14+ *
15+ * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
17+ * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
18+ * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
19+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21+ *
22+ * Nevertheless, I would like to know about bugs in this library or
23+ * suggestions for improvment. Send bug reports and feedback to
24+ * davegottner@delphi.com.
25+ *---------------------------------------------------------------------------*/
226
327#include <stdio.h>
428#include <string.h>
529
6- #define bool int
7- #define TRUE 1
8- #define FALSE 0
9- #define EOS '\0'
30+ #define bool int
31+ #define TRUE 1
32+ #define FALSE 0
33+
1034
1135bool opterr = TRUE; /* generate error messages */
1236int optind = 1 ; /* index into argv array */
@@ -15,40 +39,47 @@ char * optarg = NULL; /* optional argument */
1539
1640int getopt (int argc , char * argv [], const char optstring [])
1741{
18- static char * opt_ptr = "" ;
19- register char * ptr ;
20- int option ;
42+ static char * opt_ptr = "" ;
43+ register char * ptr ;
44+ int option ;
2145
22- if (* opt_ptr == EOS ) {
46+ if (* opt_ptr == '\0' ) {
2347
24- if (optind >= argc || argv [optind ][0 ] != '-' )
25- return -1 ;
48+ if (optind >= argc || argv [optind ][0 ] != '-' )
49+ return -1 ;
2650
27- else if (strcmp (argv [optind ], "--" ) == 0 ) {
28- ++ optind ;
29- return -1 ;
30- }
51+ else if (strcmp (argv [optind ], "--" ) == 0 ) {
52+ ++ optind ;
53+ return -1 ;
54+ }
3155
32- opt_ptr = argv [optind ++ ] + 1 ;
33- }
56+ opt_ptr = & argv [optind ++ ][ 1 ];
57+ }
3458
35- if ((ptr = strchr (optstring , option = * opt_ptr ++ )) == NULL ) {
36- if (opterr )
37- fprintf (stderr , "Unknown option: -%c\n" , option );
59+ if ((ptr = strchr (optstring , option = * opt_ptr ++ )) == NULL ) {
60+ if (opterr )
61+ fprintf (stderr , "Unknown option: -%c\n" , option );
3862
39- return '?' ;
40- }
63+ return '?' ;
64+ }
4165
42- if (* (ptr + 1 ) == ':' ) {
43- if (optind >= argc ) {
44- if (opterr )
45- fprintf (stderr , "Argument expected for the -%c option\n" , option );
66+ if (* (ptr + 1 ) == ':' ) {
67+ if (* opt_ptr != '\0' ) {
68+ optarg = opt_ptr ;
69+ opt_ptr = "" ;
70+ }
4671
47- return '?' ;
48- }
72+ else {
73+ if (optind >= argc ) {
74+ if (opterr )
75+ fprintf (stderr ,
76+ "Argument expected for the -%c option\n" , option );
77+ return '?' ;
78+ }
4979
50- optarg = argv [optind ++ ];
51- }
80+ optarg = argv [optind ++ ];
81+ }
82+ }
5283
53- return option ;
84+ return option ;
5485}
0 commit comments