1 /* SCCS Id: @(#)winnt.c 3.3 97/04/12 */
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 #include <direct.h>
16 #include <ctype.h>
17 #include "win32api.h"
18
19 #ifdef WIN32
20
21
22 /*
23 * The following WIN32 API routines are used in this file.
24 *
25 * GetDiskFreeSpace
26 * GetVolumeInformation
27 * GetUserName
28 * FindFirstFile
29 * FindNextFile
30 * FindClose
31 *
32 */
33
34
35 /* globals required within here */
36 HANDLE ffhandle = (HANDLE)0;
37 WIN32_FIND_DATA ffd;
38
39
40 /* The function pointer nt_kbhit contains a kbhit() equivalent
41 * which varies depending on which window port is active.
42 * For the tty port it is tty_kbhit() [from nttty.c]
43 * For the win32 port it is win32_kbhit() [from winmain.c]
44 * It is initialized to point to def_kbhit [in here] for safety.
45 */
46
47 int def_kbhit(void);
48 int (*nt_kbhit)() = def_kbhit;
49
50 char
switchar()51 switchar()
52 {
53 /* Could not locate a WIN32 API call for this- MJA */
54 return '-';
55 }
56
57 long
freediskspace(path)58 freediskspace(path)
59 char *path;
60 {
61 char tmppath[4];
62 DWORD SectorsPerCluster = 0;
63 DWORD BytesPerSector = 0;
64 DWORD FreeClusters = 0;
65 DWORD TotalClusters = 0;
66
67 tmppath[0] = *path;
68 tmppath[1] = ':';
69 tmppath[2] = '\\';
70 tmppath[3] = '\0';
71 GetDiskFreeSpace(tmppath, &SectorsPerCluster,
72 &BytesPerSector,
73 &FreeClusters,
74 &TotalClusters);
75 return (long)(SectorsPerCluster * BytesPerSector *
76 FreeClusters);
77 }
78
79 /*
80 * Functions to get filenames using wildcards
81 */
82 int
findfirst(path)83 findfirst(path)
84 char *path;
85 {
86 if (ffhandle){
87 FindClose(ffhandle);
88 ffhandle = (HANDLE)0;
89 }
90 ffhandle = FindFirstFile(path,&ffd);
91 return
92 (ffhandle == INVALID_HANDLE_VALUE) ? 0 : 1;
93 }
94
95 int
findnext()96 findnext()
97 {
98 return FindNextFile(ffhandle,&ffd) ? 1 : 0;
99 }
100
101 char *
foundfile_buffer()102 foundfile_buffer()
103 {
104 return &ffd.cFileName[0];
105 }
106
107 long
filesize(file)108 filesize(file)
109 char *file;
110 {
111 if (findfirst(file)) {
112 return ((long)ffd.nFileSizeLow);
113 } else
114 return -1L;
115 }
116
117 /*
118 * Chdrive() changes the default drive.
119 */
120 void
chdrive(str)121 chdrive(str)
122 char *str;
123 {
124 char *ptr;
125 char drive;
126 if ((ptr = index(str, ':')) != (char *)0)
127 {
128 drive = toupper(*(ptr - 1));
129 _chdrive((drive - 'A') + 1);
130 }
131 }
132
133 static int
max_filename()134 max_filename()
135 {
136 DWORD maxflen;
137 int status=0;
138
139 status = GetVolumeInformation((LPTSTR)0,(LPTSTR)0, 0
140 ,(LPDWORD)0,&maxflen,(LPDWORD)0,(LPTSTR)0,0);
141 if (status) return maxflen;
142 else return 0;
143 }
144
145 int
def_kbhit()146 def_kbhit()
147 {
148 return 0;
149 }
150
151 /*
152 * Windows NT version >= 3.5x and above supports long file names,
153 * even on FAT volumes (VFAT), so no need for nt_regularize.
154 * Windows NT 3.1 could not do long file names except on NTFS,
155 * so nt_regularize was required.
156 */
157
158 void
nt_regularize(s)159 nt_regularize(s) /* normalize file name */
160 register char *s;
161 {
162 register char *lp;
163
164 for (lp = s; *lp; lp++)
165 if ( *lp == '?' || *lp == '"' || *lp == '\\' ||
166 *lp == '/' || *lp == '>' || *lp == '<' ||
167 *lp == '*' || *lp == '|' || *lp == ':' || (*lp > 127))
168 *lp = '_';
169 }
170
171 /*
172 * This is used in nhlan.c to implement some of the LAN_FEATURES.
173 */
get_username(lan_username_size)174 char *get_username(lan_username_size)
175 int *lan_username_size;
176 {
177 static char username_buffer[BUFSZ];
178 unsigned int status;
179 int i = 0;
180
181 i = BUFSZ - 1;
182 /* i gets updated with actual size */
183 status = GetUserName(username_buffer, &i);
184 if (status) username_buffer[i] = '\0';
185 else Strcpy(username_buffer, "NetHack");
186 if (lan_username_size) *lan_username_size = strlen(username_buffer);
187 return username_buffer;
188 }
189
190 # if 0
getxxx()191 char *getxxx()
192 {
193 char szFullPath[MAX_PATH] = "";
194 HMODULE hInst = NULL; /* NULL gets the filename of this module */
195
196 GetModuleFileName(hInst, szFullPath, sizeof(szFullPath));
197 return &szFullPath[0];
198 }
199 # endif
200
201
202 /* fatal error */
203 /*VARARGS1*/
204
205 void
206 error VA_DECL(const char *,s)
207 VA_START(s);
208 VA_INIT(s, const char *);
209 /* error() may get called before tty is initialized */
210 if (iflags.window_inited) end_screen();
211 putchar('\n');
212 Vprintf(s,VA_ARGS);
213 putchar('\n');
214 VA_END();
215 exit(EXIT_FAILURE);
216 }
217
218 void Delay(int ms)
219 {
220 (void)Sleep(ms);
221 }
222 #endif /* WIN32 */
223
224 /*winnt.c*/
225