[12] | 1 | /* |
---|
| 2 | * Revision Control Information |
---|
| 3 | * |
---|
| 4 | * $Id: getopt.c,v 1.1.1.1 2008-11-14 20:40:10 hhkim Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | /* LINTLIBRARY */ |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include "util.h" |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | /* File : getopt.c |
---|
| 14 | * Author : Henry Spencer, University of Toronto |
---|
| 15 | * Updated: 28 April 1984 |
---|
| 16 | * |
---|
| 17 | * Changes: (R Rudell) |
---|
| 18 | * changed index() to strchr(); |
---|
| 19 | * added getopt_reset() to reset the getopt argument parsing |
---|
| 20 | * |
---|
| 21 | * Purpose: get option letter from argv. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | char *util_optarg; /* Global argument pointer. */ |
---|
| 25 | int util_optind = 0; /* Global argv index. */ |
---|
| 26 | static char *scan; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | void |
---|
| 30 | util_getopt_reset(void) |
---|
| 31 | { |
---|
| 32 | util_optarg = 0; |
---|
| 33 | util_optind = 0; |
---|
| 34 | scan = 0; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | int |
---|
| 40 | util_getopt(int argc, char *argv[], char *optstring) |
---|
| 41 | { |
---|
| 42 | register int c; |
---|
| 43 | register char *place; |
---|
| 44 | |
---|
| 45 | util_optarg = NIL(char); |
---|
| 46 | |
---|
| 47 | if (scan == NIL(char) || *scan == '\0') { |
---|
| 48 | if (util_optind == 0) util_optind++; |
---|
| 49 | if (util_optind >= argc) return EOF; |
---|
| 50 | place = argv[util_optind]; |
---|
| 51 | if (place[0] != '-' || place[1] == '\0') return EOF; |
---|
| 52 | util_optind++; |
---|
| 53 | if (place[1] == '-' && place[2] == '\0') return EOF; |
---|
| 54 | scan = place+1; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | c = *scan++; |
---|
| 58 | place = strchr(optstring, c); |
---|
| 59 | if (place == NIL(char) || c == ':') { |
---|
| 60 | (void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c); |
---|
| 61 | return '?'; |
---|
| 62 | } |
---|
| 63 | if (*++place == ':') { |
---|
| 64 | if (*scan != '\0') { |
---|
| 65 | util_optarg = scan; |
---|
| 66 | scan = NIL(char); |
---|
| 67 | } else { |
---|
| 68 | if (util_optind >= argc) { |
---|
| 69 | (void) fprintf(stderr, "%s: %c requires an argument\n", |
---|
| 70 | argv[0], c); |
---|
| 71 | return '?'; |
---|
| 72 | } |
---|
| 73 | util_optarg = argv[util_optind]; |
---|
| 74 | util_optind++; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | return c; |
---|
| 78 | } |
---|