comparison arogue7/mdport.c @ 125:adfa37e67084

Import Advanced Rogue 7.7 from the Roguelike Restoration Project (r1490)
author John "Elwin" Edwards
date Fri, 08 May 2015 15:24:40 -0400
parents
children b786053d2f37
comparison
equal deleted inserted replaced
124:d10fc4a065ac 125:adfa37e67084
1 /*
2 mdport.c - Machine Dependent Code for Porting Unix/Curses games
3
4 Copyright (C) 2005 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 <Shlwapi.h>
38 #include <sys/types.h>
39 #undef MOUSE_MOVED
40 #elif defined(__DJGPP__)
41 #include <process.h>
42 #else
43 #include <pwd.h>
44 #include <sys/utsname.h>
45 #include <unistd.h>
46 #endif
47
48 #include <stdlib.h>
49
50 #if defined(_WIN32) && !defined(__MINGW32__)
51 #define PATH_MAX _PATH_MAX
52 #endif
53
54 #include <curses.h>
55
56 #if defined(__INTERIX) || defined(__MSYS__)
57 #include <term.h>
58 #else
59 #ifdef NCURSES_VERSION
60 #include <ncurses/term.h>
61 #endif
62 #endif
63
64 #include <stdio.h>
65 #include <fcntl.h>
66 #include <limits.h>
67 #include <sys/stat.h>
68 #include <signal.h>
69
70 #define MOD_MOVE(c) (toupper(c) )
71
72 void
73 md_init()
74 {
75 #ifdef __INTERIX
76 char *term;
77
78 term = getenv("TERM");
79
80 if (term == NULL)
81 setenv("TERM","interix");
82 #endif
83 #if defined(__DJGPP__) || defined(_WIN32)
84 _fmode = _O_BINARY;
85 #endif
86 #if defined(__CYGWIN__) || defined(__MSYS__)
87 ESCDELAY=250;
88 #endif
89 }
90
91 int
92 md_hasclreol()
93 {
94 #ifndef attron
95 return(!CE);
96 #elif !defined(__PDCURSES__)
97 return(clr_eol != NULL);
98 #else
99 return(TRUE);
100 #endif
101 }
102
103 #ifdef attron
104 # define _puts(s) tputs(s, 0, md_putchar);
105 # define SO enter_standout_mode
106 # define SE exit_standout_mode
107 #endif
108
109 int
110 md_putchar(int c)
111 {
112 putchar(c);
113 }
114
115 static int md_standout_mode = 0;
116
117 int
118 md_raw_standout()
119 {
120 #ifdef _WIN32
121 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
122 HANDLE hStdout;
123 int fgattr,bgattr;
124
125 if (md_standout_mode == 0)
126 {
127 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
128 GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
129 fgattr = (csbiInfo.wAttributes & 0xF);
130 bgattr = (csbiInfo.wAttributes & 0xF0);
131 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4));
132 md_standout_mode = 1;
133 }
134 #elif !defined(__PDCURSES__)
135 _puts(SO);
136 fflush(stdout);
137 #endif
138 }
139
140 int
141 md_raw_standend()
142 {
143 #ifdef _WIN32
144 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
145 HANDLE hStdout;
146 int fgattr,bgattr;
147
148 if (md_standout_mode == 1)
149 {
150 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
151 GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
152 fgattr = (csbiInfo.wAttributes & 0xF);
153 bgattr = (csbiInfo.wAttributes & 0xF0);
154 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4));
155 md_standout_mode = 0;
156 }
157 #elif !defined(__PDCURSES__)
158 _puts(SE);
159 fflush(stdout);
160 #endif
161 }
162
163 int
164 md_unlink_open_file(char *file, int inf)
165 {
166 #ifdef _WIN32
167 close(inf);
168 chmod(file, 0600);
169 return( _unlink(file) );
170 #else
171 return(unlink(file));
172 #endif
173 }
174
175 int
176 md_unlink(char *file)
177 {
178 #ifdef _WIN32
179 chmod(file, 0600);
180 return( _unlink(file) );
181 #else
182 return(unlink(file));
183 #endif
184 }
185
186 int
187 md_creat(char *file, int mode)
188 {
189 int fd;
190 #ifdef _WIN32
191 mode = _S_IREAD | _S_IWRITE;
192 #endif
193 fd = open(file,O_CREAT | O_EXCL | O_WRONLY, mode);
194
195 return(fd);
196 }
197
198
199 int
200 md_normaluser()
201 {
202 #ifndef _WIN32
203 setuid(getuid());
204 setgid(getgid());
205 #endif
206 }
207
208 int
209 md_getuid()
210 {
211 #ifndef _WIN32
212 return( getuid() );
213 #else
214 return(42);
215 #endif
216 }
217
218 char *
219 md_getusername()
220 {
221 static char login[80];
222 char *l = NULL;
223 #ifdef _WIN32
224 LPSTR mybuffer;
225 DWORD size = UNLEN + 1;
226 TCHAR buffer[UNLEN + 1];
227
228 mybuffer = buffer;
229 GetUserName(mybuffer,&size);
230 l = mybuffer;
231 #endif
232 #if !defined(_WIN32) && !defined(DJGPP)
233 struct passwd *pw;
234
235 pw = getpwuid(getuid());
236
237 l = pw->pw_name;
238 #endif
239
240 if ((l == NULL) || (*l == '\0'))
241 if ( (l = getenv("USERNAME")) == NULL )
242 if ( (l = getenv("LOGNAME")) == NULL )
243 if ( (l = getenv("USER")) == NULL )
244 l = "nobody";
245
246 strncpy(login,l,80);
247 login[79] = 0;
248
249 return(login);
250 }
251
252 char *
253 md_gethomedir()
254 {
255 static char homedir[PATH_MAX];
256 char *h = NULL;
257 size_t len;
258 #if defined(_WIN32)
259 TCHAR szPath[PATH_MAX];
260 #endif
261 #if defined(_WIN32) || defined(DJGPP)
262 char slash = '\\';
263 #else
264 char slash = '/';
265 struct passwd *pw;
266 pw = getpwuid(getuid());
267
268 h = pw->pw_dir;
269
270 if (strcmp(h,"/") == 0)
271 h = NULL;
272 #endif
273 homedir[0] = 0;
274 #ifdef _WIN32
275 if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, szPath)))
276 h = szPath;
277 #endif
278
279 if ( (h == NULL) || (*h == '\0') )
280 if ( (h = getenv("HOME")) == NULL )
281 if ( (h = getenv("HOMEDRIVE")) == NULL)
282 h = "";
283 else
284 {
285 strncpy(homedir,h,PATH_MAX-1);
286 homedir[PATH_MAX-1] = 0;
287
288 if ( (h = getenv("HOMEPATH")) == NULL)
289 h = "";
290 }
291
292
293 len = strlen(homedir);
294 strncat(homedir,h,PATH_MAX-len-1);
295 len = strlen(homedir);
296
297 if ((len > 0) && (homedir[len-1] != slash)) {
298 homedir[len] = slash;
299 homedir[len+1] = 0;
300 }
301
302 return(homedir);
303 }
304
305 int
306 md_sleep(int s)
307 {
308 #ifdef _WIN32
309 _sleep(s);
310 #else
311 sleep(s);
312 #endif
313 }
314
315 char *
316 md_getshell()
317 {
318 static char shell[PATH_MAX];
319 char *s = NULL;
320 #ifdef _WIN32
321 char *def = "C:\\WINDOWS\\SYSTEM32\\CMD.EXE";
322 #elif defined(__DJGPP__)
323 char *def = "C:\\COMMAND.COM";
324 #else
325 char *def = "/bin/sh";
326 struct passwd *pw;
327 pw = getpwuid(getuid());
328 s = pw->pw_shell;
329 #endif
330 if ((s == NULL) || (*s == '\0'))
331 if ( (s = getenv("COMSPEC")) == NULL)
332 if ( (s = getenv("SHELL")) == NULL)
333 if ( (s = getenv("SystemRoot")) == NULL)
334 s = def;
335
336 strncpy(shell,s,PATH_MAX);
337 shell[PATH_MAX-1] = 0;
338
339 return(shell);
340 }
341
342 int
343 md_shellescape()
344 {
345 #if (!defined(_WIN32) && !defined(__DJGPP__))
346 int ret_status;
347 int pid;
348 void (*myquit)(int);
349 void (*myend)(int);
350 #endif
351 char *sh;
352
353 sh = md_getshell();
354
355 #if defined(_WIN32)
356 return((int)_spawnl(_P_WAIT,sh,"shell",NULL,0));
357 #elif defined(__DJGPP__)
358 return ( spawnl(P_WAIT,sh,"shell",NULL,0) );
359 #else
360 while((pid = fork()) < 0)
361 sleep(1);
362
363 if (pid == 0) /* Shell Process */
364 {
365 /*
366 * Set back to original user, just in case
367 */
368 setuid(getuid());
369 setgid(getgid());
370 execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", 0);
371 perror("No shelly");
372 _exit(-1);
373 }
374 else /* Application */
375 {
376 myend = signal(SIGINT, SIG_IGN);
377 #ifdef SIGQUIT
378 myquit = signal(SIGQUIT, SIG_IGN);
379 #endif
380 while (wait(&ret_status) != pid)
381 continue;
382
383 signal(SIGINT, myquit);
384 #ifdef SIGQUIT