Skip to content

Conversation

@GitMensch
Copy link
Collaborator

@GitMensch
Copy link
Collaborator Author

Hm, for some reason I've trashed this PR...

But I'd still like the review for the following code, which works on any system I've tested:

/* ftime() is consided obsolete.  But it's all we have for
millisecond precision on older compilers/systems.  We'll
use clock_gettime() or gettimeofday() when available. */

#if defined( _POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 199309L) \
     && (!defined( __MINGW32__) || defined( CLOCK_REALTIME))
   /* only newer MinGW environments have clock_gettime and those
      have CLOCK_REALTIME as a macro */
   #define HAVE_CLOCK_GETTIME
#elif defined( _DEFAULT_SOURCE) || defined( _BSD_SOURCE) \
     || defined( __FreeBSD__) || defined( __MINGW32__)
   /* POSIX.1-2008 marks gettimeofday() as obsolete,
      recommending the use of clock_gettime instead, so we
      only use that alternative to POSIX_C conditionally */
   #define HAVE_GETTIMEOFDAY
#endif

#if defined( HAVE_CLOCK_GETTIME)
#include <time.h>

long PDC_millisecs( void)
{
    struct timespec t;

    clock_gettime( CLOCK_REALTIME, &t);
    return( t.tv_sec * 1000 + t.tv_nsec / 1000000);
}
#elif defined( HAVE_GETTIMEOFDAY)
#include <sys/time.h>

long PDC_millisecs( void)
{
    struct timeval t;

    gettimeofday( &t, NULL);
    return( t.tv_sec * 1000 + t.tv_usec / 1000);
}
#else    /* neither gettimeofday() or clock_gettime() available */
#include <sys/timeb.h>

long PDC_millisecs( void)
{
    struct timeb t;

    ftime( &t);
    return( (long)t.time * 1000L + (long)t.millitm);
}
#endif

I that's considered OK, then I'm directly checking this in.

@Bill-Gray
Copy link
Owner

One minor fix : the test on CLOCK_REALTIME is occurring before #including <time.h> and <sys/time.h>. So it'll always fail. And the latter file doesn't exist for Digital Mars or Borland. So before doing that test, I'd put in

#include <time.h>
#ifdef __has_include
   #if __has_include(<sys/time.h>)
       #include <sys/time.h>
   #endif
#endif

(and remove the inclusion of both further down, of course).

With that modification, I can at least confirm that the code compiles without warnings on Digital Mars, Borland 5.5, MinGW on Linux, clang-10, clang-18, and gcc 9.4.

Without that modification, and compiling your snippet separately (outside getch.c), both clangs and gcc failed to recognize that either clock_gettime() or gettimeofday() were available, defaulted to ftime(), and emitted warnings that ftime() is deprecated.

@GitMensch
Copy link
Collaborator Author

So we think that each environment that does has sys/time.h with clock_gettime also provides __has_include?

@Bill-Gray
Copy link
Owner

I think so, but there's no guarantee that I know of. There may be some platform out there that doesn't support _has_include, does have sys/time.h and CLOCK_REALTIME (but we'd miss it), and where the result would be that we'd percolate down to using either gettimeofday() or ftime(), and we'd get a deprecation warning.

I think it's very unlikely to happen, though. Compilers old enough to lack _has_include are probably also old enough not to deprecate the older functions. And I know for a fact that at least one compiler (Borland) lacks sys/time.h.

@GitMensch
Copy link
Collaborator Author

Let's see what I've broke. I really have to go now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MinGW: getch.c does not build, no option to work around via Makefile

2 participants