File: getopt.c

package info (click to toggle)
af 2.4.3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,968 kB
  • ctags: 3,006
  • sloc: ansic: 31,528; makefile: 389; sh: 71
file content (144 lines) | stat: -rw-r--r-- 4,177 bytes parent folder | download | duplicates (2)
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
/* Getopt.c - Parse command line options.
   Copyright (C) 1996, 1997 Malc Arnold.

   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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */


#include <stdio.h>
   
/* Handle which string header we include */

#ifdef HAVE_STRING_H
#include <string.h>
#else /* ! HAVE_STRING_H */
#include <strings.h>
#ifndef strchr
#define strchr(s, c) index(s, c)
#endif /* ! strchr */
#endif /* ! HAVE_STRING_H */

/****************************************************************************/
/* RCS info. */

#ifndef lint
static char *RcsId = "$Id: getopt.c,v 2.1 1997/09/07 00:35:35 malc Exp $";
#endif /* ! lint */

/****************************************************************************/
/*LINTLIBRARY*/
/****************************************************************************/
/* The character returned by getopt on failure */

#define GETOPT_FAIL	'?'

/****************************************************************************/
/* These error messages are defined by POSIX */

#define	GETOPT_ILLEGAL	"%s: illegal option -- %c\n"
#define GETOPT_NOARG	"%s: option requires an argument -- %c\n"

/****************************************************************************/
/* Global variables defined by getopt */

char *optarg = NULL;		/* The value of an option's argument */
int optind = 1;			/* The next argument in argv[] to scan */
int opterr = 1;			/* Print error messages only if set to 1 */
int optopt = GETOPT_FAIL;	/* The value of any unmatched option */

/****************************************************************************/
/* Global function declarations */

extern int strcmp();

/****************************************************************************/
int getopt(argc, argv, optstring)
int argc;
char **argv, *optstring;
{
	/* Return the next option in argv, or EOF if none */

	static char *option = NULL;
	char *c;

	/* Check that we have options to process */

	if (option == NULL) {
		/* Try to find the next option argument */

		option = (optind < argc && *argv[optind] == '-')
			? argv[optind] + 1 : NULL;

		/* Check for a terminating '--' argument */

		if (option != NULL && !strcmp(argv[optind], "--")) {
			/* Clear the option and skip the argument */

			option = NULL;
			optind++;
		}

		/* Return end-of-options if we didn't find one */

		if (option == NULL) {
			return(EOF);
		}
	}

	/* Store the option found in optopt */

	optopt = *option;

	/* Now search for the option in optstring */

	if ((c = strchr(optstring, *option++)) == NULL && opterr) {
		/* Bad option, print an error message */

		(void) fprintf(stderr, GETOPT_ILLEGAL, argv[0], optopt);
	}

	/* Update the position in options and arguments */

	option = (*option != '\0') ? option : NULL;
	optind = (option == NULL && optind < argc) ? optind + 1 : optind;

	/* If we have an option, do we need an argument? */

	if (c != NULL && *(c + 1) == ':') {
		/* Get the argument and update the positions */

		optarg = (option != NULL) ? option :
			(optind < argc) ? argv[optind] : NULL;
		optind = (optarg != NULL) ? optind + 1 : optind;
		option = NULL;

		/* Handle an error extracting the argument */

		if (optarg == NULL && opterr) {
			/* No argument, print an error message */

			(void) fprintf(stderr, GETOPT_NOARG, argv[0], optopt);
			return(GETOPT_FAIL);
		} else if (optarg == NULL) {
			/* Just silently return failure */

			return(GETOPT_FAIL);
		}
	}

	/* And return the option character */

	return((c != NULL) ? optopt : GETOPT_FAIL);
}
/****************************************************************************/