Getopt'ed by Marc Nijdam <marc_nijdam@hp.com>

-Erik
This commit is contained in:
Eric Andersen 2000-07-14 18:39:08 +00:00
parent 17ad45aace
commit add09fd558
6 changed files with 128 additions and 174 deletions

View File

@ -159,7 +159,7 @@ int date_main(int argc, char **argv)
char *date_str = NULL; char *date_str = NULL;
char *date_fmt = NULL; char *date_fmt = NULL;
char *t_buff; char *t_buff;
int i; char c;
int set_time = 0; int set_time = 0;
int rfc822 = 0; int rfc822 = 0;
int utc = 0; int utc = 0;
@ -168,49 +168,39 @@ int date_main(int argc, char **argv)
struct tm tm_time; struct tm tm_time;
/* Interpret command line args */ /* Interpret command line args */
i = --argc; while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
argv++; switch (c) {
while (i > 0 && **argv) { case 'R':
if (**argv == '-') { rfc822 = 1;
while (i > 0 && *++(*argv)) break;
switch (**argv) { case 's':
case 'R': set_time = 1;
rfc822 = 1; if ((date_str != NULL) || ((date_str = optarg) == NULL))
break;
case 's':
set_time = 1;
if (date_str != NULL)
usage(date_usage);
date_str = *argv;
break;
case 'u':
utc = 1;
if (putenv("TZ=UTC0") != 0)
fatalError(memory_exhausted);
break;
case 'd':
use_arg = 1;
if (date_str != NULL)
usage(date_usage);
date_str = *argv;
break;
case '-':
usage(date_usage);
}
} else {
if ((date_fmt == NULL) && (**argv == '+'))
date_fmt = *argv + 1; /* Skip over the '+' */
else if (date_str == NULL) {
set_time = 1;
date_str = *argv;
} else {
usage(date_usage); usage(date_usage);
} break;
case 'u':
utc = 1;
if (putenv("TZ=UTC0") != 0)
fatalError(memory_exhausted);
break;
case 'd':
use_arg = 1;
if ((date_str != NULL) || ((date_str = optarg) == NULL))
usage(date_usage);
break;
default:
usage(date_usage);
} }
i--;
argv++;
} }
if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
date_fmt = &argv[optind][1]; /* Skip over the '+' */
else if (date_str == NULL) {
set_time = 1;
date_str = argv[optind];
} else {
usage(date_usage);
}
/* Now we have parsed all the information except the date format /* Now we have parsed all the information except the date format
which depends on whether the clock is being set or read */ which depends on whether the clock is being set or read */

View File

@ -45,23 +45,24 @@ echo_main(int argc, char** argv)
int nflag = 0; int nflag = 0;
int eflag = 0; int eflag = 0;
ap = argv;
if (argc) while ((c = getopt(argc, argv, "neE")) != EOF) {
ap++; switch (c) {
while ((p = *ap) != NULL && *p == '-') { case 'n':
if (strcmp(p, "-n")==0) {
nflag = 1; nflag = 1;
} else if (strcmp(p, "-e")==0) { break;
case 'e':
eflag = 1; eflag = 1;
} else if (strcmp(p, "-E")==0) { break;
case 'E':
eflag = 0; eflag = 0;
break;
default:
usage(uname_usage);
} }
else if (strncmp(p, "--", 2)==0) {
usage( uname_usage);
}
else break;
ap++;
} }
ap = &argv[optind];
while ((p = *ap++) != NULL) { while ((p = *ap++) != NULL) {
while ((c = *p++) != '\0') { while ((c = *p++) != '\0') {
if (c == '\\' && eflag) { if (c == '\\' && eflag) {

70
date.c
View File

@ -159,7 +159,7 @@ int date_main(int argc, char **argv)
char *date_str = NULL; char *date_str = NULL;
char *date_fmt = NULL; char *date_fmt = NULL;
char *t_buff; char *t_buff;
int i; char c;
int set_time = 0; int set_time = 0;
int rfc822 = 0; int rfc822 = 0;
int utc = 0; int utc = 0;
@ -168,49 +168,39 @@ int date_main(int argc, char **argv)
struct tm tm_time; struct tm tm_time;
/* Interpret command line args */ /* Interpret command line args */
i = --argc; while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
argv++; switch (c) {
while (i > 0 && **argv) { case 'R':
if (**argv == '-') { rfc822 = 1;
while (i > 0 && *++(*argv)) break;
switch (**argv) { case 's':
case 'R': set_time = 1;
rfc822 = 1; if ((date_str != NULL) || ((date_str = optarg) == NULL))
break;
case 's':
set_time = 1;
if (date_str != NULL)
usage(date_usage);
date_str = *argv;
break;
case 'u':
utc = 1;
if (putenv("TZ=UTC0") != 0)
fatalError(memory_exhausted);
break;
case 'd':
use_arg = 1;
if (date_str != NULL)
usage(date_usage);
date_str = *argv;
break;
case '-':
usage(date_usage);
}
} else {
if ((date_fmt == NULL) && (**argv == '+'))
date_fmt = *argv + 1; /* Skip over the '+' */
else if (date_str == NULL) {
set_time = 1;
date_str = *argv;
} else {
usage(date_usage); usage(date_usage);
} break;
case 'u':
utc = 1;
if (putenv("TZ=UTC0") != 0)
fatalError(memory_exhausted);
break;
case 'd':
use_arg = 1;
if ((date_str != NULL) || ((date_str = optarg) == NULL))
usage(date_usage);
break;
default:
usage(date_usage);
} }
i--;
argv++;
} }
if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
date_fmt = &argv[optind][1]; /* Skip over the '+' */
else if (date_str == NULL) {
set_time = 1;
date_str = argv[optind];
} else {
usage(date_usage);
}
/* Now we have parsed all the information except the date format /* Now we have parsed all the information except the date format
which depends on whether the clock is being set or read */ which depends on whether the clock is being set or read */

56
dmesg.c
View File

@ -44,50 +44,36 @@ static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n"
int dmesg_main(int argc, char **argv) int dmesg_main(int argc, char **argv)
{ {
char *buf; char *buf, c;
int bufsize = 8196; int bufsize = 8196;
int i; int i;
int n; int n;
int level = 0; int level = 0;
int lastc; int lastc;
int cmd = 3; int cmd = 3;
int stopDoingThat;
argc--; while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
argv++; switch (c) {
case 'c':
/* Parse any options */ cmd = 4;
while (argc && **argv == '-') { break;
stopDoingThat = FALSE; case 'n':
while (stopDoingThat == FALSE && *++(*argv)) { cmd = 8;
switch (**argv) { if (optarg == NULL)
case 'c': usage(dmesg_usage);
cmd = 4; level = atoi(optarg);
break; break;
case 'n': case 's':
cmd = 8; if (optarg == NULL)
if (--argc == 0) usage(dmesg_usage);
goto end; bufsize = atoi(optarg);
level = atoi(*(++argv)); break;
if (--argc > 0) default:
++argv; usage(dmesg_usage);
stopDoingThat = TRUE;
break;
case 's':
if (--argc == 0)
goto end;
bufsize = atoi(*(++argv));
if (--argc > 0)
++argv;
stopDoingThat = TRUE;
break;
default:
goto end;
}
} }
} }
if (argc > 1) { if (optind < argc) {
goto end; goto end;
} }

25
echo.c
View File

@ -45,23 +45,24 @@ echo_main(int argc, char** argv)
int nflag = 0; int nflag = 0;
int eflag = 0; int eflag = 0;
ap = argv;
if (argc) while ((c = getopt(argc, argv, "neE")) != EOF) {
ap++; switch (c) {
while ((p = *ap) != NULL && *p == '-') { case 'n':
if (strcmp(p, "-n")==0) {
nflag = 1; nflag = 1;
} else if (strcmp(p, "-e")==0) { break;
case 'e':
eflag = 1; eflag = 1;
} else if (strcmp(p, "-E")==0) { break;
case 'E':
eflag = 0; eflag = 0;
break;
default:
usage(uname_usage);
} }
else if (strncmp(p, "--", 2)==0) {
usage( uname_usage);
}
else break;
ap++;
} }
ap = &argv[optind];
while ((p = *ap++) != NULL) { while ((p = *ap++) != NULL) {
while ((c = *p++) != '\0') { while ((c = *p++) != '\0') {
if (c == '\\' && eflag) { if (c == '\\' && eflag) {

View File

@ -44,50 +44,36 @@ static const char dmesg_usage[] = "dmesg [-c] [-n LEVEL] [-s SIZE]\n"
int dmesg_main(int argc, char **argv) int dmesg_main(int argc, char **argv)
{ {
char *buf; char *buf, c;
int bufsize = 8196; int bufsize = 8196;
int i; int i;
int n; int n;
int level = 0; int level = 0;
int lastc; int lastc;
int cmd = 3; int cmd = 3;
int stopDoingThat;
argc--; while ((c = getopt(argc, argv, "cn:s:")) != EOF) {
argv++; switch (c) {
case 'c':
/* Parse any options */ cmd = 4;
while (argc && **argv == '-') { break;
stopDoingThat = FALSE; case 'n':
while (stopDoingThat == FALSE && *++(*argv)) { cmd = 8;
switch (**argv) { if (optarg == NULL)
case 'c': usage(dmesg_usage);
cmd = 4; level = atoi(optarg);
break; break;
case 'n': case 's':
cmd = 8; if (optarg == NULL)
if (--argc == 0) usage(dmesg_usage);
goto end; bufsize = atoi(optarg);
level = atoi(*(++argv)); break;
if (--argc > 0) default:
++argv; usage(dmesg_usage);
stopDoingThat = TRUE;
break;
case 's':
if (--argc == 0)
goto end;
bufsize = atoi(*(++argv));
if (--argc > 0)
++argv;
stopDoingThat = TRUE;
break;
default:
goto end;
}
} }
} }
if (argc > 1) { if (optind < argc) {
goto end; goto end;
} }