]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/machdep.c
1 /* $NetBSD: machdep.c,v 1.11 1999/09/13 17:14:08 jsm Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
42 static char sccsid
[] = "@(#)machdep.c 8.1 (Berkeley) 5/31/93";
44 __RCSID("$NetBSD: machdep.c,v 1.11 1999/09/13 17:14:08 jsm Exp $");
51 * This source herein may be modified and/or distributed by anybody who
52 * so desires, with the following restrictions:
53 * 1.) No portion of this notice shall be removed.
54 * 2.) Credit shall not be taken for the creation of this source.
55 * 3.) This code is not to be traded, sold, or used for personal
60 /* Included in this file are all system dependent routines. Extensive use
61 * of #ifdef's will be used to compile the appropriate code on each system:
63 * UNIX: all UNIX systems.
64 * UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?)
65 * UNIX_SYSV: UNIX system V
66 * UNIX_V7: UNIX version 7
68 * All UNIX code should be included between the single "#ifdef UNIX" at the
69 * top of this file, and the "#endif" at the bottom.
71 * To change a routine to include a new UNIX system, simply #ifdef the
72 * existing routine, as in the following example:
74 * To make a routine compatible with UNIX system 5, change the first
75 * function to the second:
91 * Appropriate variations of this are of course acceptible.
92 * The use of "#elseif" is discouraged because of non-portability.
93 * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up
94 * and insert it in the list at the top of the file. Alter the CFLAGS
95 * in you Makefile appropriately.
101 #include <sys/types.h>
102 #include <sys/wait.h>
103 #include <sys/file.h>
104 #include <sys/stat.h>
108 #include <sys/time.h>
120 #include "pathnames.h"
124 * This routine throws away all keyboard input that has not
125 * yet been read. It is used to get rid of input that the user may have
128 * This function is not necessary, so it may be stubbed. The might cause
129 * message-line output to flash by because the game has continued to read
130 * input without waiting for the user to read the message. Not such a
140 /* md_heed_signals():
142 * This routine tells the program to call particular routines when
143 * certain interrupts/events occur:
145 * SIGINT: call onintr() to interrupt fight with monster or long rest.
146 * SIGQUIT: call byebye() to check for game termination.
147 * SIGHUP: call error_save() to save game when terminal hangs up.
149 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
151 * This routine is not strictly necessary and can be stubbed. This will
152 * mean that the game cannot be interrupted properly with keyboard
153 * input, this is not usually critical.
159 signal(SIGINT
, onintr
);
160 signal(SIGQUIT
, byebye
);
161 signal(SIGHUP
, error_save
);
164 /* md_ignore_signals():
166 * This routine tells the program to completely ignore the events mentioned
167 * in md_heed_signals() above. The event handlers will later be turned on
168 * by a future call to md_heed_signals(), so md_heed_signals() and
169 * md_ignore_signals() need to work together.
171 * This function should be implemented or the user risks interrupting
172 * critical sections of code, which could cause score file, or saved-game
179 signal(SIGQUIT
, SIG_IGN
);
180 signal(SIGINT
, SIG_IGN
);
181 signal(SIGHUP
, SIG_IGN
);
186 * This function returns an integer that uniquely identifies the specified
187 * file. It need not check for the file's existence. In UNIX, the inode
190 * This function is used to identify saved-game files.
194 md_get_file_id(fname
)
199 if (stat(fname
, &sbuf
)) {
202 return((int) sbuf
.st_ino
);
207 * This routine returns the number of hard links to the specified file.
209 * This function is not strictly necessary. On systems without hard links
210 * this routine can be stubbed by just returning 1.
220 return((int) sbuf
.st_nlink
);
223 /* md_gct(): (Get Current Time)
225 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
226 * minute(0-59), and second(0-59). This is used for identifying the time
227 * at which a game is saved.
229 * This function is not strictly necessary. It can be stubbed by returning
230 * zeros instead of the correct year, month, etc. If your operating
231 * system doesn't provide all of the time units requested here, then you
232 * can provide only those that it does, and return zeros for the others.
233 * If you cannot provide good time values, then users may be able to copy
234 * saved-game files and play them.
239 struct rogue_time
*rt_buf
;
245 t
= localtime(&seconds
);
247 rt_buf
->year
= t
->tm_year
;
248 rt_buf
->month
= t
->tm_mon
+ 1;
249 rt_buf
->day
= t
->tm_mday
;
250 rt_buf
->hour
= t
->tm_hour
;
251 rt_buf
->minute
= t
->tm_min
;
252 rt_buf
->second
= t
->tm_sec
;
255 /* md_gfmt: (Get File Modification Time)
257 * This routine returns a file's date of last modification in the same format
260 * This function is not strictly necessary. It is used to see if saved-game
261 * files have been modified since they were saved. If you have stubbed the
262 * routine md_gct() above by returning constant values, then you may do
263 * exactly the same here.
264 * Or if md_gct() is implemented correctly, but your system does not provide
265 * file modification dates, you may return some date far in the past so
266 * that the program will never know that a saved-game file being modified.
267 * You may also do this if you wish to be able to restore games from
268 * saved-games that have been modified.
272 md_gfmt(fname
, rt_buf
)
274 struct rogue_time
*rt_buf
;
281 seconds
= (long) sbuf
.st_mtime
;
282 t
= localtime(&seconds
);
284 rt_buf
->year
= t
->tm_year
;
285 rt_buf
->month
= t
->tm_mon
+ 1;
286 rt_buf
->day
= t
->tm_mday
;
287 rt_buf
->hour
= t
->tm_hour
;
288 rt_buf
->minute
= t
->tm_min
;
289 rt_buf
->second
= t
->tm_sec
;
292 /* md_df: (Delete File)
294 * This function deletes the specified file, and returns true (1) if the
295 * operation was successful. This is used to delete saved-game files
296 * after restoring games from them.
298 * Again, this function is not strictly necessary, and can be stubbed
299 * by simply returning 1. In this case, saved-game files will not be
300 * deleted and can be replayed.
313 /* md_gln: (Get login name)
315 * This routine returns the login name of the user. This string is
316 * used mainly for identifying users in score files.
318 * A dummy string may be returned if you are unable to implement this
319 * function, but then the score file would only have one name in it.
327 if (!(p
= getpwuid(getuid())))
328 return((char *)NULL
);
334 * This routine causes the game to pause for the specified number of
337 * This routine is not particularly necessary at all. It is used for
338 * delaying execution, which is useful to this program at some times.
350 * This routine gets certain values from the user's environment. These
351 * values are strings, and each string is identified by a name. The names
352 * of the values needed, and their use, is as follows:
355 * A string containing the various game options. This need not be
358 * The user's home directory. This is only used when the user specifies
359 * '~' as the first character of a saved-game file. This string need
362 * The user's favorite shell. If not found, "/bin/sh" is assumed.
364 * If your system does not provide a means of searching for these values,
365 * you will have to do it yourself. None of the values above really need
366 * to be defined; you can get by with simply always returning zero.
367 * Returning zero indicates that their is no defined value for the
377 value
= getenv(name
);
384 * This routine allocates, and returns a pointer to, the specified number
385 * of bytes. This routines absolutely MUST be implemented for your
386 * particular system or the program will not run at all. Return zero
387 * when no more memory can be allocated.
400 /* md_gseed() (Get Seed)
402 * This function returns a seed for the random number generator (RNG). This
403 * seed causes the RNG to begin generating numbers at some point in it's
404 * sequence. Without a random seed, the RNG will generate the same set
405 * of numbers, and every game will start out exactly the same way. A good
406 * number to use is the process id, given by getpid() on most UNIX systems.
408 * You need to find some single random integer, such as:
410 * current time (minutes + seconds) returned from md_gct(), if implemented.
412 * It will not help to return "get_rand()" or "rand()" or the return value of
413 * any pseudo-RNG. If you don't have a random number, you can just return 1,
414 * but this means your games will ALWAYS start the same way, and will play
415 * exactly the same way given the same input.
424 return((int) seconds
);
429 * This function causes the program to discontinue execution and exit.
430 * This function must be implemented or the program will continue to
431 * hang when it should quit.
443 * This function is intended to give the user exclusive access to the score
444 * file. It does so by flock'ing the score file. The full path name of the
445 * score file should be defined for any particular site in rogue.h. The
446 * constants _PATH_SCOREFILE defines this file name.
448 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
449 * the lock is released.
461 if ((fd
= open(_PATH_SCOREFILE
, O_RDONLY
)) < 1) {
463 message("cannot lock score file", 0);
467 for (tries
= 0; tries
< 5; tries
++)
468 if (!flock(fd
, LOCK_EX
|LOCK_NB
))
471 (void)flock(fd
, LOCK_NB
);
478 * This function spawns a shell for the user to use. When this shell is
479 * terminated, the game continues. Since this program may often be run
480 * setuid to gain access to privileged files, care is taken that the shell
481 * is run with the user's REAL user id, and not the effective user id.
482 * The effective user id is restored after the shell completes.
492 execl(shell
, shell
, 0);