OSDN Git Service

fix #48255
[jnethack/source.git] / sys / share / pcunix.c
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. */
5
6 /* This file collects some Unix dependencies; pager.c contains some more */
7
8 #include "hack.h"
9 #include "wintty.h"
10
11 #include <sys/stat.h>
12 #if defined(MSDOS)
13 #include <errno.h>
14 #endif
15
16 #if defined(MSDOS)
17 extern char orgdir[];
18 #endif
19
20 #if defined(TTY_GRAPHICS)
21 extern void NDECL(backsp);
22 extern void NDECL(clear_screen);
23 #endif
24
25 #if 0
26 static struct stat buf;
27 #endif
28
29 #ifdef WANT_GETHDATE
30 static struct stat hbuf;
31 #endif
32
33 #ifdef PC_LOCKING
34 static int NDECL(eraseoldlocks);
35 #endif
36
37 #if 0
38 int
39 uptodate(fd)
40 int fd;
41 {
42 #ifdef WANT_GETHDATE
43     if(fstat(fd, &buf)) {
44         pline("Cannot get status of saved level? ");
45         return(0);
46     }
47     if(buf.st_mtime < hbuf.st_mtime) {
48         pline("Saved level is out of date. ");
49         return(0);
50     }
51 #else
52 #if (defined(MICRO)) && !defined(NO_FSTAT)
53     if(fstat(fd, &buf)) {
54         if(moves > 1) pline("Cannot get status of saved level? ");
55         else pline("Cannot get status of saved game.");
56         return(0);
57     } 
58     if(comp_times(buf.st_mtime)) { 
59         if(moves > 1) pline("Saved level is out of date.");
60         else pline("Saved game is out of date. ");
61         /* This problem occurs enough times we need to give the player
62          * some more information about what causes it, and how to fix.
63          */
64 #ifdef MSDOS
65             pline("Make sure that your system's date and time are correct.");
66             pline("They must be more current than NetHack.EXE's date/time stamp.");
67 #endif /* MSDOS */
68         return(0);
69     }
70 #endif /* MICRO */
71 #endif /* WANT_GETHDATE */
72     return(1);
73 }
74 #endif
75
76 #ifdef PC_LOCKING
77 static int
78 eraseoldlocks()
79 {
80     register int i;
81
82     /* cannot use maxledgerno() here, because we need to find a lock name
83      * before starting everything (including the dungeon initialization
84      * that sets astral_level, needed for maxledgerno()) up
85      */
86     for (i = 1; i <= MAXDUNGEON * MAXLEVEL + 1; i++) {
87         /* try to remove all */
88         set_levelfile_name(lock, i);
89         (void) unlink(fqname(lock, LEVELPREFIX, 0));
90     }
91     set_levelfile_name(lock, 0);
92 #ifdef HOLD_LOCKFILE_OPEN
93     really_close();
94 #endif
95     if (unlink(fqname(lock, LEVELPREFIX, 0)))
96         return 0; /* cannot remove it */
97     return (1);   /* success! */
98 }
99
100 void
101 getlock()
102 {
103     register int fd, c, ci, ct, ern;
104     int fcmask = FCMASK;
105     char tbuf[BUFSZ];
106     const char *fq_lock;
107 #if defined(MSDOS) && defined(NO_TERMS)
108     int grmode = iflags.grmode;
109 #endif
110     /* we ignore QUIT and INT at this point */
111     if (!lock_file(HLOCK, LOCKPREFIX, 10)) {
112         wait_synch();
113 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
114         chdirx(orgdir, 0);
115 #endif
116         error("Quitting.");
117     }
118
119     /* regularize(lock); */ /* already done in pcmain */
120     Sprintf(tbuf, "%s", fqname(lock, LEVELPREFIX, 0));
121     set_levelfile_name(lock, 0);
122     fq_lock = fqname(lock, LEVELPREFIX, 1);
123     if ((fd = open(fq_lock, 0)) == -1) {
124         if (errno == ENOENT)
125             goto gotlock; /* no such file */
126 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
127         chdirx(orgdir, 0);
128 #endif
129         perror(fq_lock);
130         unlock_file(HLOCK);
131         error("Cannot open %s", fq_lock);
132     }
133
134     (void) nhclose(fd);
135
136     if (iflags.window_inited) {
137 #ifdef SELF_RECOVER
138         c = yn("There are files from a game in progress under your name. "
139                "Recover?");
140 #else
141         pline("There is already a game in progress under your name.");
142         pline("You may be able to use \"recover %s\" to get it back.\n",
143               tbuf);
144         c = yn("Do you want to destroy the old game?");
145 #endif
146     } else {
147 #if defined(MSDOS) && defined(NO_TERMS)
148         grmode = iflags.grmode;
149         if (grmode)
150             gr_finish();
151 #endif
152         c = 'n';
153         ct = 0;
154 #ifdef SELF_RECOVER
155         msmsg("There are files from a game in progress under your name. "
156               "Recover? [yn]");
157 #else
158         msmsg("\nThere is already a game in progress under your name.\n");
159         msmsg("If this is unexpected, you may be able to use \n");
160         msmsg("\"recover %s\" to get it back.", tbuf);
161         msmsg("\nDo you want to destroy the old game? [yn] ");
162 #endif
163         while ((ci = nhgetch()) != '\n') {
164             if (ct > 0) {
165                 msmsg("\b \b");
166                 ct = 0;
167                 c = 'n';
168             }
169             if (ci == 'y' || ci == 'n' || ci == 'Y' || ci == 'N') {
170                 ct = 1;
171                 c = ci;
172                 msmsg("%c", c);
173             }
174         }
175     }
176     if (c == 'y' || c == 'Y')
177 #ifndef SELF_RECOVER
178         if (eraseoldlocks()) {
179             goto gotlock;
180         } else {
181             unlock_file(HLOCK);
182 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
183             chdirx(orgdir, 0);
184 #endif
185             error("Couldn't destroy old game.");
186         }
187 #else /*SELF_RECOVER*/
188         if (recover_savefile()) {
189 #if defined(TTY_GRAPHICS)
190             if (WINDOWPORT("tty"))
191                 clear_screen(); /* display gets fouled up otherwise */
192 #endif
193             goto gotlock;
194         } else {
195             unlock_file(HLOCK);
196 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
197             chdirx(orgdir, 0);
198 #endif
199             error("Couldn't recover old game.");
200         }
201 #endif /*SELF_RECOVER*/
202     else {
203         unlock_file(HLOCK);
204 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
205         chdirx(orgdir, 0);
206 #endif
207         error("%s", "Cannot start a new game.");
208     }
209
210 gotlock:
211     fd = creat(fq_lock, fcmask);
212     if (fd == -1)
213         ern = errno;
214     unlock_file(HLOCK);
215     if (fd == -1) {
216 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
217         chdirx(orgdir, 0);
218 #endif
219         error("cannot creat file (%s.)", fq_lock);
220     } else {
221         if (write(fd, (char *) &hackpid, sizeof(hackpid))
222             != sizeof(hackpid)) {
223 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
224             chdirx(orgdir, 0);
225 #endif
226             error("cannot write lock (%s)", fq_lock);
227         }
228         if (nhclose(fd) == -1) {
229 #if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
230             chdirx(orgdir, 0);
231 #endif
232             error("cannot close lock (%s)", fq_lock);
233         }
234     }
235 #if defined(MSDOS) && defined(NO_TERMS)
236     if (grmode)
237         gr_init();
238 #endif
239 }
240 #endif /* PC_LOCKING */
241
242 void
243 regularize(s)
244 /*
245  * normalize file name - we don't like .'s, /'s, spaces, and
246  * lots of other things
247  */
248 register char *s;
249 {
250 /*JP
251     register char *lp;
252 */
253     register unsigned char *lp;
254
255     for (lp = s; *lp; lp++)
256         if (*lp <= ' ' || *lp == '"' || (*lp >= '*' && *lp <= ',')
257             || *lp == '.' || *lp == '/' || (*lp >= ':' && *lp <= '?') ||
258 #ifdef OS2
259             *lp == '&' || *lp == '(' || *lp == ')' ||
260 #endif
261             *lp == '|' || *lp >= 127 || (*lp >= '[' && *lp <= ']'))
262             *lp = '_';
263 }
264
265 #ifdef __EMX__
266 void
267 seteuid(int i)
268 {
269     ;
270 }
271 #endif