Remove inclusion of <sys/cdefs.h> from kernel .c files.
[dragonfly.git] / games / rogue / machdep.c
blob3d8abe435c7733a1122e1657110e3ceef36b98c4
1 /*
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
6 * Timothy C. Stoehr.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
34 * SUCH DAMAGE.
36 * @(#)machdep.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/rogue/machdep.c,v 1.6.2.1 2001/12/17 12:43:23 phantom Exp $
38 * $DragonFly: src/games/rogue/machdep.c,v 1.4 2006/09/09 02:21:49 pavalos Exp $
42 * machdep.c
44 * This source herein may be modified and/or distributed by anybody who
45 * so desires, with the following restrictions:
46 * 1.) No portion of this notice shall be removed.
47 * 2.) Credit shall not be taken for the creation of this source.
48 * 3.) This code is not to be traded, sold, or used for personal
49 * gain or profit.
53 /* Included in this file are all system dependent routines. Extensive use
54 * of #ifdef's will be used to compile the appropriate code on each system:
56 * UNIX: all UNIX systems.
57 * UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?)
58 * UNIX_SYSV: UNIX system V
59 * UNIX_V7: UNIX version 7
61 * All UNIX code should be included between the single "#ifdef UNIX" at the
62 * top of this file, and the "#endif" at the bottom.
64 * To change a routine to include a new UNIX system, simply #ifdef the
65 * existing routine, as in the following example:
67 * To make a routine compatible with UNIX system 5, change the first
68 * function to the second:
70 * md_function()
71 * {
72 * code;
73 * }
75 * md_function()
76 * {
77 * #ifdef UNIX_SYSV
78 * sys5code;
79 * #else
80 * code;
81 * #endif
82 * }
84 * Appropriate variations of this are of course acceptible.
85 * The use of "#elseif" is discouraged because of non-portability.
86 * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up
87 * and insert it in the list at the top of the file. Alter the CFLAGS
88 * in you Makefile appropriately.
92 #ifdef UNIX
94 #include <stdio.h>
95 #include <sys/types.h>
96 #include <sys/file.h>
97 #include <sys/stat.h>
98 #include <sys/wait.h>
99 #include <pwd.h>
100 #include <time.h>
102 #ifdef UNIX_BSD4_2
103 #include <sys/time.h>
104 #include <sgtty.h>
105 #endif
107 #ifdef UNIX_SYSV
108 #include <time.h>
109 #include <termio.h>
110 #endif
112 #include <signal.h>
113 #include <stdlib.h>
114 #include <unistd.h>
115 #include "rogue.h"
116 #include "pathnames.h"
118 /* md_slurp:
120 * This routine throws away all keyboard input that has not
121 * yet been read. It is used to get rid of input that the user may have
122 * typed-ahead.
124 * This function is not necessary, so it may be stubbed. The might cause
125 * message-line output to flash by because the game has continued to read
126 * input without waiting for the user to read the message. Not such a
127 * big deal.
130 void
131 md_slurp(void)
133 fpurge(stdin);
136 /* md_control_keybord():
138 * This routine is much like md_cbreak_no_echo_nonl() below. It sets up the
139 * keyboard for appropriate input. Specifically, it prevents the tty driver
140 * from stealing characters. For example, ^Y is needed as a command
141 * character, but the tty driver intercepts it for another purpose. Any
142 * such behavior should be stopped. This routine could be avoided if
143 * we used RAW mode instead of CBREAK. But RAW mode does not allow the
144 * generation of keyboard signals, which the program uses.
146 * The parameter 'mode' when true, indicates that the keyboard should
147 * be set up to play rogue. When false, it should be restored if
148 * necessary.
150 * This routine is not strictly necessary and may be stubbed. This may
151 * cause certain command characters to be unavailable.
154 void
155 md_control_keybord(boolean mode)
157 static boolean called_before = 0;
158 #ifdef UNIX_BSD4_2
159 static struct ltchars ltc_orig;
160 static struct tchars tc_orig;
161 struct ltchars ltc_temp;
162 struct tchars tc_temp;
163 #endif
164 #ifdef UNIX_SYSV
165 static struct termio _oldtty;
166 struct termio _tty;
167 #endif
169 if (!called_before) {
170 called_before = 1;
171 #ifdef UNIX_BSD4_2
172 ioctl(0, TIOCGETC, &tc_orig);
173 ioctl(0, TIOCGLTC, &ltc_orig);
174 #endif
175 #ifdef UNIX_SYSV
176 ioctl(0, TCGETA, &_oldtty);
177 #endif
179 #ifdef UNIX_BSD4_2
180 ltc_temp = ltc_orig;
181 tc_temp = tc_orig;
182 #endif
183 #ifdef UNIX_SYSV
184 _tty = _oldtty;
185 #endif
187 if (!mode) {
188 #ifdef UNIX_BSD4_2
189 ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1;
190 ltc_temp.t_rprntc = ltc_temp.t_flushc = -1;
191 ltc_temp.t_werasc = ltc_temp.t_lnextc = -1;
192 tc_temp.t_startc = tc_temp.t_stopc = -1;
193 #endif
194 #ifdef UNIX_SYSV
195 _tty.c_cc[VSWTCH] = CNSWTCH;
196 #endif
198 #ifdef UNIX_BSD4_2
199 ioctl(0, TIOCSETC, &tc_temp);
200 ioctl(0, TIOCSLTC, &ltc_temp);
201 #endif
202 #ifdef UNIX_SYSV
203 ioctl(0, TCSETA, &_tty);
204 #endif
207 /* md_heed_signals():
209 * This routine tells the program to call particular routines when
210 * certain interrupts/events occur:
212 * SIGINT: call onintr() to interrupt fight with monster or long rest.
213 * SIGQUIT: call byebye() to check for game termination.
214 * SIGHUP: call error_save() to save game when terminal hangs up.
216 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
218 * This routine is not strictly necessary and can be stubbed. This will
219 * mean that the game cannot be interrupted properly with keyboard
220 * input, this is not usually critical.
223 void
224 md_heed_signals(void)
226 signal(SIGINT, (sig_t)onintr);
227 signal(SIGQUIT, (sig_t)byebye);
228 signal(SIGHUP, (sig_t)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
240 * file, corruption.
243 void
244 md_ignore_signals(void)
246 signal(SIGQUIT, SIG_IGN);
247 signal(SIGINT, SIG_IGN);
248 signal(SIGHUP, SIG_IGN);
251 /* md_get_file_id():
253 * This function returns an integer that uniquely identifies the specified
254 * file. It need not check for the file's existence. In UNIX, the inode
255 * number is used.
257 * This function is used to identify saved-game files.
261 md_get_file_id(const char *fname)
263 struct stat sbuf;
265 if (stat(fname, &sbuf)) {
266 return(-1);
268 return((int) sbuf.st_ino);
271 /* md_link_count():
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.
280 md_link_count(const char *fname)
282 struct stat sbuf;
284 stat(fname, &sbuf);
285 return((int) sbuf.st_nlink);
288 /* md_gct(): (Get Current Time)
290 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
291 * minute(0-59), and second(0-59). This is used for identifying the time
292 * at which a game is saved.
294 * This function is not strictly necessary. It can be stubbed by returning
295 * zeros instead of the correct year, month, etc. If your operating
296 * system doesn't provide all of the time units requested here, then you
297 * can provide only those that it does, and return zeros for the others.
298 * If you cannot provide good time values, then users may be able to copy
299 * saved-game files and play them.
302 void
303 md_gct(struct rogue_time *rt_buf)
305 struct tm *t;
306 time_t seconds;
308 time(&seconds);
309 t = localtime(&seconds);
311 rt_buf->year = t->tm_year;
312 rt_buf->month = t->tm_mon + 1;
313 rt_buf->day = t->tm_mday;
314 rt_buf->hour = t->tm_hour;
315 rt_buf->minute = t->tm_min;
316 rt_buf->second = t->tm_sec;
319 /* md_gfmt: (Get File Modification Time)
321 * This routine returns a file's date of last modification in the same format
322 * as md_gct() above.
324 * This function is not strictly necessary. It is used to see if saved-game
325 * files have been modified since they were saved. If you have stubbed the
326 * routine md_gct() above by returning constant values, then you may do
327 * exactly the same here.
328 * Or if md_gct() is implemented correctly, but your system does not provide
329 * file modification dates, you may return some date far in the past so
330 * that the program will never know that a saved-game file being modified.
331 * You may also do this if you wish to be able to restore games from
332 * saved-games that have been modified.
335 void
336 md_gfmt(const char *fname, struct rogue_time *rt_buf)
338 struct stat sbuf;
339 time_t seconds;
340 struct tm *t;
342 stat(fname, &sbuf);
343 seconds = sbuf.st_mtime;
344 t = localtime(&seconds);
346 rt_buf->year = t->tm_year;
347 rt_buf->month = t->tm_mon + 1;
348 rt_buf->day = t->tm_mday;
349 rt_buf->hour = t->tm_hour;
350 rt_buf->minute = t->tm_min;
351 rt_buf->second = t->tm_sec;
354 /* md_df: (Delete File)
356 * This function deletes the specified file, and returns true (1) if the
357 * operation was successful. This is used to delete saved-game files
358 * after restoring games from them.
360 * Again, this function is not strictly necessary, and can be stubbed
361 * by simply returning 1. In this case, saved-game files will not be
362 * deleted and can be replayed.
365 boolean
366 md_df(const char *fname)
368 if (unlink(fname)) {
369 return(0);
371 return(1);
374 /* md_gln: (Get login name)
376 * This routine returns the login name of the user. This string is
377 * used mainly for identifying users in score files.
379 * A dummy string may be returned if you are unable to implement this
380 * function, but then the score file would only have one name in it.
383 const char *
384 md_gln(void)
386 struct passwd *p;
387 char *s;
389 if ((s = getlogin()))
390 return s;
391 if (!(p = getpwuid(getuid())))
392 return(NULL);
393 return(p->pw_name);
396 /* md_sleep:
398 * This routine causes the game to pause for the specified number of
399 * seconds.
401 * This routine is not particularly necessary at all. It is used for
402 * delaying execution, which is useful to this program at some times.
405 void
406 md_sleep(int nsecs)
408 sleep(nsecs);
411 /* md_getenv()
413 * This routine gets certain values from the user's environment. These
414 * values are strings, and each string is identified by a name. The names
415 * of the values needed, and their use, is as follows:
417 * ROGUEOPTS
418 * A string containing the various game options. This need not be
419 * defined.
420 * HOME
421 * The user's home directory. This is only used when the user specifies
422 * '~' as the first character of a saved-game file. This string need
423 * not be defined.
424 * SHELL
425 * The user's favorite shell. If not found, "/bin/sh" is assumed.
429 char *
430 md_getenv(const char *name)
432 char *value;
434 value = getenv(name);
436 return(value);
439 /* md_malloc()
441 * This routine allocates, and returns a pointer to, the specified number
442 * of bytes. This routines absolutely MUST be implemented for your
443 * particular system or the program will not run at all. Return zero
444 * when no more memory can be allocated.
447 char *
448 md_malloc(int n)
450 char *t;
452 t = malloc(n);
453 return(t);
456 /* md_gseed() (Get Seed)
458 * This function returns a seed for the random number generator (RNG). This
459 * seed causes the RNG to begin generating numbers at some point in it's
460 * sequence. Without a random seed, the RNG will generate the same set
461 * of numbers, and every game will start out exactly the same way. A good
462 * number to use is the process id, given by getpid() on most UNIX systems.
464 * You need to find some single random integer, such as:
465 * process id.
466 * current time (minutes + seconds) returned from md_gct(), if implemented.
468 * It will not help to return "get_rand()" or "rand()" or the return value of
469 * any pseudo-RNG. If you don't have a random number, you can just return 1,
470 * but this means your games will ALWAYS start the same way, and will play
471 * exactly the same way given the same input.
475 md_gseed(void)
477 time_t seconds;
479 time(&seconds);
480 return((int) seconds);
483 /* md_exit():
485 * This function causes the program to discontinue execution and exit.
486 * This function must be implemented or the program will continue to
487 * hang when it should quit.
490 void
491 md_exit(int status)
493 exit(status);
496 /* md_lock():
498 * This function is intended to give the user exclusive access to the score
499 * file. It does so by flock'ing the score file. The full path name of the
500 * score file should be defined for any particular site in rogue.h. The
501 * constants _PATH_SCOREFILE defines this file name.
503 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
504 * the lock is released.
507 void
508 md_lock(boolean l)
510 static int fd;
511 short tries;
513 if (l) {
514 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
515 message("cannot lock score file", 0);
516 return;
518 for (tries = 0; tries < 5; tries++)
519 if (!flock(fd, LOCK_EX|LOCK_NB))
520 return;
521 } else {
522 flock(fd, LOCK_NB);
523 close(fd);
527 /* md_shell():
529 * This function spawns a shell for the user to use. When this shell is
530 * terminated, the game continues. Since this program may often be run
531 * setuid to gain access to privileged files, care is taken that the shell
532 * is run with the user's REAL user id, and not the effective user id.
533 * The effective user id is restored after the shell completes.
536 void
537 md_shell(const char *shell)
539 int w;
540 pid_t pid;
542 pid = fork();
543 switch (pid) {
544 case -1:
545 break;
546 case 0:
547 /* revoke */
548 setgid(getgid());
549 execl(shell, shell, NULL);
550 _exit(255);
551 default:
552 waitpid(pid, &w, 0);
553 break;
557 #endif /* UNIX */