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 /*-Copyright (c) Michael Allison, 2006. */
4 /* NetHack may be freely redistributed. See license for details. */
6 /* This file collects some Unix dependencies; pager.c contains some more */
12 #if defined(WIN32) || defined(MSDOS)
16 #if defined(WIN32) || defined(MSDOS)
19 extern void NDECL(backsp);
21 extern void NDECL(clear_screen);
25 static struct stat buf;
29 static struct stat hbuf;
33 static int NDECL(eraseoldlocks);
43 pline("Cannot get status of saved level? ");
46 if(buf.st_mtime < hbuf.st_mtime) {
47 pline("Saved level is out of date. ");
51 #if (defined(MICRO) || defined(WIN32)) && !defined(NO_FSTAT)
53 if(moves > 1) pline("Cannot get status of saved level? ");
54 else pline("Cannot get status of saved game.");
57 if(comp_times(buf.st_mtime)) {
58 if(moves > 1) pline("Saved level is out of date.");
59 else pline("Saved game is out of date. ");
60 /* This problem occurs enough times we need to give the player
61 * some more information about what causes it, and how to fix.
64 pline("Make sure that your system's date and time are correct.");
65 pline("They must be more current than NetHack.EXE's date/time stamp.");
70 #endif /* WANT_GETHDATE */
81 /* cannot use maxledgerno() here, because we need to find a lock name
82 * before starting everything (including the dungeon initialization
83 * that sets astral_level, needed for maxledgerno()) up
85 for (i = 1; i <= MAXDUNGEON * MAXLEVEL + 1; i++) {
86 /* try to remove all */
87 set_levelfile_name(lock, i);
88 (void) unlink(fqname(lock, LEVELPREFIX, 0));
90 set_levelfile_name(lock, 0);
91 #ifdef HOLD_LOCKFILE_OPEN
94 if (unlink(fqname(lock, LEVELPREFIX, 0)))
95 return 0; /* cannot remove it */
96 return (1); /* success! */
102 register int fd, c, ci, ct, ern;
106 #if defined(MSDOS) && defined(NO_TERMS)
107 int grmode = iflags.grmode;
109 /* we ignore QUIT and INT at this point */
110 if (!lock_file(HLOCK, LOCKPREFIX, 10)) {
112 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
118 /* regularize(lock); */ /* already done in pcmain */
119 Sprintf(tbuf, "%s", fqname(lock, LEVELPREFIX, 0));
120 set_levelfile_name(lock, 0);
121 fq_lock = fqname(lock, LEVELPREFIX, 1);
122 if ((fd = open(fq_lock, 0)) == -1) {
124 goto gotlock; /* no such file */
125 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
128 #if defined(WIN32) || defined(HOLD_LOCKFILE_OPEN)
129 #if defined(HOLD_LOCKFILE_OPEN)
130 if (errno == EACCES) {
131 #define OOPS_BUFSZ 512
132 char oops[OOPS_BUFSZ];
135 "\nThere are files from a game in progress under your name.");
136 Strcat(oops, "\nThe files are locked or inaccessible.");
137 Strcat(oops, " Is the other game still running?\n");
138 if (strlen(fq_lock) < ((OOPS_BUFSZ - 16) - strlen(oops)))
139 Sprintf(eos(oops), "Cannot open %s", fq_lock);
145 error("Bad directory or name: %s\n%s\n", fq_lock,
151 error("Cannot open %s", fq_lock);
156 if (iflags.window_inited) {
158 c = yn("There are files from a game in progress under your name. "
161 pline("There is already a game in progress under your name.");
162 pline("You may be able to use \"recover %s\" to get it back.\n",
164 c = yn("Do you want to destroy the old game?");
167 #if defined(MSDOS) && defined(NO_TERMS)
168 grmode = iflags.grmode;
175 msmsg("There are files from a game in progress under your name. "
178 msmsg("\nThere is already a game in progress under your name.\n");
179 msmsg("If this is unexpected, you may be able to use \n");
180 msmsg("\"recover %s\" to get it back.", tbuf);
181 msmsg("\nDo you want to destroy the old game? [yn] ");
183 while ((ci = nhgetch()) != '\n') {
189 if (ci == 'y' || ci == 'n' || ci == 'Y' || ci == 'N') {
196 if (c == 'y' || c == 'Y')
198 if (eraseoldlocks()) {
200 if (!strncmpi(windowprocs.name, "tty", 3))
201 clear_screen(); /* display gets fouled up otherwise */
206 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
209 error("Couldn't destroy old game.");
211 #else /*SELF_RECOVER*/
212 if (recover_savefile()) {
214 if (!strncmpi(windowprocs.name, "tty", 3))
215 clear_screen(); /* display gets fouled up otherwise */
220 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
223 error("Couldn't recover old game.");
225 #endif /*SELF_RECOVER*/
228 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
231 error("%s", "Cannot start a new game.");
235 fd = creat(fq_lock, fcmask);
240 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
244 error("cannot creat file (%s.)\n%s\n%s\"%s\" exists?\n", fq_lock,
245 strerror(ern), " Are you sure that the directory",
246 fqn_prefix[LEVELPREFIX]);
248 error("cannot creat file (%s.)", fq_lock);
251 if (write(fd, (char *) &hackpid, sizeof(hackpid))
252 != sizeof(hackpid)) {
253 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
256 error("cannot write lock (%s)", fq_lock);
258 if (nhclose(fd) == -1) {
259 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
262 error("cannot close lock (%s)", fq_lock);
265 #if defined(MSDOS) && defined(NO_TERMS)
270 #endif /* PC_LOCKING */
276 * normalize file name - we don't like .'s, /'s, spaces, and
277 * lots of other things
284 register unsigned char *lp;
286 for (lp = s; *lp; lp++)
287 if (*lp <= ' ' || *lp == '"' || (*lp >= '*' && *lp <= ',')
288 || *lp == '.' || *lp == '/' || (*lp >= ':' && *lp <= '?') ||
290 *lp == '&' || *lp == '(' || *lp == ')' ||
292 *lp == '|' || *lp >= 127 || (*lp >= '[' && *lp <= ']'))