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