OSDN Git Service

add translation
[jnethack/source.git] / sys / winnt / winnt.c
1 /* NetHack 3.6  winnt.c $NHDT-Date: 1431737068 2015/05/16 00:44:28 $  $NHDT-Branch: master $:$NHDT-Revision: 1.26 $ */
2 /* Copyright (c) NetHack PC Development Team 1993, 1994 */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 /*
6  *  WIN32 system functions.
7  *
8  *  Initial Creation: Michael Allison - January 31/93
9  *
10  */
11
12 #define NEED_VARARGS
13 #include "hack.h"
14 #include <dos.h>
15 #ifndef __BORLANDC__
16 #include <direct.h>
17 #endif
18 #include <ctype.h>
19 #include "win32api.h"
20 #include "wintty.h"
21 #ifdef WIN32
22
23 /*
24  * The following WIN32 API routines are used in this file.
25  *
26  * GetDiskFreeSpace
27  * GetVolumeInformation
28  * GetUserName
29  * FindFirstFile
30  * FindNextFile
31  * FindClose
32  *
33  */
34
35 /* globals required within here */
36 HANDLE ffhandle = (HANDLE) 0;
37 WIN32_FIND_DATA ffd;
38
39 /* The function pointer nt_kbhit contains a kbhit() equivalent
40  * which varies depending on which window port is active.
41  * For the tty port it is tty_kbhit() [from nttty.c]
42  * For the win32 port it is win32_kbhit() [from winmain.c]
43  * It is initialized to point to def_kbhit [in here] for safety.
44  */
45
46 int def_kbhit(void);
47 int (*nt_kbhit)() = def_kbhit;
48
49 char
50 switchar()
51 {
52     /* Could not locate a WIN32 API call for this- MJA */
53     return '-';
54 }
55
56 long
57 freediskspace(path)
58 char *path;
59 {
60     char tmppath[4];
61     DWORD SectorsPerCluster = 0;
62     DWORD BytesPerSector = 0;
63     DWORD FreeClusters = 0;
64     DWORD TotalClusters = 0;
65
66     tmppath[0] = *path;
67     tmppath[1] = ':';
68     tmppath[2] = '\\';
69     tmppath[3] = '\0';
70     GetDiskFreeSpace(tmppath, &SectorsPerCluster, &BytesPerSector,
71                      &FreeClusters, &TotalClusters);
72     return (long) (SectorsPerCluster * BytesPerSector * FreeClusters);
73 }
74
75 /*
76  * Functions to get filenames using wildcards
77  */
78 int
79 findfirst(path)
80 char *path;
81 {
82     if (ffhandle) {
83         FindClose(ffhandle);
84         ffhandle = (HANDLE) 0;
85     }
86     ffhandle = FindFirstFile(path, &ffd);
87     return (ffhandle == INVALID_HANDLE_VALUE) ? 0 : 1;
88 }
89
90 int
91 findnext()
92 {
93     return FindNextFile(ffhandle, &ffd) ? 1 : 0;
94 }
95
96 char *
97 foundfile_buffer()
98 {
99     return &ffd.cFileName[0];
100 }
101
102 long
103 filesize(file)
104 char *file;
105 {
106     if (findfirst(file)) {
107         return ((long) ffd.nFileSizeLow);
108     } else
109         return -1L;
110 }
111
112 /*
113  * Chdrive() changes the default drive.
114  */
115 void
116 chdrive(str)
117 char *str;
118 {
119     char *ptr;
120     char drive;
121     if ((ptr = index(str, ':')) != (char *) 0) {
122         drive = toupper(*(ptr - 1));
123         _chdrive((drive - 'A') + 1);
124     }
125 }
126
127 static int
128 max_filename()
129 {
130     DWORD maxflen;
131     int status = 0;
132
133     status = GetVolumeInformation((LPTSTR) 0, (LPTSTR) 0, 0, (LPDWORD) 0,
134                                   &maxflen, (LPDWORD) 0, (LPTSTR) 0, 0);
135     if (status)
136         return maxflen;
137     else
138         return 0;
139 }
140
141 int
142 def_kbhit()
143 {
144     return 0;
145 }
146
147 /*
148  * Strip out troublesome file system characters.
149  */
150
151 void nt_regularize(s) /* normalize file name */
152 register char *s;
153 {
154     register unsigned char *lp;
155
156     for (lp = s; *lp; lp++)
157         if (*lp == '?' || *lp == '"' || *lp == '\\' || *lp == '/'
158             || *lp == '>' || *lp == '<' || *lp == '*' || *lp == '|'
159 /*JP
160             || *lp == ':' || (*lp > 127))
161 */
162             || *lp == ':')
163             *lp = '_';
164 }
165
166 /*
167  * This is used in nhlan.c to implement some of the LAN_FEATURES.
168  */
169 char *
170 get_username(lan_username_size)
171 int *lan_username_size;
172 {
173     static TCHAR username_buffer[BUFSZ];
174     unsigned int status;
175     DWORD i = BUFSZ - 1;
176
177     /* i gets updated with actual size */
178     status = GetUserName(username_buffer, &i);
179     if (status)
180         username_buffer[i] = '\0';
181     else
182         Strcpy(username_buffer, "NetHack");
183     if (lan_username_size)
184         *lan_username_size = strlen(username_buffer);
185     return username_buffer;
186 }
187
188 #if 0
189 char *getxxx()
190 {
191 char     szFullPath[MAX_PATH] = "";
192 HMODULE  hInst = NULL;          /* NULL gets the filename of this module */
193
194 GetModuleFileName(hInst, szFullPath, sizeof(szFullPath));
195 return &szFullPath[0];
196 }
197 #endif
198
199 /* fatal error */
200 /*VARARGS1*/
201 void error
202 VA_DECL(const char *, s)
203 {
204     char buf[BUFSZ];
205     VA_START(s);
206     VA_INIT(s, const char *);
207     /* error() may get called before tty is initialized */
208     if (iflags.window_inited)
209         end_screen();
210     if (!strncmpi(windowprocs.name, "tty", 3)) {
211         buf[0] = '\n';
212         (void) vsprintf(&buf[1], s, VA_ARGS);
213         Strcat(buf, "\n");
214         msmsg(buf);
215     } else {
216         (void) vsprintf(buf, s, VA_ARGS);
217         Strcat(buf, "\n");
218         raw_printf(buf);
219     }
220     VA_END();
221     exit(EXIT_FAILURE);
222 }
223
224 void
225 Delay(int ms)
226 {
227     (void) Sleep(ms);
228 }
229
230 extern void NDECL(backsp);
231
232 void
233 win32_abort()
234 {
235     if (wizard) {
236         int c, ci, ct;
237
238         if (!iflags.window_inited)
239             c = 'n';
240         ct = 0;
241         msmsg("Execute debug breakpoint wizard?");
242         while ((ci = nhgetch()) != '\n') {
243             if (ct > 0) {
244                 backsp(); /* \b is visible on NT */
245                 (void) putchar(' ');
246                 backsp();
247                 ct = 0;
248                 c = 'n';
249             }
250             if (ci == 'y' || ci == 'n' || ci == 'Y' || ci == 'N') {
251                 ct = 1;
252                 c = ci;
253                 msmsg("%c", c);
254             }
255         }
256         if (c == 'y')
257             DebugBreak();
258     }
259     abort();
260 }
261
262 static char interjection_buf[INTERJECTION_TYPES][1024];
263 static int interjection[INTERJECTION_TYPES];
264
265 void
266 interject_assistance(num, interjection_type, ptr1, ptr2)
267 int num;
268 int interjection_type;
269 genericptr_t ptr1;
270 genericptr_t ptr2;
271 {
272     switch (num) {
273     case 1: {
274         char *panicmsg = (char *) ptr1;
275         char *datadir = (char *) ptr2;
276         char *tempdir = nh_getenv("TEMP");
277         interjection_type = INTERJECT_PANIC;
278         interjection[INTERJECT_PANIC] = 1;
279         /*
280          * ptr1 = the panic message about to be delivered.
281          * ptr2 = the directory prefix of the dungeon file
282          *        that failed to open.
283          * Check to see if datadir matches tempdir or a
284          * common windows temp location. If it does, inform
285          * the user that they are probably trying to run the
286          * game from within their unzip utility, so the required
287          * files really don't exist at the location. Instruct
288          * them to unpack them first.
289          */
290         if (panicmsg && datadir) {
291             if (!strncmpi(datadir, "C:\\WINDOWS\\TEMP", 15)
292                 || strstri(datadir, "TEMP")
293                 || (tempdir && strstri(datadir, tempdir))) {
294                 (void) strncpy(
295                     interjection_buf[INTERJECT_PANIC],
296                     "\nOne common cause of this error is attempting to "
297                     "execute\n"
298                     "the game by double-clicking on it while it is "
299                     "displayed\n"
300                     "inside an unzip utility.\n\n"
301                     "You have to unzip the contents of the zip file into a\n"
302                     "folder on your system, and then run \"NetHack.exe\" or "
303                     "\n"
304                     "\"NetHackW.exe\" from there.\n\n"
305                     "If that is not the situation, you are encouraged to\n"
306                     "report the error as shown above.\n\n",
307                     1023);
308             }
309         }
310     } break;
311     }
312 }
313
314 void
315 interject(interjection_type)
316 int interjection_type;
317 {
318     if (interjection_type >= 0 && interjection_type < INTERJECTION_TYPES)
319         msmsg(interjection_buf[interjection_type]);
320 }
321
322 #ifdef RUNTIME_PORT_ID
323 /*
324  * _M_IX86 is Defined for x86 processors. This is not defined for x64
325  * processors.
326  * _M_X64  is Defined for x64 processors.
327  * _M_IA64 is Defined for Itanium Processor Family 64-bit processors.
328  * _WIN64  is Defined for applications for Win64.
329  */
330 #ifndef _M_IX86
331 #ifdef _M_X64
332 #define TARGET_PORT "(x64) "
333 #endif
334 #ifdef _M_IA64
335 #define TARGET_PORT "(IA64) "
336 #endif
337 #endif
338
339 #ifndef TARGET_PORT
340 #define TARGET_PORT "(x86) "
341 #endif
342
343 void
344 append_port_id(buf)
345 char *buf;
346 {
347     char *portstr = TARGET_PORT;
348     Sprintf(eos(buf), " %s", portstr);
349 }
350 #endif /* RUNTIME_PORT_ID */
351
352 #endif /* WIN32 */
353
354 /*winnt.c*/