From 0498ff00c580f9e1459517597642750b04174f49 Mon Sep 17 00:00:00 2001 From: Erik Rigtorp Date: Mon, 25 Sep 2023 18:25:34 -0500 Subject: [PATCH] Fix for maxOS and OpenBSD Fallback to nanosleep if clock_nanosleep doesn't exist. Closes #33 --- src/udpreplay.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/udpreplay.cpp b/src/udpreplay.cpp index 3aeeb9f..dc61f2a 100644 --- a/src/udpreplay.cpp +++ b/src/udpreplay.cpp @@ -215,11 +215,25 @@ int main(int argc, char *argv[]) { if (deadline.tv_sec > now.tv_sec || (deadline.tv_sec == now.tv_sec && deadline.tv_nsec > now.tv_nsec)) { +#if _POSIX_C_SOURCE >= 200112L if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, nullptr) == -1) { std::cerr << "clock_nanosleep: " << strerror(errno) << std::endl; return 1; } +#else + timespec duration; + duration.tv_sec = deadline.tv_sec - now.tv_sec; + duration.tv_nsec = deadline.tv_nsec - now.tv_nsec; + if (duration.tv_nsec < 0) { + --duration.tv_sec; + duration.tv_nsec += NANOSECONDS_PER_SECOND; + } + if (nanosleep(&duration, nullptr) == -1) { + std::cerr << "nanosleep: " << strerror(errno) << std::endl; + return 1; + } +#endif } #ifdef __GLIBC__