Mercurial > hg > early-roguelike
view rogue4/mdport.c @ 189:7c552cbc6ad9
srogue: make checking directories slightly more portable.
MSVC sys/stat.h doesn't define S_ISDIR().
| author | John "Elwin" Edwards |
|---|---|
| date | Mon, 03 Aug 2015 09:05:15 -0400 |
| parents | d53b13637783 |
| children | 1b73a8641b37 |
line wrap: on
line source
/* mdport.c - Machine Dependent Copyright (C) 2005-2008 Nicholas J. Kisseberth All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name(s) of the author(s) nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #if defined(_WIN32) #include <Windows.h> #include <Lmcons.h> #include <shlobj.h> #include <sys/types.h> #include <io.h> #include <conio.h> #undef MOUSE_MOVED #elif defined(__DJGPP__) #include <process.h> #endif #ifdef HAVE_PWD_H #include <pwd.h> #endif #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #ifdef HAVE_UTMPX_H #include <utmpx.h> #endif #ifdef __INTERIX char *strdup(const char *s); #endif #include <stdlib.h> #include <string.h> #include <errno.h> #if defined(_WIN32) && !defined(__MINGW32__) #define PATH_MAX MAX_PATH #endif #include <curses.h> #ifdef HAVE_TERM_H #include <term.h> #endif #if defined(_WIN32) #include <process.h> #endif #include <stdio.h> #include <fcntl.h> #include <limits.h> #include <sys/stat.h> #include <signal.h> #define MOD_MOVE(c) (toupper(c) ) void md_init() { #ifdef __INTERIX char *term; term = getenv("TERM"); if (term == NULL) setenv("TERM","interix",1); #endif #if defined(__DJGPP__) || defined(_WIN32) _fmode = _O_BINARY; #endif #if defined(__CYGWIN__) || defined(__MSYS__) ESCDELAY=250; #endif } int md_hasclreol() { #ifdef CE return((CE != NULL) && (*CE != 0)); #elif defined (clr_eol) return((clr_eol != NULL) && (*clr_eol != 0)); #elif !defined(__PDCURSES__) return(clr_eol != NULL); #else return(TRUE); #endif } void md_putchar(int c) { putchar(c); } static int md_standout_mode = 0; void md_raw_standout() { #ifdef _WIN32 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; HANDLE hStdout; int fgattr,bgattr; if (md_standout_mode == 0) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdout, &csbiInfo); fgattr = (csbiInfo.wAttributes & 0xF); bgattr = (csbiInfo.wAttributes & 0xF0); SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); md_standout_mode = 1; } #elif defined(SO) tputs(SO,0,md_putchar); fflush(stdout); #endif } void md_raw_standend() { #ifdef _WIN32 CONSOLE_SCREEN_BUFFER_INFO csbiInfo; HANDLE hStdout; int fgattr,bgattr; if (md_standout_mode == 1) { hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdout, &csbiInfo); fgattr = (csbiInfo.wAttributes & 0xF); bgattr = (csbiInfo.wAttributes & 0xF0); SetConsoleTextAttribute(hStdout,(fgattr << 4) | (bgattr >> 4)); md_standout_mode = 0; } #elif defined(SE) tputs(SE,0,md_putchar); fflush(stdout); #endif } int md_unlink_open_file(char *file, int inf) { #ifdef _WIN32 _close(inf); _chmod(file, 0600); return( _unlink(file) ); #else return(unlink(file)); #endif } int md_unlink(char *file) { #ifdef _WIN32 _chmod(file, 0600); return( _unlink(file) ); #else return(unlink(file)); #endif } FILE * md_fdopen(int fd, char *mode) { #ifdef _WIN32 return( _fdopen(fd, mode) ); #else return( fdopen(fd, mode) ); #endif } int md_fileno(FILE *fp) { #ifdef _WIN32 return( _fileno(fp) ); #else return( fileno(fp) ); #endif } int md_creat(char *file, int mode) { int fd; #ifdef _WIN32 mode = _S_IREAD | _S_IWRITE; fd = _open(file,O_CREAT | O_EXCL | O_WRONLY, mode); #else fd = open(file,O_CREAT | O_EXCL | O_WRONLY, mode); #endif return(fd); } void md_normaluser() { #ifndef _WIN32 setuid(getuid()); setgid(getgid()); #endif } int md_getuid() { #ifndef _WIN32 return( getuid() ); #else return(42); #endif } char * md_getusername(int uid) { static char login[80]; char *l = NULL; /* POSIX Shell has priority, then O/S specific methods */ if ( (uid == md_getuid()) && ((l = getenv("LOGNAME")) != NULL) ) { strncpy(login,l,80); login[79] = 0; return(login); } #ifdef _WIN32 LPSTR mybuffer; DWORD size = UNLEN + 1; TCHAR buffer[UNLEN + 1]; mybuffer = buffer; if (uid != md_getuid()) strcpy(mybuffer, "someone"); else GetUserName(mybuffer,&size); l = mybuffer; #endif #if !defined(_WIN32) && !defined(DJGPP) struct passwd *pw; pw = getpwuid(getuid()); if (pw != NULL) l = pw->pw_name; else l = NULL; #endif if ((l == NULL) || (*l == '\0')) if ( (l = getenv("USERNAME")) == NULL ) if ( (l = getenv("USER")) == NULL ) l = "nobody"; strncpy(login,l,80); login[79] = 0; return(login); } char * md_gethomedir() { static char homedir[PATH_MAX]; char *h = NULL; size_t len; #if defined(_WIN32) TCHAR szPath[PATH_MAX]; #endif #if defined(_WIN32) || defined(DJGPP) char slash = '\\'; #else char slash = '/'; struct passwd *pw; pw = getpwuid(getuid()); if (pw != NULL) h = pw->pw_dir; else h = ""; if (strcmp(h,"/") == 0) h = NULL; #endif homedir[0] = 0; #ifdef _WIN32 if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, szPath))) h = szPath; #endif if ( (h == NULL) || (*h == '\0') ) if ( (h = getenv("HOME")) == NULL ) h = ""; strncpy(homedir,h,PATH_MAX-1); len = strlen(homedir); if ((len > 0) && (homedir[len-1] == slash)) homedir[len-1] = 0; return(homedir); } void md_sleep(int s) { #ifdef _WIN32 Sleep(s); #else sleep(s); #endif } char * md_getshell() { static char shell[PATH_MAX]; char *s = NULL; #ifdef _WIN32 char *def = "C:\\WINDOWS\\SYSTEM32\\CMD.EXE"; #elif defined(__DJGPP__) char *def = "C:\\COMMAND.COM"; #else char *def = "/bin/sh"; struct passwd *pw; pw = getpwuid(getuid()); if (pw != NULL) s = pw->pw_shell; else s = NULL; #endif if ((s == NULL) || (*s == '\0')) if ( (s = getenv("COMSPEC")) == NULL) if ( (s = getenv("SHELL")) == NULL) if ( (s = getenv("SystemRoot")) == NULL) s = def; strncpy(shell,s,PATH_MAX); shell[PATH_MAX-1] = 0; return(shell); } void md_ignore_signals() { #ifndef _WIN32 int i; for (i = 0; i < NSIG; i++) signal(i, SIG_IGN); #else signal(SIGABRT, SIG_IGN); signal(SIGFPE, SIG_IGN); signal(SIGILL, SIG_IGN); signal(SIGINT, SIG_IGN); signal(SIGSEGV, SIG_IGN); signal(SIGTERM, SIG_IGN); #endif } int md_shellescape() { #if (!defined(_WIN32) && !defined(__DJGPP__)) int ret_status; int pid; void (*myquit)(int); void (*myend)(int); #endif char *sh; sh = md_getshell(); #if defined(_WIN32) return((int)_spawnl(_P_WAIT,sh,"shell",NULL,0)); #elif defined(__DJGPP__) return ( spawnl(P_WAIT,sh,"shell",NULL,0) ); #else while((pid = fork()) < 0) sleep(1); if (pid == 0) /* Shell Process */ { /* * Set back to original user, just in case */ setuid(getuid()); setgid(getgid()); execl(sh == NULL ? "/bin/sh" : sh, "shell", "-i", 0); perror("No shelly"); _exit(-1); } else /* Application */ { myend = signal(SIGINT, SIG_IGN); #ifdef SIGQUIT myquit = signal(SIGQUIT, SIG_IGN); #endif while (wait(&ret_status) != pid) continue; signal(SIGINT, myend); #ifdef SIGQUIT signal(SIGQUIT, myquit); #endif }
