Mercurial > hg > early-roguelike
annotate srogue/mdport.c @ 162:600873555ec0
Don't swap signal handlers in md_shellescape().
md_shellescape() sets SIGINT and SIGQUIT to be ignored, storing the
previous handlers, and restores them after the shell exits. But it
mixed up the two handlers.
Since the signals were usually handled by the same function, this fix
doesn't have much effect, but anything that makes signal code less
confusing is a good thing.
author | John "Elwin" Edwards |
---|---|
date | Mon, 08 Jun 2015 10:01:25 -0400 |
parents | e6c8652473fe |
children | 10c273a62228 |
rev | line source |
---|---|
86 | 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 | |
118 | 47 #include <curses.h> |
48 #include "rogue.h" | |
49 | |
50 #if defined(HAVE_SYS_TYPES) | |
86 | 51 #include <sys/types.h> |
118 | 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 | |
86 | 83 |
84 #include <curses.h> /* AIX requires curses.h be included before term.h */ | |
85 | |
118 | 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 | |
86 | 96 #include <ctype.h> |
118 | 97 #include <fcntl.h> |
98 #include <limits.h> | |
99 #include <sys/stat.h> | |
100 #include <signal.h> | |
101 | |
102 #if !defined(PATH_MAX) && defined(_MAX_PATH) | |
103 #define PATH_MAX _MAX_PATH | |
104 #endif | |
105 | |
106 #if !defined(PATH_MAX) && defined(_PATH_MAX) | |
107 #define PATH_MAX _PATH_MAX | |
108 #endif | |
109 | |
110 #define NOOP(x) (x += 0) | |
111 | |
112 void | |
113 md_init(void) | |
114 { | |
115 #if defined(__INTERIX) | |
116 char *term; | |
117 | |
118 term = getenv("TERM"); | |
119 | |
120 if (term == NULL) | |
121 setenv("TERM","interix"); | |
122 #elif defined(__DJGPP__) | |
123 _fmode = _O_BINARY; | |
124 #elif defined(_WIN32) | |
125 _fmode = _O_BINARY; | |
126 #endif | |
127 | |
128 #if defined(HAVE_ESCDELAY) || defined(NCURSES_VERSION) | |
129 ESCDELAY=64; | |
130 #endif | |
131 | |
132 #if defined(DUMP) | |
133 md_onsignal_default(); | |
134 #else | |
135 md_onsignal_exit(); | |
136 #endif | |
137 } | |
138 | |
139 void | |
140 md_onsignal_default(void) | |
141 { | |
142 #ifdef SIGHUP | |
143 signal(SIGHUP, SIG_DFL); | |
144 #endif | |
145 #ifdef SIGQUIT | |
146 signal(SIGQUIT, SIG_DFL); | |
147 #endif | |
148 #ifdef SIGILL | |
149 signal(SIGILL, SIG_DFL); | |
150 #endif | |
151 #ifdef SIGTRAP | |
152 signal(SIGTRAP, SIG_DFL); | |
153 #endif | |
154 #ifdef SIGIOT | |
155 signal(SIGIOT, SIG_DFL); | |
156 #endif | |
157 #ifdef SIGEMT | |
158 signal(SIGEMT, SIG_DFL); | |
159 #endif | |
160 #ifdef SIGFPE | |
161 signal(SIGFPE, SIG_DFL); | |
162 #endif | |
163 #ifdef SIGBUS | |
164 signal(SIGBUS, SIG_DFL); | |
165 #endif | |
166 #ifdef SIGSEGV | |
167 signal(SIGSEGV, SIG_DFL); | |
168 #endif | |
169 #ifdef SIGSYS | |
170 signal(SIGSYS, SIG_DFL); | |
171 #endif | |
172 #ifdef SIGTERM | |
173 signal(SIGTERM, SIG_DFL); | |
174 #endif | |
175 } | |
176 | |
177 void | |
178 md_onsignal_exit(void) | |
179 { | |
180 #ifdef SIGHUP | |
181 signal(SIGHUP, SIG_DFL); | |
182 #endif | |
183 #ifdef SIGQUIT | |
184 signal(SIGQUIT, exit); | |
185 #endif | |
186 #ifdef SIGILL | |
187 signal(SIGILL, exit); | |
188 #endif | |
189 #ifdef SIGTRAP | |
190 signal(SIGTRAP, exit); | |
191 #endif | |
192 #ifdef SIGIOT | |
193 signal(SIGIOT, exit); | |
194 #endif | |
195 #ifdef SIGEMT | |
196 signal(SIGEMT, exit); | |
197 #endif | |
198 #ifdef SIGFPE | |
199 signal(SIGFPE, exit); | |
200 #endif | |
201 #ifdef SIGBUS | |
202 signal(SIGBUS, exit); | |
203 #endif | |
204 #ifdef SIGSEGV | |
205 signal(SIGSEGV, exit); | |
206 #endif | |
207 #ifdef SIGSYS | |
208 signal(SIGSYS, exit); | |
209 #endif | |
210 #ifdef SIGTERM | |
211 signal(SIGTERM, exit); | |
212 #endif | |
119 | 213 #ifdef SIGINT |
214 signal(SIGINT, exit); | |
215 #endif | |
118 | 216 } |
217 | |
218 extern void auto_save(int sig); | |
219 extern void endit(int sig); | |
220 extern void quit(int sig); | |
221 | |
222 void | |
223 md_onsignal_autosave(void) | |
224 { | |
225 | |
226 #ifdef SIGHUP | |
227 signal(SIGHUP, auto_save); | |
228 #endif | |
229 #ifdef SIGQUIT | |
230 signal(SIGQUIT, endit); | |
231 #endif | |
232 #ifdef SIGILL | |
233 signal(SIGILL, auto_save); | |
234 #endif | |
235 #ifdef SIGTRAP | |
236 signal(SIGTRAP, auto_save); | |
237 #endif | |
238 #ifdef SIGIOT | |
239 signal(SIGIOT, auto_save); | |
240 #endif | |
241 #ifdef SIGEMT | |
242 signal(SIGEMT, auto_save); | |
243 #endif | |
244 #ifdef SIGFPE | |
245 signal(SIGFPE, auto_save); | |
246 #endif | |
247 #ifdef SIGBUS | |
248 signal(SIGBUS, auto_save); | |
249 #endif | |
250 #ifdef SIGSEGV | |
251 signal(SIGSEGV, auto_save); | |
252 #endif | |
253 #ifdef SIGSYS | |
254 signal(SIGSYS, auto_save); | |
255 #endif | |
256 #ifdef SIGTERM | |
257 signal(SIGTERM, auto_save); | |
258 #endif | |
259 #ifdef SIGINT | |
260 signal(SIGINT, quit); | |
261 #endif | |
262 } | |
263 | |
264 int | |
265 md_hasclreol(void) | |
266 { | |
267 #if defined(clr_eol) | |
268 #ifdef NCURSES_VERSION | |
269 if (cur_term == NULL) | |
270 return(0); | |
271 if (cur_term->type.Strings == NULL) | |
272 return(0); | |
273 #endif | |
274 return((clr_eol != NULL) && (*clr_eol != 0)); | |
275 #elif defined(__PDCURSES__) | |
276 return(TRUE); | |
277 #else | |
278 return((CE != NULL) && (*CE != 0)); | |
279 #endif | |
280 } | |
281 | |
282 void | |
283 md_putchar(int c) | |
284 { | |
285 putchar(c); | |
286 } | |
287 | |
288 #ifdef _WIN32 | |
289 static int md_standout_mode = 0; | |
290 #endif | |
291 | |
292 void | |
293 md_raw_standout(void) | |
294 { | |
295 #ifdef _WIN32 | |
296 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
297 HANDLE hStdout; | |
298 WORD fgattr,bgattr; | |
299 | |
300 if (md_standout_mode == 0) | |
301 { | |
302 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
303 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
304 fgattr = (csbiInfo.wAttributes & 0xF); | |
305 bgattr = (csbiInfo.wAttributes & 0xF0); | |
306 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
307 md_standout_mode = 1; | |
308 } | |
309 #elif defined(SO) | |
310 tputs(SO,0,md_putchar); | |
311 fflush(stdout); | |
312 #endif | |
313 } | |
314 | |
315 void | |
316 md_raw_standend(void) | |
317 { | |
318 #ifdef _WIN32 | |
319 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
320 HANDLE hStdout; | |
321 WORD fgattr,bgattr; | |
322 | |
323 if (md_standout_mode == 1) | |
324 { | |
325 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
326 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
327 fgattr = (csbiInfo.wAttributes & 0xF); | |
328 bgattr = (csbiInfo.wAttributes & 0xF0); | |
329 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
330 md_standout_mode = 0; | |
331 } | |
332 #elif defined(SE) | |
333 tputs(SE,0,md_putchar); | |
334 fflush(stdout); | |
335 #endif | |
336 } | |
337 | |
338 int | |
339 md_unlink_open_file(const char *file, FILE *inf) | |
340 { | |
341 #ifdef _WIN32 | |
342 fclose(inf); | |
343 (void) _chmod(file, 0600); | |
344 return( _unlink(file) ); | |
345 #else | |
346 return(unlink(file)); | |
347 #endif | |
348 } | |
349 | |
350 int | |
351 md_unlink(char *file) | |
352 { | |
353 #ifdef _WIN32 | |
354 (void) _chmod(file, 0600); | |
355 return( _unlink(file) ); | |
356 #else | |
357 return(unlink(file)); | |
358 #endif | |
359 } | |
360 | |
361 int | |
362 md_chmod(const char *filename, int mode) | |
363 { | |
364 #ifdef _WIN32 | |
365 return( _chmod(filename, mode) ); | |
366 #else | |
367 return( chmod(filename, mode) ); | |
368 #endif | |
369 } | |
370 | |
371 void | |
372 md_normaluser(void) | |
373 { | |
374 #if defined(HAVE_GETGID) && defined(HAVE_GETUID) | |
375 gid_t realgid = getgid(); | |
376 uid_t realuid = getuid(); | |
377 | |
378 #if defined(HAVE_SETRESGID) | |
379 if (setresgid(-1, realgid, realgid) != 0) { | |
380 #elif defined (HAVE_SETREGID) | |
381 if (setregid(realgid, realgid) != 0) { | |
382 #elif defined (HAVE_SETGID) | |
383 if (setgid(realgid) != 0) { | |
384 #else | |
385 if (0) { | |
386 #endif | |
387 perror("Could not drop setgid privileges. Aborting."); | |
388 exit(1); | |
389 } | |
390 | |
391 #if defined(HAVE_SETRESUID) | |
392 if (setresuid(-1, realuid, realuid) != 0) { | |
393 #elif defined(HAVE_SETREUID) | |
394 if (setreuid(realuid, realuid) != 0) { | |
395 #elif defined(HAVE_SETUID) | |
396 if (setuid(realuid) != 0) { | |
397 #else | |
398 if (0) { | |
399 #endif | |
400 perror("Could not drop setuid privileges. Aborting."); | |
401 exit(1); | |
402 } | |
403 #endif | |
404 } | |
405 | |
406 uid_t | |
407 md_getuid(void) | |
408 { | |
409 #ifdef HAVE_GETUID | |
410 return( getuid() ); | |
411 #else | |
412 return(42); | |
413 #endif | |
414 } | |
415 | |
120
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
416 gid_t |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
417 md_getgid(void) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
418 { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
419 #ifdef HAVE_GETGID |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
420 return( getgid() ); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
421 #else |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
422 return(42); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
423 #endif |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
424 } |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
425 |
118 | 426 pid_t |
427 md_getpid(void) | |
428 { | |
429 #ifdef _WIN32 | |
430 return( _getpid() ); | |
431 #else | |
432 return( getpid() ); | |
433 #endif | |
434 } | |
435 | |
436 char * | |
437 md_getusername(void) | |
438 { | |
439 static char login[80]; | |
440 char *l = NULL; | |
441 | |
442 /* POSIX Shell has priority, then O/S specific methods */ | |
443 if ( (l = getenv("LOGNAME")) != NULL ) | |
444 { | |
445 strncpy(login,l,80); | |
446 login[79] = 0; | |
447 return(login); | |
448 } | |
449 | |
450 #ifdef _WIN32 | |
451 LPSTR mybuffer; | |
452 DWORD size = UNLEN + 1; | |
453 TCHAR buffer[UNLEN + 1]; | |
454 | |
455 mybuffer = buffer; | |
456 GetUserName(mybuffer,&size); | |
457 l = mybuffer; | |
458 #elif defined(HAVE_GETPWUID)&& !defined(__DJGPP__) | |
459 struct passwd *pw; | |
460 | |
461 pw = getpwuid(getuid()); | |
462 | |
463 if (pw != NULL) | |
464 l = pw->pw_name; | |
465 else | |
466 l = NULL; | |
467 #endif | |
468 | |
469 if ((l == NULL) || (*l == '\0')) | |
470 if ( (l = getenv("USERNAME")) == NULL ) | |
471 if ( (l = getenv("LOGNAME")) == NULL ) | |
472 if ( (l = getenv("USER")) == NULL ) | |
473 l = "nobody"; | |
474 | |
475 strncpy(login,l,80); | |
476 login[79] = 0; | |
477 | |
478 return(login); | |
479 } | |
480 | |
481 char * | |
482 md_gethomedir(void) | |
483 { | |
484 static char homedir[PATH_MAX]; | |
485 char *h = NULL; | |
486 size_t len; | |
487 #if defined(_WIN32) | |
488 TCHAR szPath[PATH_MAX]; | |
489 #endif | |
490 #if defined(_WIN32) || defined(DJGPP) | |
491 char slash = '\\'; | |
492 #else | |
493 char slash = '/'; | |
494 struct passwd *pw; | |
495 pw = getpwuid(getuid()); | |
496 | |
497 if (pw != NULL) | |
498 h = pw->pw_dir; | |
499 else | |
500 h = ""; | |
501 | |
502 if (strcmp(h,"/") == 0) | |
503 h = ""; | |
504 #endif | |
505 homedir[0] = 0; | |
506 #ifdef _WIN32 | |
507 if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, szPath))) | |
508 h = szPath; | |
509 #endif | |
510 | |
511 if ( (h == NULL) || (*h == '\0') ) | |
512 { | |
513 if ( (h = getenv("HOME")) == NULL ) | |
514 { | |
515 if ( (h = getenv("HOMEDRIVE")) == NULL) | |
516 h = ""; | |
517 else | |
518 { | |
519 strncpy(homedir,h,PATH_MAX-1); | |
520 homedir[PATH_MAX-1] = 0; | |
521 | |
522 if ( (h = getenv("HOMEPATH")) == NULL) | |
523 h = ""; | |
524 } | |
525 } | |
526 } | |
527 | |
528 | |
529 len = strlen(homedir); | |
530 strncat(homedir,h,PATH_MAX-len-1); | |
531 len = strlen(homedir); | |
532 | |
533 if ((len > 0) && (homedir[len-1] != slash)) { | |
534 homedir[len] = slash; | |
535 homedir[len+1] = 0; | |
536 } | |
537 | |
538 return(homedir); | |
539 } | |
540 | |
541 void | |
542 md_sleep(int s) | |
543 { | |
544 #ifdef _WIN32 | |
545 Sleep(s); | |
546 #else | |
547 sleep(s); | |
548 #endif | |
549 } | |
550 | |
551 char * | |
552 md_getshell(void) | |
553 { | |
554 static char shell[PATH_MAX]; | |
555 char *s = NULL; | |
556 #ifdef _WIN32 | |
557 char *def = "C:\\WINDOWS\\SYSTEM32\\CMD.EXE"; | |
558 #elif defined(__DJGPP__) | |
559 char *def = "C:\\COMMAND.COM"; | |
560 #else | |
561 char *def = "/bin/sh"; | |
562 struct passwd *pw; | |
563 pw = getpwuid(getuid()); | |
564 if (pw != NULL) | |
565 s = pw->pw_shell; | |
566 #endif | |
567 if ((s == NULL) || (*s == '\0')) | |
568 if ( (s = getenv("COMSPEC")) == NULL) | |
569 if ( (s = getenv("SHELL")) == NULL) | |
570 if ( (s = getenv("SystemRoot")) == NULL) | |
571 s = def; | |
572 | |
573 strncpy(shell,s,PATH_MAX); | |
574 shell[PATH_MAX-1] = 0; | |
575 | |
576 return(shell); | |
577 } | |
578 | |
579 int | |
580 md_shellescape(void) | |
581 { | |
582 #if defined(HAVE_WORKING_FORK) | |
583 int ret_status; | |
584 int pid; | |
585 void (*myquit)(int); | |
586 void (*myend)(int); | |
587 char *sh; | |
588 | |
589 sh = md_getshell(); | |
590 | |
591 while((pid = fork()) < 0) | |
592 sleep(1); | |
593 | |
594 if (pid == 0) /* Shell Process */ | |
595 { | |
596 /* | |
597 * Set back to original user, just in case | |
598 */ | |
599 md_normaluser(); | |
600 execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", NULL); | |
601 perror("No shelly"); | |
602 _exit(-1); | |
603 } | |
604 else /* Application */ | |
605 { | |
606 myend = signal(SIGINT, SIG_IGN); | |
607 #ifdef SIGQUIT | |
608 myquit = signal(SIGQUIT, SIG_IGN); | |
609 #endif | |
610 while (wait(&ret_status) != pid) | |
611 continue; | |
612 | |
162
600873555ec0
Don't swap signal handlers in md_shellescape().
John "Elwin" Edwards
parents:
121
diff
changeset
|
613 signal(SIGINT, myend); |
118 | 614 #ifdef SIGQUIT |
162
600873555ec0
Don't swap signal handlers in md_shellescape().
John "Elwin" Edwards
parents:
121
diff
changeset
|
615 signal(SIGQUIT, myquit); |
118 | 616 #endif |
617 } | |
618 return(ret_status); | |
619 #elif defined(HAVE__SPAWNL) | |
620 return((int)_spawnl(_P_WAIT,md_getshell(),"shell",NULL,0)); | |
621 #elif defined(HAVE_SPAWNL) | |
622 return ( spawnl(P_WAIT,md_getshell(),"shell",NULL,0) ); | |
623 #else | |
624 return(0); | |
625 #endif | |
626 } | |
627 | |
628 char * | |
629 md_getrealname(uid_t uid) | |
630 { | |
631 static char uidstr[20]; | |
632 #if !defined(_WIN32) && !defined(DJGPP) | |
633 struct passwd *pp; | |
634 | |
635 if ((pp = getpwuid(uid)) == NULL) | |
636 { | |
637 sprintf(uidstr,"%d", uid); | |
638 return(uidstr); | |
639 } | |
640 else | |
641 return(pp->pw_name); | |
642 #else | |
643 sprintf(uidstr,"%ld", uid); | |
644 return(uidstr); | |
645 #endif | |
646 } | |
647 | |
648 char * | |
649 md_crypt(const char *key, const char *salt) | |
650 { | |
651 return( xcrypt(key,salt) ); | |
652 } | |
653 | |
654 char * | |
655 md_getpass(char *prompt) | |
656 { | |
657 #ifndef HAVE_GETPASS | |
658 static char password_buffer[9]; | |
659 char *p = password_buffer; | |
660 int c, count = 0; | |
661 int max_length = 9; | |
662 | |
663 fflush(stdout); | |
664 /* If we can't prompt, abort */ | |
665 if (fputs(prompt, stderr) < 0) | |
666 { | |
667 *p = '\0'; | |
668 return NULL; | |
669 } | |
670 | |
671 for(;;) | |
672 { | |
673 /* Get a character with no echo */ | |
674 c = _getch(); | |
675 | |
676 /* Exit on interrupt (^c or ^break) */ | |
677 if (c == '\003' || c == 0x100) | |
678 exit(1); | |
679 | |
680 /* Terminate on end of line or file (^j, ^m, ^d, ^z) */ | |
681 if (c == '\r' || c == '\n' || c == '\004' || c == '\032') | |
682 break; | |
683 | |
684 /* Back up on backspace */ | |
685 if (c == '\b') | |
686 { | |
687 if (count) | |
688 count--; | |
689 else if (p > password_buffer) | |
690 p--; | |
691 continue; | |
692 } | |
693 | |
694 /* Ignore DOS extended characters */ | |
695 if ((c & 0xff) != c) | |
696 continue; | |
697 | |
698 /* Add to password if it isn't full */ | |
699 if (p < password_buffer + max_length - 1) | |
700 *p++ = (char) c; | |
701 else | |
702 count++; | |
703 } | |
704 *p = '\0'; | |
705 | |
706 fputc('\n', stderr); | |
707 | |
708 return password_buffer; | |
709 #else | |
710 return( getpass(prompt) ); | |
711 #endif | |
712 } | |
713 | |
714 int | |
715 md_erasechar(void) | |
716 { | |
717 #ifdef HAVE_ERASECHAR | |
718 return( erasechar() ); /* process erase character */ | |
719 #elif defined(VERASE) | |
720 return(_tty.c_cc[VERASE]); /* process erase character */ | |
721 #else | |
722 return(_tty.sg_erase); /* process erase character */ | |
723 #endif | |
724 } | |
725 | |
726 int | |
727 md_killchar(void) | |
728 { | |
729 #ifdef HAVE_KILLCHAR | |
730 return( killchar() ); | |
731 #elif defined(VKILL) | |
732 return(_tty.c_cc[VKILL]); | |
733 #else | |
734 return(_tty.sg_kill); | |
735 #endif | |
736 } | |
737 | |
738 int | |
739 md_dsuspchar(void) | |
740 { | |
741 #if defined(VDSUSP) /* POSIX has priority */ | |
742 struct termios attr; | |
743 tcgetattr(STDIN_FILENO, &attr); | |
744 return( attr.c_cc[VDSUSP] ); | |
745 #elif defined(TIOCGLTC) | |
746 struct ltchars ltc; | |
747 ioctl(1, TIOCGLTC, <c); | |
748 return(ltc.t_dsuspc); | |
749 #elif defined(_POSIX_VDISABLE) | |
750 return(_POSIX_VDISABLE); | |
751 #else | |
752 return(0); | |
753 #endif | |
754 } | |
755 | |
756 int | |
757 md_setdsuspchar(int c) | |
758 { | |
759 #if defined(VDSUSP) /* POSIX has priority */ | |
760 struct termios attr; | |
761 tcgetattr(STDIN_FILENO, &attr); | |
762 attr.c_cc[VDSUSP] = c; | |
763 tcgetattr(STDIN_FILENO, &attr); | |
764 #elif defined(TIOCSLTC) | |
765 struct ltchars ltc; | |
766 ioctl(1, TIOCGLTC, <c); | |
767 ltc.t_dsuspc = c; | |
768 ioctl(1, TIOCSLTC, <c); | |
769 #else | |
770 NOOP(c); | |
771 #endif | |
772 return(0); | |
773 } | |
774 | |
775 int | |
776 md_suspchar(void) | |
777 { | |
778 #if defined(VSUSP) /* POSIX has priority */ | |
779 struct termios attr; | |
780 tcgetattr(STDIN_FILENO, &attr); | |
781 return( attr.c_cc[VSUSP] ); | |
782 #elif defined(TIOCGLTC) | |
783 struct ltchars ltc; | |
784 ioctl(1, TIOCGLTC, <c); | |
785 return(ltc.t_suspc); | |
786 #elif defined(_POSIX_VDISABLE) | |
787 return(_POSIX_VDISABLE); | |
788 #else | |
789 return(0); | |
790 #endif | |
791 } | |
792 | |
793 int | |
794 md_setsuspchar(int c) | |
795 { | |
796 #if defined(VSUSP) /* POSIX has priority */ | |
797 struct termios attr; | |
798 tcgetattr(STDIN_FILENO, &attr); | |
799 attr.c_cc[VSUSP] = c; | |
800 tcgetattr(STDIN_FILENO, &attr); | |
801 #elif defined(TIOCSLTC) | |
802 struct ltchars ltc; | |
803 ioctl(1, TIOCGLTC, <c); | |
804 ltc.t_suspc = c; | |
805 ioctl(1, TIOCSLTC, <c); | |
806 #else | |
807 NOOP(c); | |
808 #endif | |
809 | |
810 return(0); | |
811 } | |
86 | 812 |
813 /* | |
814 Cursor/Keypad Support | |
815 | |
816 Sadly Cursor/Keypad support is less straightforward than it should be. | |
817 | |
818 The various terminal emulators/consoles choose to differentiate the | |
819 cursor and keypad keys (with modifiers) in different ways (if at all!). | |
820 Furthermore they use different code set sequences for each key only | |
821 a subset of which the various curses libraries recognize. Partly due | |
822 to incomplete termcap/terminfo entries and partly due to inherent | |
823 limitations of those terminal capability databases. | |
824 | |
825 I give curses first crack at decoding the sequences. If it fails to decode | |
826 it we check for common ESC-prefixed sequences. | |
827 | |
828 All cursor/keypad results are translated into standard rogue movement | |
829 commands. | |
830 | |
831 Unmodified keys are translated to walk commands: hjklyubn | |
832 Modified (shift,control,alt) are translated to run commands: HJKLYUBN | |
833 | |
834 Console and supported (differentiated) keys | |
835 Interix: Cursor Keys, Keypad, Ctl-Keypad | |
836 Cygwin: Cursor Keys, Keypad, Alt-Cursor Keys | |
837 MSYS: Cursor Keys, Keypad, Ctl-Cursor Keys, Ctl-Keypad | |
838 Win32: Cursor Keys, Keypad, Ctl/Shift/Alt-Cursor Keys, Ctl/Alt-Keypad | |
839 DJGPP: Cursor Keys, Keypad, Ctl/Shift/Alt-Cursor Keys, Ctl/Alt-Keypad | |
840 | |
841 Interix Console (raw, ncurses) | |
842 ============================== | |
843 normal shift ctrl alt | |
844 ESC [D, ESC F^, ESC [D, ESC [D /# Left #/ | |
845 ESC [C, ESC F$, ESC [C, ESC [C /# Right #/ | |
846 ESC [A, ESC F-, local win, ESC [A /# Up #/ | |
847 ESC [B, ESC F+, local win, ESC [B /# Down #/ | |
848 ESC [H, ESC [H, ESC [H, ESC [H /# Home #/ | |
849 ESC [S, local win, ESC [S, ESC [S /# Page Up #/ | |
850 ESC [T, local win, ESC [T, ESC [T /# Page Down #/ | |
851 ESC [U, ESC [U, ESC [U, ESC [U /# End #/ | |
852 ESC [D, ESC F^, ESC [D, O /# Keypad Left #/ | |
853 ESC [C, ESC F$, ESC [C, O /# Keypad Right #/ | |
854 ESC [A, ESC [A, ESC [-1, O /# Keypad Up #/ | |
855 ESC [B, ESC [B, ESC [-2, O /# Keypad Down #/ | |
856 ESC [H, ESC [H, ESC [-263, O /# Keypad Home #/ | |
857 ESC [S, ESC [S, ESC [-19, O /# Keypad PgUp #/ | |
858 ESC [T, ESC [T, ESC [-20, O /# Keypad PgDn #/ | |
859 ESC [U, ESC [U, ESC [-21, O /# Keypad End #/ | |
860 nothing, nothing, nothing, O /# Kaypad 5 #/ | |
861 | |
862 Interix Console (term=interix, ncurses) | |
863 ============================== | |
864 KEY_LEFT, ESC F^, KEY_LEFT, KEY_LEFT /# Left #/ | |
865 KEY_RIGHT, ESC F$, KEY_RIGHT, KEY_RIGHT /# Right #/ | |
866 KEY_UP, 0x146, local win, KEY_UP /# Up #/ | |
867 KEY_DOWN, 0x145, local win, KEY_DOWN /# Down #/ | |
868 ESC [H, ESC [H, ESC [H, ESC [H /# Home #/ | |
869 KEY_PPAGE, local win, KEY_PPAGE, KEY_PPAGE /# Page Up #/ | |
870 KEY_NPAGE, local win, KEY_NPAGE, KEY_NPAGE /# Page Down #/ | |
871 KEY_LL, KEY_LL, KEY_LL, KEY_LL /# End #/ | |
872 KEY_LEFT, ESC F^, ESC [-4, O /# Keypad Left #/ | |
873 KEY_RIGHT, ESC F$, ESC [-3, O /# Keypad Right #/ | |
874 KEY_UP, KEY_UP, ESC [-1, O /# Keypad Up #/ | |
875 KEY_DOWN, KEY_DOWN, ESC [-2, O /# Keypad Down #/ | |
876 ESC [H, ESC [H, ESC [-263, O /# Keypad Home #/ | |
877 KEY_PPAGE, KEY_PPAGE, ESC [-19, O /# Keypad PgUp #/ | |
878 KEY_NPAGE, KEY_NPAGE, ESC [-20, O /# Keypad PgDn #/ | |
879 KEY_LL, KEY_LL, ESC [-21, O /# Keypad End #/ | |
880 nothing, nothing, nothing, O /# Keypad 5 #/ | |
881 | |
882 Cygwin Console (raw, ncurses) | |
883 ============================== | |
884 normal shift ctrl alt | |
885 ESC [D, ESC [D, ESC [D, ESC ESC [D /# Left #/ | |
886 ESC [C, ESC [C, ESC [C, ESC ESC [C /# Rght #/ | |
887 ESC [A, ESC [A, ESC [A, ESC ESC [A /# Up #/ | |
888 ESC [B, ESC [B, ESC [B, ESC ESC [B /# Down #/ | |
889 ESC [1~, ESC [1~, ESC [1~, ESC ESC [1~ /# Home #/ | |
890 ESC [5~, ESC [5~, ESC [5~, ESC ESC [5~ /# Page Up #/ | |
891 ESC [6~, ESC [6~, ESC [6~, ESC ESC [6~ /# Page Down #/ | |
892 ESC [4~, ESC [4~, ESC [4~, ESC ESC [4~ /# End #/ | |
893 ESC [D, ESC [D, ESC [D, ESC ESC [D,O /# Keypad Left #/ | |
894 ESC [C, ESC [C, ESC [C, ESC ESC [C,O /# Keypad Right #/ | |
895 ESC [A, ESC [A, ESC [A, ESC ESC [A,O /# Keypad Up #/ | |
896 ESC [B, ESC [B, ESC [B, ESC ESC [B,O /# Keypad Down #/ | |
897 ESC [1~, ESC [1~, ESC [1~, ESC ESC [1~,O /# Keypad Home #/ | |
898 ESC [5~, ESC [5~, ESC [5~, ESC ESC [5~,O /# Keypad PgUp #/ | |
899 ESC [6~, ESC [6~, ESC [6~, ESC ESC [6~,O /# Keypad PgDn #/ | |
900 ESC [4~, ESC [4~, ESC [4~, ESC ESC [4~,O /# Keypad End #/ | |
901 ESC [-71, nothing, nothing, O /# Keypad 5 #/ | |
902 | |
903 Cygwin Console (term=cygwin, ncurses) | |
904 ============================== | |
905 KEY_LEFT, KEY_LEFT, KEY_LEFT, ESC-260 /# Left #/ | |
906 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, ESC-261 /# Rght #/ | |
907 KEY_UP, KEY_UP, KEY_UP, ESC-259 /# Up #/ | |
908 KEY_DOWN, KEY_DOWN, KEY_DOWN, ESC-258 /# Down #/ | |
909 KEY_HOME, KEY_HOME, KEY_HOME, ESC-262 /# Home #/ | |
910 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, ESC-339 /# Page Up #/ | |
911 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, ESC-338 /# Page Down #/ | |
912 KEY_END, KEY_END, KEY_END, ESC-360 /# End #/ | |
913 KEY_LEFT, KEY_LEFT, KEY_LEFT, ESC-260,O /# Keypad Left #/ | |
914 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, ESC-261,O /# Keypad Right #/ | |
915 KEY_UP, KEY_UP, KEY_UP, ESC-259,O /# Keypad Up #/ | |
916 KEY_DOWN, KEY_DOWN, KEY_DOWN, ESC-258,O /# Keypad Down #/ | |
917 KEY_HOME, KEY_HOME, KEY_HOME, ESC-262,O /# Keypad Home #/ | |
918 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, ESC-339,O /# Keypad PgUp #/ | |
919 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, ESC-338,O /# Keypad PgDn #/ | |
920 KEY_END, KEY_END, KEY_END, ESC-360,O /# Keypad End #/ | |
921 ESC [G, nothing, nothing, O /# Keypad 5 #/ | |
922 | |
923 MSYS Console (raw, ncurses) | |
924 ============================== | |
925 normal shift ctrl alt | |
926 ESC OD, ESC [d, ESC Od nothing /# Left #/ | |
927 ESC OE, ESC [e, ESC Oe, nothing /# Right #/ | |
928 ESC OA, ESC [a, ESC Oa, nothing /# Up #/ | |
929 ESC OB, ESC [b, ESC Ob, nothing /# Down #/ | |
930 ESC [7~, ESC [7$, ESC [7^, nothing /# Home #/ | |
931 ESC [5~, local window, ESC [5^, nothing /# Page Up #/ | |
932 ESC [6~, local window, ESC [6^, nothing /# Page Down #/ | |
933 ESC [8~, ESC [8$, ESC [8^, nothing /# End #/ | |
934 ESC OD, ESC [d, ESC Od O /# Keypad Left #/ | |
935 ESC OE, ESC [c, ESC Oc, O /# Keypad Right #/ | |
936 ESC OA, ESC [a, ESC Oa, O /# Keypad Up #/ | |
937 ESC OB, ESC [b, ESC Ob, O /# Keypad Down #/ | |
938 ESC [7~, ESC [7$, ESC [7^, O /# Keypad Home #/ | |
939 ESC [5~, local window, ESC [5^, O /# Keypad PgUp #/ | |
940 ESC [6~, local window, ESC [6^, O /# Keypad PgDn #/ | |
941 ESC [8~, ESC [8$, ESC [8^, O /# Keypad End #/ | |
942 11, 11, 11, O /# Keypad 5 #/ | |
943 | |
944 MSYS Console (term=rxvt, ncurses) | |
945 ============================== | |
946 normal shift ctrl alt | |
947 KEY_LEFT, KEY_SLEFT, 514 nothing /# Left #/ | |
948 KEY_RIGHT, KEY_SRIGHT, 516, nothing /# Right #/ | |
949 KEY_UP, 518, 519, nothing /# Up #/ | |
950 KEY_DOWN, 511, 512, nothing /# Down #/ | |
951 KEY_HOME, KEY_SHOME, ESC [7^, nothing /# Home #/ | |
952 KEY_PPAGE, local window, ESC [5^, nothing /# Page Up #/ | |
953 KEY_NPAGE, local window, ESC [6^, nothing /# Page Down #/ | |
954 KEY_END, KEY_SEND, KEY_EOL, nothing /# End #/ | |
955 KEY_LEFT, KEY_SLEFT, 514 O /# Keypad Left #/ | |
956 KEY_RIGHT, KEY_SRIGHT, 516, O /# Keypad Right #/ | |
957 KEY_UP, 518, 519, O /# Keypad Up #/ | |
958 KEY_DOWN, 511, 512, O /# Keypad Down #/ | |
959 KEY_HOME, KEY_SHOME, ESC [7^, O /# Keypad Home #/ | |
960 KEY_PPAGE, local window, ESC [5^, O /# Keypad PgUp #/ | |
961 KEY_NPAGE, local window, ESC [6^, O /# Keypad PgDn #/ | |
962 KEY_END, KEY_SEND, KEY_EOL, O /# Keypad End #/ | |
963 11, 11, 11, O /# Keypad 5 #/ | |
964 | |
965 Win32 Console (raw, pdcurses) | |
966 DJGPP Console (raw, pdcurses) | |
967 ============================== | |
968 normal shift ctrl alt | |
969 260, 391, 443, 493 /# Left #/ | |
970 261, 400, 444, 492 /# Right #/ | |
971 259, 547, 480, 490 /# Up #/ | |
972 258, 548, 481, 491 /# Down #/ | |
973 262, 388, 447, 524 /# Home #/ | |
974 339, 396, 445, 526 /# Page Up #/ | |
975 338, 394, 446, 520 /# Page Down #/ | |
976 358, 384, 448, 518 /# End #/ | |
977 452, 52('4'), 511, 521 /# Keypad Left #/ | |
978 454, 54('6'), 513, 523 /# Keypad Right #/ | |
979 450, 56('8'), 515, 525 /# Keypad Up #/ | |
980 456, 50('2'), 509, 519 /# Keypad Down #/ | |
981 449, 55('7'), 514, 524 /# Keypad Home #/ | |
982 451, 57('9'), 516, 526 /# Keypad PgUp #/ | |
983 457, 51('3'), 510, 520 /# Keypad PgDn #/ | |
984 455, 49('1'), 508, 518 /# Keypad End #/ | |
985 453, 53('5'), 512, 522 /# Keypad 5 #/ | |
986 | |
987 Win32 Console (pdcurses, MSVC/MingW32) | |
988 DJGPP Console (pdcurses) | |
989 ============================== | |
990 normal shift ctrl alt | |
991 KEY_LEFT, KEY_SLEFT, CTL_LEFT, ALT_LEFT /# Left #/ | |
992 KEY_RIGHT, KEY_SRIGHT, CTL_RIGHT, ALT_RIGHT /# Right #/ | |
993 KEY_UP, KEY_SUP, CTL_UP, ALT_UP /# Up #/ | |
994 KEY_DOWN, KEY_SDOWN, CTL_DOWN, ALT_DOWN /# Down #/ | |
995 KEY_HOME, KEY_SHOME, CTL_HOME, ALT_HOME /# Home #/ | |
996 KEY_PPAGE, KEY_SPREVIOUS, CTL_PGUP, ALT_PGUP /# Page Up #/ | |
997 KEY_NPAGE, KEY_SNEXTE, CTL_PGDN, ALT_PGDN /# Page Down #/ | |
998 KEY_END, KEY_SEND, CTL_END, ALT_END /# End #/ | |
999 KEY_B1, 52('4'), CTL_PAD4, ALT_PAD4 /# Keypad Left #/ | |
1000 KEY_B3, 54('6'), CTL_PAD6, ALT_PAD6 /# Keypad Right #/ | |
1001 KEY_A2, 56('8'), CTL_PAD8, ALT_PAD8 /# Keypad Up #/ | |
1002 KEY_C2, 50('2'), CTL_PAD2, ALT_PAD2 /# Keypad Down #/ | |
1003 KEY_A1, 55('7'), CTL_PAD7, ALT_PAD7 /# Keypad Home #/ | |
1004 KEY_A3, 57('9'), CTL_PAD9, ALT_PAD9 /# Keypad PgUp #/ | |
1005 KEY_C3, 51('3'), CTL_PAD3, ALT_PAD3 /# Keypad PgDn #/ | |
1006 KEY_C1, 49('1'), CTL_PAD1, ALT_PAD1 /# Keypad End #/ | |
1007 KEY_B2, 53('5'), CTL_PAD5, ALT_PAD5 /# Keypad 5 #/ | |
1008 | |
1009 Windows Telnet (raw) | |
1010 ============================== | |
1011 normal shift ctrl alt | |
1012 ESC [D, ESC [D, ESC [D, ESC [D /# Left #/ | |
1013 ESC [C, ESC [C, ESC [C, ESC [C /# Right #/ | |
1014 ESC [A, ESC [A, ESC [A, ESC [A /# Up #/ | |
1015 ESC [B, ESC [B, ESC [B, ESC [B /# Down #/ | |
1016 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Home #/ | |
1017 ESC [5~, ESC [5~, ESC [5~, ESC [5~ /# Page Up #/ | |
1018 ESC [6~, ESC [6~, ESC [6~, ESC [6~ /# Page Down #/ | |
1019 ESC [4~, ESC [4~, ESC [4~, ESC [4~ /# End #/ | |
1020 ESC [D, ESC [D, ESC [D, ESC [D /# Keypad Left #/ | |
1021 ESC [C, ESC [C, ESC [C, ESC [C /# Keypad Right #/ | |
1022 ESC [A, ESC [A, ESC [A, ESC [A /# Keypad Up #/ | |
1023 ESC [B, ESC [B, ESC [B, ESC [B /# Keypad Down #/ | |
1024 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Keypad Home #/ | |
1025 ESC [5~, ESC [5~, ESC [5~, ESC [5~ /# Keypad PgUp #/ | |
1026 ESC [6~, ESC [6~, ESC [6~, ESC [6~ /# Keypad PgDn #/ | |
1027 ESC [4~, ESC [4~, ESC [4~, ESC [4~ /# Keypad End #/ | |
1028 nothing, nothing, nothing, nothing /# Keypad 5 #/ | |
1029 | |
1030 Windows Telnet (term=xterm) | |
1031 ============================== | |
1032 normal shift ctrl alt | |
1033 KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT /# Left #/ | |
1034 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, KEY_RIGHT /# Right #/ | |
1035 KEY_UP, KEY_UP, KEY_UP, KEY_UP /# Up #/ | |
1036 KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN /# Down #/ | |
1037 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Home #/ | |
1038 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, KEY_PPAGE /# Page Up #/ | |
1039 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, KEY_NPAGE /# Page Down #/ | |
1040 ESC [4~, ESC [4~, ESC [4~, ESC [4~ /# End #/ | |
1041 KEY_LEFT, KEY_LEFT, KEY_LEFT, O /# Keypad Left #/ | |
1042 KEY_RIGHT, KEY_RIGHT, KEY_RIGHT, O /# Keypad Right #/ | |
1043 KEY_UP, KEY_UP, KEY_UP, O /# Keypad Up #/ | |
1044 KEY_DOWN, KEY_DOWN, KEY_DOWN, O /# Keypad Down #/ | |
1045 ESC [1~, ESC [1~, ESC [1~, ESC [1~ /# Keypad Home #/ | |
1046 KEY_PPAGE, KEY_PPAGE, KEY_PPAGE, KEY_PPAGE /# Keypad PgUp #/ | |
1047 KEY_NPAGE, KEY_NPAGE, KEY_NPAGE, KEY_NPAGE /# Keypad PgDn #/ | |
1048 ESC [4~, ESC [4~, ESC [4~, O /# Keypad End #/ | |
1049 ESC [-71, nothing, nothing, O /# Keypad 5 #/ | |
1050 | |
1051 PuTTY | |
1052 ============================== | |
1053 normal shift ctrl alt | |
1054 ESC [D, ESC [D, ESC OD, ESC [D /# Left #/ | |
1055 ESC [C, ESC [C, ESC OC, ESC [C /# Right #/ | |
1056 ESC [A, ESC [A, ESC OA, ESC [A /# Up #/ | |
1057 ESC [B, ESC [B, ESC OB, ESC [B /# Down #/ | |
1058 ESC [1~, ESC [1~, local win, ESC [1~ /# Home #/ | |
1059 ESC [5~, local win, local win, ESC [5~ /# Page Up #/ | |
1060 ESC [6~, local win, local win, ESC [6~ /# Page Down #/ | |
1061 ESC [4~, ESC [4~, local win, ESC [4~ /# End #/ | |
1062 ESC [D, ESC [D, ESC [D, O /# Keypad Left #/ | |
1063 ESC [C, ESC [C, ESC [C, O /# Keypad Right #/ | |
1064 ESC [A, ESC [A, ESC [A, O /# Keypad Up #/ | |
1065 ESC [B, ESC [B, ESC [B, O /# Keypad Down #/ | |
1066 ESC [1~, ESC [1~, ESC [1~, O /# Keypad Home #/ | |
1067 ESC [5~, ESC [5~, ESC [5~, O /# Keypad PgUp #/ | |
1068 ESC [6~, ESC [6~, ESC [6~, O /# Keypad PgDn #/ | |
1069 ESC [4~, ESC [4~, ESC [4~, O /# Keypad End #/ | |
1070 nothing, nothing, nothing, O /# Keypad 5 #/ | |
1071 | |
1072 PuTTY | |
1073 ============================== | |
1074 normal shift ctrl alt | |
1075 KEY_LEFT, KEY_LEFT, ESC OD, ESC KEY_LEFT /# Left #/ | |
1076 KEY_RIGHT KEY_RIGHT, ESC OC, ESC KEY_RIGHT /# Right #/ | |
1077 KEY_UP, KEY_UP, ESC OA, ESC KEY_UP /# Up #/ | |
1078 KEY_DOWN, KEY_DOWN, ESC OB, ESC KEY_DOWN /# Down #/ | |
1079 ESC [1~, ESC [1~, local win, ESC ESC [1~ /# Home #/ | |
1080 KEY_PPAGE local win, local win, ESC KEY_PPAGE /# Page Up #/ | |
1081 KEY_NPAGE local win, local win, ESC KEY_NPAGE /# Page Down #/ | |
1082 ESC [4~, ESC [4~, local win, ESC ESC [4~ /# End #/ | |
1083 ESC Ot, ESC Ot, ESC Ot, O /# Keypad Left #/ | |
1084 ESC Ov, ESC Ov, ESC Ov, O /# Keypad Right #/ | |
1085 ESC Ox, ESC Ox, ESC Ox, O /# Keypad Up #/ | |
1086 ESC Or, ESC Or, ESC Or, O /# Keypad Down #/ | |
1087 ESC Ow, ESC Ow, ESC Ow, O /# Keypad Home #/ | |
1088 ESC Oy, ESC Oy, ESC Oy, O /# Keypad PgUp #/ | |
1089 ESC Os, ESC Os, ESC Os, O /# Keypad PgDn #/ | |
1090 ESC Oq, ESC Oq, ESC Oq, O /# Keypad End #/ | |
1091 ESC Ou, ESC Ou, ESC Ou, O /# Keypad 5 #/ | |
1092 */ | |
1093 | |
1094 #define M_NORMAL 0 | |
1095 #define M_ESC 1 | |
1096 #define M_KEYPAD 2 | |
1097 #define M_TRAIL 3 | |
1098 | |
1099 int undo[5]; | |
1100 int uindex = -1; | |
1101 | |
1102 int | |
1103 reread() | |
1104 { | |
1105 int redo; | |
1106 | |
1107 if (uindex < 0) | |
1108 return 0; | |
1109 | |
1110 redo = undo[0]; | |
1111 undo[0] = undo[1]; | |
1112 undo[1] = undo[2]; | |
1113 undo[2] = undo[3]; | |
1114 undo[3] = undo[4]; | |
1115 uindex--; | |
1116 return redo; | |
1117 } | |
1118 | |
1119 void | |
1120 unread(int c) | |
1121 { | |
1122 if (uindex >= 4) | |
1123 abort(); | |
1124 | |
1125 undo[++uindex] = c; | |
1126 } | |
1127 | |
1128 int | |
1129 md_readchar(WINDOW *win) | |
1130 { | |
1131 int ch = 0; | |
1132 int lastch = 0; | |
1133 int mode = M_NORMAL; | |
1134 int mode2 = M_NORMAL; | |
1135 int nodelayf = 0; | |
1136 int count = 0; | |
1137 | |
1138 for(;;) | |
1139 { | |
1140 if (mode == M_NORMAL && uindex >= 0) | |
1141 { | |
1142 ch = reread(); | |
1143 break; | |
1144 } | |
1145 | |
1146 ch = wgetch(win); | |
1147 | |
1148 if (ch == ERR) /* timed out or error */ | |
1149 { | |
1150 if (nodelayf) /* likely timed out, switch to */ | |
1151 { /* normal mode and block on */ | |
1152 mode = M_NORMAL; /* next read */ | |
1153 nodelayf = 0; | |
1154 nodelay(win,0); | |
1155 } | |
1156 else if (count > 10) /* after 10 errors assume */ | |
1157 auto_save(0); /* input stream is broken and */ | |
1158 else /* auto save and exit */ | |
1159 count++; | |
1160 | |
1161 continue; | |
1162 } | |
1163 | |
1164 count = 0; /* reset input error count */ | |
1165 | |
1166 if (mode == M_TRAIL) | |
1167 { | |
1168 if (ch == '^') /* msys console : 7,5,6,8: modified*/ | |
1169 ch = CTRL( toupper(lastch) ); | |
1170 else if (ch == '~') /* cygwin console: 1,5,6,4: normal */ | |
1171 ch = tolower(lastch); /* windows telnet: 1,5,6,4: normal */ | |
1172 /* msys console : 7,5,6,8: normal */ | |
1173 else if (mode2 == M_ESC) /* cygwin console: 1,5,6,4: modified*/ | |
1174 ch = CTRL( toupper(ch) ); | |
1175 else | |
1176 { | |
1177 mode = M_NORMAL; | |
1178 unread(ch); | |
1179 continue; | |
1180 } | |
1181 | |
1182 break; | |
1183 } | |
1184 | |
1185 if (mode == M_ESC) | |
1186 { | |
1187 if (ch == 27) | |
1188 { | |
1189 mode2 = M_ESC; | |
1190 unread(ch); | |
1191 continue; | |
1192 } | |
1193 | |
1194 if ((ch == 'F') || (ch == 'O') || (ch == '[')) | |
1195 { | |
1196 mode = M_KEYPAD; | |
1197 unread(ch); | |
1198 continue; | |
1199 } | |
1200 | |
1201 | |
1202 switch(ch) | |
1203 { | |
1204 /* Cygwin Console */ | |
1205 /* PuTTY */ | |
1206 case KEY_LEFT : ch = CTRL('H'); break; | |
1207 case KEY_RIGHT: ch = CTRL('L'); break; | |
1208 case KEY_UP : ch = CTRL('K'); break; | |
1209 case KEY_DOWN : ch = CTRL('J'); break; | |
1210 case KEY_HOME : ch = CTRL('Y'); break; | |
1211 case KEY_PPAGE: ch = CTRL('U'); break; | |
1212 case KEY_NPAGE: ch = CTRL('N'); break; | |
1213 case KEY_END : ch = CTRL('B'); break; | |
1214 | |
1215 default: mode = M_NORMAL; | |
1216 mode2 = M_NORMAL; | |
1217 unread(ch); | |
1218 continue; | |
1219 } | |
1220 | |
1221 break; | |
1222 } | |
1223 | |
1224 if (mode == M_KEYPAD) | |
1225 { | |
1226 switch(ch) | |
1227 { | |
1228 /* ESC F - Interix Console codes */ | |
1229 case '^': ch = CTRL('H'); break; /* Shift-Left */ | |
1230 case '$': ch = CTRL('L'); break; /* Shift-Right */ | |
1231 | |
1232 /* ESC [ - Interix Console codes */ | |
1233 case 'H': ch = 'y'; break; /* Home */ | |
1234 case 1: ch = CTRL('K'); break; /* Ctl-Keypad Up */ | |
1235 case 2: ch = CTRL('J'); break; /* Ctl-Keypad Down */ | |
1236 case 3: ch = CTRL('L'); break; /* Ctl-Keypad Right */ | |
1237 case 4: ch = CTRL('H'); break; /* Ctl-Keypad Left */ | |
1238 case 263: ch = CTRL('Y'); break; /* Ctl-Keypad Home */ | |
1239 case 19: ch = CTRL('U'); break; /* Ctl-Keypad PgUp */ | |
1240 case 20: ch = CTRL('N'); break; /* Ctl-Keypad PgDn */ | |
1241 case 21: ch = CTRL('B'); break; /* Ctl-Keypad End */ | |
1242 | |
1243 /* ESC [ - Cygwin Console codes */ | |
1244 case 'G': ch = '.'; break; /* Keypad 5 */ | |
1245 case '7': lastch = 'Y'; mode=M_TRAIL; break; /* Ctl-Home */ | |
1246 case '5': lastch = 'U'; mode=M_TRAIL; break; /* Ctl-PgUp */ | |
1247 case '6': lastch = 'N'; mode=M_TRAIL; break; /* Ctl-PgDn */ | |
1248 | |
1249 /* ESC [ - Win32 Telnet, PuTTY */ | |
1250 case '1': lastch = 'y'; mode=M_TRAIL; break; /* Home */ | |
1251 case '4': lastch = 'b'; mode=M_TRAIL; break; /* End */ | |
1252 | |
1253 /* ESC [ - not understood by screen/tmux */ | |
1254 case 'E': ch = '.'; break; /* Keypad 5 */ | |
1255 | |
1256 /* ESC O - PuTTY */ | |
1257 case 'D': ch = CTRL('H'); break; | |
1258 case 'C': ch = CTRL('L'); break; | |
1259 case 'A': ch = CTRL('K'); break; | |
1260 case 'B': ch = CTRL('J'); break; | |
1261 case 't': ch = 'h'; break; | |
1262 case 'v': ch = 'l'; break; | |
1263 case 'x': ch = 'k'; break; | |
1264 case 'r': ch = 'j'; break; | |
1265 case 'w': ch = 'y'; break; | |
1266 case 'y': ch = 'u'; break; | |
1267 case 's': ch = 'n'; break; | |
1268 case 'q': ch = 'b'; break; | |
1269 case 'u': ch = '.'; break; | |
1270 } | |
1271 | |
1272 if (mode != M_KEYPAD) | |
1273 { | |
1274 unread(ch); | |
1275 continue; | |
1276 } | |
1277 } | |
1278 | |
1279 if (ch == 27) | |
1280 { | |
1281 nodelay(win,1); | |
1282 mode = M_ESC; | |
1283 nodelayf = 1; | |
1284 unread(ch); | |
1285 continue; | |
1286 } | |
1287 | |
1288 switch(ch) | |
1289 { | |
1290 case KEY_LEFT : ch = 'h'; break; | |
1291 case KEY_DOWN : ch = 'j'; break; | |
1292 case KEY_UP : ch = 'k'; break; | |
1293 case KEY_RIGHT : ch = 'l'; break; | |
1294 case KEY_HOME : ch = 'y'; break; | |
1295 case KEY_PPAGE : ch = 'u'; break; | |
1296 case KEY_END : ch = 'b'; break; | |
1297 #ifdef KEY_LL | |
1298 case KEY_LL : ch = 'b'; break; | |
1299 #endif | |
1300 case KEY_NPAGE : ch = 'n'; break; | |
1301 case KEY_BEG : ch = '.'; break; | |
1302 | |
1303 #ifdef KEY_B1 | |
1304 case KEY_B1 : ch = 'h'; break; | |
1305 case KEY_C2 : ch = 'j'; break; | |
1306 case KEY_A2 : ch = 'k'; break; | |
1307 case KEY_B3 : ch = 'l'; break; | |
1308 #endif | |
1309 case KEY_A1 : ch = 'y'; break; | |
1310 case KEY_A3 : ch = 'u'; break; | |
1311 case KEY_C1 : ch = 'b'; break; | |
1312 case KEY_C3 : ch = 'n'; break; | |
1313 case KEY_B2 : ch = '.'; break; | |
1314 | |
1315 #ifdef KEY_SLEFT | |
1316 case KEY_SRIGHT : ch = CTRL('L'); break; | |
1317 case KEY_SLEFT : ch = CTRL('H'); break; | |
1318 #ifdef KEY_SUP | |
1319 case KEY_SUP : ch = CTRL('K'); break; | |
1320 case KEY_SDOWN : ch = CTRL('J'); break; | |
1321 #endif | |
1322 case KEY_SHOME : ch = CTRL('Y'); break; | |
1323 case KEY_SPREVIOUS:ch = CTRL('U'); break; | |
1324 case KEY_SEND : ch = CTRL('B'); break; | |
1325 case KEY_SNEXT : ch = CTRL('N'); break; | |
1326 #endif | |
1327 case 0x146 : ch = CTRL('K'); break; /* Shift-Up */ | |
1328 case 0x145 : ch = CTRL('J'); break; /* Shift-Down */ | |
1329 | |
1330 | |
1331 #ifdef CTL_RIGHT | |
1332 case CTL_RIGHT : ch = CTRL('L'); break; | |
1333 case CTL_LEFT : ch = CTRL('H'); break; | |
1334 case CTL_UP : ch = CTRL('K'); break; | |
1335 case CTL_DOWN : ch = CTRL('J'); break; | |
1336 case CTL_HOME : ch = CTRL('Y'); break; | |
1337 case CTL_PGUP : ch = CTRL('U'); break; | |
1338 case CTL_END : ch = CTRL('B'); break; | |
1339 case CTL_PGDN : ch = CTRL('N'); break; | |
1340 #endif | |
1341 #ifdef KEY_EOL | |
1342 case KEY_EOL : ch = CTRL('B'); break; | |
1343 #endif | |
1344 | |
1345 #ifndef CTL_PAD1 | |
1346 /* MSYS rxvt console */ | |
1347 case 511 : ch = CTRL('J'); break; /* Shift Dn */ | |
1348 case 512 : ch = CTRL('J'); break; /* Ctl Down */ | |
1349 case 514 : ch = CTRL('H'); break; /* Ctl Left */ | |
1350 case 516 : ch = CTRL('L'); break; /* Ctl Right*/ | |
1351 case 518 : ch = CTRL('K'); break; /* Shift Up */ | |
1352 case 519 : ch = CTRL('K'); break; /* Ctl Up */ | |
1353 #endif | |
1354 | |
1355 #ifdef CTL_PAD1 | |
1356 case CTL_PAD1 : ch = CTRL('B'); break; | |
1357 case CTL_PAD2 : ch = CTRL('J'); break; | |
1358 case CTL_PAD3 : ch = CTRL('N'); break; | |
1359 case CTL_PAD4 : ch = CTRL('H'); break; | |
1360 case CTL_PAD5 : ch = '.'; break; | |
1361 case CTL_PAD6 : ch = CTRL('L'); break; | |
1362 case CTL_PAD7 : ch = CTRL('Y'); break; | |
1363 case CTL_PAD8 : ch = CTRL('K'); break; | |
1364 case CTL_PAD9 : ch = CTRL('U'); break; | |
1365 #endif | |
1366 | |
1367 #ifdef ALT_RIGHT | |
1368 case ALT_RIGHT : ch = CTRL('L'); break; | |
1369 case ALT_LEFT : ch = CTRL('H'); break; | |
1370 case ALT_DOWN : ch = CTRL('J'); break; | |
1371 case ALT_HOME : ch = CTRL('Y'); break; | |
1372 case ALT_PGUP : ch = CTRL('U'); break; | |
1373 case ALT_END : ch = CTRL('B'); break; | |
1374 case ALT_PGDN : ch = CTRL('N'); break; | |
1375 #endif | |
1376 | |
1377 #ifdef ALT_PAD1 | |
1378 case ALT_PAD1 : ch = CTRL('B'); break; | |
1379 case ALT_PAD2 : ch = CTRL('J'); break; | |
1380 case ALT_PAD3 : ch = CTRL('N'); break; | |
1381 case ALT_PAD4 : ch = CTRL('H'); break; | |
1382 case ALT_PAD5 : ch = '.'; break; | |
1383 case ALT_PAD6 : ch = CTRL('L'); break; | |
1384 case ALT_PAD7 : ch = CTRL('Y'); break; | |
1385 case ALT_PAD8 : ch = CTRL('K'); break; | |
1386 case ALT_PAD9 : ch = CTRL('U'); break; | |
1387 #endif | |
118 | 1388 #ifdef KEY_BACKSPACE |
1389 case KEY_BACKSPACE: ch = md_erasechar(); break; | |
86 | 1390 #endif |
1391 } | |
1392 | |
1393 break; | |
1394 } | |
1395 | |
1396 if (nodelayf) | |
1397 nodelay(win,0); | |
1398 | |
1399 uindex = -1; | |
1400 | |
1401 return(ch & 0x7F); | |
1402 } | |
118 | 1403 |
1404 #if defined(LOADAV) && defined(HAVE_NLIST_H) && defined(HAVE_NLIST) | |
1405 /* | |
1406 * loadav: | |
1407 * Looking up load average in core (for system where the loadav() | |
1408 * system call isn't defined | |
1409 */ | |
1410 | |
1411 #include <nlist.h> | |
1412 | |
1413 struct nlist avenrun = { | |
1414 "_avenrun" | |
1415 }; | |
1416 | |
1417 void | |
1418 md_loadav(double *avg) | |
1419 { | |
1420 int kmem; | |
1421 | |
1422 if ((kmem = open("/dev/kmem", 0)) < 0) | |
1423 goto bad; | |
1424 nlist(NAMELIST, &avenrun); | |
1425 if (avenrun.n_type == 0) | |
1426 { | |
1427 close(kmem); | |
1428 bad: | |
1429 avg[0] = 0.0; | |
1430 avg[1] = 0.0; | |
1431 avg[2] = 0.0; | |
1432 return; | |
1433 } | |
1434 | |
1435 lseek(kmem, avenrun.n_value, 0); | |
1436 read(kmem, (char *) avg, 3 * sizeof (double)); | |
1437 close(kmem); | |
1438 } | |
1439 #else | |
1440 void | |
1441 md_loadav(double *avg) | |
1442 { | |
1443 #if defined(HAVE_LOADAV) | |
1444 loadav(avg); | |
1445 #elif defined(HAVE_GETLOADAVG) | |
1446 getloadavg(avg,3); | |
1447 #else | |
1448 avg[0] = avg[1] = avg[2] = 0; | |
1449 #endif | |
1450 } | |
1451 #endif | |
1452 | |
1453 #ifndef NSIG | |
1454 #define NSIG 32 | |
1455 #endif | |
1456 | |
1457 void | |
1458 md_ignoreallsignals(void) | |
1459 { | |
1460 int i; | |
1461 | |
1462 for (i = 0; i < NSIG; i++) | |
1463 signal(i, SIG_IGN); | |
1464 } | |
1465 | |
1466 #if 0 | |
1467 /* tstp() isn't ported until I make sure it really works. */ | |
1468 void | |
1469 md_tstphold(void) | |
1470 { | |
1471 #ifdef SIGTSTP | |
1472 /* | |
1473 * If a process can be suspended, this code wouldn't work | |
1474 */ | |
1475 # ifdef SIG_HOLD | |
1476 signal(SIGTSTP, SIG_HOLD); | |
1477 # else | |
1478 signal(SIGTSTP, SIG_IGN); | |
1479 # endif | |
1480 #endif | |
1481 } | |
1482 | |
1483 void | |
1484 md_tstpresume(void) | |
1485 { | |
1486 #ifdef SIGTSTP | |
1487 signal(SIGTSTP, tstp); | |
1488 #endif | |
1489 } | |
1490 | |
1491 void | |
1492 md_tstpsignal(void) | |
1493 { | |
1494 #ifdef SIGTSTP | |
1495 kill(0, SIGTSTP); /* send actual signal and suspend process */ | |
1496 #endif | |
1497 } | |
1498 #endif | |
1499 | |
1500 #if defined(CHECKTIME) | |
1501 void | |
1502 md_start_checkout_timer(int time) | |
1503 { | |
1504 int checkout(); | |
1505 | |
1506 #if defined(HAVE_ALARM) && defined(SIGALRM) | |
1507 signal(SIGALRM, checkout); | |
1508 alarm(time); | |
1509 #endif | |
1510 } | |
1511 | |
1512 void | |
1513 md_stop_checkout_timer(void) | |
1514 { | |
1515 #if defined(SIGALRM) | |
1516 signal(SIGALRM, SIG_IGN); | |
1517 #endif | |
1518 } | |
1519 | |
1520 #endif | |
1521 | |
120
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1522 long |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1523 md_memused(void) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1524 { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1525 #ifdef _WIN32 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1526 MEMORYSTATUS stat; |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1527 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1528 GlobalMemoryStatus(&stat); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1529 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1530 return((long)stat.dwTotalPageFile); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1531 #else |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1532 return( (long)sbrk(0) ); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1533 #endif |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1534 } |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1535 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1536 void |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1537 md_droppriv(void) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1538 { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1539 #if defined(HAVE_GETUID) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1540 uid_t realuid = getuid(); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1541 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1542 #if defined(HAVE_SETRESUID) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1543 if (setresuid(-1, realuid, realuid) != 0) { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1544 #elif defined (HAVE_SETREUID) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1545 if (setreuid(realuid, realuid) != 0) { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1546 #elif defined (HAVE_SETUID) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1547 if (setuid(realuid) != 0) { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1548 #else |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1549 if (0) { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1550 #endif |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1551 printf("Cannot change to effective uid: %d\n", realuid); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1552 exit(1); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1553 } |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1554 #endif |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1555 } |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1556 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1557 extern uid_t playuid; |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1558 extern gid_t playgid; |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1559 |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1560 void |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1561 md_resetpriv(void) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1562 { |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1563 #if defined (HAVE_SETUID) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1564 setuid(playuid); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1565 #endif |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1566 #if defined (HAVE_SETGID) |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1567 setgid(playgid); |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1568 #endif |
d6b7c3fb37ea
srogue: add and use more md_* portable functions.
John "Elwin" Edwards
parents:
119
diff
changeset
|
1569 } |
121
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1570 |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1571 int |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1572 md_random(void) |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1573 { |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1574 #if defined (HAVE_LRAND48) |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1575 return lrand48(); |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1576 #elif defined (HAVE_RANDOM) |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1577 return random(); |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1578 #else |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1579 return rand(); |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1580 #endif |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1581 } |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1582 |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1583 void |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1584 md_srandom(unsigned int seed) |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1585 { |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1586 #if defined (HAVE_SRAND48) |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1587 srand48(seed); |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1588 #elif defined (HAVE_SRANDOM) |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1589 srandom(seed); |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1590 #else |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1591 srand(seed); |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1592 #endif |
e6c8652473fe
srogue: more compatibility improvements.
John "Elwin" Edwards
parents:
120
diff
changeset
|
1593 } |