1 /* NetHack 3.6 pcsys.c $NHDT-Date: 1524689500 2018/04/25 20:51:40 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.31 $ */
2 /* Copyright (c) 2012 by Michael Allison */
3 /* NetHack may be freely redistributed. See license for details. */
6 * System related functions for MSDOS, OS/2, TOS
15 #if !defined(MSDOS) && !defined(WIN_CE) /* already done */
25 #if defined(MSDOS) && !defined(__GO32__)
26 #define findfirst findfirst_file
27 #define findnext findnext_file
28 #define filesize filesize_nh
31 #if defined(MICRO) || defined(OS2)
32 void FDECL(nethack_exit, (int));
34 #define nethack_exit exit
36 STATIC_DCL void NDECL(msexit);
39 extern void __far __cdecl _movepause(void);
40 extern void __far __cdecl _moveresume(void);
41 extern unsigned short __far __cdecl _movefpause;
42 extern unsigned short __far __cdecl _movefpaused;
43 #define __MOVE_PAUSE_DISK 2 /* Represents the executable file */
44 #define __MOVE_PAUSE_CACHE 4 /* Represents the cache memory */
48 STATIC_DCL boolean NDECL(record_exists);
50 STATIC_DCL boolean NDECL(comspec_exists);
59 (void) fflush(stdout);
63 static const char *COMSPEC =
70 #define getcomspec() nh_getenv(COMSPEC)
81 #if defined(MSDOS) && defined(NO_TERMS)
82 int grmode = iflags.grmode;
84 if ((comspec = getcomspec())) {
85 #ifndef TOS /* TOS has a variety of shells */
87 "To return to NetHack, enter \"exit\" at the system prompt.\n");
89 #if defined(MSDOS) && defined(NO_TERMS)
90 grmode = iflags.grmode;
92 suspend_nhwindows((char *) 0);
94 #ifndef NOCWD_ASSUMPTIONS
98 if (system(comspec) < 0) { /* wsu@eecs.umich.edu */
101 /* Free the cache memory used by overlays, close .exe */
102 _movefpause |= __MOVE_PAUSE_DISK;
103 _movefpause |= __MOVE_PAUSE_CACHE;
106 spawnstat = spawnl(P_WAIT, comspec, comspec, (char *) 0);
113 raw_printf("Can't spawn \"%s\"!", comspec);
114 getreturn("to continue");
117 /* Some shells (e.g. Gulam) turn the cursor off when they exit */
119 (void) Cursconf(1, -1);
121 #ifndef NOCWD_ASSUMPTIONS
124 get_scr_size(); /* maybe the screen mode changed (TH) */
125 #if defined(MSDOS) && defined(NO_TERMS)
131 pline("Can't find %s.", COMSPEC);
139 eraseall(path, files)
140 const char *path, *files;
145 foundfile = foundfile_buffer();
146 Sprintf(buf, "%s%s", path, files);
149 Sprintf(buf, "%s%s", path, foundfile);
151 } while (findnext());
156 * Rewritten for version 3.3 to be faster
162 char from[PATHLEN], to[PATHLEN], last[13];
163 char *frompath, *topath;
167 char copy[8], *comspec;
173 /* Find the name of the last file to be transferred
175 frompath = (mode != TOPERM) ? permbones : levels;
176 foundfile = foundfile_buffer();
178 Sprintf(from, "%s%s", frompath, allbones);
179 topath = (mode == TOPERM) ? permbones : levels;
181 eraseall(topath, allbones);
186 Sprintf(from, "%s%s", frompath, foundfile);
187 Sprintf(to, "%s%s", topath, foundfile);
188 if (_copyfile(from, to))
191 Strcpy(last, foundfile);
192 } while (findnext());
198 Sprintf(copy, "%cC copy", switchar());
200 /* Remove any bones files in `to' directory.
202 eraseall(topath, allbones);
204 /* Copy `from' to `to' */
205 Sprintf(to, "%s%s", topath, allbones);
206 comspec = getcomspec();
207 status = spawnl(P_WAIT, comspec, comspec, copy, from, to, "> nul",
213 /* See if the last file got there. If so, remove the ramdisk bones
216 Sprintf(to, "%s%s", topath, last);
219 eraseall(frompath, allbones);
226 /* Last file didn't get there.
228 Sprintf(to, "%s%s", topath, allbones);
229 msmsg("Can't copy \"%s\" to \"%s\" -- ", from, to);
232 msmsg("can't spawn \"%s\"!", comspec);
235 msmsg((freediskspace(topath) < filesize(from))
236 ? "insufficient disk space."
238 if (mode == TOPERM) {
239 msmsg("Bones will be left in \"%s\"\n", *levels ? levels : hackdir);
241 /* Remove all bones files on the RAMdisk */
242 eraseall(levels, allbones);
253 msmsg("Do you wish to play without a RAMdisk? [yn] (n)");
255 /* Set ramdisk false *before* exit-ing (because msexit calls
263 settty("Be seeing you...\n");
264 nethack_exit(EXIT_SUCCESS);
266 set_lock_and_bones();
271 saveDiskPrompt(start)
274 char buf[BUFSIZ], *bp;
279 if (sysflags.asksavedisk) {
280 /* Don't prompt if you can find the save file */
281 if ((fd = open_savefile()) >= 0) {
285 clear_nhwindow(WIN_MESSAGE);
286 pline("If save file is on a save disk, insert that disk now.");
288 Sprintf(qbuf, "File name (default \"%s\"%s) ?", SAVEF,
289 start ? "" : ", <Esc> cancels save");
291 clear_nhwindow(WIN_MESSAGE);
292 if (!start && *buf == '\033')
295 /* Strip any whitespace. Also, if nothing was entered except
296 * whitespace, do not change the value of SAVEF.
298 for (bp = buf; *bp; bp++)
299 if (!isspace((uchar) *bp)) {
300 strncpy(SAVEF, bp, PATHLEN);
307 /* Return 1 if the record file was found */
313 fp = fopen_datafile(RECORD, "r", TRUE);
323 #define comspec_exists() 1
326 /* Return 1 if the comspec was found */
333 if ((comspec = getcomspec()))
334 if ((fd = open(comspec, O_RDONLY)) >= 0) {
344 /* Prompt for game disk, then check for record file.
349 if (sysflags.asksavedisk) {
350 if (record_exists() && comspec_exists())
352 (void) putchar('\n');
353 getreturn("when the game disk has been inserted");
355 if (comspec_exists() && record_exists())
358 if (!comspec_exists())
359 msmsg("\n\nWARNING: can't find command processor \"%s\"!\n",
361 if (!record_exists())
362 msmsg("\n\nWARNING: can't find record file \"%s\"!\n", RECORD);
363 msmsg("If the game disk is not in, insert it now.\n");
364 getreturn("to continue");
371 * Add a backslash to any name not ending in /, \ or : There must
382 ptr = name + (strlen(name) - 1);
383 if (*ptr != '\\' && *ptr != '/' && *ptr != ':') {
395 msmsg("Hit <Return> %s.", str);
397 msmsg("Hit <Enter> %s.", str);
399 while (pgetchar() != '\n')
405 VA_DECL(const char *, fmt)
408 VA_INIT(fmt, const char *);
409 #if defined(MSDOS) && defined(NO_TERMS)
413 Vprintf(fmt, VA_ARGS);
420 * Follow the PATH, trying to fopen the file.
434 const char *name, *mode;
436 char buf[BUFSIZ], *bp, *pp, lastch = 0;
439 /* Try the default directory first. Then look along PATH.
441 (void) strncpy(buf, name, BUFSIZ - 1);
442 buf[BUFSIZ - 1] = '\0';
443 if ((fp = fopen(buf, mode)))
450 while (*pp && *pp != PATHSEP) {
451 lastch = *bp++ = *pp++;
454 if (lastch != '\\' && lastch != '/') {
458 (void) strncpy(bp, name, (BUFSIZ - ccnt) - 2);
459 bp[BUFSIZ - ccnt - 1] = '\0';
460 if ((fp = fopen(buf, mode)))
466 #ifdef OS2_CODEVIEW /* one more try for hackdir */
467 (void) strncpy(buf, hackdir, BUFSZ);
468 buf[BUFSZ - 1] = '\0';
469 if ((strlen(name) + 1 + strlen(buf)) < BUFSZ - 1) {
473 impossible("fopenp() buffer too small for complete filename!");
474 if (fp = fopen(buf, mode))
480 #if defined(MICRO) || defined(OS2)
489 /* Chdir back to original directory
492 extern boolean run_from_desktop; /* set in pcmain.c */
499 extern char orgdir[];
504 enable_ctrlP(); /* in case this wasn't done */
510 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
511 chdir(orgdir); /* chdir, not chdirx */
515 if (run_from_desktop)
516 getreturn("to continue"); /* so the user can read the score list */
525 #endif /* MICRO || OS2 */