annotate src/XGetopt.c @ 359:a3e674fade6c

From Jeffrey Morlan: pst_parse_block misreads Table Contexts (aka "type 2") with a multi-block Row Matrix ("ind2"). Rows are never split between blocks - every block except the last has padding at the end which should be ignored. I've only seen this affect the recipients table, but presumably it could affect attachments too. This was causing out-of-bounds memory ranges to be returned from pst_getBlockOffset and later access; patch fixes both the table reading issue and adds a missing bounds check to pst_getBlockOffset (so as not to risk a segfault if the PST is corrupted).
author Carl Byington <carl@five-ten-sg.com>
date Wed, 06 Jul 2016 10:20:12 -0700
parents fc11b1d1ad34
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 // XGetopt.cpp Version 1.1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 // Author: Hans Dietrich
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 // hdietrich2@hotmail.com
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6 // Modified: David Smith
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 // dave.s@earthcorp.com
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 // Moved two char declarations from body of function so
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 // that it can compile as a C function.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 // Thanks so much Hans
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 // This software is released into the public domain.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 // You are free to use it in any way you like.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 // This software is provided "as is" with no expressed
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 // or implied warranty. I accept no liability for any
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17 // damage or loss of business that this software may cause.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 ///////////////////////////////////////////////////////////////////////////////
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20
118
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
21 #ifndef __MINGW32__ /* mingw has getopt() */
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
22
123
ab2a11e72250 more cleanup of #include files.
Carl Byington <carl@five-ten-sg.com>
parents: 118
diff changeset
23 #include "define.h"
ab2a11e72250 more cleanup of #include files.
Carl Byington <carl@five-ten-sg.com>
parents: 118
diff changeset
24
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
25
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
26 ///////////////////////////////////////////////////////////////////////////////
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 // X G e t o p t . c p p
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31 // NAME
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 // getopt -- parse command line options
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
34 // SYNOPSIS
118
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
35 // int getopt(int argc, char* const* argv, const char *optstring)
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 // extern char *optarg;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 // extern int optind;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40 // DESCRIPTION
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41 // The getopt() function parses the command line arguments. Its
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 // arguments argc and argv are the argument count and array as
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
43 // passed into the application on program invocation. In the case
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44 // of Visual C++ programs, argc and argv are available via the
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45 // variables __argc and __argv (double underscores), respectively.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 // getopt returns the next option letter in argv that matches a
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 // letter in optstring.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49 // optstring is a string of recognized option letters; if a letter
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 // is followed by a colon, the option is expected to have an argument
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51 // that may or may not be separated from it by white space. optarg
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 // is set to point to the start of the option argument on return from
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53 // getopt.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 // Option letters may be combined, e.g., "-ab" is equivalent to
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
56 // "-a -b". Option letters are case sensitive.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
57 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
58 // getopt places in the external variable optind the argv index
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
59 // of the next argument to be processed. optind is initialized
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
60 // to 0 before the first call to getopt.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
61 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
62 // When all options have been processed (i.e., up to the first
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63 // non-option argument), getopt returns EOF, optarg will point
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
64 // to the argument, and optind will be set to the argv index of
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
65 // the argument. If there are no non-option arguments, optarg
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
66 // will be set to NULL.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
67 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68 // The special option "--" may be used to delimit the end of the
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69 // options; EOF will be returned, and "--" (and everything after it)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
70 // will be skipped.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
72 // RETURN VALUE
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 // For option letters contained in the string optstring, getopt
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74 // will return the option letter. getopt returns a question mark (?)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75 // when it encounters an option letter not included in optstring.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 // EOF is returned when processing is finished.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
77 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78 // BUGS
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
79 // 1) Long options are not supported.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80 // 2) The GNU double-colon extension is not supported.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
81 // 3) The environment variable POSIXLY_CORRECT is not supported.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
82 // 4) The + syntax is not supported.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
83 // 5) The automatic permutation of arguments is not supported.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
84 // 6) This implementation of getopt() returns EOF if an error is
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
85 // encountered, instead of -1 as the latest standard requires.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
86 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
87 // EXAMPLE
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
88 // BOOL CMyApp::ProcessCommandLine(int argc, char *argv[])
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
89 // {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
90 // int c;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
91 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
92 // while ((c = getopt(argc, argv, "aBn:")) != EOF)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
93 // {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
94 // switch (c)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
95 // {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 // case 'a':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 // TRACE(_T("option a\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 // // set some flag here
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 // break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 // case 'B':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 // TRACE( _T("option B\n"));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106 // // set some other flag here
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 // break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110 // case 'n':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 // TRACE(_T("option n: value=%d\n"), atoi(optarg));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113 // // do something with value here
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115 // break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117 // case '?':
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118 // TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 // return FALSE;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120 // break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122 // default:
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 // TRACE(_T("WARNING: no handler for option %c\n"), c);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
124 // return FALSE;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
125 // break;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
126 // }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
127 // }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
128 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
129 // // check for non-option args here
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
130 // //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
131 // return TRUE;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
132 // }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
133 //
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
134 ///////////////////////////////////////////////////////////////////////////////
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
135
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
136 char *optarg; // global argument pointer
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
137 int optind = 0; // global argv index
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
138
118
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
139 int getopt(int argc, char* const* argv, char *optstring)
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
140 {
118
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
141 static char *next = NULL;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
142 char c, *cp;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
143 if (optind == 0)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
144 next = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
145
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
146 optarg = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
147
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
148 if (next == NULL || *next == '\0')
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
149 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
150 if (optind == 0)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
151 optind++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
152
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
153 if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
154 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
155 optarg = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
156 if (optind < argc)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
157 optarg = argv[optind];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
158 return EOF;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
159 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
160
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
161 if (strcmp(argv[optind], "--") == 0)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
162 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
163 optind++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
164 optarg = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
165 if (optind < argc)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
166 optarg = argv[optind];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
167 return EOF;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
168 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
169
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
170 next = argv[optind]+1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
171 optind++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
172 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
173
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
174 c = *next++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
175 cp = strchr(optstring, c);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
176
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
177 if (cp == NULL || c == ':')
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
178 return '?';
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
179
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
180 cp++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
181 if (*cp == ':')
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
182 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
183 if (*next != '\0')
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
184 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
185 optarg = next;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
186 next = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
187 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
188 else if (optind < argc)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
189 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
190 optarg = argv[optind];
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
191 optind++;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
192 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
193 else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
194 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
195 return '?';
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
196 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
197 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
198
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
199 return c;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
200 }
118
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
201
0f1492b7fe8b patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents: 16
diff changeset
202 #endif /* !__MINGW32__ */