Mercurial > hg > early-roguelike
comparison srogue/mdport.c @ 118:8d1dfc5a912c
srogue: add a complete mdport.c.
srogue/mdport.c is copied from rogue5/mdport.c, with slight changes.
| author | John "Elwin" Edwards | 
|---|---|
| date | Sun, 27 Apr 2014 08:29:14 -0700 | 
| parents | ea71ef31d9be | 
| children | 458df24e973d | 
   comparison
  equal
  deleted
  inserted
  replaced
| 117:2c62bd925c17 | 118:8d1dfc5a912c | 
|---|---|
| 27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 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 | 28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
| 29 SUCH DAMAGE. | 29 SUCH DAMAGE. | 
| 30 */ | 30 */ | 
| 31 | 31 | 
| 32 /* This is a temporary stub version of rogue5's mdport.c. It is only to make | |
| 33 * md_readchar() available until srogue is ported to autoconf. Then the | |
| 34 * whole file should work. | |
| 35 */ | |
| 36 | |
| 37 #include <stdlib.h> | 32 #include <stdlib.h> | 
| 38 #include <string.h> | 33 #include <string.h> | 
| 39 | 34 | 
| 40 #if defined(_WIN32) | 35 #if defined(_WIN32) | 
| 41 #include <Windows.h> | 36 #include <Windows.h> | 
| 47 #pragma warning( default: 4201 ) | 42 #pragma warning( default: 4201 ) | 
| 48 #include <Shlwapi.h> | 43 #include <Shlwapi.h> | 
| 49 #undef MOUSE_MOVED | 44 #undef MOUSE_MOVED | 
| 50 #endif | 45 #endif | 
| 51 | 46 | 
| 47 #include <curses.h> | |
| 48 #include "rogue.h" | |
| 49 | |
| 50 #if defined(HAVE_SYS_TYPES) | |
| 52 #include <sys/types.h> | 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 | |
| 53 | 83 | 
| 54 #include <curses.h> /* AIX requires curses.h be included before term.h */ | 84 #include <curses.h> /* AIX requires curses.h be included before term.h */ | 
| 55 | 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 | |
| 56 #include <ctype.h> | 96 #include <ctype.h> | 
| 57 #include "rogue.h" | 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 | |
| 213 } | |
| 214 | |
| 215 extern void auto_save(int sig); | |
| 216 extern void endit(int sig); | |
| 217 extern void quit(int sig); | |
| 218 | |
| 219 void | |
| 220 md_onsignal_autosave(void) | |
| 221 { | |
| 222 | |
| 223 #ifdef SIGHUP | |
| 224 signal(SIGHUP, auto_save); | |
| 225 #endif | |
| 226 #ifdef SIGQUIT | |
| 227 signal(SIGQUIT, endit); | |
| 228 #endif | |
| 229 #ifdef SIGILL | |
| 230 signal(SIGILL, auto_save); | |
| 231 #endif | |
| 232 #ifdef SIGTRAP | |
| 233 signal(SIGTRAP, auto_save); | |
| 234 #endif | |
| 235 #ifdef SIGIOT | |
| 236 signal(SIGIOT, auto_save); | |
| 237 #endif | |
| 238 #ifdef SIGEMT | |
| 239 signal(SIGEMT, auto_save); | |
| 240 #endif | |
| 241 #ifdef SIGFPE | |
| 242 signal(SIGFPE, auto_save); | |
| 243 #endif | |
| 244 #ifdef SIGBUS | |
| 245 signal(SIGBUS, auto_save); | |
| 246 #endif | |
| 247 #ifdef SIGSEGV | |
| 248 signal(SIGSEGV, auto_save); | |
| 249 #endif | |
| 250 #ifdef SIGSYS | |
| 251 signal(SIGSYS, auto_save); | |
| 252 #endif | |
| 253 #ifdef SIGTERM | |
| 254 signal(SIGTERM, auto_save); | |
| 255 #endif | |
| 256 #ifdef SIGINT | |
| 257 signal(SIGINT, quit); | |
| 258 #endif | |
| 259 } | |
| 260 | |
| 261 int | |
| 262 md_hasclreol(void) | |
| 263 { | |
| 264 #if defined(clr_eol) | |
| 265 #ifdef NCURSES_VERSION | |
| 266 if (cur_term == NULL) | |
| 267 return(0); | |
| 268 if (cur_term->type.Strings == NULL) | |
| 269 return(0); | |
| 270 #endif | |
| 271 return((clr_eol != NULL) && (*clr_eol != 0)); | |
| 272 #elif defined(__PDCURSES__) | |
| 273 return(TRUE); | |
| 274 #else | |
| 275 return((CE != NULL) && (*CE != 0)); | |
| 276 #endif | |
| 277 } | |
| 278 | |
| 279 void | |
| 280 md_putchar(int c) | |
| 281 { | |
| 282 putchar(c); | |
| 283 } | |
| 284 | |
| 285 #ifdef _WIN32 | |
| 286 static int md_standout_mode = 0; | |
| 287 #endif | |
| 288 | |
| 289 void | |
| 290 md_raw_standout(void) | |
| 291 { | |
| 292 #ifdef _WIN32 | |
| 293 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
| 294 HANDLE hStdout; | |
| 295 WORD fgattr,bgattr; | |
| 296 | |
| 297 if (md_standout_mode == 0) | |
| 298 { | |
| 299 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
| 300 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
| 301 fgattr = (csbiInfo.wAttributes & 0xF); | |
| 302 bgattr = (csbiInfo.wAttributes & 0xF0); | |
| 303 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
| 304 md_standout_mode = 1; | |
| 305 } | |
| 306 #elif defined(SO) | |
| 307 tputs(SO,0,md_putchar); | |
| 308 fflush(stdout); | |
| 309 #endif | |
| 310 } | |
| 311 | |
| 312 void | |
| 313 md_raw_standend(void) | |
| 314 { | |
| 315 #ifdef _WIN32 | |
| 316 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; | |
| 317 HANDLE hStdout; | |
| 318 WORD fgattr,bgattr; | |
| 319 | |
| 320 if (md_standout_mode == 1) | |
| 321 { | |
| 322 hStdout = GetStdHandle(STD_OUTPUT_HANDLE); | |
| 323 GetConsoleScreenBufferInfo(hStdout, &csbiInfo); | |
| 324 fgattr = (csbiInfo.wAttributes & 0xF); | |
| 325 bgattr = (csbiInfo.wAttributes & 0xF0); | |
| 326 SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); | |
| 327 md_standout_mode = 0; | |
| 328 } | |
| 329 #elif defined(SE) | |
| 330 tputs(SE,0,md_putchar); | |
| 331 fflush(stdout); | |
| 332 #endif | |
| 333 } | |
| 334 | |
| 335 int | |
| 336 md_unlink_open_file(const char *file, FILE *inf) | |
| 337 { | |
| 338 #ifdef _WIN32 | |
| 339 fclose(inf); | |
| 340 (void) _chmod(file, 0600); | |
| 341 return( _unlink(file) ); | |
| 342 #else | |
| 343 return(unlink(file)); | |
| 344 #endif | |
| 345 } | |
| 346 | |
| 347 int | |
| 348 md_unlink(char *file) | |
| 349 { | |
| 350 #ifdef _WIN32 | |
| 351 (void) _chmod(file, 0600); | |
| 352 return( _unlink(file) ); | |
| 353 #else | |
| 354 return(unlink(file)); | |
| 355 #endif | |
| 356 } | |
| 357 | |
| 358 int | |
| 359 md_chmod(const char *filename, int mode) | |
| 360 { | |
| 361 #ifdef _WIN32 | |
| 362 return( _chmod(filename, mode) ); | |
| 363 #else | |
| 364 return( chmod(filename, mode) ); | |
| 365 #endif | |
| 366 } | |
| 367 | |
| 368 void | |
| 369 md_normaluser(void) | |
| 370 { | |
| 371 #if defined(HAVE_GETGID) && defined(HAVE_GETUID) | |
| 372 gid_t realgid = getgid(); | |
| 373 uid_t realuid = getuid(); | |
| 374 | |
| 375 #if defined(HAVE_SETRESGID) | |
| 376 if (setresgid(-1, realgid, realgid) != 0) { | |
| 377 #elif defined (HAVE_SETREGID) | |
| 378 if (setregid(realgid, realgid) != 0) { | |
| 379 #elif defined (HAVE_SETGID) | |
| 380 if (setgid(realgid) != 0) { | |
| 381 #else | |
| 382 if (0) { | |
| 383 #endif | |
| 384 perror("Could not drop setgid privileges. Aborting."); | |
| 385 exit(1); | |
| 386 } | |
| 387 | |
| 388 #if defined(HAVE_SETRESUID) | |
| 389 if (setresuid(-1, realuid, realuid) != 0) { | |
| 390 #elif defined(HAVE_SETREUID) | |
| 391 if (setreuid(realuid, realuid) != 0) { | |
| 392 #elif defined(HAVE_SETUID) | |
| 393 if (setuid(realuid) != 0) { | |
| 394 #else | |
| 395 if (0) { | |
| 396 #endif | |
| 397 perror("Could not drop setuid privileges. Aborting."); | |
| 398 exit(1); | |
| 399 } | |
| 400 #endif | |
| 401 } | |
| 402 | |
| 403 uid_t | |
| 404 md_getuid(void) | |
| 405 { | |
| 406 #ifdef HAVE_GETUID | |
| 407 return( getuid() ); | |
| 408 #else | |
| 409 return(42); | |
| 410 #endif | |
| 411 } | |
