File: winnt.c

package info (click to toggle)
glhack 1.2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,744 kB
  • sloc: ansic: 208,571; cpp: 13,139; yacc: 2,005; makefile: 1,152; lex: 377; sh: 121; awk: 89; sed: 11
file content (324 lines) | stat: -rw-r--r-- 6,608 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*	SCCS Id: @(#)winnt.c	 3.4	 $Date: 2003/10/26 15:58:22 $		  */
/* Copyright (c) NetHack PC Development Team 1993, 1994 */
/* NetHack may be freely redistributed.  See license for details. */

/*
 *  WIN32 system functions.
 *
 *  Initial Creation: Michael Allison - January 31/93
 *
 */

#define NEED_VARARGS
#include "hack.h"
#include <dos.h>
#ifndef __BORLANDC__
#include <direct.h>
#endif
#include <ctype.h>
#include "win32api.h"
#ifdef WIN32CON
#include "wintty.h"
#endif
#ifdef WIN32


/*
 * The following WIN32 API routines are used in this file.
 *
 * GetDiskFreeSpace
 * GetVolumeInformation
 * GetUserName
 * FindFirstFile
 * FindNextFile
 * FindClose
 *
 */


/* globals required within here */
HANDLE ffhandle = (HANDLE)0;
WIN32_FIND_DATA ffd;

/* The function pointer nt_kbhit contains a kbhit() equivalent
 * which varies depending on which window port is active.
 * For the tty port it is tty_kbhit() [from nttty.c]
 * For the win32 port it is win32_kbhit() [from winmain.c]
 * It is initialized to point to def_kbhit [in here] for safety.
 */

int def_kbhit(void);
int (*nt_kbhit)() = def_kbhit;

char
switchar()
{
 /* Could not locate a WIN32 API call for this- MJA */
	return '-';
}

long
freediskspace(path)
char *path;
{
	char tmppath[4];
	DWORD SectorsPerCluster = 0;
	DWORD BytesPerSector = 0;
	DWORD FreeClusters = 0;
	DWORD TotalClusters = 0;

	tmppath[0] = *path;
	tmppath[1] = ':';
	tmppath[2] = '\\';
	tmppath[3] = '\0';
	GetDiskFreeSpace(tmppath, &SectorsPerCluster,
			&BytesPerSector,
			&FreeClusters,
			&TotalClusters);
	return (long)(SectorsPerCluster * BytesPerSector *
			FreeClusters);
}

/*
 * Functions to get filenames using wildcards
 */
int
findfirst(path)
char *path;
{
	if (ffhandle){
		 FindClose(ffhandle);
		 ffhandle = (HANDLE)0;
	}
	ffhandle = FindFirstFile(path,&ffd);
	return 
	  (ffhandle == INVALID_HANDLE_VALUE) ? 0 : 1;
}

int
findnext() 
{
	return FindNextFile(ffhandle,&ffd) ? 1 : 0;
}

char *
foundfile_buffer()
{
	return &ffd.cFileName[0];
}

long
filesize(file)
char *file;
{
	if (findfirst(file)) {
		return ((long)ffd.nFileSizeLow);
	} else
		return -1L;
}

/*
 * Chdrive() changes the default drive.
 */
void
chdrive(str)
char *str;
{
	char *ptr;
	char drive;
	if ((ptr = index(str, ':')) != (char *)0) 
	{
		drive = toupper(*(ptr - 1));
		_chdrive((drive - 'A') + 1);
	}
}

static int
max_filename()
{
	DWORD maxflen;
	int status=0;
	
	status = GetVolumeInformation((LPTSTR)0,(LPTSTR)0, 0
			,(LPDWORD)0,&maxflen,(LPDWORD)0,(LPTSTR)0,0);
	if (status) return maxflen;
	else return 0;
}

int
def_kbhit()
{
	return 0;
}

/* 
 * Strip out troublesome file system characters.
 */

void
nt_regularize(s)	/* normalize file name */
register char *s;
{
	register unsigned char *lp;

	for (lp = s; *lp; lp++)
	    if ( *lp == '?' || *lp == '"' || *lp == '\\' ||
		 *lp == '/' || *lp == '>' || *lp == '<'  ||
		 *lp == '*' || *lp == '|' || *lp == ':'  || (*lp > 127))
			*lp = '_';
}

/*
 * This is used in nhlan.c to implement some of the LAN_FEATURES.
 */
char *get_username(lan_username_size)
int *lan_username_size;
{
	static TCHAR username_buffer[BUFSZ];
	unsigned int status;
	DWORD i = BUFSZ - 1;

	/* i gets updated with actual size */
	status = GetUserName(username_buffer, &i);		
	if (status) username_buffer[i] = '\0';
	else Strcpy(username_buffer, "NetHack");
	if (lan_username_size) *lan_username_size = strlen(username_buffer);
	return username_buffer;
}

# if 0
char *getxxx()
{
char     szFullPath[MAX_PATH] = "";
HMODULE  hInst = NULL;  	/* NULL gets the filename of this module */

GetModuleFileName(hInst, szFullPath, sizeof(szFullPath));
return &szFullPath[0];
}
# endif

#ifndef WIN32CON
/* fatal error */
/*VARARGS1*/
void
error VA_DECL(const char *,s)
	char buf[BUFSZ];
	VA_START(s);
	VA_INIT(s, const char *);
	/* error() may get called before tty is initialized */
	if (iflags.window_inited) end_screen();
	if (!strncmpi(windowprocs.name, "tty", 3)) {
		buf[0] = '\n';
		(void) vsprintf(&buf[1], s, VA_ARGS);
		Strcat(buf, "\n");
		msmsg(buf);
	} else {
		(void) vsprintf(buf, s, VA_ARGS);
		Strcat(buf, "\n");
		raw_printf(buf);
	}
	VA_END();
	exit(EXIT_FAILURE);
}
#endif

void Delay(int ms)
{
	(void)Sleep(ms);
}

#ifdef WIN32CON
extern void NDECL(backsp);
#endif

void win32_abort()
{
#ifdef WIZARD
   	if (wizard) {
# ifdef WIN32CON
	    int c, ci, ct;

   	    if (!iflags.window_inited)
		c = 'n';
		ct = 0;
		msmsg("Execute debug breakpoint wizard?");
		while ((ci=nhgetch()) != '\n') {
		    if (ct > 0) {
			backsp();       /* \b is visible on NT */
			(void) putchar(' ');
			backsp();
			ct = 0;
			c = 'n';
		    }
		    if (ci == 'y' || ci == 'n' || ci == 'Y' || ci == 'N') {
		    	ct = 1;
		        c = ci;
		        msmsg("%c",c);
		    }
		}
		if (c == 'y')
			DebugBreak();
# endif
	}
#endif
	abort();
}

static char interjection_buf[INTERJECTION_TYPES][1024];
static int interjection[INTERJECTION_TYPES];

void
interject_assistance(num, interjection_type, ptr1, ptr2)
int num;
int interjection_type;
genericptr_t ptr1;
genericptr_t ptr2;
{
	switch(num) {
	    case 1: {
		char *panicmsg = (char *)ptr1;
		char *datadir =  (char *)ptr2;
		char *tempdir = nh_getenv("TEMP");
		interjection_type = INTERJECT_PANIC;
		interjection[INTERJECT_PANIC] = 1;
		/*
		 * ptr1 = the panic message about to be delivered.
		 * ptr2 = the directory prefix of the dungeon file
		 *        that failed to open.
		 * Check to see if datadir matches tempdir or a
		 * common windows temp location. If it does, inform
		 * the user that they are probably trying to run the
		 * game from within their unzip utility, so the required
		 * files really don't exist at the location. Instruct
		 * them to unpack them first.
		 */
		if (panicmsg && datadir) {
		    if (!strncmpi(datadir, "C:\\WINDOWS\\TEMP", 15) ||
			    strstri(datadir, "TEMP")   ||
			    (tempdir && strstri(datadir, tempdir))) {
			(void)strncpy(interjection_buf[INTERJECT_PANIC],
			"\nOne common cause of this error is attempting to execute\n"
			"the game by double-clicking on it while it is displayed\n"
			"inside an unzip utility.\n\n"
			"You have to unzip the contents of the zip file into a\n"
			"folder on your system, and then run \"NetHack.exe\" or \n"
			"\"NetHackW.exe\" from there.\n\n"
			"If that is not the situation, you are encouraged to\n"
			"report the error as shown above.\n\n", 1023);
		    }
		}
	    }
	    break;
	}
}

void
interject(interjection_type)
int interjection_type;
{
	if (interjection_type >= 0 && interjection_type < INTERJECTION_TYPES)
		msmsg(interjection_buf[interjection_type]);
}
#endif /* WIN32 */

/*winnt.c*/