From 25b4485b766a2328ff638d846d18cf520e99c1e1 Mon Sep 17 00:00:00 2001 From: David Griffith Date: Sat, 21 Sep 2019 15:52:39 -0700 Subject: [PATCH] Convert getopt.c to K&R style. --- src/common/getopt.c | 70 ++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/src/common/getopt.c b/src/common/getopt.c index 415fcde..efe99af 100644 --- a/src/common/getopt.c +++ b/src/common/getopt.c @@ -22,51 +22,43 @@ const char *optarg = NULL; int cdecl getopt (int argc, char *argv[], const char *options) { - static int pos = 1; + static int pos = 1; + const char *p; - const char *p; + if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == 0) + return EOF; - if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == 0) - return EOF; + optopt = argv[optind][pos++]; + optarg = NULL; - optopt = argv[optind][pos++]; - optarg = NULL; - - if (argv[optind][pos] == 0) - { pos = 1; optind++; } - - p = strchr (options, optopt); - - if (optopt == ':' || p == NULL) { - - fputs ("illegal option -- ", stdout); - goto error; - - } else if (p[1] == ':') { - - if (optind >= argc) { - - fputs ("option requires an argument -- ", stdout); - goto error; - - } else { - - optarg = argv[optind]; - - if (pos != 1) - optarg += pos; - - pos = 1; optind++; + if (argv[optind][pos] == 0) { + pos = 1; + optind++; + } + p = strchr(options, optopt); + + if (optopt == ':' || p == NULL) { + fputs("illegal option -- ", stdout); + goto error; + } else if (p[1] == ':') { + if (optind >= argc) { + fputs("option requires an argument -- ", stdout); + goto error; + } else { + optarg = argv[optind]; + if (pos != 1) + optarg += pos; + pos = 1; + optind++; + } } - } - return optopt; + return optopt; error: - fputc (optopt, stdout); - fputc ('\n', stdout); - - return '?'; + fputc(optopt, stdout); + fputc('\n', stdout); -}/* getopt */ + return '?'; +} /* getopt */ -- 2.34.1