summaryrefslogtreecommitdiff
path: root/getopt.c
diff options
context:
space:
mode:
authorToby Goodwin <tjg@star.le.ac.uk>1999-01-11 10:55:18 +0000
committerToby Goodwin <toby@paccrat.org>2015-05-16 21:22:09 +0100
commite11c7df86144bed383ac6bbeaecc3017d18b37db (patch)
tree6fad44921723eab2f2eb1c76b7214b4e79b818f3 /getopt.c
parent671e1e2d1599aa8c5f1010b3fe75c151c56a7403 (diff)
beta: rc-1.5b4
Diffstat (limited to 'getopt.c')
-rw-r--r--getopt.c73
1 files changed, 37 insertions, 36 deletions
diff --git a/getopt.c b/getopt.c
index 423b84a..3c54f53 100644
--- a/getopt.c
+++ b/getopt.c
@@ -9,42 +9,43 @@ char *rc_optarg;
extern int rc_getopt(int argc, char **argv, char *opts) {
static int sp = 1;
- int c;
- char *cp;
+ int c;
+ char *cp;
if (rc_optind == 0) /* reset rc_getopt() */
rc_optind = sp = 1;
- if (sp == 1)
- if (rc_optind >= argc || argv[rc_optind][0] != '-' || argv[rc_optind][1] == '\0') {
- return -1;
- } else if (strcmp(argv[rc_optind], "--") == 0) {
- rc_optind++;
- return -1;
- }
- rc_optopt = c = argv[rc_optind][sp];
- if (c == ':' || (cp=strchr(opts, c)) == 0) {
- fprint(2, "%s: bad option: -%c\n", argv[0], c);
- if (argv[rc_optind][++sp] == '\0') {
- rc_optind++;
- sp = 1;
- }
- return '?';
- }
- if (*++cp == ':') {
- if (argv[rc_optind][sp+1] != '\0') {
- rc_optarg = &argv[rc_optind++][sp+1];
- } else if (++rc_optind >= argc) {
- fprint(2, "%s: option requires an argument -- %c\n", argv[0], c);
- sp = 1;
- return '?';
- } else
- rc_optarg = argv[rc_optind++];
- sp = 1;
- } else {
- if (argv[rc_optind][++sp] == '\0') {
- sp = 1;
- rc_optind++;
- }
- rc_optarg = NULL;
- }
- return c;
+ if (sp == 1) {
+ if (rc_optind >= argc || argv[rc_optind][0] != '-' || argv[rc_optind][1] == '\0') {
+ return -1;
+ } else if (strcmp(argv[rc_optind], "--") == 0) {
+ rc_optind++;
+ return -1;
+ }
+ }
+ rc_optopt = c = argv[rc_optind][sp];
+ if (c == ':' || (cp=strchr(opts, c)) == 0) {
+ fprint(2, "%s: bad option: -%c\n", argv[0], c);
+ if (argv[rc_optind][++sp] == '\0') {
+ rc_optind++;
+ sp = 1;
+ }
+ return '?';
+ }
+ if (*++cp == ':') {
+ if (argv[rc_optind][sp+1] != '\0') {
+ rc_optarg = &argv[rc_optind++][sp+1];
+ } else if (++rc_optind >= argc) {
+ fprint(2, "%s: option requires an argument -- %c\n", argv[0], c);
+ sp = 1;
+ return '?';
+ } else
+ rc_optarg = argv[rc_optind++];
+ sp = 1;
+ } else {
+ if (argv[rc_optind][++sp] == '\0') {
+ sp = 1;
+ rc_optind++;
+ }
+ rc_optarg = NULL;
+ }
+ return c;
}