Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
getopt.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * From Henry Spencer @ U of Toronto Zoology, slightly edited.
20  */
21 
22 /*
23  * getopt - get option letter from argv
24  */
25 
26 #include "rle_config.h"
27 #include <stdio.h>
28 
29 char *optarg; /* Global argument pointer. */
30 int optind = 0; /* Global argv index. */
31 
32 static char *scan = NULL; /* Private scan pointer. */
33 
34 extern char *index();
35 
36 int
38 int argc;
39 char *argv[];
40 char *optstring;
41 {
42  register char c;
43  register char *place;
44 
45  optarg = NULL;
46 
47  if (scan == NULL || *scan == '\0') {
48  if (optind == 0)
49  optind++;
50 
51  if (optind >= argc || argv[optind][0] != '-' ||
52  argv[optind][1] == '\0')
53  return( EOF );
54 
55  if (strcmp(argv[optind], "--")==0) {
56  optind++;
57  return( EOF );
58  }
59 
60  scan = argv[optind]+1;
61  optind++;
62  }
63 
64  c = *scan++;
65  place = index(optstring, c);
66 
67  if (place == NULL || c == ':') {
68  fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
69  return( '?' );
70  }
71 
72  place++;
73  if (*place == ':') {
74  if (*scan != '\0') {
75  optarg = scan;
76  scan = NULL;
77  } else {
78  if (optind >= argc) {
79  fprintf(stderr,
80  "%s: missing argument after -%c\n",
81  argv[0], c);
82  return( '?' );
83  }
84  optarg = argv[optind];
85  optind++;
86  }
87  }
88 
89  return( c );
90 }
int getopt(int argc, argv, char *optstring)
Definition: getopt.c:37
int optind
Definition: getopt.c:30
static char * scan
Definition: getopt.c:32
#define index
Definition: rle_config.h:96
char * optarg
Definition: getopt.c:29