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