12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499 |
- #include <stdio.h>
- #include <dirent.h>
- #if _WIN32
- #define _USE_32BIT_TIME_T
- #endif
- #include <sys/types.h>
- #include <sys/stat.h>
- #if _WIN32
- #include <time.h>
- #include <direct.h>
- #define WIN32_LEAN_AND_MEAN
- #include <winsock2.h>
- #include <windows.h>
- #include <ws2tcpip.h>
- #include <utime.h>
- extern int bmx_inet_pton(int af, const char *src, void *dst);
- #define inet_pton bmx_inet_pton
- extern int _bbusew;
- #else
- #include <time.h>
- #include <unistd.h>
- #include <limits.h> //PATH_MAX
- #include <sys/time.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <utime.h>
- #endif
- #if __APPLE__
- #include <TargetConditionals.h>
- #endif
- #include <brl.mod/blitz.mod/blitz.h>
-
- FILE* stdin_;
- FILE* stdout_;
- FILE* stderr_;
- extern BBObject * pub_stdc_TAddrInfo__Create(struct addrinfo * info, int owner);
- extern BBArray * pub_stdc_TAddrInfo__CreateArray(int count);
- extern void pub_stdc_TAddrInfo__SetAtIndex(BBArray * arr, BBObject * info, int index);
- #if _WIN32
- int getchar_(){
- if( _bbusew ) return getwchar();
- return getchar();
- }
- int puts_( BBString *str ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( str );
- int res = _putws( p );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( str );
- int res = puts( p );
- bbMemFree(p);
- return res;
- }
- int putenv_( BBString *str ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( str );
- int res = _wputenv( p );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( str );
- int res = putenv( p );
- bbMemFree(p);
- return res;
- }
- BBString *getenv_( BBString *str ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( str );
- BBString * res = bbStringFromWString( _wgetenv( p ) );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( str );
- BBString * res = bbStringFromCString( getenv( p ) );
- bbMemFree(res);
- return res;
- }
- int fputs_( BBString *str,FILE* file ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( str );
- int res = fputws( p,file );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( str );
- int res = fputs( p,file );
- bbMemFree(p);
- return res;
- }
- int chdir_( BBString *path ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( path );
- int res = _wchdir( p );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( path );
- int res = _chdir( p );
- bbMemFree(p);
- return res;
- }
- FILE* fopen_( BBString *file,BBString *mode ){
- if( _bbusew ) {
- BBChar *f=bbStringToWString( file );
- BBChar *m=bbStringToWString( mode );
- FILE * res = _wfopen( f, m );
- bbMemFree(m);
- bbMemFree(f);
- return res;
- }
- char *f=bbStringToCString( file );
- char *m=bbStringToCString( mode );
- FILE * res = fopen( f, m );
- bbMemFree(m);
- bbMemFree(f);
- return res;
- }
- BBString *getcwd_(){
- if( _bbusew ){
- wchar_t buf[MAX_PATH];
- _wgetcwd( buf,MAX_PATH );
- return bbStringFromWString( buf );
- }else{
- char buf[MAX_PATH];
- _getcwd( buf,MAX_PATH );
- return bbStringFromCString( buf );
- }
- return &bbEmptyString;
- }
- int chmod_( BBString *path,int mode ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( path );
- int res = _wchmod( p,mode );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( path );
- int res = _chmod( p,mode );
- bbMemFree(p);
- return res;
- }
- int mkdir_( BBString *path,int mode ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( path );
- int res = _wmkdir( p );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( path );
- int res = _mkdir( p );
- bbMemFree(p);
- return res;
- }
- int rmdir_( BBString *path ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( path );
- int res = _wrmdir( p );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( path );
- int res = _rmdir( p );
- bbMemFree(p);
- return res;
- }
- int rename_( BBString *src,BBString *dst ){
- if( _bbusew ) {
- BBChar *s=bbStringToWString( src );
- BBChar *d=bbStringToWString( dst );
- int res = _wrename( s, d );
- bbMemFree(d);
- bbMemFree(s);
- return res;
- }
- char *s=bbStringToCString( src );
- char *d=bbStringToCString( dst );
- int res = rename( s, d );
- bbMemFree(d);
- bbMemFree(s);
- return res;
- }
- void remove_( BBString *path ){
- chmod_( path,0x1b6 );
- if( _bbusew ){
- BBChar *p=bbStringToWString( path );
- _wremove( p );
- bbMemFree(p);
- }else{
- char *p=bbStringToCString( path );
- remove( p );
- bbMemFree(p);
- }
- }
- void* opendir_( BBString *path ){
- if( _bbusew ) {
- BBChar *p=bbStringToWString( path );
- void * res = _wopendir( p );
- bbMemFree(p);
- return res;
- }
- char *p=bbStringToCString( path );
- void * res = opendir( p );
- bbMemFree(p);
- return res;
- }
- int closedir_( void* dir ){
- if( _bbusew ) return _wclosedir( (_WDIR*)dir );
- return closedir( (DIR*)dir );
- }
- BBString *readdir_( void* dir ){
- if( _bbusew ){
- struct _wdirent *t=_wreaddir( (_WDIR*)dir );
- return t ? bbStringFromWString( t->d_name ) : &bbEmptyString;
- }
- struct dirent *t=readdir( (DIR*)dir );
- return t ? bbStringFromCString( t->d_name ) : &bbEmptyString;
- }
- int stat_( BBString *path,int *t_mode,BBLONG *t_size,int *t_mtime,int *t_ctime,int *t_atime ){
- int i;
- struct _stati64 st;
-
- for( i=0;i<path->length;++i ){
- if( path->buf[i]=='<' || path->buf[i]=='>' ) return -1;
- }
-
- if( _bbusew ){
- BBChar *p = bbStringToWString(path);
- if( _wstati64( p,&st ) ) {
- bbMemFree(p);
- return -1;
- }
- bbMemFree(p);
- }else{
- char *p = bbStringToCString(path);
- if( _stati64( p,&st ) ) {
- bbMemFree(p);
- return -1;
- }
- bbMemFree(p);
- }
- *t_mode=st.st_mode;
- *t_size=st.st_size;
- *t_mtime=st.st_mtime;
- *t_ctime=st.st_ctime;
- *t_atime=st.st_atime;
- return 0;
- }
- int stat64_( BBString *path,int *t_mode,BBLONG *t_size,BBLONG *t_mtime,BBLONG *t_ctime,BBLONG *t_atime ){
- int i;
- struct _stati64 st;
-
- for( i=0;i<path->length;++i ){
- if( path->buf[i]=='<' || path->buf[i]=='>' ) return -1;
- }
-
- if( _bbusew ){
- BBChar *p = bbStringToWString(path);
- if( _wstati64( p,&st ) ) {
- bbMemFree(p);
- return -1;
- }
- bbMemFree(p);
- }else{
- char *p = bbStringToCString(path);
- if( _stati64( p,&st ) ) {
- bbMemFree(p);
- return -1;
- }
- bbMemFree(p);
- }
- *t_mode=st.st_mode;
- *t_size=st.st_size;
- *t_mtime=st.st_mtime;
- *t_ctime=st.st_ctime;
- *t_atime=st.st_atime;
- return 0;
- }
- int system_( BBString *cmd ){
- int res;
- PROCESS_INFORMATION pi={0};
-
- if( _bbusew ){
- STARTUPINFOW si={sizeof(si)};
- wchar_t *tmp = bbStringToWString(cmd);
-
- if( CreateProcessW( 0,tmp,0,0,1,CREATE_DEFAULT_ERROR_MODE,0,0,&si,&pi ) ){
- WaitForSingleObject( pi.hProcess,INFINITE );
-
- res=GetExitCodeProcess( pi.hProcess,(DWORD*)&res ) ? res : -1;
-
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }else{
- res=GetLastError();
- }
- bbMemFree(tmp);
-
- } else {
- STARTUPINFO si={sizeof(si)};
- char *tmp = bbStringToCString(cmd);
-
- if( CreateProcessA( 0,tmp,0,0,1,CREATE_DEFAULT_ERROR_MODE,0,0,&si,&pi ) ){
- WaitForSingleObject( pi.hProcess,INFINITE );
-
- res=GetExitCodeProcess( pi.hProcess,(DWORD*)&res ) ? res : -1;
-
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }else{
- res=GetLastError();
- }
- bbMemFree(tmp);
- }
- return res;
- }
- int fseek_( FILE* stream, BBLONG offset, int origin ) {
- return (_fseeki64(stream, offset, origin) == 0) ? 0 : 1;
- }
- BBLONG ftell_( FILE* stream ) {
- return _ftelli64(stream);
- }
- int ftruncate_(FILE* stream, BBLONG size) {
- return _chsize_s(fileno(stream), size);
- }
- int clock_gettime_(int id, struct timespec * spec) {
- __int64 ftime;
- union {
- unsigned __int64 ftime;
- FILETIME ft;
- } ft;
- GetSystemTimeAsFileTime(&ft.ft);
- ftime = ft.ftime - 116444736000000000LL;
- spec->tv_sec = ftime / 10000000LL;
- spec->tv_nsec = ((int)(ftime % 10000000LL)) *100;
- return 0;
- }
- int utime_( BBString *path, int type, BBLONG time){
- struct _utimbuf times = {0,0};
-
- if ( type == 0 ) {
- times.modtime = time;
- } else if ( type == 2 ) {
- times.actime = time;
- } else {
- return -1;
- }
- wchar_t *p = bbStringToWString(path);
- int res = _wutime( p, ×);
- bbMemFree(p);
-
- return res == 0 ? 0 : -1;
- }
- #else
- int getchar_(){
- return getchar();
- }
- int puts_( BBString *str ){
- char *p = bbStringToUTF8String( str );
- int res = puts( p );
- bbMemFree(p);
- return res;
- }
- int putenv_( BBString *str ){
- char *t=bbStringToUTF8String( str );
- char *p=(char*)malloc( strlen(t)+1 );
- strcpy( p,t );
- bbMemFree(t);
- return putenv( p );
- }
- BBString *getenv_( BBString *str ){
- char *p = bbStringToUTF8String( str );
- BBString * res = bbStringFromUTF8String( getenv( p ) );
- bbMemFree(p);
- return res;
- }
- FILE* fopen_( BBString *file,BBString *mode ){
- char *f = bbStringToUTF8String( file );
- char *m = bbStringToUTF8String( mode );
- FILE * res = fopen( f, m );
- bbMemFree(m);
- bbMemFree(f);
- return res;
- }
- int fputs_( BBString *str,FILE* file ){
- char *p = bbStringToUTF8String( str );
- int res = fputs( p,file );
- bbMemFree(p);
- return res;
- }
- int chdir_( BBString *path ){
- char *p = bbStringToUTF8String( path );
- int res = chdir( p );
- bbMemFree(p);
- return res;
- }
- BBString *getcwd_(){
- char buf[PATH_MAX];
- getcwd( buf,PATH_MAX );
- return bbStringFromUTF8String( buf );
- }
- int chmod_( BBString *path,int mode ){
- char *p = bbStringToUTF8String( path );
- int res = chmod( p, mode );
- bbMemFree(p);
- return res;
- }
- int mkdir_( BBString *path,int mode ){
- char *p = bbStringToUTF8String( path );
- int res = mkdir( p, mode );
- bbMemFree(p);
- return res;
- }
- int rmdir_( BBString *path ){
- char *p = bbStringToUTF8String( path );
- int res = rmdir( p );
- bbMemFree(p);
- return res;
- }
- int rename_( BBString *src,BBString *dst ){
- char *s = bbStringToUTF8String( src );
- char *d = bbStringToUTF8String( dst );
- int res = rename( s, d );
- bbMemFree(d);
- bbMemFree(s);
- return res;
- }
- int remove_( BBString *path ){
- char *p = bbStringToUTF8String( path );
- int res = remove( p );
- bbMemFree(p);
- return res;
- }
- DIR* opendir_( BBString *path ){
- char *p = bbStringToUTF8String( path );
- DIR * res = opendir( p );
- bbMemFree(p);
- return res;
- }
- BBString *readdir_( DIR* dir ){
- struct dirent *t=readdir( dir );
- return t ? bbStringFromUTF8String( t->d_name ) : &bbEmptyString;
- }
- int closedir_( DIR* dir ){
- return closedir( dir );
- }
- int stat_( BBString *path,int *t_mode,BBLONG *t_size,int *t_mtime,int *t_ctime,int *t_atime ){
- struct stat st;
- char *p = bbStringToUTF8String( path );
- if( stat( p,&st ) ) {
- bbMemFree(p);
- return -1;
- }
- bbMemFree(p);
- *t_mode=st.st_mode;
- *t_size=st.st_size;
- *t_mtime=st.st_mtime;
- *t_ctime=st.st_ctime;
- *t_atime=st.st_atime;
- return 0;
- }
- int stat64_( BBString *path,int *t_mode,BBLONG *t_size,BBLONG *t_mtime,BBLONG *t_ctime,BBLONG *t_atime ){
- struct stat st;
- char *p = bbStringToUTF8String( path );
- if( stat( p,&st ) ) {
- bbMemFree(p);
- return -1;
- }
- bbMemFree(p);
- *t_mode=st.st_mode;
- *t_size=st.st_size;
- *t_mtime=st.st_mtime;
- *t_ctime=st.st_ctime;
- *t_atime=st.st_atime;
- return 0;
- }
- int utime_( BBString *path, int type, BBLONG time){
- struct utimbuf times = {0,0};
- if ( type == 0 ) {
- times.modtime = time;
- } else if ( type == 2 ) {
- times.actime = time;
- } else {
- return -1;
- }
- char *p = (char*)bbStringToUTF8String( path );
- int res = utime( p, ×);
- bbMemFree(p);
-
- return res == 0 ? 0 : -1;
- }
- int system_( BBString *cmd ){
- #if TARGET_OS_IPHONE || TARGET_OS_TV
- bbExThrowCString("Not available on iOS");
- return -1;
- #elif defined(__ANDROID__)
- bbExThrowCString("Not available on Android");
- return -1;
- #else
- char *p = bbStringToUTF8String( cmd );
- int res = system( p );
- bbMemFree(p);
- return res;
- #endif
- }
- int fseek_( FILE* stream, BBLONG offset, int origin ) {
- return fseeko(stream, offset, origin);
- }
- BBLONG ftell_( FILE* stream ) {
- return ftello(stream);
- }
- int ftruncate_(FILE* stream, BBLONG size) {
- return ftruncate(fileno(stream), size);
- }
- #ifndef __APPLE__
- int clock_gettime_(int id, struct timespec * spec) {
- return clock_gettime(id, spec);
- }
- #endif
- #endif
- #ifdef __APPLE__
- #include <mach/mach_time.h>
- BBULONG mach_absolute_time_ns() {
- static mach_timebase_info_data_t s_timebase_info;
- static int inited = 0;
- if (!inited) {
- mach_timebase_info(&s_timebase_info);
- };
- return (mach_absolute_time() * s_timebase_info.numer) / s_timebase_info.denom;
- }
- #endif
- int fclose_( FILE* stream ) {
- return fclose(stream);
- }
- int feof_(FILE* stream) {
- return feof(stream);
- }
- int fflush_( FILE* stream ) {
- return fflush(stream);
- }
- int htons_( int n ){
- return htons( n );
- }
- int ntohs_( int n ){
- return ntohs( n );
- }
- int htonl_( int n ){
- return htonl( n );
- }
- int ntohl_( int n ){
- return ntohl( n );
- }
- #if _WIN32
- SOCKET socket_( int addr_type,int comm_type,int protocol ){
- return socket( addr_type,comm_type,protocol );
- #else
- int socket_( int addr_type,int comm_type,int protocol ){
- return socket( addr_type,comm_type,protocol );
- #endif
- }
- #if _WIN32
- void closesocket_( SOCKET s ){
- closesocket( s );
- #else
- void closesocket_( int s ){
- close( s );
- #endif
- }
- int bmx_stdc_convertAFFamily(int family) {
- switch (family) {
- case 2:
- return AF_INET;
- case 10:
- return AF_INET6;
- }
-
- // unmapped
- return family;
- }
- #if _WIN32
- int bind_( SOCKET socket,int addr_type,int port ){
- #else
- int bind_( int socket,int addr_type,int port ){
- #endif
- int r;
-
- // if ( addr_type!=AF_INET ) return -1;
- switch(addr_type) {
- case AF_INET:
- {
- struct sockaddr_in sa;
- memset( &sa,0,sizeof(sa) );
- sa.sin_family= bmx_stdc_convertAFFamily(addr_type);
- sa.sin_addr.s_addr=htonl(INADDR_ANY);
- sa.sin_port=htons( port );
- return bind( socket,(void*)&sa,sizeof(sa) );
- }
- case AF_INET6:
- {
- struct sockaddr_in6 sa;
- memset( &sa,0,sizeof(sa) );
- sa.sin6_family= bmx_stdc_convertAFFamily(addr_type);
- sa.sin6_addr=in6addr_any;
- sa.sin6_port=htons( port );
- return bind( socket,(void*)&sa,sizeof(sa) );
- }
- default:
- return -1;
- }
-
- }
- #if _WIN32
- int bmx_stdc_bind_info(SOCKET socket, struct addrinfo * info) {
- #else
- int bmx_stdc_bind_info(int socket, struct addrinfo * info) {
- #endif
- return bind(socket, info->ai_addr, info->ai_addrlen);
- }
- char *gethostbyaddr_( void *addr,int addr_len,int addr_type ){
- return NULL;
- //struct hostent *e=gethostbyaddr( addr,addr_len,addr_type );
- //return e ? e->h_name : 0;
- }
- BBARRAY getaddrinfo_hints(BBString *name, BBString *service, struct addrinfo * hints) {
- struct addrinfo * info;
- struct addrinfo * ip;
-
- char * n = bbStringToUTF8String(name);
- char * s = 0;
- if (service != &bbEmptyString) {
- s = bbStringToUTF8String(service);
- }
-
- int res = getaddrinfo(n, s, hints, &info);
-
- bbMemFree(s);
- bbMemFree(n);
-
- if (res != 0) {
- return &bbEmptyArray;
- }
-
- int count = 0;
- for (ip = info; ip != NULL; ip = ip->ai_next) {
- count++;
- }
-
- BBArray * arr = pub_stdc_TAddrInfo__CreateArray(count);
- count = 0;
- for (ip = info; ip != NULL; ip = ip->ai_next) {
- BBObject * obj = pub_stdc_TAddrInfo__Create(ip, count == 0);
-
- pub_stdc_TAddrInfo__SetAtIndex(arr, obj, count);
- count++;
- }
-
- return arr;
- }
- BBARRAY getaddrinfo_(BBString *name, BBString *service, int family) {
- struct addrinfo hints;
-
- memset(&hints, 0, sizeof(struct addrinfo));
-
- hints.ai_family = bmx_stdc_convertAFFamily(family);
-
- return getaddrinfo_hints(name, service, &hints);
- }
- struct addrinfo * bmx_stdc_addrinfo_new() {
- return (struct addrinfo *)calloc(1, sizeof(struct addrinfo));
- }
- void freeaddrinfo_(struct addrinfo * info ) {
- freeaddrinfo(info);
- }
- #if _WIN32
- int connect_( SOCKET socket, struct addrinfo * info ){
- #else
- int connect_( int socket, struct addrinfo * info ){
- #endif
- return connect( socket, info->ai_addr, info->ai_addrlen);
- }
- #if _WIN32
- int listen_( SOCKET socket,int backlog ){
- #else
- int listen_( int socket,int backlog ){
- #endif
- return listen( socket,backlog );
- }
- #if _WIN32
- int accept_( SOCKET socket,const char *addr,unsigned int *addr_len ){
- #else
- int accept_( int socket,const char *addr,unsigned int *addr_len ){
- #endif
- return accept( socket,(void*)addr,addr_len );
- }
- #if _WIN32
- int bmx_stdc_accept_(SOCKET socket, struct sockaddr_storage * storage) {
- #else
- int bmx_stdc_accept_(int socket, struct sockaddr_storage * storage) {
- #endif
- if (storage) {
- int size = sizeof(struct sockaddr_storage );
- return accept(socket, (struct sockaddr *)storage, &size);
- } else {
- return accept(socket, NULL, NULL);
- }
- }
- #if _WIN32
- int select_( int n_read,SOCKET *r_socks,int n_write,SOCKET *w_socks,int n_except,SOCKET *e_socks,int millis ){
- #else
- int select_( int n_read,int *r_socks,int n_write,int *w_socks,int n_except,int *e_socks,int millis ){
- #endif
- int i,n,r;
- struct timeval tv,*tvp;
- fd_set r_set,w_set,e_set;
-
- n=-1;
-
- FD_ZERO( &r_set );
- for( i=0;i<n_read;++i ){
- FD_SET( r_socks[i],&r_set );
- if( r_socks[i]>n ) n=r_socks[i];
- }
- FD_ZERO( &w_set );
- for( i=0;i<n_write;++i ){
- FD_SET( w_socks[i],&w_set );
- if( w_socks[i]>n ) n=w_socks[i];
- }
- FD_ZERO( &e_set );
- for( i=0;i<n_except;++i ){
- FD_SET( e_socks[i],&e_set );
- if( e_socks[i]>n ) n=e_socks[i];
- }
-
- if( millis<0 ){
- tvp=0;
- }else{
- tv.tv_sec=millis/1000;
- tv.tv_usec=(millis%1000)*1000;
- tvp=&tv;
- }
-
- r=select( n+1,&r_set,&w_set,&e_set,tvp );
- if( r<0 ) return r;
-
- for( i=0;i<n_read;++i ){
- if( !FD_ISSET(r_socks[i],&r_set) ) r_socks[i]=0;
- }
- for( i=0;i<n_write;++i ){
- if( !FD_ISSET(w_socks[i],&w_set) ) w_socks[i]=0;
- }
- for( i=0;i<n_except;++i ){
- if( !FD_ISSET(e_socks[i],&e_set) ) e_socks[i]=0;
- }
- return r;
- }
- #if _WIN32
- ssize_t send_( SOCKET socket,const char *buf,size_t size,int flags ){
- #else
- ssize_t send_( int socket,const char *buf,size_t size,int flags ){
- #endif
- return send( socket,buf,size,flags );
- }
- #if _WIN32
- int sendto_( SOCKET socket,const char *buf,int size,int flags,const char * dest_ip,int dest_port, int addr_type ){
- #else
- int sendto_( int socket,const char *buf,int size,int flags,const char * dest_ip,int dest_port, int addr_type ){
- #endif
- addr_type = bmx_stdc_convertAFFamily(addr_type);
-
- switch (addr_type) {
- case AF_INET:
- {
- struct sockaddr_in sa;
- memset( &sa,0,sizeof(sa) );
- sa.sin_family=AF_INET;
- #ifdef _WIN32
- sa.sin_addr.s_addr=inet_addr( dest_ip );
- #else
- inet_pton(AF_INET, dest_ip, &(sa.sin_addr));
- #endif
- // memcpy( &sa.sin_addr,dest_ip,4 );
- sa.sin_port=htons( dest_port );
- return sendto( socket,buf,size,flags,(void*)&sa,sizeof(sa));
- }
- case AF_INET6:
- {
- struct sockaddr_in6 sa;
- memset( &sa,0,sizeof(sa) );
- sa.sin6_family=AF_INET6;
- sa.sin6_port=htons( dest_port );
- memcpy( &sa.sin6_addr, dest_ip,16 );
-
- return sendto( socket,buf,size,flags,(void*)&sa,sizeof(sa));
- }
- }
- return 0;
- }
- #if _WIN32
- ssize_t recv_( SOCKET socket,char *buf,size_t size,int flags ){
- #else
- ssize_t recv_( int socket,char *buf,size_t size,int flags ){
- #endif
- return recv( socket,buf,size,flags );
- }
- #if _WIN32
- int recvfrom_( SOCKET socket,char *buf,int size,int flags,int *_ip,int *_port){
- #else
- int recvfrom_( int socket,char *buf,int size,int flags,int *_ip,int *_port){
- #endif
- struct sockaddr_in sa;
- int sasize;
- int count;
- memset( &sa,0,sizeof(sa) );
- sasize=sizeof(sa);
- count=recvfrom(socket,buf,size,flags,(void*)&sa,&sasize);
- *_ip=ntohl_(sa.sin_addr.s_addr);
- *_port=ntohs_(sa.sin_port);
- return count;
- }
- #if _WIN32
- int setsockopt_( SOCKET socket,int level,int optname,const void *optval,int count){
- #else
- int setsockopt_( int socket,int level,int optname,const void *optval,int count){
- #endif
- return setsockopt( socket,level,optname,optval,count);
- }
- #if _WIN32
- int getsockopt_( SOCKET socket,int level,int optname,void *optval,int *count){
- #else
- int getsockopt_( int socket,int level,int optname,void *optval,int *count){
- #endif
- return getsockopt( socket,level,optname,optval,count);
- }
- #if _WIN32
- int shutdown_( SOCKET socket,int how ){
- #else
- int shutdown_( int socket,int how ){
- #endif
- return shutdown( socket,how );
- }
- #if _WIN32
- int getsockname_( SOCKET socket,void *addr,int *len ){
- #else
- int getsockname_( int socket,void *addr,int *len ){
- #endif
- return getsockname( socket,addr,len );
- }
- #if _WIN32
- int getpeername_( SOCKET socket,void *addr,int *len ){
- #else
- int getpeername_( int socket,void *addr,int *len ){
- #endif
- return getpeername( socket,addr,len );
- }
- typedef struct {
- int year;
- int month;
- int day;
- int hour;
- int minute;
- int second;
- int millisecond;
- int utc;
- int offset;
- int dst;
- } SDateTime;
- #ifdef __WIN32__
- int bmx_calc_timeoffset_mins(SDateTime * dt) {
- TIME_ZONE_INFORMATION tz;
- DWORD rc = GetTimeZoneInformation(&tz);
- int offset_minutes = tz.Bias + (TIME_ZONE_ID_DAYLIGHT != rc ? tz.StandardBias : tz.DaylightBias);
-
- dt->dst = (rc == TIME_ZONE_ID_DAYLIGHT) ? 1 : 0;
- return offset_minutes;
- }
- void bmx_current_datetime(SDateTime * dt, int utc) {
- SYSTEMTIME systemTime;
- if (utc) {
- GetSystemTime(&systemTime);
- } else {
- GetLocalTime(&systemTime);
- }
- dt->year = systemTime.wYear;
- dt->month = systemTime.wMonth;
- dt->day = systemTime.wDay;
- dt->hour = systemTime.wHour;
- dt->minute = systemTime.wMinute;
- dt->second = systemTime.wSecond;
- dt->millisecond = systemTime.wMilliseconds;
- dt->utc = utc;
- dt->offset = utc ? 0 : bmx_calc_timeoffset_mins(dt);
- }
- #else
- int bmx_calc_timeoffset_mins() {
- time_t rawtime;
- struct tm local_tm, utc_tm;
- time(&rawtime);
- localtime_r(&rawtime, &local_tm);
- gmtime_r(&rawtime, &utc_tm);
- // Calculate the time difference in minutes
- int diff_minutes = (local_tm.tm_hour - utc_tm.tm_hour) * 60 + (local_tm.tm_min - utc_tm.tm_min);
- // Adjust the difference if crossing a day boundary
- if (local_tm.tm_yday < utc_tm.tm_yday) {
- diff_minutes -= 24 * 60;
- } else if (local_tm.tm_yday > utc_tm.tm_yday) {
- diff_minutes += 24 * 60;
- }
- return diff_minutes;
- }
- void bmx_current_datetime(SDateTime * dt, int utc) {
- struct timespec ts;
- struct tm tm;
- clock_gettime(CLOCK_REALTIME, &ts);
- if (utc) {
- gmtime_r(&ts.tv_sec, &tm);
- dt->dst = 0;
- } else {
- localtime_r(&ts.tv_sec, &tm);
- dt->dst = tm.tm_isdst > 0 ? 1 : 0;
- }
- dt->year = tm.tm_year + 1900;
- dt->month = tm.tm_mon + 1;
- dt->day = tm.tm_mday;
- dt->hour = tm.tm_hour;
- dt->minute = tm.tm_min;
- dt->second = tm.tm_sec;
- dt->millisecond = ts.tv_nsec / 1000000;
- dt->utc = utc;
- dt->offset = utc ? 0 : bmx_calc_timeoffset_mins();
- }
- #endif
- int bmx_datetime_from_local_epoch(BBLONG epoch, SDateTime* dt) {
- struct tm* local;
- #if defined(__linux__) || defined(__APPLE__)
- struct tm result;
- local = localtime_r(&epoch, &result);
- #elif defined(_WIN32) || defined(_WIN64)
- local = localtime(&epoch);
- #else
- return -1; // Platform not supported
- #endif
- if (!local) {
- return -1;
- }
- dt->year = local->tm_year + 1900;
- dt->month = local->tm_mon + 1;
- dt->day = local->tm_mday;
- dt->hour = local->tm_hour;
- dt->minute = local->tm_min;
- dt->second = local->tm_sec;
- dt->millisecond = 0;
- dt->utc = 0; // This is local time
- #if defined(__linux__) || defined(__APPLE__)
- dt->offset = local->tm_gmtoff / 60; // 'tm_gmtoff' holds seconds east of UTC
- dt->dst = local->tm_isdst;
- #elif defined(_WIN32) || defined(_WIN64)
- dt->offset = -bmx_calc_timeoffset_mins(dt); // - is because Windows returns minutes west of UTC
- #endif
- return 0;
- }
- SDateTime bmx_datetime_from_epoch(BBLONG epochTimeSecs, BBLONG fracNanoseconds, int isLocalTime) {
- SDateTime dt;
- if (isLocalTime) {
- int res = bmx_datetime_from_local_epoch(epochTimeSecs, &dt);
- if (res == 0) {
- dt.millisecond = fracNanoseconds / 1000000;
- }
- return dt;
- }
- struct tm timeinfo;
- #if defined(_WIN32) || defined(_WIN64)
- gmtime_s(&timeinfo, &epochTimeSecs);
- #else
- gmtime_r(&epochTimeSecs, &timeinfo);
- #endif
- dt.year = timeinfo.tm_year + 1900;
- dt.month = timeinfo.tm_mon + 1;
- dt.day = timeinfo.tm_mday;
- dt.hour = timeinfo.tm_hour;
- dt.minute = timeinfo.tm_min;
- dt.second = timeinfo.tm_sec;
- dt.millisecond = fracNanoseconds / 1000000;
- dt.utc = 1;
- dt.offset = 0;
- dt.dst = 0;
- return dt;
- }
- time_t bmx_datetime_to_time_t(SDateTime * dt) {
- struct tm t;
- t.tm_year = dt->year - 1900;
- t.tm_mon = dt->month - 1;
- t.tm_mday = dt->day;
- t.tm_hour = dt->hour;
- t.tm_min = dt->minute;
- t.tm_sec = dt->second;
- t.tm_isdst = -1; // timegm and _mkgmtime do not use this field
- if (!dt->utc) {
- // Convert the offset to seconds
- int offsetSeconds = dt->offset * 60;
- if (dt->dst == 1) {
- offsetSeconds += 3600;
- }
- // Convert struct tm to time_t as if it was UTC
- time_t ts;
- #if defined(_WIN32) || defined(_WIN64)
- ts = _mkgmtime(&t);
- #else
- ts = timegm(&t);
- #endif
- if (ts == -1) {
- return -1;
- }
- // Apply the offset
- ts -= offsetSeconds;
- return (BBLONG)ts;
- } else {
- // Convert struct tm to time_t as UTC
- time_t ts;
- #if defined(_WIN32) || defined(_WIN64)
- ts = _mkgmtime(&t);
- #else
- ts = timegm(&t);
- #endif
- if (ts == -1) {
- return -1;
- }
- return ts;
- }
- }
- BBLONG bmx_datetime_to_epoch(SDateTime * dt) {
- if (dt->utc) {
- return (BBLONG)bmx_datetime_to_time_t(dt);
- }
- struct tm t;
- t.tm_year = dt->year - 1900;
- t.tm_mon = dt->month - 1;
- t.tm_mday = dt->day;
- t.tm_hour = dt->hour;
- t.tm_min = dt->minute;
- t.tm_sec = dt->second;
- t.tm_isdst = dt->dst;
- return (BBLONG)mktime(&t);
- }
- int bmx_datetime_convert_to_utc(const SDateTime* dt, SDateTime* dt_utc) {
- if (!dt || !dt_utc)
- return -1; // Return error if either pointer is NULL
- if (dt->utc == 1) {
- *dt_utc = *dt;
- return 0;
- }
- time_t ts = bmx_datetime_to_time_t(dt);
- if (ts == -1) {
- return -1;
- }
- struct tm utc;
- #if defined(_WIN32) || defined(_WIN64)
- gmtime_s(&timeinfo, &epochTimeSecs);
- #else
- gmtime_r(&ts, &utc);
- #endif
- dt_utc->year = utc.tm_year + 1900;
- dt_utc->month = utc.tm_mon + 1;
- dt_utc->day = utc.tm_mday;
- dt_utc->hour = utc.tm_hour;
- dt_utc->minute = utc.tm_min;
- dt_utc->second = utc.tm_sec;
- dt_utc->millisecond = dt->millisecond;
- dt_utc->utc = 1;
- dt_utc->offset = 0;
- dt_utc->dst = 0;
- return 0;
- }
- BBString * bmx_current_datetime_format(BBString * format) {
- struct tm tm;
- time_t rawtime;
- char buf[256];
- unsigned char * f = bbStringToUTF8String(format);
- time(&rawtime);
-
- #if defined(_WIN32) || defined(_WIN64)
- localtime_s(&tm, &rawtime);
- #else
- localtime_r(&rawtime, &tm);
- #endif
- strftime(buf, sizeof(buf), f, &tm);
- BBString * res = bbStringFromUTF8String(buf);
- bbMemFree(f);
- return res;
- }
- BBString * bmx_datetime_iso8601(const SDateTime *dt, int showMillis) {
- char buf[32];
- if (dt->utc) {
- if (showMillis) {
- snprintf(buf, 32, "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
- dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second, dt->millisecond);
- } else {
- snprintf(buf, 32, "%04d-%02d-%02dT%02d:%02d:%02dZ",
- dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second);
- }
- } else {
- int offset = dt->dst == 1 ? dt->offset + 60 : dt->offset;
- int offset_sign = dt->offset >= 0 ? 1 : -1;
- int offset_hours = offset / 60;
- int offset_minutes = offset % 60 * offset_sign;
- if (showMillis) {
- snprintf(buf, 32, "%04d-%02d-%02dT%02d:%02d:%02d.%03d%+03d:%02d",
- dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second, dt->millisecond,
- offset_hours, offset_minutes);
- } else {
- snprintf(buf, 32, "%04d-%02d-%02dT%02d:%02d:%02d%+03d:%02d",
- dt->year, dt->month, dt->day, dt->hour, dt->minute, dt->second,
- offset_hours, offset_minutes);
- }
- }
- return bbStringFromCString(buf);
- }
- int time_( void *ttime ){
- return (int)time( (time_t*)ttime );
- }
- void *localtime_( void *ttime ){
- return localtime( (time_t*)ttime );
- }
- int strftime_( char *buf,int size,BBString *fmt,void *ttime ){
- char *p = bbStringToCString( fmt );
- int res = strftime( buf,size,p,ttime );
- bbMemFree(p);
- return res;
- }
- int bmx_stdc_addrinfo_flags(struct addrinfo * info) {
- return info->ai_flags;
- }
- int bmx_stdc_addrinfo_family(struct addrinfo * info) {
- return info->ai_family;
- }
- int bmx_stdc_addrinfo_socktype(struct addrinfo * info) {
- return info->ai_socktype;
- }
- int bmx_stdc_addrinfo_protocol(struct addrinfo * info) {
- return info->ai_protocol;
- }
- int bmx_stdc_addrinfo_addrlen(struct addrinfo * info) {
- return info->ai_addrlen;
- }
- struct sockaddr * bmx_stdc_addrinfo_addr(struct addrinfo * info) {
- return info->ai_addr;
- }
- BBString * bmx_stdc_addrinfo_canonname(struct addrinfo * info) {
- return bbStringFromUTF8String(info->ai_canonname);
- }
- void bmx_stdc_addrinfo_setflags(struct addrinfo * info, int flags) {
- info->ai_flags = flags;
- }
- void bmx_stdc_addrinfo_setfamily(struct addrinfo * info, int family) {
- info->ai_family = bmx_stdc_convertAFFamily(family);
- }
- void bmx_stdc_addrinfo_setsocktype(struct addrinfo * info, int sockType) {
- info->ai_socktype = sockType;
- }
- void bmx_stdc_addrinfo_setprotocol(struct addrinfo * info, int protocol) {
- info->ai_protocol = protocol;
- }
- int bmx_stdc_convertNIFlags(int flags) {
- int niFlags = 0;
-
- if (flags & 0x0001) {
- niFlags |= NI_DGRAM;
- }
- if (flags & 0x0002) {
- niFlags |= NI_NAMEREQD;
- }
- if (flags & 0x0004) {
- niFlags |= NI_NOFQDN;
- }
- if (flags & 0x0008) {
- niFlags |= NI_NUMERICHOST;
- }
- if (flags & 0x0010) {
- niFlags |= NI_NUMERICSERV;
- }
-
- return niFlags;
- }
- BBString * bmx_stdc_addrinfo_hostname(struct addrinfo * info, int flags) {
- char host[256];
- int res = getnameinfo(info->ai_addr, info->ai_addrlen, host, 256, 0, 0, bmx_stdc_convertNIFlags(flags));
- if (res != 0) {
- return &bbEmptyString;
- }
- return bbStringFromUTF8String(host);
- }
- int inet_pton_(int family, BBString * src, void * dst) {
- char * s = bbStringToCString(src);
- int res = inet_pton(bmx_stdc_convertAFFamily(family), s, dst);
- bbMemFree(s);
- return res;
- }
- struct sockaddr_storage * bmx_stdc_sockaddrestorage_new() {
- return calloc(1, sizeof(struct sockaddr_storage));
- }
- BBString * bmx_stdc_sockaddrestorage_address(struct sockaddr_storage * storage) {
- BBString * address = &bbEmptyString;
-
- #if _WIN32
- TCHAR add[256];
- typedef LPTSTR (__stdcall RTLIPV6ADDRESSTOSTRING)(const IN6_ADDR*, PTSTR);
- typedef LPTSTR (__stdcall RTLIPV4ADDRESSTOSTRING)(const IN_ADDR*, PTSTR);
-
- HMODULE ntdll = GetModuleHandle("NTDLL.DLL");
- if (storage->ss_family == AF_INET) {
- RTLIPV4ADDRESSTOSTRING* RtlIpv4AddressToStringFunc = (RTLIPV4ADDRESSTOSTRING*)GetProcAddress(ntdll, "RtlIpv4AddressToStringW");
- RtlIpv4AddressToStringFunc(&((struct sockaddr_in*)storage)->sin_addr, add);
- } else {
- RTLIPV6ADDRESSTOSTRING* RtlIpv6AddressToStringFunc = (RTLIPV6ADDRESSTOSTRING*)GetProcAddress(ntdll, "RtlIpv6AddressToStringW");
- RtlIpv6AddressToStringFunc(&((struct sockaddr_in6*)storage)->sin6_addr, add);
- }
-
- address = bbStringFromWString((BBChar*)add);
- #else
- char add[256];
- if (storage->ss_family == AF_INET) {
- inet_ntop(storage->ss_family, &((struct sockaddr_in*)storage)->sin_addr, add, sizeof(add));
- } else {
- inet_ntop(storage->ss_family, &((struct sockaddr_in6*)storage)->sin6_addr, add, sizeof(add));
- }
-
- address = bbStringFromCString(add);
- #endif
- return address;
- }
- #if _WIN32
- int bmx_stdc_getsockname(SOCKET socket, int * port, BBSTRING * address) {
- #else
- int bmx_stdc_getsockname(int socket, int * port, BBSTRING * address) {
- #endif
- struct sockaddr_storage storage;
- int len = sizeof(struct sockaddr_storage);
-
- int res = getsockname(socket, (struct sockaddr *)&storage, &len);
-
- if (res >= 0) {
- if (storage.ss_family == AF_INET) {
- *port = ntohs(((struct sockaddr_in*)&storage)->sin_port);
- } else {
- *port = ntohs(((struct sockaddr_in6*)&storage)->sin6_port);
- }
-
- *address = bmx_stdc_sockaddrestorage_address(&storage);
- }
-
- return res;
- }
- #if _WIN32
- int bmx_stdc_getpeername(SOCKET socket, int * port, BBSTRING * address) {
- #else
- int bmx_stdc_getpeername(int socket, int * port, BBSTRING * address) {
- #endif
- struct sockaddr_storage storage;
- int len = sizeof(struct sockaddr_storage);
-
- int res = getpeername(socket, (struct sockaddr *)&storage, &len);
-
- if (res >= 0) {
- if (storage.ss_family == AF_INET) {
- *port = ntohs(((struct sockaddr_in*)&storage)->sin_port);
- } else {
- *port = ntohs(((struct sockaddr_in6*)&storage)->sin6_port);
- }
-
- *address = bmx_stdc_sockaddrestorage_address(&storage);
- }
-
- return res;
- }
- #if _WIN32
- static void CleanupWSA(){
- WSACleanup();
- }
- #endif
- void bb_stdc_Startup(){
- #if _WIN32
- WSADATA ws;
-
- WSAStartup( MAKEWORD(2, 2),&ws );
- atexit( CleanupWSA );
- #endif
- stdin_=stdin;
- stdout_=stdout;
- stderr_=stderr;
-
- }
|