]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/machdep.c
Coverity CID 2452: possible negative array index; CID 1518 and CID 1517: possible...
[bsdgames-darwin.git] / rogue / machdep.c
1 /* $NetBSD: machdep.c,v 1.13 2005/02/15 12:56:20 jsm 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.13 2005/02/15 12:56:20 jsm 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 acceptible.
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
103 #ifdef UNIX_BSD4_2
104 #include <sys/time.h>
105 #endif
106
107 #ifdef UNIX_SYSV
108 #include <time.h>
109 #endif
110
111 #include <signal.h>
112 #include <stdlib.h>
113 #include <termios.h>
114 #include <unistd.h>
115 #include "rogue.h"
116 #include "pathnames.h"
117
118 /* md_slurp:
119 *
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.
123 *
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.
128 */
129
130 void
131 md_slurp()
132 {
133 (void)fpurge(stdin);
134 }
135
136 /* md_heed_signals():
137 *
138 * This routine tells the program to call particular routines when
139 * certain interrupts/events occur:
140 *
141 * SIGINT: call onintr() to interrupt fight with monster or long rest.
142 * SIGQUIT: call byebye() to check for game termination.
143 * SIGHUP: call error_save() to save game when terminal hangs up.
144 *
145 * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
146 *
147 * This routine is not strictly necessary and can be stubbed. This will
148 * mean that the game cannot be interrupted properly with keyboard
149 * input, this is not usually critical.
150 */
151
152 void
153 md_heed_signals()
154 {
155 signal(SIGINT, onintr);
156 signal(SIGQUIT, byebye);
157 signal(SIGHUP, error_save);
158 }
159
160 /* md_ignore_signals():
161 *
162 * This routine tells the program to completely ignore the events mentioned
163 * in md_heed_signals() above. The event handlers will later be turned on
164 * by a future call to md_heed_signals(), so md_heed_signals() and
165 * md_ignore_signals() need to work together.
166 *
167 * This function should be implemented or the user risks interrupting
168 * critical sections of code, which could cause score file, or saved-game
169 * file, corruption.
170 */
171
172 void
173 md_ignore_signals()
174 {
175 signal(SIGQUIT, SIG_IGN);
176 signal(SIGINT, SIG_IGN);
177 signal(SIGHUP, SIG_IGN);
178 }
179
180 /* md_get_file_id():
181 *
182 * This function returns an integer that uniquely identifies the specified
183 * file. It need not check for the file's existence. In UNIX, the inode
184 * number is used.
185 *
186 * This function is used to identify saved-game files.
187 */
188
189 int
190 md_get_file_id(fname)
191 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(fname)
211 const char *fname;
212 {
213 struct stat sbuf;
214
215 stat(fname, &sbuf);
216 return((int) sbuf.st_nlink);
217 }
218
219 /* md_gct(): (Get Current Time)
220 *
221 * This function returns the current year, month(1-12), day(1-31), hour(0-23),
222 * minute(0-59), and second(0-59). This is used for identifying the time
223 * at which a game is saved.
224 *
225 * This function is not strictly necessary. It can be stubbed by returning
226 * zeros instead of the correct year, month, etc. If your operating
227 * system doesn't provide all of the time units requested here, then you
228 * can provide only those that it does, and return zeros for the others.
229 * If you cannot provide good time values, then users may be able to copy
230 * saved-game files and play them.
231 */
232
233 void
234 md_gct(rt_buf)
235 struct rogue_time *rt_buf;
236 {
237 struct tm *t;
238 time_t seconds;
239
240 time(&seconds);
241 t = localtime(&seconds);
242
243 rt_buf->year = t->tm_year;
244 rt_buf->month = t->tm_mon + 1;
245 rt_buf->day = t->tm_mday;
246 rt_buf->hour = t->tm_hour;
247 rt_buf->minute = t->tm_min;
248 rt_buf->second = t->tm_sec;
249 }
250
251 /* md_gfmt: (Get File Modification Time)
252 *
253 * This routine returns a file's date of last modification in the same format
254 * as md_gct() above.
255 *
256 * This function is not strictly necessary. It is used to see if saved-game
257 * files have been modified since they were saved. If you have stubbed the
258 * routine md_gct() above by returning constant values, then you may do
259 * exactly the same here.
260 * Or if md_gct() is implemented correctly, but your system does not provide
261 * file modification dates, you may return some date far in the past so
262 * that the program will never know that a saved-game file being modified.
263 * You may also do this if you wish to be able to restore games from
264 * saved-games that have been modified.
265 */
266
267 void
268 md_gfmt(fname, rt_buf)
269 const char *fname;
270 struct rogue_time *rt_buf;
271 {
272 struct stat sbuf;
273 time_t seconds;
274 struct tm *t;
275
276 stat(fname, &sbuf);
277 seconds = (long) sbuf.st_mtime;
278 t = localtime(&seconds);
279
280 rt_buf->year = t->tm_year;
281 rt_buf->month = t->tm_mon + 1;
282 rt_buf->day = t->tm_mday;
283 rt_buf->hour = t->tm_hour;
284 rt_buf->minute = t->tm_min;
285 rt_buf->second = t->tm_sec;
286 }
287
288 /* md_df: (Delete File)
289 *
290 * This function deletes the specified file, and returns true (1) if the
291 * operation was successful. This is used to delete saved-game files
292 * after restoring games from them.
293 *
294 * Again, this function is not strictly necessary, and can be stubbed
295 * by simply returning 1. In this case, saved-game files will not be
296 * deleted and can be replayed.
297 */
298
299 boolean
300 md_df(fname)
301 const char *fname;
302 {
303 if (unlink(fname)) {
304 return(0);
305 }
306 return(1);
307 }
308
309 /* md_gln: (Get login name)
310 *
311 * This routine returns the login name of the user. This string is
312 * used mainly for identifying users in score files.
313 *
314 * A dummy string may be returned if you are unable to implement this
315 * function, but then the score file would only have one name in it.
316 */
317
318 const char *
319 md_gln()
320 {
321 struct passwd *p;
322
323 if (!(p = getpwuid(getuid())))
324 return((char *)NULL);
325 return(p->pw_name);
326 }
327
328 /* md_sleep:
329 *
330 * This routine causes the game to pause for the specified number of
331 * seconds.
332 *
333 * This routine is not particularly necessary at all. It is used for
334 * delaying execution, which is useful to this program at some times.
335 */
336
337 void
338 md_sleep(nsecs)
339 int nsecs;
340 {
341 (void) sleep(nsecs);
342 }
343
344 /* md_getenv()
345 *
346 * This routine gets certain values from the user's environment. These
347 * values are strings, and each string is identified by a name. The names
348 * of the values needed, and their use, is as follows:
349 *
350 * ROGUEOPTS
351 * A string containing the various game options. This need not be
352 * defined.
353 * HOME
354 * The user's home directory. This is only used when the user specifies
355 * '~' as the first character of a saved-game file. This string need
356 * not be defined.
357 * SHELL
358 * The user's favorite shell. If not found, "/bin/sh" is assumed.
359 *
360 * If your system does not provide a means of searching for these values,
361 * you will have to do it yourself. None of the values above really need
362 * to be defined; you can get by with simply always returning zero.
363 * Returning zero indicates that their is no defined value for the
364 * given string.
365 */
366
367 char *
368 md_getenv(name)
369 const char *name;
370 {
371 char *value;
372
373 value = getenv(name);
374
375 return(value);
376 }
377
378 /* md_malloc()
379 *
380 * This routine allocates, and returns a pointer to, the specified number
381 * of bytes. This routines absolutely MUST be implemented for your
382 * particular system or the program will not run at all. Return zero
383 * when no more memory can be allocated.
384 */
385
386 char *
387 md_malloc(n)
388 int n;
389 {
390 char *t;
391
392 t = malloc(n);
393 return(t);
394 }
395
396 /* md_gseed() (Get Seed)
397 *
398 * This function returns a seed for the random number generator (RNG). This
399 * seed causes the RNG to begin generating numbers at some point in it's
400 * sequence. Without a random seed, the RNG will generate the same set
401 * of numbers, and every game will start out exactly the same way. A good
402 * number to use is the process id, given by getpid() on most UNIX systems.
403 *
404 * You need to find some single random integer, such as:
405 * process id.
406 * current time (minutes + seconds) returned from md_gct(), if implemented.
407 *
408 * It will not help to return "get_rand()" or "rand()" or the return value of
409 * any pseudo-RNG. If you don't have a random number, you can just return 1,
410 * but this means your games will ALWAYS start the same way, and will play
411 * exactly the same way given the same input.
412 */
413
414 int
415 md_gseed()
416 {
417 time_t seconds;
418
419 time(&seconds);
420 return((int) seconds);
421 }
422
423 /* md_exit():
424 *
425 * This function causes the program to discontinue execution and exit.
426 * This function must be implemented or the program will continue to
427 * hang when it should quit.
428 */
429
430 void
431 md_exit(status)
432 int status;
433 {
434 exit(status);
435 }
436
437 /* md_lock():
438 *
439 * This function is intended to give the user exclusive access to the score
440 * file. It does so by flock'ing the score file. The full path name of the
441 * score file should be defined for any particular site in rogue.h. The
442 * constants _PATH_SCOREFILE defines this file name.
443 *
444 * When the parameter 'l' is non-zero (true), a lock is requested. Otherwise
445 * the lock is released.
446 */
447
448 void
449 md_lock(l)
450 boolean l;
451 {
452 static int fd;
453 short tries;
454
455 if (l) {
456 setegid(egid);
457 if ((fd = open(_PATH_SCOREFILE, O_RDONLY)) < 1) {
458 setegid(gid);
459 message("cannot lock score file", 0);
460 return;
461 }
462 setegid(gid);
463 for (tries = 0; tries < 5; tries++)
464 if (!flock(fd, LOCK_EX|LOCK_NB))
465 return;
466 } else {
467 (void)flock(fd, LOCK_NB);
468 (void)close(fd);
469 }
470 }
471
472 /* md_shell():
473 *
474 * This function spawns a shell for the user to use. When this shell is
475 * terminated, the game continues. Since this program may often be run
476 * setuid to gain access to privileged files, care is taken that the shell
477 * is run with the user's REAL user id, and not the effective user id.
478 * The effective user id is restored after the shell completes.
479 */
480
481 void
482 md_shell(shell)
483 const char *shell;
484 {
485 int w;
486
487 if (!fork()) {
488 execl(shell, shell, (char *) 0);
489 }
490 wait(&w);
491 }
492
493 #endif