Skip to content

Commit

Permalink
オプション解析エラー時の動作を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
takamin committed Apr 6, 2015
1 parent 621149e commit fd31c99
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions source/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* Released under the MIT license
* https://github.com/takamin/win-c/blob/master/LICENSE
*/
#include <stdio.h>
#include <string.h>
#include "getopt.h"

char* optarg = 0;
int optind = 1;
int opterr = 0;
int opterr = 1;
int optopt = 0;

int getopt(int argc, char* const argv[],
Expand Down Expand Up @@ -42,8 +43,13 @@ int getopt(int argc, char* const argv[],
}
optptr = strchr(optstring, c);
if(optptr == NULL) {
++optarg;
nextchar = 0;
optopt = c;
if(opterr) {
fprintf(stderr,
"%s : unknown option '%c'.\n",
argv[0], c);
}
++nextchar;
return '?';
}
if(*(optptr+1) != ':') {
Expand All @@ -61,6 +67,12 @@ int getopt(int argc, char* const argv[],
if(optind < argc) {
optarg = argv[optind];
} else {
optopt = c;
if(opterr) {
fprintf(stderr,
"%s : argument required for option '%c'.\n",
argv[0], c);
}
if(optstring[0] == ':') {
c = ':';
} else {
Expand Down

0 comments on commit fd31c99

Please sign in to comment.