Home | History | Annotate | Download | only in disktest
      1 /*
      2 * Disktest
      3 * Copyright (c) International Business Machines Corp., 2001
      4 *
      5 *
      6 * This program is free software; you can redistribute it and/or modify
      7 * it under the terms of the GNU General Public License as published by
      8 * the Free Software Foundation; either version 2 of the License, or
      9 * (at your option) any later version.
     10 *
     11 * This program is distributed in the hope that it will be useful,
     12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 * GNU General Public License for more details.
     15 *
     16 * You should have received a copy of the GNU General Public License
     17 * along with this program; if not, write to the Free Software
     18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     19 *
     20 *  Please send e-mail to yardleyb (at) us.ibm.com if you have
     21 *  questions or comments.
     22 *
     23 *  Project Website:  TBD
     24 *
     25 *
     26 * $Id: Getopt.c,v 1.5 2008/02/14 08:22:22 subrata_modak Exp $
     27 * $Log: Getopt.c,v $
     28 * Revision 1.5  2008/02/14 08:22:22  subrata_modak
     29 * Disktest application update to version 1.4.2, by, Brent Yardley <yardleyb (at) us.ibm.com>
     30 *
     31 * Revision 1.4  2002/03/30 01:32:14  yardleyb
     32 * Major Changes:
     33 *
     34 * Added Dumping routines for
     35 * data miscompares,
     36 *
     37 * Updated performance output
     38 * based on command line.  Gave
     39 * one decimal in MB/s output.
     40 *
     41 * Rewrote -pL IO routine to show
     42 * correct stats.  Now show pass count
     43 * when using -C.
     44 *
     45 * Minor Changes:
     46 *
     47 * Code cleanup to remove the plethera
     48 * if #ifdef for windows/unix functional
     49 * differences.
     50 *
     51 * Revision 1.3  2002/02/21 21:32:19  yardleyb
     52 * Added more unix compatability
     53 * ifdef'd function out when
     54 * compiling for unix env. that
     55 * have getopt
     56 *
     57 * Revision 1.2  2002/02/04 20:35:38  yardleyb
     58 * Changed max. number of threads to 64k.
     59 * Check for max threads in parsing.
     60 * Fixed windows getopt to return correctly
     61 * when a bad option is given.
     62 * Update time output to be in the format:
     63 *   YEAR/MONTH/DAY-HOUR:MIN:SEC
     64 * instead of epoch time.
     65 *
     66 * Revision 1.1  2001/12/04 18:57:36  yardleyb
     67 * This source add for windows compatability only.
     68 *
     69 */
     70 
     71 #ifdef WINDOWS
     72 
     73 #include <stdio.h>
     74 #include <stddef.h>
     75 #include <ctype.h>
     76 #include <string.h>
     77 #include "getopt.h"
     78 
     79 /*
     80  *
     81  *  FUNCTION: getopt()
     82  *
     83  *	  Get next command line option and parameter
     84  *	  source gathered from MS examples and modified
     85  *	  to conform with unix like getopt.
     86  *
     87  */
     88 
     89 /* Global Exportable */
     90 int optind;
     91 char *optarg;
     92 
     93 int getopt(int argc, char **argv, char *pszValidOpts)
     94 {
     95 	char chOpt;
     96 	char *psz = NULL;
     97 	char *pszParam = NULL;
     98 	static int iArg = 1;
     99 
    100 	if (iArg < argc) {
    101 		psz = &(argv[iArg][0]);
    102 		if (*psz == '-' || *psz == '/') {
    103 			/* we have an option specifier */
    104 			chOpt = argv[iArg][1];
    105 			if (isalnum(chOpt) || ispunct(chOpt)) {
    106 				/* we have an option character */
    107 				psz = strchr(pszValidOpts, chOpt);
    108 				if (psz != NULL) {
    109 					/* option is valid, we want to return chOpt */
    110 					if (psz[1] == ':') {
    111 						/* option can have a parameter */
    112 						psz = &(argv[iArg][2]);
    113 						if (*psz == '\0') {
    114 							/* must look at next argv for param */
    115 							if (iArg + 1 < argc) {
    116 								psz =
    117 								    &(argv
    118 								      [iArg +
    119 								       1][0]);
    120 								if (*psz == '-'
    121 								    || *psz ==
    122 								    '/') {
    123 									/* next argv is a new option, so param
    124 									 * not given for current option
    125 									 */
    126 									fprintf
    127 									    (stderr,
    128 									     "-%c option requires an argument.\n",
    129 									     chOpt);
    130 									chOpt =
    131 									    '?';
    132 									pszParam
    133 									    =
    134 									    NULL;
    135 								} else {
    136 									/* next argv is the param */
    137 									iArg++;
    138 									pszParam
    139 									    =
    140 									    psz;
    141 								}
    142 							} else {
    143 								/* reached end of args looking for param */
    144 							}
    145 						} else {
    146 							/* param is attached to option */
    147 							pszParam = psz;
    148 						}
    149 					} else {
    150 						/* option is alone, has no parameter */
    151 					}
    152 				} else {
    153 					/* option specified is not in list of valid options */
    154 					fprintf(stderr,
    155 						"Invalid option -- %c\n",
    156 						chOpt);
    157 					chOpt = '?';
    158 					pszParam = NULL;
    159 				}
    160 			} else {
    161 				/* though option specifier was given, option character
    162 				 * is not alpha or was was not specified
    163 				 */
    164 				chOpt = 0;
    165 				pszParam = &(argv[iArg][0]);
    166 			}
    167 		} else {
    168 			/* standalone arg given with no option specifier */
    169 			chOpt = -1;
    170 			pszParam = &(argv[iArg][0]);
    171 		}
    172 	} else {
    173 		/* end of argument list */
    174 		chOpt = -1;
    175 	}
    176 
    177 	optind = iArg++;
    178 	optarg = pszParam;
    179 	return (chOpt);
    180 }
    181 
    182 #endif /* defined WINDOWS */
    183