xref: /openbsd-src/games/sail/main.c (revision 747bd222030b875dd230fbd8422f9826a993d133)
1*747bd222Smestre /*	$OpenBSD: main.c,v 1.11 2016/01/08 20:26:33 mestre Exp $	*/
2df930be7Sderaadt /*	$NetBSD: main.c,v 1.3 1995/04/22 10:37:01 cgd Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1983, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
167a09557bSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33*747bd222Smestre #include <err.h>
344f095801Spjanzen #include <fcntl.h>
354f095801Spjanzen #include <stdlib.h>
364f095801Spjanzen #include <string.h>
37*747bd222Smestre #include <unistd.h>
38df930be7Sderaadt 
39*747bd222Smestre #include "extern.h"
40*747bd222Smestre 
414f095801Spjanzen int
main(int argc,char ** argv)42*747bd222Smestre main(int argc, char **argv)
43df930be7Sderaadt {
4448aa6080Stb 	extern char *__progname;
454f095801Spjanzen 	char *p;
46df930be7Sderaadt 	int i;
474f095801Spjanzen 	int fd;
48df930be7Sderaadt 
494f095801Spjanzen 	gid = getgid();
504f095801Spjanzen 	egid = getegid();
514f095801Spjanzen 	setegid(gid);
524f095801Spjanzen 
534f095801Spjanzen 	fd = open("/dev/null", O_RDONLY);
544f095801Spjanzen 	if (fd < 3)
5517641e31Stb 		return 1;
564f095801Spjanzen 	close(fd);
574f095801Spjanzen 
5848aa6080Stb 	p = __progname;
59df930be7Sderaadt 	if (strcmp(p, "driver") == 0 || strcmp(p, "saildriver") == 0)
60df930be7Sderaadt 		mode = MODE_DRIVER;
61df930be7Sderaadt 	else if (strcmp(p, "sail.log") == 0)
62df930be7Sderaadt 		mode = MODE_LOGGER;
63df930be7Sderaadt 	else
64df930be7Sderaadt 		mode = MODE_PLAYER;
65df930be7Sderaadt 	while ((p = *++argv) && *p == '-')
66df930be7Sderaadt 		switch (p[1]) {
67df930be7Sderaadt 		case 'd':
68df930be7Sderaadt 			mode = MODE_DRIVER;
69df930be7Sderaadt 			break;
70df930be7Sderaadt 		case 's':
71df930be7Sderaadt 			mode = MODE_LOGGER;
72df930be7Sderaadt 			break;
73df930be7Sderaadt 		case 'D':
74df930be7Sderaadt 			debug++;
75df930be7Sderaadt 			break;
76df930be7Sderaadt 		case 'x':
774f095801Spjanzen 			randomize++;
78df930be7Sderaadt 			break;
79df930be7Sderaadt 		case 'l':
80df930be7Sderaadt 			longfmt++;
81df930be7Sderaadt 			break;
82df930be7Sderaadt 		case 'b':
83df930be7Sderaadt 			nobells++;
84df930be7Sderaadt 			break;
85df930be7Sderaadt 		default:
864f095801Spjanzen 			errx(1, "unknown flag %s", p);
87df930be7Sderaadt 		}
88df930be7Sderaadt 	if (*argv)
89df930be7Sderaadt 		game = atoi(*argv);
90df930be7Sderaadt 	else
91df930be7Sderaadt 		game = -1;
924f095801Spjanzen 	if ((i = setjmp(restart)))
93df930be7Sderaadt 		mode = i;
94df930be7Sderaadt 	switch (mode) {
95df930be7Sderaadt 	case MODE_PLAYER:
96c56d7eccSguenther 		pl_main();
97df930be7Sderaadt 	case MODE_DRIVER:
98df930be7Sderaadt 		return dr_main();
99df930be7Sderaadt 	case MODE_LOGGER:
100df930be7Sderaadt 		return lo_main();
101df930be7Sderaadt 	default:
1024f095801Spjanzen 		warnx("Unknown mode %d", mode);
103df930be7Sderaadt 		abort();
104df930be7Sderaadt 	}
105df930be7Sderaadt }
106