Corrected gettimeofday() handling

This commit is contained in:
Gerhard Rieger 2021-10-31 13:04:12 +01:00
parent 4cebaf45e4
commit 25cef1c540
6 changed files with 49 additions and 35 deletions

View file

@ -68,7 +68,11 @@ Documentation:
Thanks to Emanuele Torre for reporting this issue.
Corrected more typos and added missing bug info to CHANGES, performed
some non functional corrections
some non functional corrections.
Porting:
Corrected building when clock_gettime() not available, with or without
gettimeofday().
####################### V 1.7.4.1:

View file

@ -156,6 +156,9 @@
/* Define if you have the ftruncate64 function */
#undef HAVE_FTRUNCATE64
/* Define if you have the gettimeofday function */
#undef HAVE_PROTOTYPE_LIB_gettimeofday
/* Define if you have the clock_gettime function */
#undef HAVE_CLOCK_GETTIME

View file

@ -1,4 +1,4 @@
dnl source: configure.in
dnl source: configure.ac
dnl Copyright Gerhard Rieger and contributors (see file CHANGES)
dnl Published under the GNU General Public License V.2, see file COPYING
@ -1491,6 +1491,8 @@ AC_CHECK_LIB(bsd, openpty,
AC_CHECK_LIB(util, openpty,
[LIBS="-lutil $LIBS"; AC_DEFINE(HAVE_OPENPTY)])
AC_CHECK_PROTOTYPE_LIB(gettimeofday)
AC_CHECK_LIB(rt, clock_gettime,
[LIBS="-lrt $LIBS"; AC_DEFINE(HAVE_CLOCK_GETTIME)])

12
error.c
View file

@ -49,7 +49,7 @@ struct diag_opts diagopts =
static void msg2(
#if HAVE_CLOCK_GETTIME
struct timespec *now,
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval *now,
#else
time_t *now,
@ -269,7 +269,7 @@ void msg(int level, const char *format, ...) {
diag_dgram.op = DIAG_OP_MSG;
#if HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &diag_dgram.now);
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
gettimeofday(&diag_dgram.now, NULL);
#else
diag_dgram.now = time(NULL);
@ -295,7 +295,7 @@ void msg(int level, const char *format, ...) {
void msg2(
#if HAVE_CLOCK_GETTIME
struct timespec *now,
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval *now,
#else
time_t *now,
@ -315,7 +315,7 @@ void msg2(
#if HAVE_CLOCK_GETTIME
epoch = now->tv_sec;
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
epoch = now->tv_sec;
#else
epoch = *now;
@ -329,7 +329,7 @@ void msg2(
if (diagopts.micros) {
#if HAVE_CLOCK_GETTIME
micros = now->tv_nsec/1000;
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
micros = now->tv_usec;
#else
micros = 0;
@ -387,7 +387,7 @@ void diag_flush(void) {
/* we want the actual time, not when this dgram was sent */
#if HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &recv_dgram.now);
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
gettimeofday(&recv_dgram.now, NULL);
#else
recv_dgram.now = time(NULL);

View file

@ -217,7 +217,7 @@ struct diag_dgram {
enum diag_op op;
#if HAVE_CLOCK_GETTIME
struct timespec now;
#elif HAVE_GETTIMEOFDAY
#elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval now;
#else
time_t now;

57
socat.c
View file

@ -1193,40 +1193,45 @@ int _socat(void) {
returns 0 on success or -1 if an error occurred */
int gettimestamp(char *timestamp) {
size_t bytes;
#if HAVE_GETTIMEOFDAY || 1
#if HAVE_CLOCK_GETTIME
struct timespec now;
#elif HAVE_PROTOTYPE_LIB_gettimeofday
struct timeval now;
int result;
#endif /* !HAVE_PROTOTYPE_LIB_gettimeofday */
time_t nowt;
#else /* !HAVE_GETTIMEOFDAY */
time_t now;
#endif /* !HAVE_GETTIMEOFDAY */
int result;
#if HAVE_GETTIMEOFDAY || 1
result = gettimeofday(&now, NULL);
#if HAVE_CLOCK_GETTIME
result = clock_gettime(CLOCK_REALTIME, &now);
if (result < 0) {
return result;
} else {
nowt = now.tv_sec;
#if HAVE_STRFTIME
bytes = strftime(timestamp, 20, "%Y/%m/%d %H:%M:%S", localtime(&nowt));
bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_usec);
#else
strcpy(timestamp, ctime(&nowt));
bytes = strlen(timestamp);
#endif
}
#else /* !HAVE_GETTIMEOFDAY */
now = time(NULL); if (now == (time_t)-1) {
nowt = now.tv_sec;
#elif HAVE_PROTOTYPE_LIB_gettimeofday
result = Gettimeofday(&now, NULL);
if (result < 0) {
return result;
}
nowt = now.tv_sec;
#else
nowt = time(NULL);
if (nowt == (time_t)-1) {
return -1;
} else {
#if HAVE_STRFTIME
bytes = strftime(timestamp, 21, "%Y/%m/%d %H:%M:%S ", localtime(&now));
#else
strcpy(timestamp, ctime(&now));
bytes = strlen(timestamp);
#endif
}
#endif /* !HAVE_GETTIMEOFDAY */
#endif
#if HAVE_STRFTIME
bytes = strftime(timestamp, 20, "%Y/%m/%d %H:%M:%S", localtime(&nowt));
#if HAVE_CLOCK_GETTIME
bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_nsec/1000);
#elif HAVE_PROTOTYPE_LIB_gettimeofday
bytes += sprintf(timestamp+19, "."F_tv_usec" ", now.tv_usec);
#else
strncpy(&timestamp[bytes++], " ", 2);
#endif
#else
strcpy(timestamp, ctime(&nowt));
bytes = strlen(timestamp);
#endif
return 0;
}