Commit | Line | Data |
---|---|---|
7b728a63 SW |
1 | /* $NetBSD: main.c,v 1.26 2010/08/06 09:14:40 dholland Exp $ */ |
2 | ||
3 | /* | |
984263bc MD |
4 | * Copyright (c) 1983, 1993 |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
6693db17 | 15 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
16 | * may be used to endorse or promote products derived from this software |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
7b728a63 SW |
32 | #include <sys/cdefs.h> |
33 | #ifndef lint | |
34 | __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ | |
35 | The Regents of the University of California. All rights reserved."); | |
36 | #endif /* not lint */ | |
37 | ||
38 | #ifndef lint | |
39 | #if 0 | |
40 | static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/28/95"; | |
41 | #else | |
42 | __RCSID("$NetBSD: main.c,v 1.26 2010/08/06 09:14:40 dholland Exp $"); | |
43 | #endif | |
44 | #endif /* not lint */ | |
45 | ||
46 | #include <ctype.h> | |
47 | #include <err.h> | |
48 | #include <fcntl.h> | |
49 | #include <pwd.h> | |
50 | #include <setjmp.h> | |
51 | #include <stdio.h> | |
52 | #include <stdlib.h> | |
53 | #include <string.h> | |
54 | #include <time.h> | |
55 | #include <unistd.h> | |
56 | #include "extern.h" | |
57 | #include "pathnames.h" | |
58 | #include "restart.h" | |
59 | ||
60 | static void | |
61 | initialize(void) | |
62 | { | |
63 | int fd; | |
64 | const char *name; | |
65 | struct passwd *pw; | |
66 | char *s; | |
67 | ||
68 | gid = getgid(); | |
69 | egid = getegid(); | |
70 | setegid(gid); | |
71 | ||
72 | fd = open("/dev/null", O_RDONLY); | |
73 | if (fd < 3) | |
74 | exit(1); | |
75 | close(fd); | |
76 | ||
77 | if (chdir(_PATH_SAILDIR) < 0) { | |
78 | err(1, "%s", _PATH_SAILDIR); | |
79 | } | |
80 | ||
81 | srandom((u_long)time(NULL)); | |
82 | ||
83 | name = getenv("SAILNAME"); | |
84 | if (name != NULL && *name != '\0') { | |
85 | strlcpy(myname, name, sizeof(myname)); | |
86 | } else { | |
87 | pw = getpwuid(getuid()); | |
88 | if (pw != NULL) { | |
89 | strlcpy(myname, pw->pw_gecos, sizeof(myname)); | |
90 | /* trim to just the realname */ | |
91 | s = strchr(myname, ','); | |
92 | if (s != NULL) { | |
93 | *s = '\0'; | |
94 | } | |
95 | /* use just the first name */ | |
96 | s = strchr(myname, ' '); | |
97 | if (s != NULL) { | |
98 | *s = '\0'; | |
99 | } | |
100 | /* should really do &-expansion properly */ | |
101 | if (!strcmp(myname, "&")) { | |
102 | strlcpy(myname, pw->pw_name, sizeof(myname)); | |
103 | myname[0] = toupper((unsigned char)myname[0]); | |
104 | } | |
105 | } | |
106 | } | |
107 | if (*myname == '\0') { | |
108 | strlcpy(myname, "Anonymous", sizeof(myname)); | |
109 | } | |
110 | } | |
984263bc | 111 | |
4be74d36 | 112 | int |
7b728a63 | 113 | main(int argc, char **argv) |
984263bc MD |
114 | { |
115 | char *p; | |
7b728a63 SW |
116 | int a, i; |
117 | ||
118 | initialize(); | |
984263bc | 119 | |
7b728a63 | 120 | if ((p = strrchr(*argv, '/')) != NULL) |
984263bc MD |
121 | p++; |
122 | else | |
123 | p = *argv; | |
7b728a63 | 124 | |
984263bc MD |
125 | if (strcmp(p, "driver") == 0 || strcmp(p, "saildriver") == 0) |
126 | mode = MODE_DRIVER; | |
127 | else if (strcmp(p, "sail.log") == 0) | |
128 | mode = MODE_LOGGER; | |
129 | else | |
130 | mode = MODE_PLAYER; | |
7b728a63 SW |
131 | |
132 | while ((a = getopt(argc, argv, "dsxlb")) != -1) | |
133 | switch (a) { | |
984263bc MD |
134 | case 'd': |
135 | mode = MODE_DRIVER; | |
136 | break; | |
137 | case 's': | |
138 | mode = MODE_LOGGER; | |
139 | break; | |
984263bc | 140 | case 'x': |
7b728a63 | 141 | randomize = true; |
984263bc MD |
142 | break; |
143 | case 'l': | |
7b728a63 | 144 | longfmt = true; |
984263bc MD |
145 | break; |
146 | case 'b': | |
7b728a63 | 147 | nobells = true; |
984263bc MD |
148 | break; |
149 | default: | |
7b728a63 | 150 | errx(1, "Usage: %s [-bdlsx] [scenario-number]", p); |
984263bc | 151 | } |
7b728a63 SW |
152 | |
153 | argc -= optind; | |
154 | argv += optind; | |
155 | ||
984263bc MD |
156 | if (*argv) |
157 | game = atoi(*argv); | |
158 | else | |
159 | game = -1; | |
6693db17 SW |
160 | |
161 | if ((i = setjmp(restart)) != 0) | |
984263bc | 162 | mode = i; |
6693db17 | 163 | |
984263bc MD |
164 | switch (mode) { |
165 | case MODE_PLAYER: | |
7b728a63 SW |
166 | initscreen(); |
167 | startup(); | |
168 | cleanupscreen(); | |
169 | return 0; | |
984263bc MD |
170 | case MODE_DRIVER: |
171 | return dr_main(); | |
172 | case MODE_LOGGER: | |
173 | return lo_main(); | |
174 | default: | |
7b728a63 | 175 | warnx("Unknown mode %d", mode); |
984263bc MD |
176 | abort(); |
177 | } | |
178 | /*NOTREACHED*/ | |
179 | } |