2 * Copyright (c) 2003 Nara Institute of Science and Technology
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name Nara Institute of Science and Technology may not be used to
15 * endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE Nara Institute
22 * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $Id: getopt.c,v 1.1.1.1 2007/03/13 07:40:10 masayu-a Exp $
40 cha_getopt(char **argv, char *optstring, FILE * fp)
42 static char *nextchar;
48 if (Cha_optind == 0) {
54 if (nextchar == argv[Cha_optind]) {
58 if (nextchar == NULL || nextchar[0] != '-' || nextchar[1] == '\0')
63 if (*++nextchar == '-') {
64 nextchar = argv[++Cha_optind];
70 * find out an option letter
73 if ((op = strchr(optstring, c)) == NULL || c == ':') {
75 fprintf(fp, "%s: invalid option -- %c\n", argv[0], c);
79 * option with an argument
81 else if (op[1] == ':') {
86 Cha_optarg = nextchar;
90 else if (argv[Cha_optind + 1] != NULL)
91 Cha_optarg = argv[++Cha_optind];
97 fprintf(fp, "%s: option requires an argument -- %c\n",
101 nextchar = argv[++Cha_optind];
104 if (nextchar != NULL && *nextchar == '\0')
105 nextchar = argv[++Cha_optind];
114 cha_getopt_chasen(char **argv, FILE * fp)
116 return cha_getopt(argv, "i:sP:D:RabmpdvfecMo:F:L:l:jr:w:O:ChV", fp);
123 main(int argc, char *argv[])
128 c = cha_getopt(argv, "abc:d:", stderr);
133 printf("option a\n");
137 printf("option b\n");
141 printf("option c with value `%s'\n", Cha_optarg);
148 printf("?? getopt returned character code 0%o ??\n", c);
152 if (Cha_optind < argc) {
153 printf("non-option ARGV-elements: ");
154 while (Cha_optind < argc)
155 printf("%s ", argv[Cha_optind++]);