File: pcunix.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (289 lines) | stat: -rw-r--r-- 7,203 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*	SCCS Id: @(#)pcunix.c	3.4	1994/11/07	*/
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed.  See license for details. */

/* This file collects some Unix dependencies; pager.c contains some more */

#include "hack.h"
#include "wintty.h"

#include	<sys/stat.h>
#if defined(WIN32) || defined(MSDOS)
#include	<errno.h>
#endif

#if defined(WIN32) || defined(MSDOS)
extern char orgdir[];
# ifdef WIN32
extern void NDECL(backsp);
# endif
extern void NDECL(clear_screen);
#endif

#ifdef OVLB

#if 0
static struct stat buf;
#endif

# ifdef WANT_GETHDATE
static struct stat hbuf;
# endif

#ifdef PC_LOCKING
static int NDECL(eraseoldlocks);
#endif

#if 0
int
uptodate(fd)
int fd;
{
# ifdef WANT_GETHDATE
    if(fstat(fd, &buf)) {
	pline("Cannot get status of saved level? ");
	return(0);
    }
    if(buf.st_mtime < hbuf.st_mtime) {
	pline("Saved level is out of date. ");
	return(0);
    }
# else
#  if (defined(MICRO) || defined(WIN32)) && !defined(NO_FSTAT)
    if(fstat(fd, &buf)) {
	if(moves > 1) pline("Cannot get status of saved level? ");
	else pline("Cannot get status of saved game.");
	return(0);
    } 
    if(comp_times(buf.st_mtime)) { 
	if(moves > 1) pline("Saved level is out of date.");
	else pline("Saved game is out of date. ");
	/* This problem occurs enough times we need to give the player
	 * some more information about what causes it, and how to fix.
	 */
#  ifdef MSDOS
	    pline("Make sure that your system's date and time are correct.");
	    pline("They must be more current than NetHack.EXE's date/time stamp.");
#  endif /* MSDOS */
	return(0);
    }
#  endif  /* MICRO */
# endif /* WANT_GETHDATE */
    return(1);
}
#endif

#ifdef PC_LOCKING
static int
eraseoldlocks()
{
	register int i;

	/* cannot use maxledgerno() here, because we need to find a lock name
	 * before starting everything (including the dungeon initialization
	 * that sets astral_level, needed for maxledgerno()) up
	 */
	for(i = 1; i <= MAXDUNGEON*MAXLEVEL + 1; i++) {
		/* try to remove all */
		set_levelfile_name(lock, i);
		(void) unlink(fqname(lock, LEVELPREFIX, 0));
	}
	set_levelfile_name(lock, 0);
#ifdef HOLD_LOCKFILE_OPEN
	really_close();
#endif
	if(unlink(fqname(lock, LEVELPREFIX, 0)))
		return 0;				/* cannot remove it */
	return(1);					/* success! */
}

void
getlock()
{
	register int fd, c, ci, ct, ern;
	char tbuf[BUFSZ];
	const char *fq_lock;
# if defined(MSDOS) && defined(NO_TERMS)
	int grmode = iflags.grmode;
# endif
	/* we ignore QUIT and INT at this point */
	if (!lock_file(HLOCK, LOCKPREFIX, 10)) {
		wait_synch();
# if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
		chdirx(orgdir, 0);
# endif
		error("Quitting.");
	}

	/* regularize(lock); */ /* already done in pcmain */
	Sprintf(tbuf,"%s",fqname(lock, LEVELPREFIX, 0));
	set_levelfile_name(lock, 0);
	fq_lock = fqname(lock, LEVELPREFIX, 1);
	if((fd = open(fq_lock,0)) == -1) {
		if(errno == ENOENT) goto gotlock;    /* no such file */
# if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
		chdirx(orgdir, 0);
# endif
# if defined(WIN32) || defined(HOLD_LOCKFILE_OPEN)
#  if defined(HOLD_LOCKFILE_OPEN)
 		if(errno == EACCES) {
#define OOPS_BUFSZ 512
 		    char oops[OOPS_BUFSZ];
 		    Strcpy(oops,
			     "\nThere are files from a game in progress under your name.");
		    Strcat(oops, "\nThe files are locked or inaccessible.");
		    Strcat(oops, " Is the other game still running?\n");
		    if (strlen(fq_lock) < ((OOPS_BUFSZ -16) - strlen(oops)))
			    Sprintf(eos(oops), "Cannot open %s", fq_lock);
		    Strcat(oops, "\n");
		    unlock_file(HLOCK);
		    error(oops);
 		} else
#  endif
		error("Bad directory or name: %s\n%s\n",
				fq_lock, strerror(errno));
# else
		perror(fq_lock);
# endif
		unlock_file(HLOCK); 
		error("Cannot open %s", fq_lock);
	}

	(void) close(fd);

	if(iflags.window_inited) { 
# ifdef SELF_RECOVER
	  c = yn("There are files from a game in progress under your name. Recover?");
# else
	  pline("There is already a game in progress under your name.");
	  pline("You may be able to use \"recover %s\" to get it back.\n",tbuf);
	  c = yn("Do you want to destroy the old game?");
# endif
	} else {
# if defined(MSDOS) && defined(NO_TERMS)
		grmode = iflags.grmode;
		if (grmode) gr_finish();
# endif
		c = 'n';
		ct = 0;
# ifdef SELF_RECOVER
		msmsg(
		"There are files from a game in progress under your name. Recover? [yn]");
# else
		msmsg("\nThere is already a game in progress under your name.\n");
		msmsg("If this is unexpected, you may be able to use \n");
		msmsg("\"recover %s\" to get it back.",tbuf);
		msmsg("\nDo you want to destroy the old game? [yn] ");
# endif
		while ((ci=nhgetch()) != '\n') {
		    if (ct > 0) {
			msmsg("\b \b");
			ct = 0;
			c = 'n';
		    }
		    if (ci == 'y' || ci == 'n' || ci == 'Y' || ci == 'N') {
		    	ct = 1;
		        c = ci;
		        msmsg("%c",c);
		    }
		}
	}
	if(c == 'y' || c == 'Y')
# ifndef SELF_RECOVER
		if(eraseoldlocks()) {
#  if defined(WIN32CON)
			clear_screen();		/* display gets fouled up otherwise */
#  endif
			goto gotlock;
		} else {
			unlock_file(HLOCK);
#  if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
			chdirx(orgdir, 0);
#  endif
			error("Couldn't destroy old game.");
		}
# else /*SELF_RECOVER*/
		if(recover_savefile()) {
#  if defined(WIN32CON)
			clear_screen();		/* display gets fouled up otherwise */
#  endif
			goto gotlock;
		} else {
			unlock_file(HLOCK);
#  if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
			chdirx(orgdir, 0);
#  endif
			error("Couldn't recover old game.");
		}
# endif /*SELF_RECOVER*/
	else {
		unlock_file(HLOCK);
# if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
		chdirx(orgdir, 0);
# endif
		error("%s", "Cannot start a new game.");
	}

gotlock:
	fd = creat(fq_lock, FCMASK);
	if (fd == -1) ern = errno;
	unlock_file(HLOCK);
	if(fd == -1) {
# if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
		chdirx(orgdir, 0);
# endif
# if defined(WIN32)
		error("cannot creat file (%s.)\n%s\n%s\"%s\" exists?\n", 
				fq_lock, strerror(ern), " Are you sure that the directory",
				fqn_prefix[LEVELPREFIX]);
# else
		error("cannot creat file (%s.)", fq_lock);
# endif
	} else {
		if(write(fd, (char *) &hackpid, sizeof(hackpid))
		    != sizeof(hackpid)){
# if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
			chdirx(orgdir, 0);
# endif
			error("cannot write lock (%s)", fq_lock);
		}
		if(close(fd) == -1) {
# if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
			chdirx(orgdir, 0);
# endif
			error("cannot close lock (%s)", fq_lock);
		}
	}
# if defined(MSDOS) && defined(NO_TERMS)
	if (grmode) gr_init();
# endif
}	
#endif /* PC_LOCKING */

# ifndef WIN32
void
regularize(s)
/*
 * normalize file name - we don't like .'s, /'s, spaces, and
 * lots of other things
 */
register char *s;
{
	register char *lp;

	for (lp = s; *lp; lp++)
		if (*lp <= ' ' || *lp == '"' || (*lp >= '*' && *lp <= ',') ||
		    *lp == '.' || *lp == '/' || (*lp >= ':' && *lp <= '?') ||
# ifdef OS2
		    *lp == '&' || *lp == '(' || *lp == ')' ||
# endif
		    *lp == '|' || *lp >= 127 || (*lp >= '[' && *lp <= ']'))
                        *lp = '_';
}
# endif /* WIN32 */
#endif /* OVLB */


#ifdef __EMX__
void seteuid(int i){;}
#endif