1 /*
2  * machdep.c
3  *
4  * This source herein may be modified and/or distributed by anybody who
5  * so desires, with the following restrictions:
6  *    1.)  No portion of this notice shall be removed.
7  *    2.)  Credit shall not be taken for the creation of this source.
8  *    3.)  This code is not to be traded, sold, or used for personal
9  *         gain or profit.
10  *
11  */
12 
13 /* Included in this file are all system dependent routines.  Extensive use
14  * of #ifdef's will be used to compile the appropriate code on each system:
15  *
16  *    UNIX:        all UNIX systems.
17  *    UNIX_BSD4_2: UNIX BSD 4.2 and later, UTEK, (4.1 BSD too?)
18  *    UNIX_SYSV:   UNIX system V
19  *    UNIX_V7:     UNIX version 7
20  *
21  * All UNIX code should be included between the single "#ifdef UNIX" at the
22  * top of this file, and the "#endif UNIX" at the bottom.
23  *
24  * To change a routine to include a new UNIX system, simply #ifdef the
25  * existing routine, as in the following example:
26  *
27  *   To make a routine compatible with UNIX system V, change the first
28  *   function to the second:
29  *
30  *      md_function()
31  *      {
32  *         code;
33  *      }
34  *
35  *      md_function()
36  *      {
37  *      #ifdef UNIX_SYSV
38  *         sysVcode;
39  *      #else
40  *         code;
41  *      #endif / * UNIX_SYSV * /
42  *      }
43  *
44  * Appropriate variations of this are of course acceptible.
45  * The use of "#elseif" is discouraged because of non-portability.
46  * If the correct #define doesn't exist, "UNIX_SYSV" in this case, make it up
47  * and insert it in the list at the top of the file.  Alter the CFLAGS
48  * in you Makefile appropriately.
49  *
50  */
51 
52 #include <stdio.h>
53 #ifdef UNIX
54 #  include <sys/types.h>
55 #  include <sys/file.h>
56 #  include <sys/stat.h>
57 #  include <sys/ioctl.h>
58 #  ifdef UNIX_SYSV
59 #    include <time.h>
60 #    include <termios.h>
61 #  endif /* UNIX_SYSV */
62 #  ifdef UNIX_BSD4_2
63 #    include <sys/time.h>
64 #    include <sgtty.h>
65 #  endif /* UNIX_BSD4_2 */
66 #endif /* UNIX */
67 #ifdef HUMAN
68 #include <doslib.h>	/* by Yasha */
69 #include <conio.h>	/* by Yasha */
70 #include <time.h>	/* by Yasha */
71 #include <direct.h>	/* by Yasha */
72 #include <signal.h>	/* by Yasha */
73 #include <stat.h>	/* by Yasha */
74 #else
75 #ifdef MSDOS
76 #  ifdef LC4
77 #    include <fcntl.h>
78 #  else
79 #    if !defined(__TURBOC__) || __TURBOC__ >= 0x0200
80 #      include <sys\types.h>
81 #    endif /* __TURBOC__ */
82 #    include <sys\stat.h>
83 #  endif /* LC4 */
84 #  include <time.h>
85 #  include <dos.h>
86 #endif /* MSDOS */
87 #endif /* HUMAN */
88 #include <signal.h>
89 #include "rogue.h"
90 
91 #ifdef HUMAN
getchar()92 getchar()		/* by Yasha */
93 {			/* by Yasha */
94 	return INKEY();	/* by Yasha */
95 }			/* by Yasha */
96 #endif
97 #if defined(MSDOS) && !defined(HUMAN)	/* by Yasha */
98 /*#ifdef MSDOS*/
getchar()99 getchar()
100 {
101 	register int c;
102 
103 	/*
104 	 * hide cursor, and do console input
105 	 */
106 	putstr(cursor_on);
107 #ifdef __TURBOC__
108 	_AH = 0x07;
109 	geninterrupt(0x21);
110 	c = _AL;
111 #else
112 	c = bdos(7, 0, 0) & 0xff;
113 #endif
114 	putstr(cursor_off);
115 	return (c);
116 }
117 
putchar(c)118 putchar(c)
119 	int c;
120 {
121 #ifdef __TURBOC__
122 	_AH = 0x02;
123 	_AL = c;
124 	geninterrupt(0x29);
125 #else /* MS-C */
126 	union REGS regs;
127 	regs.h.ah = 0x02;
128 	regs.h.al = c;
129 	int86(0x29, &regs, &regs);
130 #endif
131 }
132 
putstr(s)133 putstr(s)
134 	register char *s;
135 {
136 #ifdef __TURBOC__
137 	while (*s) {
138 		_AH = 0x02;
139 		_AL = *s++;
140 		geninterrupt(0x29);
141 	}
142 #else /* MS-C */
143 	union REGS regs;
144 	while (*s) {
145 		regs.h.ah = 0x02;
146 		regs.h.al = *s++;
147 		int86(0x29, &regs, &regs);
148 	}
149 #endif
150 }
151 
152 #else
putstr(s)153 putstr(s)
154 	register char *s;
155 {
156 	while (*s)
157 		putchar(*s++);
158 }
159 #endif
160 
161 #ifndef ORIGINAL
162 /*
163  * md_getcwd:
164  *
165  * get full pathname of current directory and place it into dir.
166  * maximum length of pathname is given as len.
167  * return dir on success, or NULL on failure.
168  */
169 
170 char *
md_getcwd(dir,len)171 md_getcwd(dir, len)
172 char *dir;
173 int len;
174 {
175 #ifdef UNIX
176 #ifdef UNIX_BSD4_2
177 	char *getwd();
178 
179 	return (getwd(dir));
180 #else
181 	char *getcwd();
182 
183 	return (getcwd(dir, len));
184 #endif
185 #else
186 #ifdef LC4
187 	return ((char *)getcwd(dir, len));
188 #else
189 	char *getcwd();
190 
191 	return (getcwd(dir, len));
192 #endif
193 #endif
194 }
195 #endif
196 
197 #ifndef ORIGINAL
198 /*
199  * md_chdir:
200  *
201  * change directory to dir.  also change drive in MSDOS environment.
202  * return 0 on success, or -1 on failure.
203  */
204 
md_chdir(dir)205 md_chdir(dir)
206 char *dir;
207 {
208 #if defined(UNIX) || defined(HUMAN)
209 /*#ifdef UNIX*/
210 	return (chdir(dir));
211 #endif /* UNIX */
212 
213 #if defined(MSDOS) && !defined(HUMAN)
214 /*#ifdef MSDOS*/
215 	chdrive(dir);
216 	return (chdir(dir));
217 #endif /* MSDOS */
218 }
219 #endif /* ORIGINAL */
220 
221 #if defined(MSDOS) && !defined(HUMAN)
222 /*#ifdef MSDOS*/
223 /*
224  * chdrive:
225  *
226  * change the default drive to drive.
227  */
228 #define SELECTDISK	0x0E
chdrive(dir)229 chdrive(dir)
230 char *dir;
231 {
232 	char *p, *strchr();
233 	char drive;
234 #ifndef __TURBOC__
235 	union REGS regs;
236 #endif
237 
238 	p = strchr(dir, ':');
239 	if (p == NULL)
240 		return;
241 	p--;
242 	drive = (*p >= 'A' && *p <= 'Z')? *p - 'A': *p - 'a';
243 #ifdef __TURBOC__
244 	_AH = SELECTDISK;
245 	_DL = drive;
246 	geninterrupt(0x21);
247 #else
248 	regs.h.ah = SELECTDISK;
249 	regs.h.dl = drive;
250 	intdos(&regs, &regs);
251 #endif
252 }
253 #endif /* MSDOS */
254 
255 /* md_slurp:
256  *
257  * This routine throws away all keyboard input that has not
258  * yet been read.  It is used to get rid of input that the user may have
259  * typed-ahead.
260  *
261  * This function is not necessary, so it may be stubbed.  The might cause
262  * message-line output to flash by because the game has continued to read
263  * input without waiting for the user to read the message.  Not such a
264  * big deal.
265  */
266 
md_slurp()267 md_slurp()
268 {
269 #ifdef UNIX
270 	long ln = 0;
271 
272 	ioctl(0, FIONREAD, &ln);
273 #ifdef UNIX_386BSD
274 	fpurge(stdin);
275 #else
276 	ln += stdin->_cnt;
277 	for (; ln > 0; ln--) {
278 		(void) getchar();
279 	}
280 #endif /*386BSD*/
281 #endif /* UNIX */
282 
283 #ifdef HUMAN
284 	while (kbhit())		/* by Yasha */
285 		INKEY();	/* by Yasha */
286 #else
287 #ifdef MSDOS
288 #ifdef __TURBOC__
289 	while (_AH = 0x0b, geninterrupt(0x21), _AL) {
290 		_AH = 0x07;
291 		geninterrupt(0x21);
292 	}
293 #else
294 	while (kbhit())
295 		bdos(7, 0, 0);
296 #endif
297 #endif
298 #endif	/* HUMAN */
299 }
300 
301 #ifndef MSDOS
302 /* md_control_keyboard():
303  *
304  * This routine is much like md_cbreak_no_echo_nonl() below.  It sets up the
305  * keyboard for appropriate input.  Specifically, it prevents the tty driver
306  * from stealing characters.  For example, ^Y is needed as a command
307  * character, but the tty driver intercepts it for another purpose.  Any
308  * such behavior should be stopped.  This routine could be avoided if
309  * we used RAW mode instead of CBREAK.  But RAW mode does not allow the
310  * generation of keyboard signals, which the program uses.
311  *
312  * The parameter 'mode' when true, indicates that the keyboard should
313  * be set up to play rogue.  When false, it should be restored if
314  * necessary.
315  *
316  * This routine is not strictly necessary and may be stubbed.  This may
317  * cause certain command characters to be unavailable.
318  */
319 
md_control_keyboard(mode)320 md_control_keyboard(mode)
321 boolean mode;
322 {
323 #ifdef UNIX
324 	static boolean called_before = 0;
325 #ifdef UNIX_BSD4_2
326 	static struct ltchars ltc_orig;
327 	static struct tchars tc_orig;
328 	struct ltchars ltc_temp;
329 	struct tchars tc_temp;
330 #endif /* UNIX_BSD4_2 */
331 #ifdef UNIX_SYSV
332 	static struct termios _oldtty;
333 	struct termios _tty;
334 #endif /* UNIX_SYSV */
335 
336 	if (!called_before) {
337 		called_before = 1;
338 #ifdef UNIX_BSD4_2
339 		ioctl(0, TIOCGETC, &tc_orig);
340 		ioctl(0, TIOCGLTC, &ltc_orig);
341 #endif /* UNIX_BSD4_2 */
342 #ifdef UNIX_SYSV
343 		tcgetattr(0, &_oldtty);
344 #endif /* UNIX_SYSV */
345 	}
346 #ifdef UNIX_BSD4_2
347 	ltc_temp = ltc_orig;
348 	tc_temp = tc_orig;
349 #endif /* UNIX_BSD4_2 */
350 #ifdef UNIX_SYSV
351 	_tty = _oldtty;
352 #endif /* UNIX_SYSV */
353 
354 	if (!mode) {
355 #ifdef UNIX_BSD4_2
356 		ltc_temp.t_suspc = ltc_temp.t_dsuspc = -1;
357 		ltc_temp.t_rprntc = ltc_temp.t_flushc = -1;
358 		ltc_temp.t_werasc = ltc_temp.t_lnextc = -1;
359 		tc_temp.t_startc = tc_temp.t_stopc = -1;
360 #endif /* UNIX_BSD4_2 */
361 #ifdef UNIX_SYSV
362 		_tty.c_cc[VSUSP] = _tty.c_cc[VDSUSP] = 0;
363 #endif /* UNIX_SYSV */
364 	}
365 #ifdef UNIX_BSD4_2
366 	ioctl(0, TIOCSETC, &tc_temp);
367 	ioctl(0, TIOCSLTC, &ltc_temp);
368 #endif /* UNIX_BSD4_2 */
369 #ifdef UNIX_SYSV
370 	tcsetattr(0, TCSANOW, &_tty);
371 #endif /* UNIX_SYSV */
372 #endif /* UNIX */
373 }
374 #endif /*MSDOS*/
375 
376 /* md_heed_signals():
377  *
378  * This routine tells the program to call particular routines when
379  * certain interrupts/events occur:
380  *
381  *      SIGINT: call onintr() to interrupt fight with monster or long rest.
382  *      SIGQUIT: call byebye() to check for game termination.
383  *      SIGHUP: call error_save() to save game when terminal hangs up.
384  *
385  *		On VMS, SIGINT and SIGQUIT correspond to ^C and ^Y.
386  *
387  * This routine is not strictly necessary and can be stubbed.  This will
388  * mean that the game cannot be interrupted properly with keyboard
389  * input, this is not usually critical.
390  */
391 
md_heed_signals()392 md_heed_signals()
393 {
394 #ifdef UNIX
395 	signal(SIGINT, (void *)onintr);
396 	signal(SIGQUIT, (void *)byebye);
397 	signal(SIGHUP, (void *)error_save);
398 #endif /* UNIX */
399 
400 #ifdef HUMAN
401 	signal(SIGINT, onintr);
402 #else
403 #ifdef MSDOS
404 #if defined(__TURBOC__) && __TURBOC__ < 0x0200
405 	ctrlbrk(onintr);
406 #else
407 	signal(SIGINT, onintr);
408 #endif /* __TURBOC__ */
409 #endif
410 #endif	/* HUMAN */
411 }
412 
413 /* md_ignore_signals():
414  *
415  * This routine tells the program to completely ignore the events mentioned
416  * in md_heed_signals() above.  The event handlers will later be turned on
417  * by a future call to md_heed_signals(), so md_heed_signals() and
418  * md_ignore_signals() need to work together.
419  *
420  * This function should be implemented or the user risks interrupting
421  * critical sections of code, which could cause score file, or saved-game
422  * file, corruption.
423  */
424 
md_ignore_signals()425 md_ignore_signals()
426 {
427 #ifdef UNIX
428 	signal(SIGQUIT, SIG_IGN);
429 	signal(SIGINT, SIG_IGN);
430 	signal(SIGHUP, SIG_IGN);
431 #endif /* UNIX */
432 
433 #ifdef HUMAN
434 	signal(SIGINT, SIG_IGN);	/* by Yasha */
435 #else
436 #ifdef MSDOS
437 #if defined(__TURBOC__) && __TURBOC__ < 0x0200
438 	ctrlbrk(ignintr);
439 #else
440 	signal(SIGINT, SIG_IGN);
441 #endif /* __TURBOC__ */
442 #endif
443 #endif	/* HUMAN */
444 }
445 
446 /* md_get_file_id():
447  *
448  * This function returns an integer that uniquely identifies the specified
449  * file.  It need not check for the file's existence.  In UNIX, the inode
450  * number is used.
451  *
452  * This function need not be implemented.  To stub the routine, just make
453  * it return 0.  This will make the game less able to prevent users from
454  * modifying saved-game files.  This is probably no big deal.
455  */
456 
457 int
md_get_file_id(fname)458 md_get_file_id(fname)
459 char *fname;
460 {
461 #ifdef UNIX
462 	struct stat sbuf;
463 
464 	if (stat(fname, &sbuf)) {
465 		return(-1);
466 	}
467 	return((int) sbuf.st_ino);
468 #endif /* UNIX */
469 
470 #ifdef MSDOS
471 	return (0);
472 #endif /* MSDOS */
473 }
474 
475 /* md_link_count():
476  *
477  * This routine returns the number of hard links to the specified file.
478  *
479  * This function is not strictly necessary.  On systems without hard links
480  * this routine can be stubbed by just returning 1.
481  */
482 
483 #ifndef MSDOS
484 int
md_link_count(fname)485 md_link_count(fname)
486 char *fname;
487 {
488 #ifdef UNIX
489 	struct stat sbuf;
490 
491 	stat(fname, &sbuf);
492 	return((int) sbuf.st_nlink);
493 #endif /* UNIX */
494 }
495 #endif /* MSDOS */
496 
497 /* md_gct(): (Get Current Time)
498  *
499  * This function returns the current year, month(1-12), day(1-31), hour(0-23),
500  * minute(0-59), and second(0-59).  This is used for identifying the time
501  * at which a game is saved.
502  *
503  * This function is not strictly necessary.  It can be stubbed by returing
504  * zeros instead of the correct year, month, etc.  If your operating
505  * system doesn't provide all of the time units requested here, then you
506  * can provide only those that it does, and return zeros for the others.
507  * If you cannot provide good time values, then users may be able to copy
508  * saved-game files and play them.
509  */
510 
511 md_gct(rt_buf)
512 struct rogue_time *rt_buf;
513 {
514 	struct tm *t, *localtime();
515 	long seconds;
516 
517 	time(&seconds);
518 	t = localtime(&seconds);
519 
520 	rt_buf->year = t->tm_year;
521 	rt_buf->month = t->tm_mon + 1;
522 	rt_buf->day = t->tm_mday;
523 	rt_buf->hour = t->tm_hour;
524 	rt_buf->minute = t->tm_min;
525 	rt_buf->second = t->tm_sec;
526 }
527 
528 /* md_gfmt: (Get File Modification Time)
529  *
530  * This routine returns a file's date of last modification in the same format
531  * as md_gct() above.
532  *
533  * This function is not strictly necessary.  It is used to see if saved-game
534  * files have been modified since they were saved.  If you have stubbed the
535  * routine md_gct() above by returning constant values, then you may do
536  * exactly the same here.
537  * Or if md_gct() is implemented correctly, but your system does not provide
538  * file modification dates, you may return some date far in the past so
539  * that the program will never know that a saved-game file being modified.
540  * You may also do this if you wish to be able to restore games from
541  * saved-games that have been modified.
542  */
543 
md_gfmt(fname,rt_buf)544 md_gfmt(fname, rt_buf)
545 char *fname;
546 struct rogue_time *rt_buf;
547 {
548 #ifdef LC4
549 	int fd;
550 	long ft;
551 	char s[6];
552 
553 	fd = open(fname, O_RDONLY|O_RAW);
554 	ft = getft(fd);
555 	close(fd);
556 	ftunpk(ft, s);
557 	rt_buf->year = s[0] + 80;
558 	rt_buf->month = s[1];
559 	rt_buf->day = s[2];
560 	rt_buf->hour = s[3];
561 	rt_buf->minute = s[4];
562 	rt_buf->second = s[5];
563 #else
564 	struct stat sbuf;
565 	long seconds;
566 	struct tm *t;
567 
568 	stat(fname, &sbuf);
569 	seconds = (long) sbuf.st_mtime;
570 	t = localtime(&seconds);
571 
572 #if defined(__TURBOC__) && __TURBOC__ < 0x0200
573 	/*
574 	 * Time routines of Turbo C 1.5J (both from MSA, SPL)
575 	 * has not been modified for Japanese use.
576 	 * So we must check the daylight saving time flag,
577 	 * and then re-correct the time.
578 	 */
579 	if (t->tm_isdst) {
580 		seconds -= 3600;
581 		t = localtime(&seconds);
582 	}
583 #endif
584 
585 	rt_buf->year = t->tm_year;
586 	rt_buf->month = t->tm_mon + 1;
587 	rt_buf->day = t->tm_mday;
588 	rt_buf->hour = t->tm_hour;
589 	rt_buf->minute = t->tm_min;
590 	rt_buf->second = t->tm_sec;
591 #endif /* !LC4 */
592 }
593 
594 /* md_df: (Delete File)
595  *
596  * This function deletes the specified file, and returns true (1) if the
597  * operation was successful.  This is used to delete saved-game files
598  * after restoring games from them.
599  *
600  * Again, this function is not strictly necessary, and can be stubbed
601  * by simply returning 1.  In this case, saved-game files will not be
602  * deleted and can be replayed.
603  */
604 
605 boolean
md_df(fname)606 md_df(fname)
607 char *fname;
608 {
609 	if (unlink(fname)) {
610 		return(0);
611 	}
612 	return(1);
613 }
614 
615 /* md_gln: (Get login name)
616  *
617  * This routine returns the login name of the user.  This string is
618  * used mainly for identifying users in score files.
619  *
620  * A dummy string may be returned if you are unable to implement this
621  * function, but then the score file would only have one name in it.
622  */
623 
624 char *
md_gln()625 md_gln()
626 {
627 #ifdef UNIX
628 	char *getlogin();
629 	char *t;
630 	char *md_getenv();	/* by Yasha */
631 
632 	if ((t = md_getenv("FIGHTER")) == NULL)	/* by Yasha */
633 		if ((t = getlogin()) == NULL)	/* by Yasha */
634 			t = md_getenv("USER");	/* by Yasha */
635 /*	t = getlogin();*/	/* killed by Yasha */
636 	return(t);
637 #endif /* UNIX */
638 
639 #ifdef MSDOS
640 	char *t;
641 
642 	if ((t = md_getenv("FIGHTER")) == NULL)		/* by Yasha */
643 		if ((t = md_getenv("USER")) == NULL)	/* by Yasha */
644 /*	if ((t = md_getenv("USER")) == NULL)*/
645 #ifdef JAPAN
646 		t = "���";
647 #else
648 		t = "Fighter";
649 #endif
650 	return (t);
651 #endif /* MSDOS */
652 }
653 
654 /* md_sleep:
655  *
656  * This routine causes the game to pause for the specified number of
657  * seconds.
658  *
659  * This routine is not necessary at all, and can be stubbed with no ill
660  * effects.
661  */
662 
md_sleep(nsecs)663 md_sleep(nsecs)
664 int nsecs;
665 {
666 	(void) sleep(nsecs);
667 }
668 #ifdef HUMAN	/* by Yasha (till "#else") */
sleep(nsecs)669 sleep(nsecs)
670 int nsecs;
671 {
672 	time_t t;
673 	if (nsecs < 1)
674 		nsecs = 1;
675 	t = time((time_t *) NULL) + (time_t) nsecs;
676 	while (time((time_t *) NULL) < t)
677 		;
678 }
679 #else
680 #ifdef MSDOS
681 #ifndef __TURBOC__
sleep(nsecs)682 sleep(nsecs)
683 int nsecs;
684 {
685 	long t, time();
686 
687 	if (nsecs < 1)
688 		nsecs = 1;
689 	t = time(0L) + (long)nsecs;
690 	while (time(0L) < t)
691 		;
692 }
693 #endif /* __TURBOC__ */
694 #endif /* MSDOS */
695 #endif /* HUMAN */
696 
697 /* md_getenv()
698  *
699  * This routine gets certain values from the user's environment.  These
700  * values are strings, and each string is identified by a name.  The names
701  * of the values needed, and their use, is as follows:
702  *
703  *   TERMCAP
704  *     The name of the users's termcap file, NOT the termcap entries
705  *     themselves.  This is used ONLY if the program is compiled with
706  *     CURSES defined (-DCURSES).  Even in this case, the program need
707  *     not find a string for TERMCAP.  If it does not, it will use the
708  *     default termcap file as returned by md_gdtcf();
709  *   TERM
710  *     The name of the users's terminal.  This is used ONLY if the program
711  *     is compiled with CURSES defined (-DCURSES).  In this case, the string
712  *     value for TERM must be found, or the routines in curses.c cannot
713  *     function, and the program will quit.
714  *   ROGUEOPTS
715  *     A string containing the various game options.  This need not be
716  *     defined.
717  *   HOME
718  *     The user's home directory.  This is only used when the user specifies
719  *     '~' as the first character of a saved-game file.  This string need
720  *     not be defined.
721  *
722  * If your system does not provide a means of searching for these values,
723  * you will have to do it yourself.  None of the values above really need
724  * to be defined except TERM when the program is compiled with CURSES
725  * defined.  In this case, as a bare minimum, you can check the 'name'
726  * parameter, and if it is "TERM" find the terminal name and return that,
727  * else return zero.  If the program is not compiled with CURSES, you can
728  * get by with simply always returning zero.  Returning zero indicates
729  * that their is no defined value for the given string.
730  */
731 
732 char *
md_getenv(name)733 md_getenv(name)
734 char *name;
735 {
736 	char *getenv();
737 
738 	return getenv(name);
739 }
740 
741 /* md_malloc()
742  *
743  * This routine allocates, and returns a pointer to, the specified number
744  * of bytes.  This routines absolutely MUST be implemented for your
745  * particular system or the program will not run at all.  Return zero
746  * when no more memory can be allocated.
747  */
748 
749 char *
md_malloc(n)750 md_malloc(n)
751 int n;
752 {
753 	char *malloc();
754 
755 	return malloc(n);
756 }
757 
758 /* md_gseed() (Get Seed)
759  *
760  * This function returns a seed for the random number generator (RNG).  This
761  * seed causes the RNG to begin generating numbers at some point in it's
762  * sequence.  Without a random seed, the RNG will generate the same set
763  * of numbers, and every game will start out exactly the same way.  A good
764  * number to use is the process id, given by getpid() on most UNIX systems.
765  *
766  * You need to find some single random integer, such as:
767  *   process id.
768  *   current time (minutes + seconds) returned from md_gct(), if implemented.
769  *
770  * It will not help to return "get_rand()" or "rand()" or the return value of
771  * any pseudo-RNG.  If you don't have a random number, you can just return 1,
772  * but this means your games will ALWAYS start the same way, and will play
773  * exactly the same way given the same input.
774  */
775 
md_gseed()776 md_gseed()
777 {
778 #ifdef UNIX
779 	return(getpid());
780 #endif /* UNIX */
781 
782 #ifdef MSDOS
783 	long time();
784 
785 	return ((int) time(0L));
786 #endif /* MSDOS */
787 }
788 
789 /* md_exit():
790  *
791  * This function causes the program to discontinue execution and exit.
792  * This function must be implemented or the program will continue to
793  * hang when it should quit.
794  */
795 
md_exit(status)796 md_exit(status)
797 int status;
798 {
799 #ifndef ORIGINAL
800 	if (org_dir && *org_dir)
801 		md_chdir(org_dir);
802 #endif
803 	exit(status);
804 }
805 
806 /* If you have a viable curses/termlib library, then use it and don't bother
807  * implementing the routines below.  And don't compile with -DCURSES.
808  */
809 
810 #ifdef CURSES
811 #if defined(MSDOS) && !defined(HUMAN)	/* by Yasha */
812 /*#ifdef MSDOS*/
813 
814 /*
815  * << ioctl routine come from PC NETHACK >>
816  *
817  * Use the IOCTL DOS function call to change stdin and stdout to raw
818  * mode.  For stdin, this prevents MSDOS from trapping ^P, thus
819  * freeing us of ^P toggling 'echo to printer'.
820  * Thanks to Mark Zbikowski (markz@microsoft.UUCP).
821  */
822 
823 #define DEVICE	0x80
824 #define RAW	0x20
825 #define IOCTL	0x44
826 #define GETBITS	0
827 #define SETBITS	1
828 
829 static unsigned
ioctl(handle,mode,setvalue)830 ioctl(handle, mode, setvalue)
831 unsigned setvalue;
832 {
833 #ifdef __TURBOC__
834 	_AH = IOCTL;
835 	_AL = mode;
836 	_BX = handle;
837 	_DL = setvalue;
838 	_DH = 0;
839 	geninterrupt(0x21);
840 	return _DX;
841 #else
842 	union REGS regs;
843 
844 	regs.h.ah = IOCTL;
845 	regs.h.al = mode;
846 	regs.x.bx = handle;
847 	regs.h.dl = setvalue;
848 	regs.h.dh = 0;			/* Zero out dh */
849 	intdos(&regs, &regs);
850 	return ((unsigned)regs.x.dx);
851 #endif
852 }
853 #endif /* MSDOS && !HUMAN */
854 
855 /* md_cbreak_no_echo_nonl:
856  *
857  * This routine sets up some terminal characteristics.  The tty-driver
858  * must be told to:
859  *   1.)  Not echo input.
860  *   2.)  Transmit input characters immediately upon typing. (cbreak mode)
861  *   3.)  Move the cursor down one line, without changing column, and
862  *        without generating a carriage-return, when it
863  *        sees a line-feed.  This is only necessary if line-feed is ever
864  *        used in the termcap 'do' (cursor down) entry, in which case,
865  *        your system should must have a way of accomplishing this.
866  *
867  * When the parameter 'on' is true, the terminal is set up as specified
868  * above.  When this parameter is false, the terminal is restored to the
869  * original state.
870  *
871  * Raw mode should not to be used.  Keyboard signals/events/interrupts should
872  * be sent, although they are not strictly necessary.  See notes in
873  * md_heed_signals().
874  *
875  * This function must be implemented for rogue to run properly if the
876  * program is compiled with CURSES defined to use the enclosed curses
877  * emulation package.  If you are not using this, then this routine is
878  * totally unnecessary.
879  *
880  * Notice that information is saved between calls.  This is used to
881  * restore the terminal to an initial saved state.
882  *
883  */
884 
md_cbreak_no_echo_nonl(on)885 md_cbreak_no_echo_nonl(on)
886 boolean on;
887 {
888 #ifdef UNIX
889 #ifdef UNIX_BSD4_2
890 	static struct sgttyb tty_buf;
891 	static int tsave_flags;
892 
893 	if (on) {
894 		ioctl(0, TIOCGETP, &tty_buf);
895 		tsave_flags = tty_buf.sg_flags;
896 		tty_buf.sg_flags |= CBREAK;
897 		tty_buf.sg_flags &= ~(ECHO | CRMOD);	/* CRMOD: see note 3 above */
898 		ioctl(0, TIOCSETP, &tty_buf);
899 	} else {
900 		tty_buf.sg_flags = tsave_flags;
901 		ioctl(0, TIOCSETP, &tty_buf);
902 	}
903 #endif /* UNIX_BSD4_2 */
904 #ifdef UNIX_SYSV
905 	struct termios tty_buf;
906 	static struct termios tty_save;
907 
908 	if (on) {
909 		tcgetattr(0, &tty_buf);
910 		tty_save = tty_buf;
911 		tty_buf.c_lflag &= ~(ICANON | ECHO);
912 		tty_buf.c_oflag &= ~ONLCR;
913 		tty_buf.c_cc[VMIN] = 1;
914 		tty_buf.c_cc[VTIME] = 2;
915 		tcsetattr(0, TCSAFLUSH, &tty_buf);
916 	} else {
917 		tcsetattr(0, TCSAFLUSH, &tty_save);
918 	}
919 #endif /* UNIX_SYSV */
920 #endif /* UNIX */
921 
922 #if defined(MSDOS) && !defined(HUMAN)
923 /*#ifdef MSDOS*/
924 	static unsigned old0, old1;
925 
926 	if (on) {
927 		old0 = ioctl(0, GETBITS, 0);
928 		if (old0 & DEVICE)
929 			ioctl(0, SETBITS, (old0 | RAW));
930 		old1 = ioctl(1, GETBITS, 0);
931 		if (old1 & DEVICE)
932 			ioctl(1, SETBITS, (old1 | RAW));
933 	} else {
934 		ioctl(0, SETBITS, (old0 & ~RAW));
935 		ioctl(1, SETBITS, (old1 & ~RAW));
936 	}
937 #endif /*MSDOS*/
938 }
939 
940 /* md_gdtcf(): (Get Default Termcap File)
941  *
942  * This function is called ONLY when the program is compiled with CURSES
943  * defined.  If you use your system's curses/termlib library, this function
944  * won't be called.  On most UNIX systems, "/etc/termcap" suffices.
945  *
946  * If their is no such termcap file, then return 0, but in that case, you
947  * must have a TERMCAP file returned from md_getenv("TERMCAP").  The latter
948  * will override the value returned from md_gdtcf().  If the program is
949  * compiled with CURSES defined, and md_gdtcf() returns 0, and
950  * md_getenv("TERMCAP") returns 0, the program will have no terminal
951  * capability information and will quit.
952  */
953 
954 #ifndef MSDOS
955 char *
md_gdtcf()956 md_gdtcf()
957 {
958 #ifdef UNIX
959 	return("/etc/termcap");
960 #endif /* UNIX */
961 }
962 #endif /* MSDOS */
963 
964 /* md_tstp():
965  *
966  * This function puts the game to sleep and returns to the shell.  This
967  * only applies to UNIX 4.2 and 4.3.  For other systems, the routine should
968  * be provided as a do-nothing routine.  md_tstp() will only be referenced
969  * in the code when compiled with CURSES defined.
970  *
971  */
972 
973 #ifndef MSDOS
md_tstp()974 md_tstp()
975 {
976 #ifdef UNIX
977 #ifdef UNIX_BSD4_2
978 	kill(0, SIGTSTP);
979 #endif /* UNIX_BSD4_2 */
980 #endif /* UNIX */
981 }
982 #endif /* MSDOS */
983 
984 #endif /* CURSES */
985