summaryrefslogtreecommitdiffstats
path: root/robots/main.c
diff options
context:
space:
mode:
authordholland <dholland@NetBSD.org>2009-08-05 04:03:47 +0000
committerdholland <dholland@NetBSD.org>2009-08-05 04:03:47 +0000
commita7345209258b0ffde396d087589510b8e7c3bb50 (patch)
tree06125e5437890fa3c90156ad8cd59589fd1aa64f /robots/main.c
parent534eafe581e5ce9b0b32d42642949f61f9cc61cf (diff)
downloadbsdgames-darwin-a7345209258b0ffde396d087589510b8e7c3bb50.tar.gz
bsdgames-darwin-a7345209258b0ffde396d087589510b8e7c3bb50.zip
Use getopt instead of hand-rolled options code. Document all the arguments
and options. Don't allow the previously undocumented method to change the maximum number of scores kept per user to be used on the system-wide high score file. Sort options list in the man page. Bump its date.
Diffstat (limited to 'robots/main.c')
-rw-r--r--robots/main.c120
1 files changed, 59 insertions, 61 deletions
diff --git a/robots/main.c b/robots/main.c
index 9868403a..75a4555a 100644
--- a/robots/main.c
+++ b/robots/main.c
@@ -1,4 +1,4 @@
-/* $NetBSD: main.c,v 1.29 2009/07/20 06:43:18 dholland Exp $ */
+/* $NetBSD: main.c,v 1.30 2009/08/05 04:03:47 dholland Exp $ */
/*
* Copyright (c) 1980, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: main.c,v 1.29 2009/07/20 06:43:18 dholland Exp $");
+__RCSID("$NetBSD: main.c,v 1.30 2009/08/05 04:03:47 dholland Exp $");
#endif
#endif /* not lint */
@@ -58,13 +58,15 @@ extern const char *Scorefile;
extern int Max_per_uid;
int
-main(int ac, char **av)
+main(int argc, char **argv)
{
- const char *sp;
- bool bad_arg;
+ const char *word;
bool show_only;
int score_wfd; /* high score writable file descriptor */
int score_err = 0; /* hold errno from score file open */
+ int maximum = 0;
+ char ch;
+ int i;
score_wfd = open(Scorefile, O_RDWR);
if (score_wfd < 0)
@@ -77,64 +79,60 @@ main(int ac, char **av)
show_only = false;
Num_games = 1;
- if (ac > 1) {
- bad_arg = false;
- for (++av; ac > 1 && *av[0]; av++, ac--)
- if (av[0][0] != '-')
- if (isdigit((unsigned char)av[0][0]))
- Max_per_uid = atoi(av[0]);
- else {
- Scorefile = av[0];
- if (score_wfd >= 0)
- close(score_wfd);
- score_wfd = open(Scorefile, O_RDWR);
- if (score_wfd < 0)
- score_err = errno;
+
+ while ((ch = getopt(argc, argv, "Aajnrst")) != -1) {
+ switch (ch) {
+ case 'A':
+ Auto_bot = true;
+ break;
+ case 'a':
+ Start_level = 4;
+ break;
+ case 'j':
+ Jump = true;
+ break;
+ case 'n':
+ Num_games++;
+ break;
+ case 'r':
+ Real_time = true;
+ break;
+ case 's':
+ show_only = true;
+ break;
+ case 't':
+ Teleport = true;
+ break;
+ default:
+ errx(1,
+ "Usage: robots [-Aajnrst] [maximum] [scorefile]");
+ break;
+ }
+ }
+
+ for (i = optind; i < argc; i++) {
+ word = argv[i];
+ if (isdigit((unsigned char)word[0])) {
+ maximum = atoi(word);
+ } else {
+ Scorefile = word;
+ Max_per_uid = maximum;
+ if (score_wfd >= 0)
+ close(score_wfd);
+ score_wfd = open(Scorefile, O_RDWR);
+ if (score_wfd < 0)
+ score_err = errno;
#ifdef FANCY
- sp = strrchr(Scorefile, '/');
- if (sp == NULL)
- sp = Scorefile;
- if (strcmp(sp, "pattern_roll") == 0)
- Pattern_roll = true;
- else if (strcmp(sp, "stand_still") == 0)
- Stand_still = true;
- if (Pattern_roll || Stand_still)
- Teleport = true;
+ word = strrchr(Scorefile, '/');
+ if (word == NULL)
+ word = Scorefile;
+ if (strcmp(word, "pattern_roll") == 0)
+ Pattern_roll = true;
+ else if (strcmp(word, "stand_still") == 0)
+ Stand_still = true;
+ if (Pattern_roll || Stand_still)
+ Teleport = true;
#endif
- }
- else
- for (sp = &av[0][1]; *sp; sp++)
- switch (*sp) {
- case 'A':
- Auto_bot = true;
- break;
- case 's':
- show_only = true;
- break;
- case 'r':
- Real_time = true;
- break;
- case 'a':
- Start_level = 4;
- break;
- case 'n':
- Num_games++;
- break;
- case 'j':
- Jump = true;
- break;
- case 't':
- Teleport = true;
- break;
-
- default:
- fprintf(stderr, "robots: unknown option: %c\n", *sp);
- bad_arg = true;
- break;
- }
- if (bad_arg) {
- exit(1);
- /* NOTREACHED */
}
}