summaryrefslogtreecommitdiffstats
path: root/db_demo/getopt.c
blob: 2c65f00f7ef8b7e424c0d0a6842c97e918108366 (plain)
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


/*
* Disktest
* Copyright (c) International Business Machines Corp., 2001
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*  Please send e-mail to yardleyb@us.ibm.com if you have
*  questions or comments.
*
*  Project Website:  TBD
*
*
* $Id: Getopt.c,v 1.4 2005/05/04 17:54:00 mridge Exp $
* $Log: Getopt.c,v $
* Revision 1.4  2005/05/04 17:54:00  mridge
* Update to version 1.2.8
*
* Revision 1.4  2002/03/30 01:32:14  yardleyb
* Major Changes:
*
* Added Dumping routines for
* data miscompares,
*
* Updated performance output
* based on command line.  Gave
* one decimal in MB/s output.
*
* Rewrote -pL IO routine to show
* correct stats.  Now show pass count
* when using -C.
*
* Minor Changes:
*
* Code cleanup to remove the plethera
* if #ifdef for windows/unix functional
* differences.
*
* Revision 1.3  2002/02/21 21:32:19  yardleyb
* Added more unix compatability
* ifdef'd function out when
* compiling for unix env. that
* have getopt
*
* Revision 1.2  2002/02/04 20:35:38  yardleyb
* Changed max. number of threads to 64k.
* Check for max threads in parsing.
* Fixed windows getopt to return correctly
* when a bad option is given.
* Update time output to be in the format:
*   YEAR/MONTH/DAY-HOUR:MIN:SEC
* instead of epoch time.
*
* Revision 1.1  2001/12/04 18:57:36  yardleyb
* This source add for windows compatability only.
*
*/

#ifdef WINDOWS

#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include "getopt.h"

/*
 *
 *  FUNCTION: getopt()
 *
 *	  Get next command line option and parameter
 *	  source gathered from MS examples and modified
 *	  to conform with unix like getopt.
 *
 */

/* Global Exportable */
int optind;
char *optarg;

int getopt (int argc, char** argv, char* pszValidOpts) {
	char chOpt;
	char* psz = NULL;
	char* pszParam = NULL;
	static int iArg = 1;

	if (iArg < argc) {
		psz = &(argv[iArg][0]);
		if (*psz == '-' || *psz == '/') {
			/* we have an option specifier */
			chOpt = argv[iArg][1];
			if (isalnum(chOpt) || ispunct(chOpt)) {
				/* we have an option character */
				psz = strchr(pszValidOpts, chOpt);
				if (psz != NULL) {
					/* option is valid, we want to return chOpt */
					if (psz[1] == ':') {
						/* option can have a parameter */
						psz = &(argv[iArg][2]);
						if (*psz == '\0') {
							/* must look at next argv for param */
							if (iArg+1 < argc) {
								psz = &(argv[iArg+1][0]);
								if (*psz == '-' || *psz == '/') {
									/* next argv is a new option, so param
									 * not given for current option
									*/
									fprintf(stderr, "-%c option requires an argument.\n", chOpt);
									chOpt = '?';
									pszParam = NULL;
								} else {
									/* next argv is the param */
									iArg++;
									pszParam = psz;
								}
							} else {
								/* reached end of args looking for param */
							}
						} else {
							/* param is attached to option */
							pszParam = psz;
						}
					} else {
						/* option is alone, has no parameter */
					}
				} else {
					/* option specified is not in list of valid options */
					fprintf(stderr, "Invalid option -- %c\n", chOpt);
					chOpt = '?';
					pszParam = NULL;
				}
			} else {
				/* though option specifier was given, option character
				 * is not alpha or was was not specified
				 */
				chOpt = 0;
				pszParam = &(argv[iArg][0]);
			}
		} else {
			/* standalone arg given with no option specifier */
			chOpt = -1;
			pszParam = &(argv[iArg][0]);
		}
	} else {
		/* end of argument list */
		chOpt = -1;
	}

	optind = iArg++;
	optarg = pszParam;
	return (chOpt);
}

#endif /* defined WINDOWS */