Skip to content

Commit

Permalink
Fixed timespec/timeval errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Palmatè committed Apr 26, 2022
1 parent 3499196 commit 12bdd25
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 91 deletions.
2 changes: 1 addition & 1 deletion library/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
7 changes: 6 additions & 1 deletion library/socket/gethostid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions library/socket/select_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 17 additions & 18 deletions library/socket/wait_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
53 changes: 25 additions & 28 deletions library/time/clock_getres.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,29 @@
#include <time.h>

/* 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;
}
10 changes: 4 additions & 6 deletions library/time/clock_gettime.c
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
45 changes: 21 additions & 24 deletions library/time/clock_settime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions library/time/settimeofday.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions library/wchar/iswxdigit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <wctype.h>
#endif /* _WCTYPE_HEADERS_H */

#include <ctype.h>

int
iswxdigit(wint_t c) {
ENTER();
Expand Down

0 comments on commit 12bdd25

Please sign in to comment.