]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/machdep.c
countmail: remove outdated comments
[bsdgames-darwin.git] / rogue / machdep.c
1 /* $NetBSD: machdep.c,v 1.20 2012/12/01 11:37:27 mbalmer Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Timothy C. Stoehr.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)machdep.c 8.1 (Berkeley) 5/31/93";
39 #else
40 __RCSID("$NetBSD: machdep.c,v 1.20 2012/12/01 11:37:27 mbalmer Exp $");
41 #endif
42 #endif /* not lint */
43
44 /*
45 * machdep.c
46 *
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
52 * gain or profit.
53 *
54 */
55
56 /* Included in this file are all system dependent routines. Extensive use
57 * of #ifdef's will be used to compile the appropriate code on each system:
58 *
59 * UNIX: all UNIX systems.
60 * UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?)
61 * UNIX_SYSV: UNIX system V
62 * UNIX_V7: UNIX version 7
63 *
64 * All UNIX code should be included between the single "#ifdef UNIX" at the
65 * top of this file, and the "#endif" at the bottom.
66 *
67 * To change a routine to include a new UNIX system, simply #ifdef the
68 * existing routine, as in the following example:
69 *
70 * To make a routine compatible with UNIX system 5, change the first
71 * function to the second:
72 *
73 * md_function()
74 * {
75 * code;
76 * }
77 *
78 * md_function()
79 * {
80 * #ifdef UNIX_SYSV
81 * sys5code;
82 * #else
83 * code;
84 * #endif
85 * }
86 *
87 * Appropriate variations of this are of course acceptable.
88 * The use of "#elseif" is discouraged because of non-portability.
89 * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up
90 * and insert it in the list at the top of the file. Alter the CFLAGS
91 * in you Makefile appropriately.
92 *
93 */
94
95 #ifdef UNIX
96
97 #include <sys/types.h>
98 #include <sys/wait.h>
99 #include <sys/file.h>
100 #include <sys/stat.h>
101 #include <pwd.h>
102 #include <time.h>
103
104 #ifdef UNIX_BSD4_2
105 #include <sys/time.h>
106 #endif
107
108 #ifdef UNIX_SYSV
109 #include <time.h>
110 #endif
111
112 #include <signal.h>
113 #include <stdlib.h>
114 #include <termios.h>
115 #include <unistd.h>
116 #include "rogue.h"
117 #include "pathnames.h"
118
119 /* md_slurp:
120 *
121 * This routine throws away all keyboard input that has not
122 * yet been read. It is used to get rid of input that the user may have
123 * typed-ahead.
124 *
125 * This function is not necessary, so it may be stubbed. The might cause
126 * message-line output to flash by because the game has continued to read
127 * input without waiting for the user to read the message. Not such a
128 * big deal.
129 */
130
131 void
132 md_slurp(void)
133 {
134 (void)fpurge(stdin);
135 }
136
137 /* md_heed_signals():
138 *
139 * This routine tells the program to call particular routines when
140 * certain interrupts/events occur:
141 *
142 * SIGINT: call onintr() to interrupt fight with monster or long rest.
143 * SIGQUIT: call byebye() to check for game termination.
144 * SIGHUP: call error_save() to save game when terminal hangs up.
145 *
146 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
147 *
148 * This routine is not strictly necessary and can be stubbed. This will
149 * mean that the game cannot be interrupted properly with keyboard
150 * input, this is not usually critical.
151 */
152
153 void
154 md_heed_signals(void)
155 {
156 signal(SIGINT, onintr);
157 signal(SIGQUIT, byebye);
158 signal(SIGHUP, error_save);
159 }
160
161 /* md_ignore_signals():
162 *
163 * This routine tells the program to completely ignore the events mentioned
164 * in md_heed_signals() above. The event handlers will later be turned on
165 * by a future call to md_heed_signals(), so md_heed_signals() and
166 * md_ignore_signals() need to work together.
167 *
168 * This function should be implemented or the user risks interrupting
169 * critical sections of code, which could cause score file, or saved-game
170 * file, corruption.
171 */
172
173 void
174 md_ignore_signals(void)
175 {
176 signal(SIGQUIT, SIG_IGN);
177 signal(SIGINT, SIG_IGN);
178 signal(SIGHUP, SIG_IGN);
179 }
180
181 /* md_get_file_id():
182 *
183 * This function returns an integer that uniquely identifies the specified
184 * file. It need not check for the file's existence. In UNIX, the inode
185 * number is used.
186 *
187 * This function is used to identify saved-game files.
188 */
189
190 int
191 md_get_file_id(const char *fname)
192 {
193 struct stat sbuf;
194
195 if (stat(fname, &sbuf)) {
196 return(-1);
197 }
198 return((int)sbuf.st_ino);
199 }
200
201 /* md_link_count():
202 *
203 * This routine returns the number of hard links to the specified file.
204 *
205 * This function is not strictly necessary. On systems without hard links
206 * this routine can be stubbed by just returning 1.
207 */
208
209 int
210 md_link_count(const char *fname)
211 {
212 struct stat sbuf;
213
214 stat(fname, &sbuf);
215 return((int)sbuf.st_nlink);
216 }
217
218 /* md_gct(): (Get Current Time)
219 *
220 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
221 * minute(0-59), and second(0-59). This is used for identifying the time
222 * at which a game is saved.
223 *
224 * This function is not strictly necessary. It can be stubbed by returning
225 * zeros instead of the correct year, month, etc. If your operating
226 * system doesn't provide all of the time units requested here, then you
227 * can provide only those that it does, and return zeros for the others.
228 * If you cannot provide good time values, then users may be able to copy
229 * saved-game files and play them.
230 */
231
232 void
233 md_gct(struct rogue_time *rt_buf)
234 {
235 struct tm *t;
236 time_t seconds;
237
238 time(&seconds);
239 t = localtime(&seconds);
240
241 rt_buf->year = t->tm_year;
242 rt_buf->month = t->tm_mon + 1;
243 rt_buf->day = t->tm_mday;
244 rt_buf->hour = t->tm_hour;
245 rt_buf->minute = t->tm_min;
246 rt_buf->second = t->tm_sec;
247 }
248
249 /* md_gfmt: (Get File Modification Time)
250 *
251 * This routine returns a file's date of last modification in the same format
252 * as md_gct() above.
253 *
254 * This function is not strictly necessary. It is used to see if saved-game
255 * files have been modified since they were saved. If you have stubbed the
256 * routine md_gct() above by returning constant values, then you may do
257 * exactly the same here.
258 * Or if md_gct() is implemented correctly, but your system does not provide
259 * file modification dates, you may return some date far in the past so
260 * that the program will never know that a saved-game file being modified.
261 * You may also do this if you wish to be able to restore games from
262 * saved-games that have been modified.
263 */
264
265 void
266 md_gfmt(const char *fname, struct rogue_time *rt_buf)
267 {
268 struct stat sbuf;
269 time_t seconds;
270 struct tm *t;
271
272 stat(fname, &sbuf);
273 seconds = sbuf.st_mtime;
274 t = localtime(&seconds);
275
276 rt_buf->year = t->tm_year;
277 rt_buf->month = t->tm_mon + 1;
278 rt_buf->day = t->tm_mday;
279 rt_buf->hour = t->tm_hour;
280 rt_buf->minute = t->tm_min;
281 rt_buf->second = t->tm_sec;
282 }
283
284 /* md_df: (Delete File)
285 *
286 * This function deletes the specified file, and returns true (1) if the
287 * operation was successful. This is used to delete saved-game files
288 * after restoring games from them.
289 *
290 * Again, this function is not strictly necessary, and can be stubbed
291 * by simply returning 1. In this case, saved-game files will not be
292 * deleted and can be replayed.
293 */
294
295 boolean
296 md_df(const char *fname)
297 {
298 if (unlink(fname)) {
299 return(0);
300 }
301 return(1);
302 }
303
304 /* md_gln: (Get login name)
305 *
306 * This routine returns the login name of the user. This string is
307 * used mainly for identifying users in score files.
308 *
309 * A dummy string may be returned if you are unable to implement this
310 * function, but then the score file would only have one name in it.
311 */
312
313 const char *
314 md_gln(void)
315 {
316 struct passwd *p;
317
318 if (!(p = getpwuid(getuid())))
319 return NULL;
320 return p->pw_name;
321 }
322
323 /* md_sleep:
324 *
325 * This routine causes the game to pause for the specified number of
326 * seconds.
327 *
328 * This routine is not particularly necessary at all. It is used for
329 * delaying execution, which is useful to this program at some times.
330 */
331
332 void
333 md_sleep(int nsecs)
334 {
335 (void)sleep(nsecs);
336 }
337
338 /* md_getenv()
339 *
340 * This routine gets certain values from the user's environment. These
341 * values are strings, and each string is identified by a name. The names
342 * of the values needed, and their use, is as follows:
343 *
344 * ROGUEOPTS
345 * A string containing the various game options. This need not be
346 * defined.
347 * HOME
348 * The user's home directory. This is only used when the user specifies
349 * '~' as the first character of a saved-game file. This string need
350 * not be defined.
351 * SHELL
352 * The user's favorite shell. If not found, "/bin/sh" is assumed.
353 *
354 * If your system does not provide a means of searching for these values,
355 * you will have to do it yourself. None of the values above really need
356 * to be defined; you can get by with simply always returning zero.
357 * Returning zero indicates that their is no defined value for the
358 * given string.
359 */
360
361 char *
362 md_getenv(const char *name)
363 {
364 char *value;
365
366 value = getenv(name);
367
368 return(value);
369 }
370
371 /* md_malloc()
372 *
373 * This routine allocates, and returns a pointer to, the specified number
374 * of bytes. This routines absolutely MUST be implemented for your
375 * particular system or the program will not run at all. Return zero
376 * when no more memory can be allocated.
377 */
378
379 void *
380 md_malloc(size_t n)
381 {
382 char *t;
383
384 t = malloc(n);
385 return(t);
386 }
387
388 /* md_gseed() (Get Seed)
389 *
390 * This function returns a seed for the random number generator (RNG). This
391 * seed causes the RNG to begin generating numbers at some point in its
392 * sequence. Without a random seed, the RNG will generate the same set
393 * of numbers, and every game will start out exactly the same way. A good
394 * number to use is the process id, given by getpid() on most UNIX systems.
395 *
396 * You need to find some single random integer, such as:
397 * process id.
398 * current time (minutes + seconds) returned from md_gct(), if implemented.
399 *
400 * It will not help to return "get_rand()" or "rand()" or the return value of
401 * any pseudo-RNG. If you don't have a random number, you can just return 1,
402 * but this means your games will ALWAYS start the same way, and will play
403 * exactly the same way given the same input.
404 */
405
406 int
407 md_gseed(void)
408 {
409 time_t seconds;
410
411 time(&seconds);
412 return((int)seconds);
413 }
414
415 /* md_exit():
416 *
417 * This function causes the program to discontinue execution and exit.
418 * This function must be implemented or the program will continue to
419 * hang when it should quit.
420 */
421
422 void
423 md_exit(int status)
424 {
425 exit(status);
426 }
427
428 /* md_lock():
429 *
430 * This function is intended to give the user exclusive access to the score
431 * file. It does so by flock'ing the score file. The full path name of the
432 * score file should be defined for any particular site in rogue.h. The
433 * constants _PATH_SCOREFILE defines this file name.
434 *
435 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
436 * the lock is released.
437 */
438
439 void
440 md_lock(boolean l)
441 {
442 static int fd = -1;
443 short tries;
444
445 if (l) {
446 setegid(egid);
447 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
448 setegid(gid);
449 messagef(0, "cannot lock score file");
450 return;
451 }
452 setegid(gid);
453 for (tries = 0; tries < 5; tries++)
454 if (!flock(fd, LOCK_EX|LOCK_NB))
455 return;
456 } else {
457 (void)flock(fd, LOCK_UN|LOCK_NB);
458 (void)close(fd);
459 }
460 }
461
462 /* md_shell():
463 *
464 * This function spawns a shell for the user to use. When this shell is
465 * terminated, the game continues.
466 *
467 * It is important that the game not give the shell the privileges the
468 * game uses to access the scores file. This version of the game runs
469 * with privileges low by default; only the saved gid (if setgid) or uid
470 * (if setuid) will be privileged, but that privilege is discarded by
471 * exec().
472 */
473
474 void
475 md_shell(const char *shell)
476 {
477 int w;
478 pid_t pid;
479
480 pid = fork();
481 switch (pid) {
482 case -1:
483 break;
484 case 0:
485 execl(shell, shell, (char *)NULL);
486 _exit(255);
487 default:
488 waitpid(pid, &w, 0);
489 break;
490 }
491 }
492
493 #endif /* UNIX */