Skip to content

Commit

Permalink
correct getopt
Browse files Browse the repository at this point in the history
  • Loading branch information
dleonard committed Jan 19, 2008
1 parent 963bdb7 commit 71be254
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ getopt(int argc, char * const argv[], const char *optstring)
const char *p;
char ch;

if (argv[optind] == NULL || *argv[optind] != '-')
if (argv[optind] == NULL || *argv[optind] != '-' || !argv[optind][1])
return -1;

p = optstring;
ch = argv[optind][optidx + 1];

if (ch == '-' && optidx == 0 && !argv[optind][2]) {
optind++;
optidx = 0;
return -1;
}

p = optstring;
if (*p == ':')
p++;
while (*p) {
Expand Down Expand Up @@ -64,7 +71,7 @@ getopt(int argc, char * const argv[], const char *optstring)
}
optind++;
optidx = 0;
} else if (argv[optind][2] == '\0') {
} else if (argv[optind][optidx + 2] == '\0') {
optind++;
optidx = 0;
} else
Expand Down

0 comments on commit 71be254

Please sign in to comment.