comparison urogue/mdport.c @ 256:c495a4f288c6

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