+/* $NetBSD: machdep.c,v 1.12 2003/08/07 09:37:38 agc Exp $ */
+
/*
- * Copyright (c) 1988 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Timothy C. Stoehr.
* 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.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char sccsid[] = "@(#)machdep.c 5.7 (Berkeley) 2/28/91";
+#if 0
+static char sccsid[] = "@(#)machdep.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: machdep.c,v 1.12 2003/08/07 09:37:38 agc Exp $");
+#endif
#endif /* not lint */
/*
#ifdef UNIX
-#include <stdio.h>
#include <sys/types.h>
+#include <sys/wait.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <pwd.h>
#ifdef UNIX_BSD4_2
#include <sys/time.h>
-#include <sgtty.h>
#endif
#ifdef UNIX_SYSV
#include <time.h>
-#include <termio.h>
#endif
#include <signal.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
#include "rogue.h"
#include "pathnames.h"
* big deal.
*/
+void
md_slurp()
{
(void)fpurge(stdin);
}
-/* md_control_keyboard():
- *
- * This routine is much like md_cbreak_no_echo_nonl() below. It sets up the
- * keyboard for appropriate input. Specifically, it prevents the tty driver
- * from stealing characters. For example, ^Y is needed as a command
- * character, but the tty driver intercepts it for another purpose. Any
- * such behavior should be stopped. This routine could be avoided if
- * we used RAW mode instead of CBREAK. But RAW mode does not allow the
- * generation of keyboard signals, which the program uses.
- *
- * The parameter 'mode' when true, indicates that the keyboard should
- * be set up to play rogue. When false, it should be restored if
- * necessary.
- *
- * This routine is not strictly necessary and may be stubbed. This may
- * cause certain command characters to be unavailable.
- */
-
-md_control_keybord(mode)
-boolean mode;
-{
- static boolean called_before = 0;
-#ifdef UNIX_BSD4_2
- static struct ltchars ltc_orig;
- static struct tchars tc_orig;
- struct ltchars ltc_temp;
- struct tchars tc_temp;
-#endif
-#ifdef UNIX_SYSV
- static struct termio _oldtty;
- struct termio _tty;
-#endif
-
- if (!called_before) {
- called_before = 1;
-#ifdef UNIX_BSD4_2
- ioctl(0, TIOCGETC, &tc_orig);
- ioctl(0, TIOCGLTC, <c_orig);
-#endif
-#ifdef UNIX_SYSV
- ioctl(0, TCGETA, &_oldtty);
-#endif
- }
-#ifdef UNIX_BSD4_2
- ltc_temp = ltc_orig;
- tc_temp = tc_orig;
-#endif
-#ifdef UNIX_SYSV
- _tty = _oldtty;
-#endif
-
- if (!mode) {
-#ifdef UNIX_BSD4_2
- ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1;
- ltc_temp.t_rprntc = ltc_temp.t_flushc = -1;
- ltc_temp.t_werasc = ltc_temp.t_lnextc = -1;
- tc_temp.t_startc = tc_temp.t_stopc = -1;
-#endif
-#ifdef UNIX_SYSV
- _tty.c_cc[VSWTCH] = CNSWTCH;
-#endif
- }
-#ifdef UNIX_BSD4_2
- ioctl(0, TIOCSETC, &tc_temp);
- ioctl(0, TIOCSLTC, <c_temp);
-#endif
-#ifdef UNIX_SYSV
- ioctl(0, TCSETA, &_tty);
-#endif
-}
-
/* md_heed_signals():
*
* This routine tells the program to call particular routines when
* input, this is not usually critical.
*/
+void
md_heed_signals()
{
signal(SIGINT, onintr);
* file, corruption.
*/
+void
md_ignore_signals()
{
signal(SIGQUIT, SIG_IGN);
int
md_get_file_id(fname)
-char *fname;
+ const char *fname;
{
struct stat sbuf;
int
md_link_count(fname)
-char *fname;
+ const char *fname;
{
struct stat sbuf;
* saved-game files and play them.
*/
+void
md_gct(rt_buf)
-struct rogue_time *rt_buf;
+ struct rogue_time *rt_buf;
{
- struct tm *t, *localtime();
- long seconds;
+ struct tm *t;
+ time_t seconds;
time(&seconds);
t = localtime(&seconds);
* saved-games that have been modified.
*/
+void
md_gfmt(fname, rt_buf)
-char *fname;
-struct rogue_time *rt_buf;
+ const char *fname;
+ struct rogue_time *rt_buf;
{
struct stat sbuf;
- long seconds;
+ time_t seconds;
struct tm *t;
stat(fname, &sbuf);
boolean
md_df(fname)
-char *fname;
+ const char *fname;
{
if (unlink(fname)) {
return(0);
* function, but then the score file would only have one name in it.
*/
-char *
+const char *
md_gln()
{
struct passwd *p;
* delaying execution, which is useful to this program at some times.
*/
+void
md_sleep(nsecs)
-int nsecs;
+ int nsecs;
{
(void) 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.
*
* 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;
+ const char *name;
{
char *value;
- char *getenv();
value = getenv(name);
char *
md_malloc(n)
-int n;
+ int n;
{
- char *malloc();
char *t;
t = malloc(n);
* exactly the same way given the same input.
*/
+int
md_gseed()
{
- return(getpid());
+ time_t seconds;
+
+ time(&seconds);
+ return((int) seconds);
}
/* md_exit():
* hang when it should quit.
*/
+void
md_exit(status)
-int status;
+ int status;
{
exit(status);
}
/* md_lock():
*
* This function is intended to give the user exclusive access to the score
- * file. It does so by "creat"ing a lock file, which can only be created
- * if it does not already exist. The file is deleted when score file
- * processing is finished. The lock file should be located in the same
- * directory as the score file. These full path names should be defined for
- * any particular site in rogue.h. The constants _PATH_SCOREFILE and
- * _PATH_LOCKFILE define these file names.
+ * file. It does so by flock'ing the score file. The full path name of the
+ * score file should be defined for any particular site in rogue.h. The
+ * constants _PATH_SCOREFILE defines this file name.
*
* When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
- * the lock is released by removing the lock file.
+ * the lock is released.
*/
+void
md_lock(l)
-boolean l;
+ boolean l;
{
+ static int fd;
short tries;
- char *lock_file = _PATH_LOCKFILE;
if (l) {
- for (tries = 0; tries < 5; tries++) {
- if (md_get_file_id(lock_file) == -1) {
- if (creat(lock_file, 0444) != -1) {
- break;
- } else {
- message("cannot lock score file", 0);
- }
- } else {
- message("waiting to lock score file", 0);
- }
- sleep(2);
+ setegid(egid);
+ if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
+ setegid(gid);
+ message("cannot lock score file", 0);
+ return;
}
+ setegid(gid);
+ for (tries = 0; tries < 5; tries++)
+ if (!flock(fd, LOCK_EX|LOCK_NB))
+ return;
} else {
- (void) unlink(lock_file);
+ (void)flock(fd, LOCK_NB);
+ (void)close(fd);
}
}
* The effective user id is restored after the shell completes.
*/
+void
md_shell(shell)
-char *shell;
+ const char *shell;
{
- long w[2];
+ int w;
if (!fork()) {
- int uid;
-
- uid = getuid();
- setuid(uid);
execl(shell, shell, 0);
}
- wait(w);
+ 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.
- *
- */
-
-md_cbreak_no_echo_nonl(on)
-boolean on;
-{
-#ifdef UNIX_BSD4_2
- static struct sgttyb tty_buf;
- static int tsave_flags;
-
- if (on) {
- ioctl(0, TIOCGETP, &tty_buf);
- tsave_flags = tty_buf.sg_flags;
- tty_buf.sg_flags |= CBREAK;
- tty_buf.sg_flags &= ~(ECHO | CRMOD); /* CRMOD: see note 3 above */
- ioctl(0, TIOCSETP, &tty_buf);
- } else {
- tty_buf.sg_flags = tsave_flags;
- ioctl(0, TIOCSETP, &tty_buf);
- }
-#endif
-#ifdef UNIX_SYSV
- struct termio tty_buf;
- static struct termio tty_save;
-
- if (on) {
- ioctl(0, TCGETA, &tty_buf);
- tty_save = tty_buf;
- tty_buf.c_lflag &= ~(ICANON | ECHO);
- tty_buf.c_oflag &= ~ONLCR;
- tty_buf.c_cc[4] = 1; /* MIN */
- tty_buf.c_cc[5] = 2; /* TIME */
- ioctl(0, TCSETAF, &tty_buf);
- } else {
- ioctl(0, TCSETAF, &tty_save);
- }
-#endif
-}
-
-/* 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.
- *
- */
-
-md_tstp()
-{
-#ifdef UNIX_BSD4_2
- kill(0, SIGTSTP);
-#endif
-}
-
-#endif
-
#endif