diff --git a/contrib/hbct/dattime3.c b/contrib/hbct/dattime3.c index 073e52fb4d..f1ef5a64dc 100644 --- a/contrib/hbct/dattime3.c +++ b/contrib/hbct/dattime3.c @@ -147,14 +147,18 @@ HB_FUNC( SETTIME ) st.wMilliseconds = ( WORD ) iTime[ 3 ] * 10; fResult = SetLocalTime( &st ); #elif defined( HB_OS_LINUX ) && ! defined( HB_OS_ANDROID ) && ! defined( __WATCOMC__ ) - /* stime() exists only in SVr4, SVID, X/OPEN and Linux */ - HB_ULONG lNewTime; - time_t tm; - - lNewTime = iTime[ 0 ] * 3600 + iTime[ 1 ] * 60 + iTime[ 2 ]; - tm = time( NULL ); - tm += lNewTime - ( tm % 86400 ); - fResult = stime( &tm ) == 0; + struct timespec ts; + time_t now = time(NULL); + struct tm *tm_info = localtime(&now); + + tm_info->tm_hour = iTime[ 0 ]; + tm_info->tm_min = iTime[ 1 ]; + tm_info->tm_sec = iTime[ 2 ]; + + ts.tv_sec = mktime(tm_info); + ts.tv_nsec = 0; + + fResult = clock_settime(CLOCK_REALTIME, &ts) == 0; #elif defined( HB_OS_DOS ) union REGS regs; regs.h.ah = 45; @@ -190,14 +194,13 @@ HB_FUNC( SETDATE ) st.wDayOfWeek = ( WORD ) hb_dateJulianDOW( lDate ); fResult = SetLocalTime( &st ); #elif defined( HB_OS_LINUX ) && ! defined( HB_OS_ANDROID ) && ! defined( __WATCOMC__ ) - /* stime() exists only in SVr4, SVID, X/OPEN and Linux */ - long lNewDate; - time_t tm; - - lNewDate = lDate - hb_dateEncode( 1970, 1, 1 ); - tm = time( NULL ); - tm = lNewDate * 86400 + ( tm % 86400 ); - fResult = stime( &tm ) == 0; + struct timespec ts; + struct tm tm_info = { .tm_year = iYear - 1900, .tm_mon = iMonth - 1, .tm_mday = iDay }; + + ts.tv_sec = mktime(&tm_info); + ts.tv_nsec = 0; + + fResult = clock_settime(CLOCK_REALTIME, &ts) == 0; #elif defined( HB_OS_DOS ) union REGS regs; regs.h.ah = 43; diff --git a/contrib/hbfbird/firebird.c b/contrib/hbfbird/firebird.c index 66d869c498..2cc73d0de8 100644 --- a/contrib/hbfbird/firebird.c +++ b/contrib/hbfbird/firebird.c @@ -149,41 +149,42 @@ HB_FUNC( FBCREATEDB ) hb_retnl( 0 ); } -HB_FUNC( FBCONNECT ) +HB_FUNC(FBCONNECT) { - ISC_STATUS_ARRAY status; - isc_db_handle db = ( isc_db_handle ) 0; - const char * db_connect = hb_parcx( 1 ); - const char * user = hb_parcx( 2 ); - const char * passwd = hb_parcx( 3 ); - char dpb[ 128 ]; - short i = 0; - int len; - - /* FIXME: Possible buffer overflow. Use hb_snprintf(). */ - dpb[ i++ ] = isc_dpb_version1; - dpb[ i++ ] = isc_dpb_user_name; - len = ( int ) strlen( user ); - if( len > ( int ) ( sizeof( dpb ) - i - 4 ) ) - len = ( int ) ( sizeof( dpb ) - i - 4 ); - dpb[ i++ ] = ( char ) len; - hb_strncpy( &( dpb[ i ] ), user, len ); - i += ( short ) len; - dpb[ i++ ] = isc_dpb_password; - len = ( int ) strlen( passwd ); - if( len > ( int ) ( sizeof( dpb ) - i - 2 ) ) - len = ( int ) ( sizeof( dpb ) - i - 2 ); - dpb[ i++ ] = ( char ) len; - hb_strncpy( &( dpb[ i ] ), passwd, len ); - i += ( short ) len; - - if( isc_attach_database( status, 0, db_connect, &db, i, dpb ) ) - hb_retnl( isc_sqlcode( status ) ); - else - hb_FB_db_handle_ret( db ); + ISC_STATUS_ARRAY status; + isc_db_handle db = (isc_db_handle)0; + const char *db_connect = hb_parcx(1); + const char *user = hb_parcx(2); + const char *passwd = hb_parcx(3); + char dpb[128]; + short i = 0; + int len; + + i += hb_snprintf(dpb + i, sizeof(dpb) - i, "%c", isc_dpb_version1); + i += hb_snprintf(dpb + i, sizeof(dpb) - i, "%c", isc_dpb_user_name); + len = (int)strlen(user); + if (len > (int)(sizeof(dpb) - i - 4)) + len = (int)(sizeof(dpb) - i - 4); + i += hb_snprintf(dpb + i, sizeof(dpb) - i, "%c", (char)len); + hb_strncpy(&(dpb[i]), user, len); + i += (short)len; + i += hb_snprintf(dpb + i, sizeof(dpb) - i, "%c", isc_dpb_password); + len = (int)strlen(passwd); + if (len > (int)(sizeof(dpb) - i - 2)) + len = (int)(sizeof(dpb) - i - 2); + i += hb_snprintf(dpb + i, sizeof(dpb) - i, "%c", (char)len); + hb_strncpy(&(dpb[i]), passwd, len); + i += (short)len; + + if (isc_attach_database(status, 0, db_connect, &db, i, dpb)) + hb_retnl(isc_sqlcode(status)); + else + hb_FB_db_handle_ret(db); } + + HB_FUNC( FBCLOSE ) { isc_db_handle db = hb_FB_db_handle_par( 1 ); diff --git a/contrib/hbnf/setdate.c b/contrib/hbnf/setdate.c index 6587fb5df2..9ba42c8f56 100644 --- a/contrib/hbnf/setdate.c +++ b/contrib/hbnf/setdate.c @@ -1,4 +1,5 @@ -/* Rewritten in 2012 by Viktor Szakats and kept in the +/* + * Rewritten in 2012 by Viktor Szakats and kept in the public domain. This is an original work by Glenn Scott and is placed in the public domain. @@ -57,14 +58,14 @@ HB_FUNC( FT_SETDATE ) } #elif defined( HB_OS_LINUX ) && ! defined( HB_OS_ANDROID ) && ! defined( __WATCOMC__ ) { - /* stime() exists only in SVr4, SVID, X/OPEN and Linux */ - long lNewDate; - time_t tm; + /* clock_settime() per a sistemes Linux moderns */ + struct timespec ts; + struct tm tm_info = { .tm_year = iYear - 1900, .tm_mon = iMonth - 1, .tm_mday = iDay }; - lNewDate = lDate - hb_dateEncode( 1970, 1, 1 ); - tm = time( NULL ); - tm = lNewDate * 86400 + ( tm % 86400 ); - fResult = stime( &tm ) == 0; + ts.tv_sec = mktime(&tm_info); + ts.tv_nsec = 0; + + fResult = clock_settime(CLOCK_REALTIME, &ts) == 0; } #elif defined( HB_OS_DOS ) { diff --git a/contrib/hbnf/settime.c b/contrib/hbnf/settime.c index 74bf246c79..b4a1cf5ccf 100644 --- a/contrib/hbnf/settime.c +++ b/contrib/hbnf/settime.c @@ -1,4 +1,5 @@ -/* Rewritten in 2012 by Viktor Szakats and kept in the +/* + * Rewritten in 2012 by Viktor Szakats and kept in the public domain. This is an original work by Glenn Scott and is placed in the public domain. @@ -67,14 +68,19 @@ HB_FUNC( FT_SETTIME ) } #elif defined( HB_OS_LINUX ) && ! defined( HB_OS_ANDROID ) && ! defined( __WATCOMC__ ) { - /* stime() exists only in SVr4, SVID, X/OPEN and Linux */ - HB_ULONG lNewTime; - time_t tm; + /* clock_settime() per a sistemes Linux moderns */ + struct timespec ts; + time_t now = time(NULL); + struct tm *tm_info = localtime(&now); - lNewTime = iHour * 3600 + iMinute * 60 + iSeconds; - tm = time( NULL ); - tm += lNewTime - ( tm % 86400 ); - fResult = stime( &tm ) == 0; + tm_info->tm_hour = iHour; + tm_info->tm_min = iMinute; + tm_info->tm_sec = iSeconds; + + ts.tv_sec = mktime(tm_info); + ts.tv_nsec = 0; + + fResult = clock_settime(CLOCK_REALTIME, &ts) == 0; } #elif defined( HB_OS_DOS ) { diff --git a/contrib/hbtest/core.prg b/contrib/hbtest/core.prg index 7aca6a595c..d0995b0dc4 100644 --- a/contrib/hbtest/core.prg +++ b/contrib/hbtest/core.prg @@ -137,8 +137,10 @@ PROCEDURE hbtest_Call( cBlock, bBlock, xResultExpected ) cBlock := "[Preprocessor error]" lPPError := .T. ENDIF - - cLangOld := hb_langSelect( "en" ) /* to always have RTEs in one language */ + + + cLangOld := __hb_langSelect( "en" ) /* to always have RTEs in one language */ + IF ! s_lBanner s_lBanner := .T. @@ -153,7 +155,7 @@ PROCEDURE hbtest_Call( cBlock, bBlock, xResultExpected ) lRTE := .T. END SEQUENCE - hb_langSelect( cLangOld ) + __hb_langSelect( cLangOld ) IF lRTE lFailed := ! XToStr( xResult ) == XToStr( xResultExpected ) diff --git a/src/rtl/arc4.c b/src/rtl/arc4.c index 1eaf74335c..703649e2c3 100644 --- a/src/rtl/arc4.c +++ b/src/rtl/arc4.c @@ -48,36 +48,11 @@ #include "hbdate.h" #include "hbthread.h" -/* XXX: Check and possibly extend this to other Unix-like platforms */ -#if ( defined( HB_OS_BSD ) && ! defined( HB_OS_DARWIN ) ) || \ - ( defined( HB_OS_LINUX ) && \ - ! defined( HB_OS_ANDROID ) && \ - ! defined( __WATCOMC__ ) && \ - ! defined( __EMSCRIPTEN__ ) ) -# define HAVE_SYS_SYSCTL_H -# define HAVE_DECL_CTL_KERN -# define HAVE_DECL_KERN_RANDOM -# if defined( HB_OS_LINUX ) -# define HAVE_DECL_RANDOM_UUID -# endif -#endif - #if defined( HB_OS_WIN ) # include #elif defined( HB_OS_DOS ) || defined( HB_OS_OS2 ) # include #else -# if ! defined( __WATCOMC__ ) -# include -# endif -# include -# include -# ifdef HAVE_SYS_SYSCTL_H -# include -# if ! defined( HB_OS_LINUX ) && defined( KERN_ARND ) -# define HAVE_DECL_KERN_ARND -# endif -# endif # include # include #endif @@ -203,182 +178,7 @@ static int arc4_seed_win( void ) } #endif /* HB_OS_WIN */ -#if defined( HAVE_SYS_SYSCTL_H ) - -#if defined( HAVE_DECL_CTL_KERN ) && defined( HAVE_DECL_KERN_RANDOM ) && \ - defined( HAVE_DECL_RANDOM_UUID ) - -#define TRY_SEED_SYSCTL_LINUX -static int arc4_seed_sysctl_linux( void ) -{ - /* - * Based on code by William Ahern, this function tries to use the - * RANDOM_UUID sysctl to get entropy from the kernel. This can work - * even if /dev/urandom is inaccessible for some reason (e.g., we're - * running in a chroot). - */ - int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID }; - HB_U8 buf[ ADD_ENTROPY ]; - size_t len, n; - unsigned int i; - int any_set; - - memset( buf, 0, sizeof( buf ) ); - - for( len = 0; len < sizeof( buf ); len += n ) - { - n = sizeof( buf ) - len; - - if( sysctl( mib, 3, &buf[ len ], &n, NULL, 0 ) != 0 ) - return -1; - } - - /* make sure that the buffer actually got set. */ - for( i = 0, any_set = 0; i < sizeof( buf ); ++i ) - any_set |= buf[ i ]; - - if( ! any_set ) - return -1; - - arc4_addrandom( buf, sizeof( buf ) ); - memset( buf, 0, sizeof( buf ) ); - - return 0; -} -#endif /* HAVE_DECL_CTL_KERN && HAVE_DECL_KERN_RANDOM && HAVE_DECL_RANDOM_UUID */ - -#if defined( HAVE_DECL_CTL_KERN ) && defined( HAVE_DECL_KERN_ARND ) - -#define TRY_SEED_SYSCTL_BSD -static int arc4_seed_sysctl_bsd( void ) -{ - /* - * Based on code from William Ahern and from OpenBSD, this function - * tries to use the KERN_ARND syscall to get entropy from the kernel. - * This can work even if /dev/urandom is inaccessible for some reason - * (e.g., we're running in a chroot). - */ - int mib[] = { CTL_KERN, KERN_ARND }; - HB_U8 buf[ ADD_ENTROPY ]; - size_t len, n; - int i, any_set; - - memset( buf, 0, sizeof( buf ) ); - - len = sizeof( buf ); - if( sysctl( mib, 2, buf, &len, NULL, 0 ) == -1 ) - { - for( len = 0; len < sizeof( buf ); len += sizeof( unsigned ) ) - { - n = sizeof( unsigned ); - - if( n + len > sizeof( buf ) ) - n = len - sizeof( buf ); - - if( sysctl( mib, 2, &buf[ len ], &n, NULL, 0 ) == -1 ) - return -1; - } - } - - /* make sure that the buffer actually got set. */ - for( i = any_set = 0; i < ( int ) sizeof( buf ); ++i ) - any_set |= buf[ i ]; - - if( ! any_set ) - return -1; - - arc4_addrandom( buf, sizeof( buf ) ); - memset( buf, 0, sizeof( buf ) ); - - return 0; -} -#endif /* HAVE_DECL_CTL_KERN && HAVE_DECL_KERN_ARND */ - -#endif /* defined( HAVE_SYS_SYSCTL_H ) */ - -#if defined( HB_OS_LINUX ) - -#define TRY_SEED_PROC_SYS_KERNEL_RANDOM_UUID -static _HB_INLINE_ int hex_char_to_int( char c ) -{ - switch( c ) - { - case '0': return 0; - case '1': return 1; - case '2': return 2; - case '3': return 3; - case '4': return 4; - case '5': return 5; - case '6': return 6; - case '7': return 7; - case '8': return 8; - case '9': return 9; - case 'A': case 'a': return 10; - case 'B': case 'b': return 11; - case 'C': case 'c': return 12; - case 'D': case 'd': return 13; - case 'E': case 'e': return 14; - case 'F': case 'f': return 15; - } - - return -1; -} - -static int arc4_seed_proc_sys_kernel_random_uuid( void ) -{ - /* - * Occasionally, somebody will make /proc/sys accessible in a chroot, - * but not /dev/urandom. Let's try /proc/sys/kernel/random/uuid. - * Its format is stupid, so we need to decode it from hex. - */ - char buf[ 128 ]; - HB_U8 entropy[ 64 ]; - int bytes, i, nybbles; - - for( bytes = 0; bytes < ADD_ENTROPY; ) - { - int fd = open( "/proc/sys/kernel/random/uuid", O_RDONLY, 0 ); - int n; - - if( fd < 0 ) - return -1; - - n = read( fd, buf, sizeof( buf ) ); - close( fd ); - - if( n <= 0 ) - return -1; - - memset( entropy, 0, sizeof( entropy ) ); - for( i = nybbles = 0; i < n; ++i ) - { - if( HB_ISXDIGIT( buf[ i ] ) ) - { - int nyb = hex_char_to_int( buf[ i ] ); - - if( nybbles & 1 ) - entropy[ nybbles / 2 ] |= nyb; - else - entropy[ nybbles / 2 ] |= nyb << 4; - - ++nybbles; - } - } - if( nybbles < 2 ) - return -1; - - arc4_addrandom( entropy, nybbles / 2 ); - bytes += nybbles / 2; - } - - memset( entropy, 0, sizeof( entropy ) ); - memset( buf, 0, sizeof( buf ) ); - - return 0; -} -#endif /* HB_OS_LINUX */ - -#if defined( HB_OS_UNIX ) +#if defined( HB_OS_LINUX ) || defined( HB_OS_UNIX ) #define TRY_SEED_URANDOM static int arc4_seed_urandom( void ) @@ -417,7 +217,7 @@ static int arc4_seed_urandom( void ) return -1; } -#endif /* HB_OS_UNIX */ +#endif /* HB_OS_LINUX || HB_OS_UNIX */ static int arc4_seed_rand( void ) { @@ -455,32 +255,12 @@ static void arc4_seed( void ) ok = 1; #endif -#if defined( TRY_SEED_PROC_SYS_KERNEL_RANDOM_UUID ) - if( arc4_seed_proc_sys_kernel_random_uuid() == 0 ) - ok = 1; -#endif - -#if defined( TRY_SEED_SYSCTL_LINUX ) /* - * Apparently Linux is deprecating sysctl, and spewing warning - * messages when you try to use it. To avoid dmesg spamming, - * only try this if no previous method worked. - */ - if( ! ok && arc4_seed_sysctl_linux() == 0 ) - ok = 1; -#endif - -#if defined( TRY_SEED_SYSCTL_BSD ) - if( arc4_seed_sysctl_bsd() == 0 ) - ok = 1; -#endif - - /* - * If nothing else worked or there is no specific seeding - * method for the current platform, fall back to rand(). - * In case an existing platform-specific method had a - * (transient) failure, it will be re-tried at the next - * seeding cycle. + * If nothing else worked or hi ha cap mètode específic de seeding + * per a la plataforma actual, recorre a rand(). + * Si un mètode de plataforma específic existent tenia un + * (fallada transitòria, es tornarà a intentar en el següent + * cicle de seeding. */ if( ! ok ) arc4_seed_rand(); @@ -660,7 +440,7 @@ HB_U32 hb_arc4random_uniform( HB_U32 upper_bound ) } else { - /* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */ + /* (2**32 - (upper_bound * 2)) % upper_bound == 2**32 % upper_bound when upper_bound <= 2**31 */ min = ( ( 0xffffffff - ( upper_bound * 2 ) ) + 1 ) % upper_bound; } #endif @@ -680,3 +460,8 @@ HB_U32 hb_arc4random_uniform( HB_U32 upper_bound ) return r % upper_bound; } + +HB_FUNC( HB_ARC4RANDOM ) +{ + hb_retni( hb_arc4random() ); +}