From 12bdd250331a72417e553d396773bcc968cf755b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrea=20Palmat=C3=A8?= Date: Tue, 26 Apr 2022 11:31:14 +0200 Subject: [PATCH] Fixed timespec/timeval errors --- library/include/time.h | 2 +- library/socket/gethostid.c | 7 ++++- library/socket/select_signal.c | 18 ++++++------ library/socket/wait_select.c | 35 +++++++++++----------- library/time/clock_getres.c | 53 ++++++++++++++++------------------ library/time/clock_gettime.c | 10 +++---- library/time/clock_settime.c | 45 ++++++++++++++--------------- library/time/settimeofday.c | 4 +-- library/wchar/iswxdigit.c | 6 ++-- 9 files changed, 89 insertions(+), 91 deletions(-) diff --git a/library/include/time.h b/library/include/time.h index 3402e6b6..3c400920 100755 --- a/library/include/time.h +++ b/library/include/time.h @@ -70,7 +70,7 @@ extern char *strptime(const char *buf, const char *fmt, struct tm *timeptr); /* Timespec declaration */ struct timespec { - time_t tv_secs; + time_t tv_sec; long tv_nsec; }; diff --git a/library/socket/gethostid.c b/library/socket/gethostid.c index 04398d18..5235443e 100644 --- a/library/socket/gethostid.c +++ b/library/socket/gethostid.c @@ -2,11 +2,16 @@ * $Id: socket_gethostid.c,v 1.3 2006-01-08 12:04:24 clib2devs Exp $ */ +#ifndef _UNISTD_HEADERS_H +#include "unistd_headers.h" +#endif /* _UNISTD_HEADERS_H */ + #ifndef _SOCKET_HEADERS_H #include "socket_headers.h" #endif /* _SOCKET_HEADERS_H */ -long gethostid(void) { +long +gethostid() { long result; ENTER(); diff --git a/library/socket/select_signal.c b/library/socket/select_signal.c index 23659d03..649eab13 100644 --- a/library/socket/select_signal.c +++ b/library/socket/select_signal.c @@ -143,9 +143,9 @@ static struct DateStamp * timeval_to_datestamp(struct DateStamp *ds, const struct timeval *tv) { assert(ds != NULL && tv != NULL); - ds->ds_Days = (tv->tv_secs / (24 * 60 * 60)); - ds->ds_Minute = (tv->tv_secs % (24 * 60 * 60)) / 60; - ds->ds_Tick = (tv->tv_secs % 60) * TICKS_PER_SECOND + (TICKS_PER_SECOND * tv->tv_micro) / 1000000; + ds->ds_Days = (tv->tv_sec / (24 * 60 * 60)); + ds->ds_Minute = (tv->tv_sec % (24 * 60 * 60)) / 60; + ds->ds_Tick = (tv->tv_sec % 60) * TICKS_PER_SECOND + (TICKS_PER_SECOND * tv->tv_usec) / 1000000; fix_datestamp(ds); @@ -434,8 +434,8 @@ __select(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, s SHOWPOINTER(timeout); if (timeout != NULL) { - SHOWVALUE(timeout->tv_secs); - SHOWVALUE(timeout->tv_micro); + SHOWVALUE(timeout->tv_sec); + SHOWVALUE(timeout->tv_usec); } assert(__SocketBase != NULL); @@ -530,7 +530,7 @@ __select(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, s SHOWMSG("we have to deal with sockets"); /* Wait for file input, too? */ - if ((total_file_fd > 0) && (timeout == NULL || timeout->tv_secs > 0 || timeout->tv_micro > 0)) { + if ((total_file_fd > 0) && (timeout == NULL || timeout->tv_sec > 0 || timeout->tv_usec > 0)) { struct DateStamp stop_when; struct timeval zero; ULONG break_mask; @@ -626,8 +626,8 @@ __select(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, s Delay(1); /* This tells WaitSelect() to poll the sockets for input. */ - zero.tv_secs = 0; - zero.tv_micro = 0; + zero.tv_sec = 0; + zero.tv_usec = 0; /* Signals to stop on; we want to stop when a break signal arrives. */ break_mask = signal_mask; @@ -786,7 +786,7 @@ __select(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, s } } else { /* Wait for file input? */ - if (timeout == NULL || timeout->tv_secs > 0 || timeout->tv_micro > 0) { + if (timeout == NULL || timeout->tv_sec > 0 || timeout->tv_usec > 0) { struct DateStamp stop_when; BOOL got_input; BOOL got_output; diff --git a/library/socket/wait_select.c b/library/socket/wait_select.c index c749f129..58f904b5 100644 --- a/library/socket/wait_select.c +++ b/library/socket/wait_select.c @@ -6,26 +6,25 @@ #include "socket_headers.h" #endif /* _SOCKET_HEADERS_H */ -int -waitselect(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, struct timeval *timeout, unsigned long *signal_mask) -{ - struct timeval other_timeout; - int result; +int +waitselect(int num_fds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, struct timeval *timeout, + unsigned long *signal_mask) { + struct timeval other_timeout; + int result; - /* This is a workaround for a bug in the Roadshow TCP/IP stack which has been - fixed long ago: if a signal is received, as given in the 'signal_mask' - parameter, the WaitSelect() function may not detect it if the timeout - is zero. */ - if (signal_mask != NULL && timeout != NULL && timeout->tv_secs == 0 && timeout->tv_micro == 0) - { - /* Substitute a ten microsecond timeout. */ - other_timeout.tv_secs = 0; - other_timeout.tv_micro = 10000; + /* This is a workaround for a bug in the Roadshow TCP/IP stack which has been + fixed long ago: if a signal is received, as given in the 'signal_mask' + parameter, the WaitSelect() function may not detect it if the timeout + is zero. */ + if (signal_mask != NULL && timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0) { + /* Substitute a ten microsecond timeout. */ + other_timeout.tv_sec = 0; + other_timeout.tv_usec = 10000; - timeout = &other_timeout; - } + timeout = &other_timeout; + } - result = __select(num_fds, read_fds, write_fds, except_fds, timeout, signal_mask); + result = __select(num_fds, read_fds, write_fds, except_fds, timeout, signal_mask); - return (result); + return (result); } diff --git a/library/time/clock_getres.c b/library/time/clock_getres.c index 416374d9..359362d5 100644 --- a/library/time/clock_getres.c +++ b/library/time/clock_getres.c @@ -9,32 +9,29 @@ #include /* Get resolution of clock. */ -int clock_getres(clockid_t clock_id, struct timespec *res) -{ - ENTER(); - - int result = -1; - - switch (clock_id) - { - case CLOCK_MONOTONIC: - case CLOCK_REALTIME: - { - /* This implementation assumes that the realtime clock has a - resolution higher than 1 second. This is the case for any - reasonable implementation. */ - res->tv_sec = 0; - res->tv_nsec = 1000000000 / 1000; - - result = 0; - } - break; - - default: - __set_errno(EINVAL); - break; - } - - RETURN(result); - return result; +int clock_getres(clockid_t clock_id, struct timespec *res) { + ENTER(); + + int result = -1; + + switch (clock_id) { + case CLOCK_MONOTONIC: + case CLOCK_REALTIME: { + /* This implementation assumes that the realtime clock has a + resolution higher than 1 second. This is the case for any + reasonable implementation. */ + res->tv_sec = 0; + res->tv_nsec = 1000000000 / 1000; + + result = 0; + } + break; + + default: + __set_errno(EINVAL); + break; + } + + RETURN(result); + return result; } diff --git a/library/time/clock_gettime.c b/library/time/clock_gettime.c index e217584e..0892c604 100644 --- a/library/time/clock_gettime.c +++ b/library/time/clock_gettime.c @@ -1,9 +1,7 @@ /* - * $Id: time_gettime.c,v 1.1 2020-01-31 16:55:42 clib2devs Exp $ + * $Id: time_clock_gettime.c,v 1.1 2020-01-31 16:55:42 clib2devs Exp $ */ -#define __USE_OLD_TIMEVAL__ - #ifndef _STDIO_HEADERS_H #include "stdio_headers.h" #endif /* _STDIO_HEADERS_H */ @@ -43,7 +41,7 @@ int clock_gettime(clockid_t clk_id, struct timespec *t) int8 dstime = -1; //Set default value for tv - tv.tv_secs = tv.tv_micro = 0; + tv.tv_sec = tv.tv_usec = 0; GetTimezoneAttrs(NULL, TZA_UTCOffset, &gmtoffset, TZA_TimeFlag, &dstime, TAG_DONE); if (result == 0) @@ -79,14 +77,14 @@ int clock_gettime(clockid_t clk_id, struct timespec *t) if (clk_id == CLOCK_MONOTONIC) { t->tv_sec = tv.tv_sec; - t->tv_nsec = tv.tv_micro * 1000; + t->tv_nsec = tv.tv_usec * 1000; } else { /* 2922 is the number of days between 1.1.1970 and 1.1.1978 */ tv.tv_sec += (2922 * 24 * 60 + gmtoffset) * 60; t->tv_sec = tv.tv_sec; - t->tv_nsec = tv.tv_micro * 1000; + t->tv_nsec = tv.tv_usec * 1000; } } } diff --git a/library/time/clock_settime.c b/library/time/clock_settime.c index f527321c..ddf3b9ea 100644 --- a/library/time/clock_settime.c +++ b/library/time/clock_settime.c @@ -5,54 +5,52 @@ #define _GNU_SOURCE #ifndef _TIME_HEADERS_H + #include "time_headers.h" + #endif /* _TIME_HEADERS_H */ #ifndef _STDIO_HEADERS_H + #include "stdio_headers.h" + #endif /* _STDIO_HEADERS_H */ #ifndef _UNISTD_HEADERS_H #include "unistd_headers.h" #endif /* _UNISTD_HEADERS_H */ -extern struct MsgPort NOCOMMON *__timer_port; +extern struct MsgPort NOCOMMON *__timer_port; extern struct TimeRequest NOCOMMON *__timer_request; extern BOOL NOCOMMON __timer_busy; int -clock_settime(clockid_t clk_id, const struct timespec *t) -{ +clock_settime(clockid_t clk_id, const struct timespec *t) { ENTER(); int result = -1; - - if ((clk_id & ~(CLOCK_MONOTONIC | CLOCK_REALTIME)) != 0) - { + + if ((clk_id & ~(CLOCK_MONOTONIC | CLOCK_REALTIME)) != 0) { __set_errno(EINVAL); RETURN(-1); return -1; } - - if (__timer_busy) - { + + if (__timer_busy) { __set_errno(EAGAIN); RETURN(result); return result; } - + DECLARE_TIMEZONEBASE(); - switch (clk_id) - { - case CLOCK_REALTIME: - { + switch (clk_id) { + case CLOCK_REALTIME: { int32 __gmtoffset = 0; int8 __dstime = -1; - if (ITimezone) - { + if (ITimezone) { GetTimezoneAttrs(NULL, TZA_UTCOffset, &__gmtoffset, TZA_TimeFlag, &__dstime, TAG_DONE); } __timer_busy = TRUE; @@ -62,28 +60,27 @@ clock_settime(clockid_t clk_id, const struct timespec *t) __timer_request->Time.Seconds = t->tv_sec - ((2922 * 24 * 60 + __gmtoffset) * 60); __timer_request->Time.Microseconds = t->tv_nsec / 1000; - DoIO((struct IORequest *)__timer_request); + DoIO((struct IORequest *) __timer_request); GetMsg(__timer_port); result = 0; __set_errno(0); } - break; - - case CLOCK_MONOTONIC: - { + break; + + case CLOCK_MONOTONIC: { struct timeval tv; TIMESPEC_TO_TIMEVAL(&tv, t); __global_clib2->clock.Seconds = tv.tv_sec; __global_clib2->clock.Microseconds = tv.tv_usec; } - break; - + break; + default: __set_errno(EINVAL); break; } - + __timer_busy = FALSE; RETURN(result); diff --git a/library/time/settimeofday.c b/library/time/settimeofday.c index ad4c3ced..8c77284d 100644 --- a/library/time/settimeofday.c +++ b/library/time/settimeofday.c @@ -40,8 +40,8 @@ settimeofday(const struct timeval *t, const struct timezone *tz) { __gmtoffset = tz->tz_minuteswest; } /* 2922 is the number of days between 1.1.1970 and 1.1.1978 */ - __timer_request->Time.Seconds = t->tv_secs - ((2922 * 24 * 60 + __gmtoffset) * 60); - __timer_request->Time.Microseconds = t->tv_micro; + __timer_request->Time.Seconds = t->tv_sec - ((2922 * 24 * 60 + __gmtoffset) * 60); + __timer_request->Time.Microseconds = t->tv_usec; DoIO((struct IORequest *) __timer_request); GetMsg(__timer_port); diff --git a/library/wchar/iswxdigit.c b/library/wchar/iswxdigit.c index 0350b276..7ebb63d6 100644 --- a/library/wchar/iswxdigit.c +++ b/library/wchar/iswxdigit.c @@ -2,12 +2,14 @@ * $Id: wctype_iswxdigit.c,v 1.4 2021-02-04 00:17:27 clib2devs Exp $ */ +#ifndef _WCHAR_HEADERS_H +#include "wchar_headers.h" +#endif /* _WCHAR_HEADERS_H */ + #ifndef _WCTYPE_HEADERS_H #include #endif /* _WCTYPE_HEADERS_H */ -#include - int iswxdigit(wint_t c) { ENTER();