From 21c145b1089bccc96e67ebb28166797ae60c35ba Mon Sep 17 00:00:00 2001
From: Peter Wu <peter@lekensteyn.nl>
Date: Wed, 6 Aug 2014 10:19:03 +0200
Subject: [PATCH] libzrtp: update zrtp_sleep for modern libc

usleep is deprecated and disabled in glibc 2.12 unless requested. Use
nanosleep instead if available.

This fixes the following compiler warning:

    ./src/zrtp_iface_scheduler.c: In function 'zrtp_sleep':
    ./src/zrtp_iface_scheduler.c:96:2: warning: implicit declaration of
    function 'usleep' [-Wimplicit-function-declaration]
      usleep(msec*1000);
      ^
---
 libs/libzrtp/configure.ac               |  2 +-
 libs/libzrtp/src/zrtp_iface_scheduler.c | 14 +++++++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/libs/libzrtp/configure.ac b/libs/libzrtp/configure.ac
index d1c40b7e3d..251c51acc1 100644
--- a/libs/libzrtp/configure.ac
+++ b/libs/libzrtp/configure.ac
@@ -68,7 +68,7 @@ AC_C_CONST
 # Checks for library functions.
 AC_FUNC_MALLOC
 AC_CHECK_FUNCS([memset memcpy malloc free]) 
-AC_CHECK_FUNCS([usleep])
+AC_CHECK_FUNCS([usleep nanosleep])
 AC_CHECK_FUNCS([fopen fread])
 AC_CHECK_FUNCS([pthread_mutex_lock pthread_mutex_unlock pthread_mutex_init pthread_mutex_destroy])
 AC_CHECK_FUNCS([pthread_attr_init pthread_attr_setdetachstate pthread_create])
diff --git a/libs/libzrtp/src/zrtp_iface_scheduler.c b/libs/libzrtp/src/zrtp_iface_scheduler.c
index 715c584b53..16386cafdb 100644
--- a/libs/libzrtp/src/zrtp_iface_scheduler.c
+++ b/libs/libzrtp/src/zrtp_iface_scheduler.c
@@ -7,6 +7,7 @@
  * Viktor Krykun <v.krikun at zfoneproject.com> 
  */
 
+#define _POSIX_C_SOURCE 199309L /* for struct timespec */
 #include "zrtp.h"
 
 #if (defined(ZRTP_USE_BUILTIN_SCEHDULER) && (ZRTP_USE_BUILTIN_SCEHDULER ==1))
@@ -80,11 +81,15 @@ int zrtp_thread_create(zrtp_thread_routine_t start_routine, void *arg)
 }
 
 #elif (ZRTP_PLATFORM == ZP_LINUX) || (ZRTP_PLATFORM == ZP_DARWIN) || (ZRTP_PLATFORM == ZP_BSD) || (ZRTP_PLATFORM == ZP_ANDROID)
-#if ZRTP_HAVE_UNISTD_H == 1
+/* POSIX.1-2008 removes usleep, so use nanosleep instead when available */
+#if ZRTP_HAVE_NANOSLEEP
+#include <time.h>               /* for nanosleep */
+#elif ZRTP_HAVE_UNISTD_H == 1
 #include <unistd.h>
 #else
 #error "Used environment dosn't have <unistd.h> - zrtp_scheduler can't be build."
 #endif
+
 #if ZRTP_HAVE_PTHREAD_H == 1
 #include <pthread.h>
 #else
@@ -93,7 +98,14 @@ int zrtp_thread_create(zrtp_thread_routine_t start_routine, void *arg)
 
 int zrtp_sleep(unsigned int msec)
 {
+#if ZRTP_HAVE_NANOSLEEP
+	struct timespec delay;
+	delay.tv_sec = msec / 1000;
+	delay.tv_nsec = (msec % 1000) * 1000000;
+	while (nanosleep(&delay, &delay)) ;
+#else
 	usleep(msec*1000);
+#endif
 	return 0;
 }