📄 machdep.c
字号:
/* * machdep.c * * This source herein may be modified and/or distributed by anybody who * so desires, with the following restrictions: * 1.) No portion of this notice shall be removed. * 2.) Credit shall not be taken for the creation of this source. * 3.) This code is not to be traded, sold, or used for personal * gain or profit. * */#ifndef lintstatic char sccsid[] = "@(#)machdep.c 5.2 (Berkeley) 11/25/87";#endif /* not lint *//* Included in this file are all system dependent routines. Extensive use * of #ifdef's will be used to compile the appropriate code on each system: * * UNIX: all UNIX systems. * UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?) * UNIX_SYSV: UNIX system V * UNIX_V7: UNIX version 7 * * All UNIX code should be included between the single "#ifdef UNIX" at the * top of this file, and the "#endif" at the bottom. * * To change a routine to include a new UNIX system, simply #ifdef the * existing routine, as in the following example: * * To make a routine compatible with UNIX system 5, change the first * function to the second: * * md_function() * { * code; * } * * md_function() * { * #ifdef UNIX_SYSV * sys5code; * #else * code; * #endif * } * * Appropriate variations of this are of course acceptible. * The use of "#elseif" is discouraged because of non-portability. * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up * and insert it in the list at the top of the file. Alter the CFLAGS * in you Makefile appropriately. * */#ifdef UNIX#include <stdio.h>#include <sys/types.h>#include <sys/file.h>#include <sys/stat.h>#include <pwd.h>#ifdef UNIX_BSD4_2#include <sys/time.h>#include <sgtty.h>#endif#ifdef UNIX_SYSV#include <time.h>#include <termio.h>#endif#include <signal.h>#include "rogue.h"/* md_slurp: * * This routine throws away all keyboard input that has not * yet been read. It is used to get rid of input that the user may have * typed-ahead. * * This function is not necessary, so it may be stubbed. The might cause * message-line output to flash by because the game has continued to read * input without waiting for the user to read the message. Not such a * big deal. */md_slurp(){ long ln = 0;#ifdef UNIX_BSD4_2 ioctl(0, FIONREAD, &ln);#endif#ifdef UNIX_SYSV ioctl(0, TCFLSH, &ln); ln = 0;#endif ln += stdin->_cnt; for (; ln > 0; ln--) { (void) getchar(); }}/* md_control_keyboard(): * * This routine is much like md_cbreak_no_echo_nonl() below. It sets up the * keyboard for appropriate input. Specifically, it prevents the tty driver * from stealing characters. For example, ^Y is needed as a command * character, but the tty driver intercepts it for another purpose. Any * such behavior should be stopped. This routine could be avoided if * we used RAW mode instead of CBREAK. But RAW mode does not allow the * generation of keyboard signals, which the program uses. * * The parameter 'mode' when true, indicates that the keyboard should * be set up to play rogue. When false, it should be restored if * necessary. * * This routine is not strictly necessary and may be stubbed. This may * cause certain command characters to be unavailable. */md_control_keybord(mode)boolean mode;{ static boolean called_before = 0;#ifdef UNIX_BSD4_2 static struct ltchars ltc_orig; static struct tchars tc_orig; struct ltchars ltc_temp; struct tchars tc_temp;#endif#ifdef UNIX_SYSV static struct termio _oldtty; struct termio _tty;#endif if (!called_before) { called_before = 1;#ifdef UNIX_BSD4_2 ioctl(0, TIOCGETC, &tc_orig); ioctl(0, TIOCGLTC, <c_orig);#endif#ifdef UNIX_SYSV ioctl(0, TCGETA, &_oldtty);#endif }#ifdef UNIX_BSD4_2 ltc_temp = ltc_orig; tc_temp = tc_orig;#endif#ifdef UNIX_SYSV _tty = _oldtty;#endif if (!mode) {#ifdef UNIX_BSD4_2 ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1; ltc_temp.t_rprntc = ltc_temp.t_flushc = -1; ltc_temp.t_werasc = ltc_temp.t_lnextc = -1; tc_temp.t_startc = tc_temp.t_stopc = -1;#endif#ifdef UNIX_SYSV _tty.c_cc[VSWTCH] = CNSWTCH;#endif }#ifdef UNIX_BSD4_2 ioctl(0, TIOCSETC, &tc_temp); ioctl(0, TIOCSLTC, <c_temp);#endif#ifdef UNIX_SYSV ioctl(0, TCSETA, &_tty);#endif}/* md_heed_signals(): * * This routine tells the program to call particular routines when * certain interrupts/events occur: * * SIGINT: call onintr() to interrupt fight with monster or long rest. * SIGQUIT: call byebye() to check for game termination. * SIGHUP: call error_save() to save game when terminal hangs up. * * On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y. * * This routine is not strictly necessary and can be stubbed. This will * mean that the game cannot be interrupted properly with keyboard * input, this is not usually critical. */md_heed_signals(){ signal(SIGINT, onintr); signal(SIGQUIT, byebye); signal(SIGHUP, error_save);}/* md_ignore_signals(): * * This routine tells the program to completely ignore the events mentioned * in md_heed_signals() above. The event handlers will later be turned on * by a future call to md_heed_signals(), so md_heed_signals() and * md_ignore_signals() need to work together. * * This function should be implemented or the user risks interrupting * critical sections of code, which could cause score file, or saved-game * file, corruption. */md_ignore_signals(){ signal(SIGQUIT, SIG_IGN); signal(SIGINT, SIG_IGN); signal(SIGHUP, SIG_IGN);}/* md_get_file_id(): * * This function returns an integer that uniquely identifies the specified * file. It need not check for the file's existence. In UNIX, the inode * number is used. * * This function is used to identify saved-game files. */intmd_get_file_id(fname)char *fname;{ struct stat sbuf; if (stat(fname, &sbuf)) { return(-1); } return((int) sbuf.st_ino);}/* md_link_count(): * * This routine returns the number of hard links to the specified file. * * This function is not strictly necessary. On systems without hard links * this routine can be stubbed by just returning 1. */intmd_link_count(fname)char *fname;{ struct stat sbuf; stat(fname, &sbuf); return((int) sbuf.st_nlink);}/* md_gct(): (Get Current Time) * * This function returns the current year, month(1-12), day(1-31), hour(0-23), * minute(0-59), and second(0-59). This is used for identifying the time * at which a game is saved. * * This function is not strictly necessary. It can be stubbed by returning * zeros instead of the correct year, month, etc. If your operating * system doesn't provide all of the time units requested here, then you * can provide only those that it does, and return zeros for the others. * If you cannot provide good time values, then users may be able to copy * saved-game files and play them. */md_gct(rt_buf)struct rogue_time *rt_buf;{ struct tm *t, *localtime(); long seconds; time(&seconds); t = localtime(&seconds); rt_buf->year = t->tm_year; rt_buf->month = t->tm_mon + 1; rt_buf->day = t->tm_mday; rt_buf->hour = t->tm_hour; rt_buf->minute = t->tm_min; rt_buf->second = t->tm_sec;}/* md_gfmt: (Get File Modification Time) * * This routine returns a file's date of last modification in the same format * as md_gct() above. * * This function is not strictly necessary. It is used to see if saved-game * files have been modified since they were saved. If you have stubbed the * routine md_gct() above by returning constant values, then you may do * exactly the same here. * Or if md_gct() is implemented correctly, but your system does not provide * file modification dates, you may return some date far in the past so * that the program will never know that a saved-game file being modified. * You may also do this if you wish to be able to restore games from * saved-games that have been modified. */md_gfmt(fname, rt_buf)char *fname;struct rogue_time *rt_buf;{ struct stat sbuf; long seconds; struct tm *t; stat(fname, &sbuf); seconds = (long) sbuf.st_mtime; t = localtime(&seconds); rt_buf->year = t->tm_year; rt_buf->month = t->tm_mon + 1; rt_buf->day = t->tm_mday; rt_buf->hour = t->tm_hour; rt_buf->minute = t->tm_min; rt_buf->second = t->tm_sec;}/* md_df: (Delete File) * * This function deletes the specified file, and returns true (1) if the * operation was successful. This is used to delete saved-game files
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -