]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/rogue/machdep.c
This commit was generated by cvs2svn to compensate for changes in r94670,
[FreeBSD/FreeBSD.git] / games / rogue / machdep.c
1 /*
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Timothy C. Stoehr.
7  *
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.
23  *
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.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)machdep.c   8.1 (Berkeley) 5/31/93";
40 #endif
41 static const char rcsid[] =
42  "$FreeBSD$";
43 #endif /* not lint */
44
45 /*
46  * machdep.c
47  *
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
53  *         gain or profit.
54  *
55  */
56
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:
59  *
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
64  *
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.
67  *
68  * To change a routine to include a new UNIX system, simply #ifdef the
69  * existing routine, as in the following example:
70  *
71  *   To make a routine compatible with UNIX system 5, change the first
72  *   function to the second:
73  *
74  *      md_function()
75  *      {
76  *         code;
77  *      }
78  *
79  *      md_function()
80  *      {
81  *      #ifdef UNIX_SYSV
82  *         sys5code;
83  *      #else
84  *         code;
85  *      #endif
86  *      }
87  *
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.
93  *
94  */
95
96 #ifdef UNIX
97
98 #include <stdio.h>
99 #include <sys/types.h>
100 #include <sys/file.h>
101 #include <sys/stat.h>
102
103 #include <pwd.h>
104 #include <time.h>
105
106 #ifdef UNIX_BSD4_2
107 #include <sys/time.h>
108 #include <sgtty.h>
109 #endif
110
111 #ifdef UNIX_SYSV
112 #include <time.h>
113 #include <termio.h>
114 #endif
115
116 #include <signal.h>
117 #include <stdlib.h>
118 #include <unistd.h>
119 #include "rogue.h"
120 #include "pathnames.h"
121
122 /* md_slurp:
123  *
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
126  * typed-ahead.
127  *
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
131  * big deal.
132  */
133
134 md_slurp()
135 {
136         (void)fpurge(stdin);
137 }
138
139 /* md_control_keyboard():
140  *
141  * This routine is much like md_cbreak_no_echo_nonl() below.  It sets up the
142  * keyboard for appropriate input.  Specifically, it prevents the tty driver
143  * from stealing characters.  For example, ^Y is needed as a command
144  * character, but the tty driver intercepts it for another purpose.  Any
145  * such behavior should be stopped.  This routine could be avoided if
146  * we used RAW mode instead of CBREAK.  But RAW mode does not allow the
147  * generation of keyboard signals, which the program uses.
148  *
149  * The parameter 'mode' when true, indicates that the keyboard should
150  * be set up to play rogue.  When false, it should be restored if
151  * necessary.
152  *
153  * This routine is not strictly necessary and may be stubbed.  This may
154  * cause certain command characters to be unavailable.
155  */
156
157 md_control_keybord(mode)
158 boolean mode;
159 {
160         static boolean called_before = 0;
161 #ifdef UNIX_BSD4_2
162         static struct ltchars ltc_orig;
163         static struct tchars tc_orig;
164         struct ltchars ltc_temp;
165         struct tchars tc_temp;
166 #endif
167 #ifdef UNIX_SYSV
168         static struct termio _oldtty;
169         struct termio _tty;
170 #endif
171
172         if (!called_before) {
173                 called_before = 1;
174 #ifdef UNIX_BSD4_2
175                 ioctl(0, TIOCGETC, &tc_orig);
176                 ioctl(0, TIOCGLTC, &ltc_orig);
177 #endif
178 #ifdef UNIX_SYSV
179                 ioctl(0, TCGETA, &_oldtty);
180 #endif
181         }
182 #ifdef UNIX_BSD4_2
183         ltc_temp = ltc_orig;
184         tc_temp = tc_orig;
185 #endif
186 #ifdef UNIX_SYSV
187         _tty = _oldtty;
188 #endif
189
190         if (!mode) {
191 #ifdef UNIX_BSD4_2
192                 ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1;
193                 ltc_temp.t_rprntc = ltc_temp.t_flushc = -1;
194                 ltc_temp.t_werasc = ltc_temp.t_lnextc = -1;
195                 tc_temp.t_startc = tc_temp.t_stopc = -1;
196 #endif
197 #ifdef UNIX_SYSV
198                 _tty.c_cc[VSWTCH] = CNSWTCH;
199 #endif
200         }
201 #ifdef UNIX_BSD4_2
202         ioctl(0, TIOCSETC, &tc_temp);
203         ioctl(0, TIOCSLTC, &ltc_temp);
204 #endif
205 #ifdef UNIX_SYSV
206         ioctl(0, TCSETA, &_tty);
207 #endif
208 }
209
210 /* md_heed_signals():
211  *
212  * This routine tells the program to call particular routines when
213  * certain interrupts/events occur:
214  *
215  *      SIGINT: call onintr() to interrupt fight with monster or long rest.
216  *      SIGQUIT: call byebye() to check for game termination.
217  *      SIGHUP: call error_save() to save game when terminal hangs up.
218  *
219  *              On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
220  *
221  * This routine is not strictly necessary and can be stubbed.  This will
222  * mean that the game cannot be interrupted properly with keyboard
223  * input, this is not usually critical.
224  */
225
226 md_heed_signals()
227 {
228         signal(SIGINT, onintr);
229         signal(SIGQUIT, byebye);
230         signal(SIGHUP, error_save);
231 }
232
233 /* md_ignore_signals():
234  *
235  * This routine tells the program to completely ignore the events mentioned
236  * in md_heed_signals() above.  The event handlers will later be turned on
237  * by a future call to md_heed_signals(), so md_heed_signals() and
238  * md_ignore_signals() need to work together.
239  *
240  * This function should be implemented or the user risks interrupting
241  * critical sections of code, which could cause score file, or saved-game
242  * file, corruption.
243  */
244
245 md_ignore_signals()
246 {
247         signal(SIGQUIT, SIG_IGN);
248         signal(SIGINT, SIG_IGN);
249         signal(SIGHUP, SIG_IGN);
250 }
251
252 /* md_get_file_id():
253  *
254  * This function returns an integer that uniquely identifies the specified
255  * file.  It need not check for the file's existence.  In UNIX, the inode
256  * number is used.
257  *
258  * This function is used to identify saved-game files.
259  */
260
261 int
262 md_get_file_id(fname)
263 const char *fname;
264 {
265         struct stat sbuf;
266
267         if (stat(fname, &sbuf)) {
268                 return(-1);
269         }
270         return((int) sbuf.st_ino);
271 }
272
273 /* md_link_count():
274  *
275  * This routine returns the number of hard links to the specified file.
276  *
277  * This function is not strictly necessary.  On systems without hard links
278  * this routine can be stubbed by just returning 1.
279  */
280
281 int
282 md_link_count(fname)
283 const char *fname;
284 {
285         struct stat sbuf;
286
287         stat(fname, &sbuf);
288         return((int) sbuf.st_nlink);
289 }
290
291 /* md_gct(): (Get Current Time)
292  *
293  * This function returns the current year, month(1-12), day(1-31), hour(0-23),
294  * minute(0-59), and second(0-59).  This is used for identifying the time
295  * at which a game is saved.
296  *
297  * This function is not strictly necessary.  It can be stubbed by returning
298  * zeros instead of the correct year, month, etc.  If your operating
299  * system doesn't provide all of the time units requested here, then you
300  * can provide only those that it does, and return zeros for the others.
301  * If you cannot provide good time values, then users may be able to copy
302  * saved-game files and play them.
303  */
304
305 md_gct(rt_buf)
306 struct rogue_time *rt_buf;
307 {
308         struct tm *t, *localtime();
309         time_t seconds;
310
311         time(&seconds);
312         t = localtime(&seconds);
313
314         rt_buf->year = t->tm_year;
315         rt_buf->month = t->tm_mon + 1;
316         rt_buf->day = t->tm_mday;
317         rt_buf->hour = t->tm_hour;
318         rt_buf->minute = t->tm_min;
319         rt_buf->second = t->tm_sec;
320 }
321
322 /* md_gfmt: (Get File Modification Time)
323  *
324  * This routine returns a file's date of last modification in the same format
325  * as md_gct() above.
326  *
327  * This function is not strictly necessary.  It is used to see if saved-game
328  * files have been modified since they were saved.  If you have stubbed the
329  * routine md_gct() above by returning constant values, then you may do
330  * exactly the same here.
331  * Or if md_gct() is implemented correctly, but your system does not provide
332  * file modification dates, you may return some date far in the past so
333  * that the program will never know that a saved-game file being modified.
334  * You may also do this if you wish to be able to restore games from
335  * saved-games that have been modified.
336  */
337
338 md_gfmt(fname, rt_buf)
339 const char *fname;
340 struct rogue_time *rt_buf;
341 {
342         struct stat sbuf;
343         time_t seconds;
344         struct tm *t;
345
346         stat(fname, &sbuf);
347         seconds = sbuf.st_mtime;
348         t = localtime(&seconds);
349
350         rt_buf->year = t->tm_year;
351         rt_buf->month = t->tm_mon + 1;
352         rt_buf->day = t->tm_mday;
353         rt_buf->hour = t->tm_hour;
354         rt_buf->minute = t->tm_min;
355         rt_buf->second = t->tm_sec;
356 }
357
358 /* md_df: (Delete File)
359  *
360  * This function deletes the specified file, and returns true (1) if the
361  * operation was successful.  This is used to delete saved-game files
362  * after restoring games from them.
363  *
364  * Again, this function is not strictly necessary, and can be stubbed
365  * by simply returning 1.  In this case, saved-game files will not be
366  * deleted and can be replayed.
367  */
368
369 boolean
370 md_df(fname)
371 const char *fname;
372 {
373         if (unlink(fname)) {
374                 return(0);
375         }
376         return(1);
377 }
378
379 /* md_gln: (Get login name)
380  *
381  * This routine returns the login name of the user.  This string is
382  * used mainly for identifying users in score files.
383  *
384  * A dummy string may be returned if you are unable to implement this
385  * function, but then the score file would only have one name in it.
386  */
387
388 const char *
389 md_gln()
390 {
391         struct passwd *p;
392         char *s;
393
394         if ((s = getlogin()))
395                 return s;
396         if (!(p = getpwuid(getuid())))
397                 return((char *)NULL);
398         return(p->pw_name);
399 }
400
401 /* md_sleep:
402  *
403  * This routine causes the game to pause for the specified number of
404  * seconds.
405  *
406  * This routine is not particularly necessary at all.  It is used for
407  * delaying execution, which is useful to this program at some times.
408  */
409
410 md_sleep(nsecs)
411 int nsecs;
412 {
413         (void) sleep(nsecs);
414 }
415
416 /* md_getenv()
417  *
418  * This routine gets certain values from the user's environment.  These
419  * values are strings, and each string is identified by a name.  The names
420  * of the values needed, and their use, is as follows:
421  *
422  *   ROGUEOPTS
423  *     A string containing the various game options.  This need not be
424  *     defined.
425  *   HOME
426  *     The user's home directory.  This is only used when the user specifies
427  *     '~' as the first character of a saved-game file.  This string need
428  *     not be defined.
429  *   SHELL
430  *     The user's favorite shell.  If not found, "/bin/sh" is assumed.
431  *
432  */
433
434 char *
435 md_getenv(name)
436 const char *name;
437 {
438         char *value;
439
440         value = getenv(name);
441
442         return(value);
443 }
444
445 /* md_malloc()
446  *
447  * This routine allocates, and returns a pointer to, the specified number
448  * of bytes.  This routines absolutely MUST be implemented for your
449  * particular system or the program will not run at all.  Return zero
450  * when no more memory can be allocated.
451  */
452
453 char *
454 md_malloc(n)
455 int n;
456 {
457         char *t;
458
459         t = malloc(n);
460         return(t);
461 }
462
463 /* md_gseed() (Get Seed)
464  *
465  * This function returns a seed for the random number generator (RNG).  This
466  * seed causes the RNG to begin generating numbers at some point in it's
467  * sequence.  Without a random seed, the RNG will generate the same set
468  * of numbers, and every game will start out exactly the same way.  A good
469  * number to use is the process id, given by getpid() on most UNIX systems.
470  *
471  * You need to find some single random integer, such as:
472  *   process id.
473  *   current time (minutes + seconds) returned from md_gct(), if implemented.
474  *
475  * It will not help to return "get_rand()" or "rand()" or the return value of
476  * any pseudo-RNG.  If you don't have a random number, you can just return 1,
477  * but this means your games will ALWAYS start the same way, and will play
478  * exactly the same way given the same input.
479  */
480
481 md_gseed()
482 {
483         time_t seconds;
484
485         time(&seconds);
486         return((int) seconds);
487 }
488
489 /* md_exit():
490  *
491  * This function causes the program to discontinue execution and exit.
492  * This function must be implemented or the program will continue to
493  * hang when it should quit.
494  */
495
496 md_exit(status)
497 int status;
498 {
499         exit(status);
500 }
501
502 /* md_lock():
503  *
504  * This function is intended to give the user exclusive access to the score
505  * file.  It does so by flock'ing the score file.  The full path name of the
506  * score file should be defined for any particular site in rogue.h.  The
507  * constants _PATH_SCOREFILE defines this file name.
508  *
509  * When the parameter 'l' is non-zero (true), a lock is requested.  Otherwise
510  * the lock is released.
511  */
512
513 md_lock(l)
514 boolean l;
515 {
516         static int fd;
517         short tries;
518
519         if (l) {
520                 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
521                         message("cannot lock score file", 0);
522                         return;
523                 }
524                 for (tries = 0; tries < 5; tries++)
525                         if (!flock(fd, LOCK_EX|LOCK_NB))
526                                 return;
527         } else {
528                 (void)flock(fd, LOCK_NB);
529                 (void)close(fd);
530         }
531 }
532
533 /* md_shell():
534  *
535  * This function spawns a shell for the user to use.  When this shell is
536  * terminated, the game continues.  Since this program may often be run
537  * setuid to gain access to privileged files, care is taken that the shell
538  * is run with the user's REAL user id, and not the effective user id.
539  * The effective user id is restored after the shell completes.
540  */
541
542 md_shell(shell)
543 const char *shell;
544 {
545         long w[2];
546
547         if (!fork()) {
548                 /* revoke */
549                 setgid(getgid());
550                 execl(shell, shell, (char *)0);
551         }
552         wait(w);
553 }
554
555 #endif /* UNIX */