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