Mercurial > cpython-lru_cache-weakref
annotate Python/getopt.c @ 8358:da4bdceeaad7 legacy-trunk
Another veeeeeery old patch...
Date: Thu, 14 Sep 1995 12:18:20 -0400
From: Alan Morse <alan@dvcorp.com>
To: python-list@cwi.nl
Subject: getargs bug in 1.2 and 1.3 BETA
We have found a bug in the part of the getargs code that we added
and submitted, and which was incorporated into 1.1.
The parsing of "O?" format specifiers is not handled correctly;
there is no "else" for the "if" and therefore it can never fail.
What's worse, the advancing of the varargs pointer is not
handled properly, so from then on it is out of sync, wreaking
all sorts of havoc. (If it had failed properly, then the out-of-sync
varargs would not have been an issue.)
Below is the context diff for the change.
Note that I have made a few stylistic changes beyond adding the
else case, namely:
1) Making the "O" case follow the convention established by the other
format specifiers of getting all their vararg arguments before
performing the test, rather than getting some before and some after
the test passes.
2) Making the logic of the tests parallel, so the "if" part indicates
that the format is accepted and the "else" part indicates that the
format has failed. They were inconsistent with each other and with the
the other format specifiers.
-Alan Morse (amorse@dvcorp.com)
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 15 May 1998 22:04:07 +0000 |
| parents | f39bee3ff22b |
| children | 4e15d175eed0 |
| rev | line source |
|---|---|
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
1 /*---------------------------------------------------------------------------* |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
2 * <RCS keywords> |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
3 * |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
4 * C++ Library |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
5 * |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
6 * Copyright 1992-1994, David Gottner |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
7 * |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
8 * All Rights Reserved |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
9 * |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
10 * Permission to use, copy, modify, and distribute this software and its |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
11 * documentation for any purpose and without fee is hereby granted, |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
12 * provided that the above copyright notice, this permission notice and |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
13 * the following disclaimer notice appear unmodified in all copies. |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
14 * |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
15 * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
19 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
21 * |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
22 * Nevertheless, I would like to know about bugs in this library or |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
23 * suggestions for improvment. Send bug reports and feedback to |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
24 * davegottner@delphi.com. |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
25 *---------------------------------------------------------------------------*/ |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 #include <stdio.h> |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 #include <string.h> |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
30 #define bool int |
|
5313
e8996884e8e1
(Jack:) Don't define TRUE and FALSE if already defined.
Guido van Rossum <guido@python.org>
parents:
2200
diff
changeset
|
31 #ifndef TRUE |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
32 #define TRUE 1 |
|
5313
e8996884e8e1
(Jack:) Don't define TRUE and FALSE if already defined.
Guido van Rossum <guido@python.org>
parents:
2200
diff
changeset
|
33 #endif |
|
e8996884e8e1
(Jack:) Don't define TRUE and FALSE if already defined.
Guido van Rossum <guido@python.org>
parents:
2200
diff
changeset
|
34 #ifndef FALSE |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
35 #define FALSE 0 |
|
5313
e8996884e8e1
(Jack:) Don't define TRUE and FALSE if already defined.
Guido van Rossum <guido@python.org>
parents:
2200
diff
changeset
|
36 #endif |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 bool opterr = TRUE; /* generate error messages */ |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 int optind = 1; /* index into argv array */ |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 char * optarg = NULL; /* optional argument */ |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 |
|
2200
5ae922cfe404
don't use function prototypes
Guido van Rossum <guido@python.org>
parents:
1598
diff
changeset
|
43 int getopt(argc,argv,optstring) |
|
5ae922cfe404
don't use function prototypes
Guido van Rossum <guido@python.org>
parents:
1598
diff
changeset
|
44 int argc; |
|
5ae922cfe404
don't use function prototypes
Guido van Rossum <guido@python.org>
parents:
1598
diff
changeset
|
45 char *argv[]; |
|
5ae922cfe404
don't use function prototypes
Guido van Rossum <guido@python.org>
parents:
1598
diff
changeset
|
46 char optstring[]; |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 { |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
48 static char *opt_ptr = ""; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
49 register char *ptr; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
50 int option; |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
52 if (*opt_ptr == '\0') { |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
53 |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
54 if (optind >= argc || argv[optind][0] != '-') |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
55 return -1; |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
57 else if (strcmp(argv[optind], "--") == 0) { |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
58 ++optind; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
59 return -1; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
60 } |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
62 opt_ptr = &argv[optind++][1]; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
63 } |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 |
|
6364
f39bee3ff22b
Fix a bug in this code that made it do the wrong thing when an option
Guido van Rossum <guido@python.org>
parents:
5313
diff
changeset
|
65 if ( (option = *opt_ptr++) == '\0') |
|
f39bee3ff22b
Fix a bug in this code that made it do the wrong thing when an option
Guido van Rossum <guido@python.org>
parents:
5313
diff
changeset
|
66 return -1; |
|
f39bee3ff22b
Fix a bug in this code that made it do the wrong thing when an option
Guido van Rossum <guido@python.org>
parents:
5313
diff
changeset
|
67 |
|
f39bee3ff22b
Fix a bug in this code that made it do the wrong thing when an option
Guido van Rossum <guido@python.org>
parents:
5313
diff
changeset
|
68 if ((ptr = strchr(optstring, option)) == NULL) { |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
69 if (opterr) |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
70 fprintf(stderr, "Unknown option: -%c\n", option); |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
72 return '?'; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
73 } |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
75 if (*(ptr + 1) == ':') { |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
76 if (*opt_ptr != '\0') { |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
77 optarg = opt_ptr; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
78 opt_ptr = ""; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
79 } |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
81 else { |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
82 if (optind >= argc) { |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
83 if (opterr) |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
84 fprintf(stderr, |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
85 "Argument expected for the -%c option\n", option); |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
86 return '?'; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
87 } |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
89 optarg = argv[optind++]; |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
90 } |
|
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
91 } |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 |
|
1598
ecbadb818847
New version handles -o<string> same as -o <string>
Guido van Rossum <guido@python.org>
parents:
1585
diff
changeset
|
93 return option; |
|
1585
7c75f60468f8
A getopt.c for your amusement
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 } |
