1 /* NetHack 3.6 pcunix.c $NHDT-Date: 1432512787 2015/05/25 00:13:07 $ $NHDT-Branch: master $:$NHDT-Revision: 1.34 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* This file collects some Unix dependencies; pager.c contains some more */
11 #if defined(WIN32) || defined(MSDOS)
15 #if defined(WIN32) || defined(MSDOS)
18 extern void NDECL(backsp
);
20 extern void NDECL(clear_screen
);
24 static struct stat buf
;
28 static struct stat hbuf
;
32 static int NDECL(eraseoldlocks
);
42 pline("Cannot get status of saved level? ");
45 if(buf
.st_mtime
< hbuf
.st_mtime
) {
46 pline("Saved level is out of date. ");
50 #if (defined(MICRO) || defined(WIN32)) && !defined(NO_FSTAT)
52 if(moves
> 1) pline("Cannot get status of saved level? ");
53 else pline("Cannot get status of saved game.");
56 if(comp_times(buf
.st_mtime
)) {
57 if(moves
> 1) pline("Saved level is out of date.");
58 else pline("Saved game is out of date. ");
59 /* This problem occurs enough times we need to give the player
60 * some more information about what causes it, and how to fix.
63 pline("Make sure that your system's date and time are correct.");
64 pline("They must be more current than NetHack.EXE's date/time stamp.");
69 #endif /* WANT_GETHDATE */
80 /* cannot use maxledgerno() here, because we need to find a lock name
81 * before starting everything (including the dungeon initialization
82 * that sets astral_level, needed for maxledgerno()) up
84 for (i
= 1; i
<= MAXDUNGEON
* MAXLEVEL
+ 1; i
++) {
85 /* try to remove all */
86 set_levelfile_name(lock
, i
);
87 (void) unlink(fqname(lock
, LEVELPREFIX
, 0));
89 set_levelfile_name(lock
, 0);
90 #ifdef HOLD_LOCKFILE_OPEN
93 if (unlink(fqname(lock
, LEVELPREFIX
, 0)))
94 return 0; /* cannot remove it */
95 return (1); /* success! */
101 register int fd
, c
, ci
, ct
, ern
;
105 #if defined(MSDOS) && defined(NO_TERMS)
106 int grmode
= iflags
.grmode
;
108 /* we ignore QUIT and INT at this point */
109 if (!lock_file(HLOCK
, LOCKPREFIX
, 10)) {
111 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
117 /* regularize(lock); */ /* already done in pcmain */
118 Sprintf(tbuf
, "%s", fqname(lock
, LEVELPREFIX
, 0));
119 set_levelfile_name(lock
, 0);
120 fq_lock
= fqname(lock
, LEVELPREFIX
, 1);
121 if ((fd
= open(fq_lock
, 0)) == -1) {
123 goto gotlock
; /* no such file */
124 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
127 #if defined(WIN32) || defined(HOLD_LOCKFILE_OPEN)
128 #if defined(HOLD_LOCKFILE_OPEN)
129 if (errno
== EACCES
) {
130 #define OOPS_BUFSZ 512
131 char oops
[OOPS_BUFSZ
];
134 "\nThere are files from a game in progress under your name.");
135 Strcat(oops
, "\nThe files are locked or inaccessible.");
136 Strcat(oops
, " Is the other game still running?\n");
137 if (strlen(fq_lock
) < ((OOPS_BUFSZ
- 16) - strlen(oops
)))
138 Sprintf(eos(oops
), "Cannot open %s", fq_lock
);
144 error("Bad directory or name: %s\n%s\n", fq_lock
,
150 error("Cannot open %s", fq_lock
);
155 if (iflags
.window_inited
) {
157 c
= yn("There are files from a game in progress under your name. "
160 pline("There is already a game in progress under your name.");
161 pline("You may be able to use \"recover %s\" to get it back.\n",
163 c
= yn("Do you want to destroy the old game?");
166 #if defined(MSDOS) && defined(NO_TERMS)
167 grmode
= iflags
.grmode
;
174 msmsg("There are files from a game in progress under your name. "
177 msmsg("\nThere is already a game in progress under your name.\n");
178 msmsg("If this is unexpected, you may be able to use \n");
179 msmsg("\"recover %s\" to get it back.", tbuf
);
180 msmsg("\nDo you want to destroy the old game? [yn] ");
182 while ((ci
= nhgetch()) != '\n') {
188 if (ci
== 'y' || ci
== 'n' || ci
== 'Y' || ci
== 'N') {
195 if (c
== 'y' || c
== 'Y')
197 if (eraseoldlocks()) {
199 if (!strncmpi(windowprocs
.name
, "tty", 3))
200 clear_screen(); /* display gets fouled up otherwise */
205 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
208 error("Couldn't destroy old game.");
210 #else /*SELF_RECOVER*/
211 if (recover_savefile()) {
213 if (!strncmpi(windowprocs
.name
, "tty", 3))
214 clear_screen(); /* display gets fouled up otherwise */
219 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
222 error("Couldn't recover old game.");
224 #endif /*SELF_RECOVER*/
227 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
230 error("%s", "Cannot start a new game.");
234 fd
= creat(fq_lock
, fcmask
);
239 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
243 error("cannot creat file (%s.)\n%s\n%s\"%s\" exists?\n", fq_lock
,
244 strerror(ern
), " Are you sure that the directory",
245 fqn_prefix
[LEVELPREFIX
]);
247 error("cannot creat file (%s.)", fq_lock
);
250 if (write(fd
, (char *) &hackpid
, sizeof(hackpid
))
251 != sizeof(hackpid
)) {
252 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
255 error("cannot write lock (%s)", fq_lock
);
257 if (nhclose(fd
) == -1) {
258 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
261 error("cannot close lock (%s)", fq_lock
);
264 #if defined(MSDOS) && defined(NO_TERMS)
269 #endif /* PC_LOCKING */
275 * normalize file name - we don't like .'s, /'s, spaces, and
276 * lots of other things
282 for (lp
= s
; *lp
; lp
++)
283 if (*lp
<= ' ' || *lp
== '"' || (*lp
>= '*' && *lp
<= ',')
284 || *lp
== '.' || *lp
== '/' || (*lp
>= ':' && *lp
<= '?') ||
286 *lp
== '&' || *lp
== '(' || *lp
== ')' ||
288 *lp
== '|' || *lp
>= 127 || (*lp
>= '[' && *lp
<= ']'))