X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/c57fa9b1accc28bd366bc1236a0a39ec8aea5130..c2997b6e22cfa183e7e73501ecdb2d5020434c9a:/rogue/machdep.c?ds=sidebyside diff --git a/rogue/machdep.c b/rogue/machdep.c index f5e90dd2..e6d6292d 100644 --- a/rogue/machdep.c +++ b/rogue/machdep.c @@ -1,4 +1,4 @@ -/* $NetBSD: machdep.c,v 1.8 1998/07/27 01:12:35 mycroft Exp $ */ +/* $NetBSD: machdep.c,v 1.20 2012/12/01 11:37:27 mbalmer Exp $ */ /* * Copyright (c) 1988, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -41,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)machdep.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: machdep.c,v 1.8 1998/07/27 01:12:35 mycroft Exp $"); +__RCSID("$NetBSD: machdep.c,v 1.20 2012/12/01 11:37:27 mbalmer Exp $"); #endif #endif /* not lint */ @@ -67,7 +63,7 @@ __RCSID("$NetBSD: machdep.c,v 1.8 1998/07/27 01:12:35 mycroft Exp $"); * * All UNIX code should be included between the single "#ifdef UNIX" at the * top of this file, and the "#endif" at the bottom. - * + * * To change a routine to include a new UNIX system, simply #ifdef the * existing routine, as in the following example: * @@ -88,7 +84,7 @@ __RCSID("$NetBSD: machdep.c,v 1.8 1998/07/27 01:12:35 mycroft Exp $"); * #endif * } * - * Appropriate variations of this are of course acceptible. + * Appropriate variations of this are of course acceptable. * The use of "#elseif" is discouraged because of non-portability. * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up * and insert it in the list at the top of the file. Alter the CFLAGS @@ -132,7 +128,7 @@ __RCSID("$NetBSD: machdep.c,v 1.8 1998/07/27 01:12:35 mycroft Exp $"); */ void -md_slurp() +md_slurp(void) { (void)fpurge(stdin); } @@ -154,7 +150,7 @@ md_slurp() */ void -md_heed_signals() +md_heed_signals(void) { signal(SIGINT, onintr); signal(SIGQUIT, byebye); @@ -174,7 +170,7 @@ md_heed_signals() */ void -md_ignore_signals() +md_ignore_signals(void) { signal(SIGQUIT, SIG_IGN); signal(SIGINT, SIG_IGN); @@ -191,15 +187,14 @@ md_ignore_signals() */ int -md_get_file_id(fname) - char *fname; +md_get_file_id(const char *fname) { struct stat sbuf; if (stat(fname, &sbuf)) { return(-1); } - return((int) sbuf.st_ino); + return((int)sbuf.st_ino); } /* md_link_count(): @@ -211,13 +206,12 @@ md_get_file_id(fname) */ int -md_link_count(fname) -char *fname; +md_link_count(const char *fname) { struct stat sbuf; stat(fname, &sbuf); - return((int) sbuf.st_nlink); + return((int)sbuf.st_nlink); } /* md_gct(): (Get Current Time) @@ -231,12 +225,11 @@ char *fname; * system doesn't provide all of the time units requested here, then you * can provide only those that it does, and return zeros for the others. * If you cannot provide good time values, then users may be able to copy - * saved-game files and play them. + * saved-game files and play them. */ void -md_gct(rt_buf) - struct rogue_time *rt_buf; +md_gct(struct rogue_time *rt_buf) { struct tm *t; time_t seconds; @@ -263,22 +256,20 @@ md_gct(rt_buf) * exactly the same here. * Or if md_gct() is implemented correctly, but your system does not provide * file modification dates, you may return some date far in the past so - * that the program will never know that a saved-game file being modified. + * that the program will never know that a saved-game file being modified. * You may also do this if you wish to be able to restore games from * saved-games that have been modified. */ void -md_gfmt(fname, rt_buf) - char *fname; - struct rogue_time *rt_buf; +md_gfmt(const char *fname, struct rogue_time *rt_buf) { struct stat sbuf; time_t seconds; struct tm *t; stat(fname, &sbuf); - seconds = (long) sbuf.st_mtime; + seconds = sbuf.st_mtime; t = localtime(&seconds); rt_buf->year = t->tm_year; @@ -301,8 +292,7 @@ md_gfmt(fname, rt_buf) */ boolean -md_df(fname) - char *fname; +md_df(const char *fname) { if (unlink(fname)) { return(0); @@ -320,13 +310,13 @@ md_df(fname) */ const char * -md_gln() +md_gln(void) { struct passwd *p; if (!(p = getpwuid(getuid()))) - return((char *)NULL); - return(p->pw_name); + return NULL; + return p->pw_name; } /* md_sleep: @@ -339,10 +329,9 @@ md_gln() */ void -md_sleep(nsecs) - int nsecs; +md_sleep(int nsecs) { - (void) sleep(nsecs); + (void)sleep(nsecs); } /* md_getenv() @@ -351,17 +340,6 @@ md_sleep(nsecs) * values are strings, and each string is identified by a name. The names * of the values needed, and their use, is as follows: * - * TERMCAP - * The name of the users's termcap file, NOT the termcap entries - * themselves. This is used ONLY if the program is compiled with - * CURSES defined (-DCURSES). Even in this case, the program need - * not find a string for TERMCAP. If it does not, it will use the - * default termcap file as returned by md_gdtcf(); - * TERM - * The name of the users's terminal. This is used ONLY if the program - * is compiled with CURSES defined (-DCURSES). In this case, the string - * value for TERM must be found, or the routines in curses.c cannot - * function, and the program will quit. * ROGUEOPTS * A string containing the various game options. This need not be * defined. @@ -374,17 +352,13 @@ md_sleep(nsecs) * * If your system does not provide a means of searching for these values, * you will have to do it yourself. None of the values above really need - * to be defined except TERM when the program is compiled with CURSES - * defined. In this case, as a bare minimum, you can check the 'name' - * parameter, and if it is "TERM" find the terminal name and return that, - * else return zero. If the program is not compiled with CURSES, you can - * get by with simply always returning zero. Returning zero indicates - * that their is no defined value for the given string. + * to be defined; you can get by with simply always returning zero. + * Returning zero indicates that their is no defined value for the + * given string. */ char * -md_getenv(name) - char *name; +md_getenv(const char *name) { char *value; @@ -401,9 +375,8 @@ md_getenv(name) * when no more memory can be allocated. */ -char * -md_malloc(n) - int n; +void * +md_malloc(size_t n) { char *t; @@ -414,7 +387,7 @@ md_malloc(n) /* md_gseed() (Get Seed) * * This function returns a seed for the random number generator (RNG). This - * seed causes the RNG to begin generating numbers at some point in it's + * seed causes the RNG to begin generating numbers at some point in its * sequence. Without a random seed, the RNG will generate the same set * of numbers, and every game will start out exactly the same way. A good * number to use is the process id, given by getpid() on most UNIX systems. @@ -422,7 +395,7 @@ md_malloc(n) * You need to find some single random integer, such as: * process id. * current time (minutes + seconds) returned from md_gct(), if implemented. - * + * * It will not help to return "get_rand()" or "rand()" or the return value of * any pseudo-RNG. If you don't have a random number, you can just return 1, * but this means your games will ALWAYS start the same way, and will play @@ -430,12 +403,12 @@ md_malloc(n) */ int -md_gseed() +md_gseed(void) { time_t seconds; time(&seconds); - return((int) seconds); + return((int)seconds); } /* md_exit(): @@ -446,8 +419,7 @@ md_gseed() */ void -md_exit(status) - int status; +md_exit(int status) { exit(status); } @@ -464,22 +436,24 @@ md_exit(status) */ void -md_lock(l) - boolean l; +md_lock(boolean l) { - static int fd; + static int fd = -1; short tries; if (l) { + setegid(egid); if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) { - message("cannot lock score file", 0); + setegid(gid); + messagef(0, "cannot lock score file"); return; } + setegid(gid); for (tries = 0; tries < 5; tries++) if (!flock(fd, LOCK_EX|LOCK_NB)) return; } else { - (void)flock(fd, LOCK_NB); + (void)flock(fd, LOCK_UN|LOCK_NB); (void)close(fd); } } @@ -487,121 +461,32 @@ md_lock(l) /* md_shell(): * * This function spawns a shell for the user to use. When this shell is - * terminated, the game continues. Since this program may often be run - * setuid to gain access to privileged files, care is taken that the shell - * is run with the user's REAL user id, and not the effective user id. - * The effective user id is restored after the shell completes. - */ - -void -md_shell(shell) - char *shell; -{ - int w; - - if (!fork()) { - int uid; - - uid = getuid(); - setuid(uid); - execl(shell, shell, 0); - } - wait(&w); -} - -/* If you have a viable curses/termlib library, then use it and don't bother - * implementing the routines below. And don't compile with -DCURSES. - */ - -#ifdef CURSES - -/* md_cbreak_no_echo_nonl: - * - * This routine sets up some terminal characteristics. The tty-driver - * must be told to: - * 1.) Not echo input. - * 2.) Transmit input characters immediately upon typing. (cbreak mode) - * 3.) Move the cursor down one line, without changing column, and - * without generating a carriage-return, when it - * sees a line-feed. This is only necessary if line-feed is ever - * used in the termcap 'do' (cursor down) entry, in which case, - * your system should must have a way of accomplishing this. - * - * When the parameter 'on' is true, the terminal is set up as specified - * above. When this parameter is false, the terminal is restored to the - * original state. - * - * Raw mode should not to be used. Keyboard signals/events/interrupts should - * be sent, although they are not strictly necessary. See notes in - * md_heed_signals(). - * - * This function must be implemented for rogue to run properly if the - * program is compiled with CURSES defined to use the enclosed curses - * emulation package. If you are not using this, then this routine is - * totally unnecessary. - * - * Notice that information is saved between calls. This is used to - * restore the terminal to an initial saved state. + * terminated, the game continues. * + * It is important that the game not give the shell the privileges the + * game uses to access the scores file. This version of the game runs + * with privileges low by default; only the saved gid (if setgid) or uid + * (if setuid) will be privileged, but that privilege is discarded by + * exec(). */ void -md_cbreak_no_echo_nonl(on) - boolean on; +md_shell(const char *shell) { - struct termios tty_buf; - static struct termios tty_save; - - if (on) { - tcgetattr(0, &tty_buf); - tty_save = tty_buf; - tty_buf.c_lflag &= ~(ICANON | ECHO); - tty_buf.c_oflag &= ~ONLCR; - tty_buf.c_cc[VMIN] = 1; - tty_buf.c_cc[VTIME] = 2; - tcsetattr(0, TCSADRAIN, &tty_buf); - } else { - tcsetattr(0, TCSADRAIN, &tty_save); + int w; + pid_t pid; + + pid = fork(); + switch (pid) { + case -1: + break; + case 0: + execl(shell, shell, (char *)NULL); + _exit(255); + default: + waitpid(pid, &w, 0); + break; } } -/* md_gdtcf(): (Get Default Termcap File) - * - * This function is called ONLY when the program is compiled with CURSES - * defined. If you use your system's curses/termlib library, this function - * won't be called. On most UNIX systems, "/etc/termcap" suffices. - * - * If their is no such termcap file, then return 0, but in that case, you - * must have a TERMCAP file returned from md_getenv("TERMCAP"). The latter - * will override the value returned from md_gdtcf(). If the program is - * compiled with CURSES defined, and md_gdtcf() returns 0, and - * md_getenv("TERMCAP") returns 0, the program will have no terminal - * capability information and will quit. - */ - -char * -md_gdtcf() -{ - return("/etc/termcap"); -} - -/* md_tstp(): - * - * This function puts the game to sleep and returns to the shell. This - * only applies to UNIX 4.2 and 4.3. For other systems, the routine should - * be provided as a do-nothing routine. md_tstp() will only be referenced - * in the code when compiled with CURSES defined. - * - */ - -void -md_tstp() -{ -#ifdef UNIX_BSD4_2 - kill(0, SIGTSTP); -#endif -} - -#endif - -#endif +#endif /* UNIX */