comparison rogue5/mdport.c @ 33:f502bf60e6e4

Import Rogue 5.4 from the Roguelike Restoration Project (r1490)
author elwin
date Mon, 24 May 2010 20:10:59 +0000
parents
children 655c317b6237
comparison
equal deleted inserted replaced
32:2dcd75e6a736 33:f502bf60e6e4
1 /*
2 mdport.c - Machine Dependent Code
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 #include <stdlib.h>
33 #include <string.h>
34
35 #if defined(_WIN32)
36 #include <Windows.h>
37 #include <Lmcons.h>
38 #include <io.h>
39 #include <conio.h>
40 #pragma warning( disable: 4201 )
41 #include <shlobj.h>
42 #pragma warning( default: 4201 )
43 #include <Shlwapi.h>
44 #undef MOUSE_MOVED
45 #endif
46
47 #include <curses.h>
48 #include "extern.h"
49
50 #if defined(HAVE_SYS_TYPES)
51 #include <sys/types.h>
52 #endif
53
54 #if defined(HAVE_PROCESS_H)
55 #include <process.h>
56 #endif
57
58 #if defined(HAVE_PWD_H)
59 #include <pwd.h>
60 #endif
61
62 #if defined(HAVE_SYS_UTSNAME)
63 #include <sys/utsname.h>
64 #endif
65
66 #if defined(HAVE_ARPA_INET_H)
67 #include <arpa/inet.h> /* Solaris 2.8 required this for htonl() and ntohl() */
68 #endif
69
70 #if defined(HAVE_TERMIOS_H)
71 #include <termios.h>
72 #endif
73
74 #if defined(HAVE_UNISTD_H)
75 #ifndef __USE_GNU
76 #define __USE_GNU
77 #include <unistd.h>
78 #undef __USE_GNU
79 #else
80 #include <unistd.h>
81 #endif
82 #endif
83
84 #include <curses.h> /* AIX requires curses.h be included before term.h */
85
86 #if defined(HAVE_TERM_H)
87 #include <term.h>
88 #elif defined(HAVE_NCURSES_TERM_H)
89 #include <ncurses/term.h>
90 #endif
91
92 #if defined(HAVE_WORKING_FORK)
93 #include <sys/wait.h>
94 #endif
95
96 #include <ctype.h>
97 #include <fcntl.h>
98 #include <limits.h>
99 #include <sys/stat.h>
100 #include <signal.h>
101 #include "extern.h"
102
103 #if !defined(PATH_MAX) && defined(_MAX_PATH)
104 #define PATH_MAX _MAX_PATH
105 #endif
106
107 #if !defined(PATH_MAX) && defined(_PATH_MAX)
108 #define PATH_MAX _PATH_MAX
109 #endif
110
111 #define NOOP(x) (x += 0)
112
113 void
114 md_init(void)
115 {
116 #if defined(__INTERIX)
117 char *term;
118
119 term = getenv("TERM");
120
121 if (term == NULL)
122 setenv("TERM","interix");
123 #elif defined(__DJGPP__)
124 _fmode = _O_BINARY;
125 #elif defined(_WIN32)
126 _fmode = _O_BINARY;
127 #endif
128
129 #if defined(HAVE_ESCDELAY) || defined(NCURSES_VERSION)
130 ESCDELAY=64;
131 #endif
132
133 #if defined(DUMP)
134 md_onsignal_default();
135 #else
136 md_onsignal_exit();
137 #endif
138 }
139
140 void
141 md_onsignal_default(void)
142 {
143 #ifdef SIGHUP
144 signal(SIGHUP, SIG_DFL);
145 #endif
146 #ifdef SIGQUIT
147 signal(SIGQUIT, SIG_DFL);
148 #endif
149 #ifdef SIGILL
150 signal(SIGILL, SIG_DFL);
151 #endif
152 #ifdef SIGTRAP
153 signal(SIGTRAP, SIG_DFL);
154 #endif
155 #ifdef SIGIOT
156 signal(SIGIOT, SIG_DFL);
157 #endif
158 #ifdef SIGEMT
159 signal(SIGEMT, SIG_DFL);
160 #endif
161 #ifdef SIGFPE
162 signal(SIGFPE, SIG_DFL);
163 #endif
164 #ifdef SIGBUS
165 signal(SIGBUS, SIG_DFL);
166 #endif
167 #ifdef SIGSEGV
168 signal(SIGSEGV, SIG_DFL);
169 #endif
170 #ifdef SIGSYS
171 signal(SIGSYS, SIG_DFL);
172 #endif
173 #ifdef SIGTERM
174 signal(SIGTERM, SIG_DFL);
175 #endif
176 }
177
178 void
179 md_onsignal_exit(void)
180 {
181 #ifdef SIGHUP
182 signal(SIGHUP, SIG_DFL);
183 #endif
184 #ifdef SIGQUIT
185 signal(SIGQUIT, exit);
186 #endif
187 #ifdef SIGILL
188 signal(SIGILL, exit);
189 #endif
190 #ifdef SIGTRAP
191 signal(SIGTRAP, exit);
192 #endif
193 #ifdef SIGIOT
194 signal(SIGIOT, exit);
195 #endif
196 #ifdef SIGEMT
197 signal(SIGEMT, exit);
198 #endif
199 #ifdef SIGFPE
200 signal(SIGFPE, exit);
201 #endif
202 #ifdef SIGBUS
203 signal(SIGBUS, exit);
204 #endif
205 #ifdef SIGSEGV
206 signal(SIGSEGV, exit);
207 #endif
208 #ifdef SIGSYS
209 signal(SIGSYS, exit);
210 #endif
211 #ifdef SIGTERM
212 signal(SIGTERM, exit);
213 #endif
214 }
215
216 extern void auto_save(int sig);
217 extern void endit(int sig);
218 extern void quit(int sig);
219
220 void
221 md_onsignal_autosave(void)
222 {
223
224 #ifdef SIGHUP
225 signal(SIGHUP, auto_save);
226 #endif
227 #ifdef SIGQUIT
228 signal(SIGQUIT, endit);
229 #endif
230 #ifdef SIGILL
231 signal(SIGILL, auto_save);
232 #endif
233 #ifdef SIGTRAP
234 signal(SIGTRAP, auto_save);
235 #endif
236 #ifdef SIGIOT
237 signal(SIGIOT, auto_save);
238 #endif
239 #ifdef SIGEMT
240 signal(SIGEMT, auto_save);
241 #endif
242 #ifdef SIGFPE
243 signal(SIGFPE, auto_save);
244 #endif
245 #ifdef SIGBUS
246 signal(SIGBUS, auto_save);
247 #endif
248 #ifdef SIGSEGV
249 signal(SIGSEGV, auto_save);
250 #endif
251 #ifdef SIGSYS
252 signal(SIGSYS, auto_save);
253 #endif
254 #ifdef SIGTERM
255 signal(SIGTERM, auto_save);
256 #endif
257 #ifdef SIGINT
258 signal(SIGINT, quit);
259 #endif
260 }
261
262 int
263 md_hasclreol(void)
264 {
265 #if defined(clr_eol)
266 #ifdef NCURSES_VERSION
267 if (cur_term == NULL)
268 return(0);
269 if (cur_term->type.Strings == NULL)
270 return(0);
271 #endif
272 return((clr_eol != NULL) && (*clr_eol != 0));
273 #elif defined(__PDCURSES__)
274 return(TRUE);
275 #else
276 return((CE != NULL) && (*CE != 0));
277 #endif
278 }
279
280 void
281 md_putchar(int c)
282 {
283 putchar(c);
284 }
285
286 #ifdef _WIN32
287 static int md_standout_mode = 0;
288 #endif
289
290 void
291 md_raw_standout(void)
292 {
293 #ifdef _WIN32
294 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
295 HANDLE hStdout;
296 WORD fgattr,bgattr;
297
298 if (md_standout_mode == 0)
299 {
300 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
301 GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
302 fgattr = (csbiInfo.wAttributes & 0xF);
303 bgattr = (csbiInfo.wAttributes & 0xF0);
304 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4));
305 md_standout_mode = 1;
306 }
307 #elif defined(SO)
308 tputs(SO,0,md_putchar);
309 fflush(stdout);
310 #endif
311 }
312
313 void
314 md_raw_standend(void)
315 {
316 #ifdef _WIN32
317 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
318 HANDLE hStdout;
319 WORD fgattr,bgattr;
320
321 if (md_standout_mode == 1)
322 {
323 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
324 GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
325 fgattr = (csbiInfo.wAttributes & 0xF);
326 bgattr = (csbiInfo.wAttributes & 0xF0);
327 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4));
328 md_standout_mode = 0;
329 }
330 #elif defined(SE)
331 tputs(SE,0,md_putchar);
332 fflush(stdout);
333 #endif
334 }
335
336 int
337 md_unlink_open_file(const char *file, FILE *inf)
338 {
339 #ifdef _WIN32
340 fclose(inf);
341 (void) _chmod(file, 0600);
342 return( _unlink(file) );
343 #else
344 return(unlink(file));
345 #endif
346 }
347
348 int
349 md_unlink(char *file)
350 {
351 #ifdef _WIN32
352 (void) _chmod(file, 0600);
353 return( _unlink(file) );
354 #else
355 return(unlink(file));
356 #endif
357 }
358
359 int
360 md_chmod(const char *filename, int mode)
361 {
362 #ifdef _WIN32
363 return( _chmod(filename, mode) );
364 #else
365 return( chmod(filename, mode) );
366 #endif
367 }
368
369 void
370 md_normaluser(void)
371 {
372 #if defined(HAVE_GETGID) && defined(HAVE_GETUID)
373 gid_t realgid = getgid();
374 uid_t realuid = getuid();
375
376 #if defined(HAVE_SETRESGID)
377 if (setresgid(-1, realgid, realgid) != 0) {
378 #elif defined (HAVE_SETREGID)
379 if (setregid(realgid, realgid) != 0) {
380 #elif defined (HAVE_SETGID)
381 if (setgid(realgid) != 0) {
382 #else
383 if (0) {
384 #endif
385 perror("Could not drop setgid privileges. Aborting.");
386 exit(1);
387 }
388