--- sbbs/src/xpdev/genwrap.c 2018/04/24 16:41:24 1.1.1.1 +++ sbbs/src/xpdev/genwrap.c 2018/04/24 16:45:20 1.1.1.2 @@ -2,13 +2,13 @@ /* General cross-platform development wrappers */ -/* $Id: genwrap.c,v 1.1.1.1 2018/04/24 16:41:24 root Exp $ */ +/* $Id: genwrap.c,v 1.1.1.2 2018/04/24 16:45:20 root Exp $ */ /**************************************************************************** * @format.tab-size 4 (Plain Text/Source Code File Header) * * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) * * * - * Copyright 2006 Rob Swindell - http://www.synchro.net/copyright.html * + * Copyright 2011 Rob Swindell - http://www.synchro.net/copyright.html * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public License * @@ -42,10 +42,12 @@ #include /* clock() */ #include /* errno */ #include /* toupper/tolower */ +#include /* CHAR_BIT */ #if defined(__unix__) #include /* ioctl() */ #include /* uname() */ + #include #endif /* __unix__ */ #include "genwrap.h" /* Verify prototypes */ @@ -275,13 +277,19 @@ char* DLLCALL strtok_r(char *str, const /****************************************************************************/ /* Initialize (seed) the random number generator */ /****************************************************************************/ -unsigned DLLCALL xp_randomize(void) +void DLLCALL xp_randomize(void) { unsigned seed=~0; - #if defined(HAS_DEV_URANDOM) && defined(URANDOM_DEV) - int rf; + int rf; +#endif +#if defined(HAS_SRANDOMDEV_FUNC) && defined(HAS_RANDOM_FUNC) + srandomdev(); + return; +#endif + +#if defined(HAS_DEV_URANDOM) && defined(URANDOM_DEV) if((rf=open(URANDOM_DEV, O_RDONLY))!=-1) { read(rf, &seed, sizeof(seed)); close(rf); @@ -303,22 +311,30 @@ unsigned DLLCALL xp_randomize(void) #ifdef HAS_RANDOM_FUNC srandom(seed); - return(seed); #else srand(seed); - return(seed); #endif } /****************************************************************************/ /* Return random number between 0 and n-1 */ /****************************************************************************/ -int DLLCALL xp_random(int n) +long DLLCALL xp_random(int n) { #ifdef HAS_RANDOM_FUNC + long curr; + unsigned long limit; + if(n<2) return(0); - return(random()%n); + + limit = ((1U<<((sizeof(long)*CHAR_BIT)-1)) / n) * n - 1; + + while(1) { + curr=random(); + if(curr <= limit) + return(curr % n); + } #else float f=0; @@ -416,7 +432,7 @@ char* DLLCALL os_cmdshell(void) { char* shell=getenv(OS_CMD_SHELL_ENV_VAR); -#if !defined(__unix__) +#if defined(__unix__) if(shell==NULL) #ifdef _PATH_BSHELL shell=_PATH_BSHELL; @@ -428,60 +444,6 @@ char* DLLCALL os_cmdshell(void) return(shell); } -#if !defined(__unix__) - -/****************************************************************************/ -/* Win32 implementations of the recursive (thread-safe) versions of std C */ -/* time functions (gmtime, localtime, ctime, and asctime) used in Unix. */ -/* The native Win32 versions of these functions are already thread-safe. */ -/****************************************************************************/ - -struct tm* DLLCALL gmtime_r(const time_t* t, struct tm* tm) -{ - struct tm* tmp = gmtime(t); - - if(tmp==NULL) - return(NULL); - - *tm = *tmp; - return(tm); -} - -struct tm* DLLCALL localtime_r(const time_t* t, struct tm* tm) -{ - struct tm* tmp = localtime(t); - - if(tmp==NULL) - return(NULL); - - *tm = *tmp; - return(tm); -} - -char* DLLCALL ctime_r(const time_t *t, char *buf) -{ - char* p = ctime(t); - - if(p==NULL) - return(NULL); - - strcpy(buf,p); - return(buf); -} - -char* DLLCALL asctime_r(const struct tm *tm, char *buf) -{ - char* p = asctime(tm); - - if(p==NULL) - return(NULL); - - strcpy(buf,p); - return(buf); -} - -#endif /* !defined(__unix__) */ - /****************************************************************/ /* Microsoft (DOS/Win32) real-time system clock implementation. */ /****************************************************************/ @@ -498,18 +460,28 @@ clock_t DLLCALL msclock(void) #endif /****************************************************************************/ +/* Skips all white-space chars at beginning of 'str' */ +/****************************************************************************/ +char* DLLCALL skipsp(char* str) +{ + SKIP_WHITESPACE(str); + return(str); +} + +/****************************************************************************/ /* Truncates all white-space chars off end of 'str' (needed by STRERRROR) */ /****************************************************************************/ char* DLLCALL truncsp(char* str) { size_t i,len; - i=len=strlen(str); - while(i && (str[i-1]==' ' || str[i-1]=='\t' || str[i-1]=='\r' || str[i-1]=='\n')) - i--; - if(i!=len) - str[i]=0; /* truncate */ - + if(str!=NULL) { + i=len=strlen(str); + while(i && isspace((unsigned char)str[i-1])) + i--; + if(i!=len) + str[i]=0; /* truncate */ + } return(str); } @@ -545,12 +517,13 @@ char* DLLCALL truncnl(char* str) { size_t i,len; - i=len=strlen(str); - while(i && (str[i-1]=='\r' || str[i-1]=='\n')) - i--; - if(i!=len) - str[i]=0; /* truncate */ - + if(str!=NULL) { + i=len=strlen(str); + while(i && (str[i-1]=='\r' || str[i-1]=='\n')) + i--; + if(i!=len) + str[i]=0; /* truncate */ + } return(str); } @@ -599,3 +572,45 @@ long double DLLCALL xp_timer(void) #endif return(ret); } + +/* Returns TRUE if specified process is running */ +BOOL DLLCALL check_pid(pid_t pid) +{ +#if defined(__unix__) + return(kill(pid,0)==0); +#elif defined(_WIN32) + HANDLE h; + BOOL result=FALSE; + + if((h=OpenProcess(PROCESS_QUERY_INFORMATION,/* inheritable: */FALSE, pid)) != NULL) { + DWORD code; + if(GetExitCodeProcess(h,&code)==TRUE && code==STILL_ACTIVE) + result=TRUE; + CloseHandle(h); + } + return result; +#else + return FALSE; /* Need check_pid() definition! */ +#endif +} + +/* Terminate (unconditionally) the specified process */ +BOOL DLLCALL terminate_pid(pid_t pid) +{ +#if defined(__unix__) + return(kill(pid,SIGKILL)==0); +#elif defined(_WIN32) + HANDLE h; + BOOL result=FALSE; + + if((h=OpenProcess(PROCESS_TERMINATE,/* inheritable: */FALSE, pid)) != NULL) { + if(TerminateProcess(h,255)) + result=TRUE; + CloseHandle(h); + } + return result; +#else + return FALSE; /* Need check_pid() definition! */ +#endif +} +