2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)machdep.c 8.1 (Berkeley) 5/31/93";
41 static const char rcsid[] =
42 "$FreeBSD: src/games/rogue/machdep.c,v 1.6.2.1 2001/12/17 12:43:23 phantom Exp $";
48 * This source herein may be modified and/or distributed by anybody who
49 * so desires, with the following restrictions:
50 * 1.) No portion of this notice shall be removed.
51 * 2.) Credit shall not be taken for the creation of this source.
52 * 3.) This code is not to be traded, sold, or used for personal
57 /* Included in this file are all system dependent routines. Extensive use
58 * of #ifdef's will be used to compile the appropriate code on each system:
60 * UNIX: all UNIX systems.
61 * UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?)
62 * UNIX_SYSV: UNIX system V
63 * UNIX_V7: UNIX version 7
65 * All UNIX code should be included between the single "#ifdef UNIX" at the
66 * top of this file, and the "#endif" at the bottom.
68 * To change a routine to include a new UNIX system, simply #ifdef the
69 * existing routine, as in the following example:
71 * To make a routine compatible with UNIX system 5, change the first
72 * function to the second:
88 * Appropriate variations of this are of course acceptible.
89 * The use of "#elseif" is discouraged because of non-portability.
90 * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up
91 * and insert it in the list at the top of the file. Alter the CFLAGS
92 * in you Makefile appropriately.
99 #include <sys/types.h>
100 #include <sys/file.h>
101 #include <sys/stat.h>
105 #include <sys/time.h>
118 #include "pathnames.h"
122 * This routine throws away all keyboard input that has not
123 * yet been read. It is used to get rid of input that the user may have
126 * This function is not necessary, so it may be stubbed. The might cause
127 * message-line output to flash by because the game has continued to read
128 * input without waiting for the user to read the message. Not such a
137 /* md_control_keyboard():
139 * This routine is much like md_cbreak_no_echo_nonl() below. It sets up the
140 * keyboard for appropriate input. Specifically, it prevents the tty driver
141 * from stealing characters. For example, ^Y is needed as a command
142 * character, but the tty driver intercepts it for another purpose. Any
143 * such behavior should be stopped. This routine could be avoided if
144 * we used RAW mode instead of CBREAK. But RAW mode does not allow the
145 * generation of keyboard signals, which the program uses.
147 * The parameter 'mode' when true, indicates that the keyboard should
148 * be set up to play rogue. When false, it should be restored if
151 * This routine is not strictly necessary and may be stubbed. This may
152 * cause certain command characters to be unavailable.
155 md_control_keybord(mode)
158 static boolean called_before = 0;
160 static struct ltchars ltc_orig;
161 static struct tchars tc_orig;
162 struct ltchars ltc_temp;
163 struct tchars tc_temp;
166 static struct termio _oldtty;
170 if (!called_before) {
173 ioctl(0, TIOCGETC, &tc_orig);
174 ioctl(0, TIOCGLTC, <c_orig);
177 ioctl(0, TCGETA, &_oldtty);
190 ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1;
191 ltc_temp.t_rprntc = ltc_temp.t_flushc = -1;
192 ltc_temp.t_werasc = ltc_temp.t_lnextc = -1;
193 tc_temp.t_startc = tc_temp.t_stopc = -1;
196 _tty.c_cc[VSWTCH] = CNSWTCH;
200 ioctl(0, TIOCSETC, &tc_temp);
201 ioctl(0, TIOCSLTC, <c_temp);
204 ioctl(0, TCSETA, &_tty);
208 /* md_heed_signals():
210 * This routine tells the program to call particular routines when
211 * certain interrupts/events occur:
213 * SIGINT: call onintr() to interrupt fight with monster or long rest.
214 * SIGQUIT: call byebye() to check for game termination.
215 * SIGHUP: call error_save() to save game when terminal hangs up.
217 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
219 * This routine is not strictly necessary and can be stubbed. This will
220 * mean that the game cannot be interrupted properly with keyboard
221 * input, this is not usually critical.
226 signal(SIGINT, onintr);
227 signal(SIGQUIT, byebye);
228 signal(SIGHUP, error_save);
231 /* md_ignore_signals():
233 * This routine tells the program to completely ignore the events mentioned
234 * in md_heed_signals() above. The event handlers will later be turned on
235 * by a future call to md_heed_signals(), so md_heed_signals() and
236 * md_ignore_signals() need to work together.
238 * This function should be implemented or the user risks interrupting
239 * critical sections of code, which could cause score file, or saved-game
245 signal(SIGQUIT, SIG_IGN);
246 signal(SIGINT, SIG_IGN);
247 signal(SIGHUP, SIG_IGN);
252 * This function returns an integer that uniquely identifies the specified
253 * file. It need not check for the file's existence. In UNIX, the inode
256 * This function is used to identify saved-game files.
260 md_get_file_id(fname)
265 if (stat(fname, &sbuf)) {
268 return((int) sbuf.st_ino);
273 * This routine returns the number of hard links to the specified file.
275 * This function is not strictly necessary. On systems without hard links
276 * this routine can be stubbed by just returning 1.
286 return((int) sbuf.st_nlink);
289 /* md_gct(): (Get Current Time)
291 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
292 * minute(0-59), and second(0-59). This is used for identifying the time
293 * at which a game is saved.
295 * This function is not strictly necessary. It can be stubbed by returning
296 * zeros instead of the correct year, month, etc. If your operating
297 * system doesn't provide all of the time units requested here, then you
298 * can provide only those that it does, and return zeros for the others.
299 * If you cannot provide good time values, then users may be able to copy
300 * saved-game files and play them.
304 struct rogue_time *rt_buf;
306 struct tm *t, *localtime();
310 t = localtime(&seconds);
312 rt_buf->year = t->tm_year;
313 rt_buf->month = t->tm_mon + 1;
314 rt_buf->day = t->tm_mday;
315 rt_buf->hour = t->tm_hour;
316 rt_buf->minute = t->tm_min;
317 rt_buf->second = t->tm_sec;
320 /* md_gfmt: (Get File Modification Time)
322 * This routine returns a file's date of last modification in the same format
325 * This function is not strictly necessary. It is used to see if saved-game
326 * files have been modified since they were saved. If you have stubbed the
327 * routine md_gct() above by returning constant values, then you may do
328 * exactly the same here.
329 * Or if md_gct() is implemented correctly, but your system does not provide
330 * file modification dates, you may return some date far in the past so
331 * that the program will never know that a saved-game file being modified.
332 * You may also do this if you wish to be able to restore games from
333 * saved-games that have been modified.
336 md_gfmt(fname, rt_buf)
338 struct rogue_time *rt_buf;
345 seconds = sbuf.st_mtime;
346 t = localtime(&seconds);
348 rt_buf->year = t->tm_year;
349 rt_buf->month = t->tm_mon + 1;
350 rt_buf->day = t->tm_mday;
351 rt_buf->hour = t->tm_hour;
352 rt_buf->minute = t->tm_min;
353 rt_buf->second = t->tm_sec;
356 /* md_df: (Delete File)
358 * This function deletes the specified file, and returns true (1) if the
359 * operation was successful. This is used to delete saved-game files
360 * after restoring games from them.
362 * Again, this function is not strictly necessary, and can be stubbed
363 * by simply returning 1. In this case, saved-game files will not be
364 * deleted and can be replayed.
377 /* md_gln: (Get login name)
379 * This routine returns the login name of the user. This string is
380 * used mainly for identifying users in score files.
382 * A dummy string may be returned if you are unable to implement this
383 * function, but then the score file would only have one name in it.
392 if ((s = getlogin()))
394 if (!(p = getpwuid(getuid())))
395 return((char *)NULL);
401 * This routine causes the game to pause for the specified number of
404 * This routine is not particularly necessary at all. It is used for
405 * delaying execution, which is useful to this program at some times.
416 * This routine gets certain values from the user's environment. These
417 * values are strings, and each string is identified by a name. The names
418 * of the values needed, and their use, is as follows:
421 * A string containing the various game options. This need not be
424 * The user's home directory. This is only used when the user specifies
425 * '~' as the first character of a saved-game file. This string need
428 * The user's favorite shell. If not found, "/bin/sh" is assumed.
438 value = getenv(name);
445 * This routine allocates, and returns a pointer to, the specified number
446 * of bytes. This routines absolutely MUST be implemented for your
447 * particular system or the program will not run at all. Return zero
448 * when no more memory can be allocated.
461 /* md_gseed() (Get Seed)
463 * This function returns a seed for the random number generator (RNG). This
464 * seed causes the RNG to begin generating numbers at some point in it's
465 * sequence. Without a random seed, the RNG will generate the same set
466 * of numbers, and every game will start out exactly the same way. A good
467 * number to use is the process id, given by getpid() on most UNIX systems.
469 * You need to find some single random integer, such as:
471 * current time (minutes + seconds) returned from md_gct(), if implemented.
473 * It will not help to return "get_rand()" or "rand()" or the return value of
474 * any pseudo-RNG. If you don't have a random number, you can just return 1,
475 * but this means your games will ALWAYS start the same way, and will play
476 * exactly the same way given the same input.
484 return((int) seconds);
489 * This function causes the program to discontinue execution and exit.
490 * This function must be implemented or the program will continue to
491 * hang when it should quit.
502 * This function is intended to give the user exclusive access to the score
503 * file. It does so by flock'ing the score file. The full path name of the
504 * score file should be defined for any particular site in rogue.h. The
505 * constants _PATH_SCOREFILE defines this file name.
507 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
508 * the lock is released.
518 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
519 message("cannot lock score file", 0);
522 for (tries = 0; tries < 5; tries++)
523 if (!flock(fd, LOCK_EX|LOCK_NB))
526 (void)flock(fd, LOCK_NB);
533 * This function spawns a shell for the user to use. When this shell is
534 * terminated, the game continues. Since this program may often be run
535 * setuid to gain access to privileged files, care is taken that the shell
536 * is run with the user's REAL user id, and not the effective user id.
537 * The effective user id is restored after the shell completes.
548 execl(shell, shell, 0);