File: getoptv.c.html

package info (click to toggle)
open-plc-utils 0.0.6%2Bgit20230504.1ba7d5a0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,212 kB
  • sloc: ansic: 60,875; xml: 16,179; sh: 1,216; makefile: 698
file content (283 lines) | stat: -rw-r--r-- 7,527 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?xml version='1.0' encoding='iso-8859-1'?>
<!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
	<head>
		<title>
			getoptv.c
			</title>
		<meta http-equiv='content-type' content='text/html;iso-8859-1'/>
		<meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
		<meta name='author' content='cmaier@cmassoc.net'/>
		<meta name='robots' content='noindex,nofollow'/>
		<link href='toolkit.css' rel='stylesheet' type='text/css'/>
		</head>
	<body>
		<div class='headerlink'>
			[<a href='getifname.c.html' title=' getifname.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='getpib.c.html' title=' getpib.c '>NEXT</a>]
			</div>
<pre>
/*====================================================================*
 *
 *   int getoptv (int argc, char const * argv[], char const * optv[]);
 *
 *   getoptv.h
 *
 *   this is a posix compliant getopt() function that supports no
 *   extensions; see the posix website for specification details;
 *
 *   &lt;http://www.opengroup.org/onlinepubs/007904975/functions/getopt.html&gt;
 *
 *   we implemented this function to ensure that linux and windows
 *   consoles act the same; microsoft c++ would not compile the
 *   debian version of getopt and so, after trying to fix things,
 *   we decided to start fresh; the debian version is too complex;
 *
 *   this function conforms to posix standard; it does not support
 *   gnu style extensions like &quot;--option&quot; for arguments or &quot;ab::c&quot;
 *   for operands; if you don't know what that means then you won't
 *   care, either; you should avoid such extentions, anyway;
 *
 *   the posix standard says that command options and operands must
 *   precede other arguments; this version of getopt allows options
 *   and operands to appear anywhere and makes non-compliant argv[]
 *   compliant in the process;
 *
 *   we define characters instead of coding them so that microsoft
 *   folks can use '/' instead of '-' and still preserve the posix
 *   behaviour;
 *
 *   we declare optarg as &quot;char const *&quot; so that the target cannot
 *   be changed by the application; this is not POSIX compliant so
 *   it will conflict with other getopt variants; getopt.h is often
 *   included with unistd.h which is a common file;
 *
 *   systems.
 *
 *   this version calls virtually no functions and should compile
 *   on any posix system;
 *
 *   you may include getoptv.h or declare these variables:
 *
 *    extern char const *optarg;
 *    extern int optopt;
 *    extern int optind;
 *    extern int opterr;
 *
 *   you may cut and paste this c language code segment to get you
 *   started; you must insert your own code and case breaks;
 *
 *    signed c;
 *    optind = 1;
 *    opterr = 1;
 *
 *    while ((c = getoptv(argc, argv, * optv)) != -1)
 *    {
 *       switch(c)
 *       {
 *          case 'a': // optopt is 'a'; optarg is NULL;
 *          case 'b': // optopt is 'b'; optarg is operand;
 *          case ':': // optopt is option; optarg is NULL; missing operand;
 *          case '?': // optopt is option; optarg is NULL; illegal option;
 *           default: // optopt is option: optarg is NULL; illegal option;
 *       }
 *    }
 *
 *    after options and operands are processed, optind points to
 *    the next argv [] string; loop until optind equals argc or
 *    argv[optind] is NULL; we check both but either will do;
 *
 *    while ((optind &lt; argc) &amp;&amp; (argv [optind]))
 *    {
 *       // do stuff to argv[optind++].
 *    }
 *
 *   alternately, and even better, the following works just fine:
 *
 *    argc -= optind;
 *    argv += optind;
 *    while ((argc) &amp;&amp; (* argv))
 *    {
 *       // do stuff to * argv.
 *
 *    }
 *
 *   Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
 *   Copyright (c) 2001-2006 by Charles Maier Associates;
 *   licensed under the Internet Software Consortium License;
 *
 *--------------------------------------------------------------------*/

#ifndef GETOPTV_SOURCE
#define GETOPTV_SOURCE

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

#include &quot;../tools/getoptv.h&quot;
#include &quot;../tools/putoptv.h&quot;
#include &quot;../tools/version.h&quot;
#include &quot;../tools/error.h&quot;

char const * program_name = &quot;program&quot;;
char * optarg = (char *) (0);
signed optopt = (char) (0);
signed optind = 1;
signed opterr = 1;
signed optmin = 0;
signed getoptv (int argc, char const * argv [], char const * optv [])

{
	static char const * string;
	static char const * option;
	static signed count;
	signed index;
	if ((optind == 0) || (optind == 1))
	{
		for (program_name = string = * argv; * string; string++)
		{
			if ((* string == '/') || (* string == '\\'))
			{
				program_name = string + 1;
			}
		}
		string = (char *) (0);
		if (argc == optmin)
		{
			putoptv (optv);
			exit (0);
		}
		count = optind = 1;
	}
	while ((count &lt; argc) || (string))
	{
		if (string)
		{
			if (*string)
			{
				optarg = (char *) (0);
				optopt = *string++;
				for (option = * optv; * option; option++)
				{
					if (optopt == GETOPTV_C_OPERAND)
					{
						continue;
					}
					if (*option == GETOPTV_C_OPERAND)
					{
						continue;
					}
					if (*option == optopt)
					{
						option++;
						if (*option != GETOPTV_C_OPERAND)
						{
							return (optopt);
						}
						if (*string)
						{
							optarg = (char *) (string);
							string = (char *) (0);
							return (optopt);
						}
						if (count &lt; argc)
						{
							optarg = (char *)(argv [count]);
							for (index = count++; index &gt; optind; index--)
							{
								argv [index] = argv [index - 1];
							}
							argv [optind++] = optarg;
							return (optopt);
						}
						if (opterr)
						{
							error (1, 0, &quot;option '%c' needs an operand.&quot;, optopt);
						}
						if (** optv == GETOPTV_C_OPERAND)
						{
							return (GETOPTV_C_OPERAND);
						}
						return (GETOPTV_C_ILLEGAL);
					}
				}
				if (opterr)
				{
					error (1, 0, &quot;option '%c' has no meaning.&quot;, optopt);
				}
				return (GETOPTV_C_ILLEGAL);
			}
			else
			{
				string = (char *) (0);
			}
		}
		if (count &lt; argc)
		{
			string = argv [count];
			if (*string == GETOPTV_C_OPTION)
			{
				for (index = count; index &gt; optind; index--)
				{
					argv [index] = argv [index - 1];
				}
				argv [optind++] = string++;
				if (*string == GETOPTV_C_VERSION)
				{
					version ();
					exit (0);
				}
				if (*string == GETOPTV_C_SUMMARY)
				{
					putoptv (optv);
					exit (0);
				}
				if (*string == GETOPTV_C_OPTION)
				{
					string++;
					if (!strcmp (string, &quot;&quot;))
					{
						optarg = (char *) (0);
						optopt = (char) (0);
						return (-1);
					}
					if (!strcmp (string, &quot;version&quot;))
					{
						version ();
						exit (0);
					}
					if (!strcmp (string, &quot;help&quot;))
					{
						putoptv (optv);
						exit (0);
					}
					optarg = (char *)(string);
					optopt = GETOPTV_C_OPTION;
					return (-1);
				}
			}
			else
			{
				string = (char *) (0);
			}
			count++;
		}
	}
	optarg = (char *) (0);
	optopt = (char) (0);
	return (-1);
}

#endif


</pre>
		<div class='footerlink'>
			[<a href='getifname.c.html' title=' getifname.c '>PREV</a>]
			[<a href='toolkit.html' title=' Index '>HOME</a>]
			[<a href='getpib.c.html' title=' getpib.c '>NEXT</a>]
			</div>
		</body>
	</html>