Mercurial > hg > early-roguelike
comparison rogue4/mdport.c @ 12:9535a08ddc39
Import Rogue 5.2 from the Roguelike Restoration Project (r1490)
author | edwarj4 |
---|---|
date | Sat, 24 Oct 2009 16:52:52 +0000 |
parents | |
children | 107a467612fb |
comparison
equal
deleted
inserted
replaced
11:949d558c2162 | 12:9535a08ddc39 |
---|---|
1 /* | |
2 mdport.c - Machine Dependent | |
3 | |
4 Copyright (C) 2005-2008 Nicholas J. Kisseberth | |
5 All rights reserved. | |
6 | |
7 Redistribution and use in source and binary forms, with or without | |
8 modification, are permitted provided that the following conditions | |
9 are met: | |
10 1. Redistributions of source code must retain the above copyright | |
11 notice, this list of conditions and the following disclaimer. | |
12 2. Redistributions in binary form must reproduce the above copyright | |
13 notice, this list of conditions and the following disclaimer in the | |
14 documentation and/or other materials provided with the distribution. | |
15 3. Neither the name(s) of the author(s) nor the names of other contributors | |
16 may be used to endorse or promote products derived from this software | |
17 without specific prior written permission. | |
18 | |
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND | |
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE | |
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 SUCH DAMAGE. | |
30 */ | |
31 | |
32 #if defined(_WIN32) | |
33 #include <Windows.h> | |
34 #include <Lmcons.h> | |
35 #include <process.h> | |
36 #include <shlobj.h> | |
37 #include <sys/types.h> | |
38 #include <io.h> | |
39 #include <conio.h> | |
40 #undef MOUSE_MOVED | |
41 #elif defined(__DJGPP__) | |
42 #include <process.h> | |
43 #else | |
44 #include <pwd.h> | |
45 #include <sys/utsname.h> | |
46 #include <unistd.h> | |
47 #include <utmpx.h> | |
48 #endif | |
49 | |
50 #ifdef __INTERIX | |
51 char *strdup(const char *s); | |
52 #endif | |
53 | |
54 #include <stdlib.h> | |
55 #include <string.h> | |
56 | |
57 #if defined(_WIN32) && !defined(__MINGW32__) | |
58 #define PATH_MAX MAX_PATH | |
59 #endif | |
60 | |
61 #include <curses.h> | |
62 #if !defined(DJGPP) | |
63 #include <term.h> | |
64 #endif | |
65 | |
66 #include <stdio.h> | |
67 #include <fcntl.h> | |
68 #include <limits.h> | |
69 #include <sys/stat.h> | |
70 #include <signal.h> | |
71 | |
72 #define MOD_MOVE(c) (toupper(c) ) | |
73 | |
74 void | |
75 md_init() | |
76 { | |
77 #ifdef __INTERIX | |
78 char *term; | |
79 | |
80 term = getenv("TERM"); | |
81 | |
82 if (term == NULL) | |
83 setenv("TERM","interix",1); | |
84 #endif | |
85 #if defined(__DJGPP__) || defined(_WIN32) | |
86 _fmode = _O_BINARY; | |
87 #endif | |
88 #if defined(__CYGWIN__) || defined(__MSYS__) | |
89 ESCDELAY=250; | |
90 #endif | |
91 } | |
92 | |
93 int | |
94 md_hasclreol() | |
95 { | |
96 #ifdef CE | |
97 return((CE != NULL) && (*CE != 0)); | |
98 #elif defined (clr_eol) | |
99 return((clr_eol != NULL) && (*clr_eol != 0)); | |
100 #elif !defined(__PDCURSES__) | |
101 return(clr_eol != NULL); | |
102 #else | |
103 return(TRUE); | |
104 #endif | |
105 } | |
106 | |
107 void | |
108 md_putchar(int c) | |
109 { | |
110 putchar(c); | |
111 } | |
112 | |
113 static int md_standout_mode = 0; | |
114 | |
115 void | |
116 md_raw_standout() | |
117 { | |
118 #ifdef _WIN32 | |
119 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
120 HANDLE hStdout; | |
121 int fgattr,bgattr; | |
122 | |
123 if (md_standout_mode == 0) | |
124 { | |
125 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
126 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
127 fgattr = (csbiInfo.wAttributes & 0xF); | |
128 bgattr = (csbiInfo.wAttributes & 0xF0); | |
129 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
130 md_standout_mode = 1; | |
131 } | |
132 #elif defined(SO) | |
133 tputs(SO,0,md_putchar); | |
134 fflush(stdout); | |
135 #endif | |
136 } | |
137 | |
138 void | |
139 md_raw_standend() | |
140 { | |
141 #ifdef _WIN32 | |
142 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
143 HANDLE hStdout; | |
144 int fgattr,bgattr; | |
145 | |
146 if (md_standout_mode == 1) | |
147 { | |
148 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
149 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
150 fgattr = (csbiInfo.wAttributes & 0xF); | |
151 bgattr = (csbiInfo.wAttributes & 0xF0); | |
152 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
153 md_standout_mode = 0; | |
154 } | |
155 #elif defined(SE) | |
156 tputs(SE,0,md_putchar); | |
157 fflush(stdout); | |
158 #endif | |
159 } | |
160 | |
161 int | |
162 md_unlink_open_file(char *file, int inf) | |
163 { | |
164 #ifdef _WIN32 | |
165 _close(inf); | |
166 _chmod(file, 0600); | |
167 return( _unlink(file) ); | |
168 #else | |
169 return(unlink(file)); | |
170 #endif | |
171 } | |
172 | |
173 int | |
174 md_unlink(char *file) | |
175 { | |
176 #ifdef _WIN32 | |
177 _chmod(file, 0600); | |
178 return( _unlink(file) ); | |
179 #else | |
180 return(unlink(file)); | |
181 #endif | |
182 } | |
183 | |
184 FILE * | |
185 md_fdopen(int fd, char *mode) | |
186 { | |
187 #ifdef _WIN32 | |
188 return( _fdopen(fd, mode) ); | |
189 #else | |
190 return( fdopen(fd, mode) ); | |
191 #endif | |
192 } | |
193 | |
194 int | |
195 md_fileno(FILE *fp) | |
196 { | |
197 #ifdef _WIN32 | |
198 return( _fileno(fp) ); | |
199 #else | |
200 return( fileno(fp) ); | |
201 #endif | |
202 } | |
203 | |
204 int | |
205 md_creat(char *file, int mode) | |
206 { | |
207 int fd; | |
208 #ifdef _WIN32 | |
209 mode = _S_IREAD | _S_IWRITE; | |
210 fd = _open(file,O_CREAT | O_EXCL | O_WRONLY, mode); | |
211 #else | |
212 fd = open(file,O_CREAT | O_EXCL | O_WRONLY, mode); | |
213 #endif | |
214 | |
215 return(fd); | |
216 } | |
217 | |
218 | |
219 void | |
220 md_normaluser() | |
221 { | |
222 #ifndef _WIN32 | |
223 setuid(getuid()); | |
224 setgid(getgid()); | |
225 #endif | |
226 } | |
227 | |
228 int | |
229 md_getuid() | |
230 { | |
231 #ifndef _WIN32 | |
232 return( getuid() ); | |
233 #else | |
234 return(42); | |
235 #endif | |
236 } | |
237 | |
238 char * | |
239 md_getusername(int uid) | |
240 { | |
241 static char login[80]; | |
242 char *l = NULL; | |
243 | |
244 /* POSIX Shell has priority, then O/S specific methods */ | |
245 if ( (uid == md_getuid()) && ((l = getenv("LOGNAME")) != NULL) ) | |
246 { | |
247 strncpy(login,l,80); | |
248 login[79] = 0; | |
249 return(login); | |
250 } | |
251 | |
252 #ifdef _WIN32 | |
253 LPSTR mybuffer; | |
254 DWORD size = UNLEN + 1; | |
255 TCHAR buffer[UNLEN + 1]; | |
256 | |
257 mybuffer = buffer; | |
258 if (uid != md_getuid()) | |
259 strcpy(mybuffer, "someone"); | |
260 else | |
261 GetUserName(mybuffer,&size); | |
262 l = mybuffer; | |
263 #endif | |
264 #if !defined(_WIN32) && !defined(DJGPP) | |
265 struct passwd *pw; | |
266 | |
267 pw = getpwuid(getuid()); | |
268 | |
269 l = pw->pw_name; | |
270 #endif | |
271 | |
272 if ((l == NULL) || (*l == '\0')) | |
273 if ( (l = getenv("USERNAME")) == NULL ) | |
274 if ( (l = getenv("USER")) == NULL ) | |
275 l = "nobody"; | |
276 | |
277 strncpy(login,l,80); | |
278 login[79] = 0; | |
279 | |
280 return(login); | |
281 } | |
282 | |
283 char * | |
284 md_gethomedir() | |
285 { | |
286 static char homedir[PATH_MAX]; | |
287 char *h = NULL; | |
288 size_t len; | |
289 #if defined(_WIN32) | |
290 TCHAR szPath[PATH_MAX]; | |
291 #endif | |
292 #if defined(_WIN32) || defined(DJGPP) | |
293 char slash = '\\'; | |
294 #else | |
295 char slash = '/'; | |
296 struct passwd *pw; | |
297 pw = getpwuid(getuid()); | |
298 | |
299 h = pw->pw_dir; | |
300 | |
301 if (strcmp(h,"/") == 0) | |
302 h = NULL; | |
303 #endif | |
304 homedir[0] = 0; | |
305 #ifdef _WIN32 | |
306 if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, szPath))) | |
307 h = szPath; | |
308 #endif | |
309 | |
310 if ( (h == NULL) || (*h == '\0') ) | |
311 if ( (h = getenv("HOME")) == NULL ) | |
312 h = ""; | |
313 | |
314 strncpy(homedir,h,PATH_MAX-1); | |
315 len = strlen(homedir); | |
316 | |
317 if ((len > 0) && (homedir[len-1] == slash)) | |
318 homedir[len-1] = 0; | |
319 | |
320 return(homedir); | |
321 } | |
322 | |
323 void | |
324 md_sleep(int s) | |
325 { | |
326 #ifdef _WIN32 | |
327 Sleep(s); | |
328 #else | |
329 sleep(s); | |
330 #endif | |
331 } | |
332 | |
333 char * | |
334 md_getshell() | |
335 { | |
336 static char shell[PATH_MAX]; | |
337 char *s = NULL; | |
338 #ifdef _WIN32 | |
339 char *def = "C:\\WINDOWS\\SYSTEM32\\CMD.EXE"; | |
340 #elif defined(__DJGPP__) | |
341 char *def = "C:\\COMMAND.COM"; | |
342 #else | |
343 char *def = "/bin/sh"; | |
344 struct passwd *pw; | |
345 pw = getpwuid(getuid()); | |
346 s = pw->pw_shell; | |
347 #endif | |
348 if ((s == NULL) || (*s == '\0')) | |
349 if ( (s = getenv("COMSPEC")) == NULL) | |
350 if ( (s = getenv("SHELL")) == NULL) | |
351 if ( (s = getenv("SystemRoot")) == NULL) | |
352 s = def; | |
353 | |
354 strncpy(shell,s,PATH_MAX); | |
355 shell[PATH_MAX-1] = 0; | |
356 | |
357 return(shell); | |
358 } | |
359 | |
360 void | |
361 md_ignore_signals() | |
362 { | |
363 #ifndef _WIN32 | |
364 int i; | |
365 for (i = 0; i < NSIG; i++) | |
366 signal(i, SIG_IGN); | |
367 #else | |
368 signal(SIGABRT, SIG_IGN); | |
369 signal(SIGFPE, SIG_IGN); | |
370 signal(SIGILL, SIG_IGN); | |
371 signal(SIGINT, SIG_IGN); | |
372 signal(SIGSEGV, SIG_IGN); | |
373 signal(SIGTERM, SIG_IGN); | |
374 #endif | |
375 } | |
376 | |
377 int | |
378 md_shellescape() | |
379 { | |
380 #if (!defined(_WIN32) && !defined(__DJGPP__)) | |
381 int ret_status; | |
382 int pid; | |
383 void (*myquit)(int); | |
384 void (*myend)(int); | |
385 #endif | |
386 char *sh; | |
387 | |