Added duplicate call to fileConfig() to ensure that it cleans up after itself correctly.
[python.git] / Python / getopt.c
blob5429fac5ad5563eb30f4d6a063087dc876561f70
1 /*---------------------------------------------------------------------------*
2 * <RCS keywords>
4 * C++ Library
6 * Copyright 1992-1994, David Gottner
8 * All Rights Reserved
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.
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.
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 *---------------------------------------------------------------------------*/
27 #include <stdio.h>
28 #include <string.h>
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
34 int _PyOS_opterr = 1; /* generate error messages */
35 int _PyOS_optind = 1; /* index into argv array */
36 char *_PyOS_optarg = NULL; /* optional argument */
38 int _PyOS_GetOpt(int argc, char **argv, char *optstring)
40 static char *opt_ptr = "";
41 char *ptr;
42 int option;
44 if (*opt_ptr == '\0') {
46 if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' ||
47 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
48 return -1;
50 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
51 ++_PyOS_optind;
52 return -1;
55 opt_ptr = &argv[_PyOS_optind++][1];
58 if ( (option = *opt_ptr++) == '\0')
59 return -1;
61 if ((ptr = strchr(optstring, option)) == NULL) {
62 if (_PyOS_opterr)
63 fprintf(stderr, "Unknown option: -%c\n", option);
65 return '?';
68 if (*(ptr + 1) == ':') {
69 if (*opt_ptr != '\0') {
70 _PyOS_optarg = opt_ptr;
71 opt_ptr = "";
74 else {
75 if (_PyOS_optind >= argc) {
76 if (_PyOS_opterr)
77 fprintf(stderr,
78 "Argument expected for the -%c option\n", option);
79 return '?';
82 _PyOS_optarg = argv[_PyOS_optind++];
86 return option;
89 #ifdef __cplusplus
91 #endif