Breaking out the usage bit, other mindless crap.

cat-town
Dan Buch 13 years ago
parent 2fa6c49132
commit 809c287645

@ -4,17 +4,29 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <libgen.h>
int
main(int argc, char *argv[]) void die_usage(char *prog, char *msg) {
{ fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", prog);
if (msg != NULL) {
fprintf(stderr, "%s\n", msg);
}
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]) {
char *prog;
int flags, opt; int flags, opt;
int nsecs, tfnd; int nsecs, tfnd;
nsecs = 0; nsecs = 0;
tfnd = 0; tfnd = 0;
flags = 0; flags = 0;
while ((opt = getopt(argc, argv, "nt:")) != -1) { prog = basename(argv[0]);
while ((opt = getopt(argc, argv, "hnt:")) != -1) {
switch (opt) { switch (opt) {
case 'n': case 'n':
flags = 1; flags = 1;
@ -23,23 +35,20 @@ main(int argc, char *argv[])
nsecs = atoi(optarg); nsecs = atoi(optarg);
tfnd = 1; tfnd = 1;
break; break;
case 'h':
default: /* '?' */ default: /* '?' */
fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", die_usage(prog, NULL);
argv[0]);
exit(EXIT_FAILURE);
} }
} }
printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind); printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind);
if (optind >= argc) { if (optind >= argc) {
fprintf(stderr, "Expected argument after options\n"); die_usage(prog, "Expected argument after options");
exit(EXIT_FAILURE);
} }
printf("name argument = %s\n", argv[optind]); printf("name argument = %s\n", argv[optind]);
printf("nsecs = %d\n", nsecs);
/* Other code omitted */
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

Loading…
Cancel
Save