libbb: provide usleep() fallback implementation
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Sun, 13 Apr 2014 14:37:57 +0000 (16:37 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 13 Apr 2014 14:37:57 +0000 (16:37 +0200)
POSIX.1-2008 removed the usleep function, provide a fallback
implementaion using the recommended nanosleep().

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/platform.h
libbb/platform.c

index bd11ad69af83f6fd8608d3d5a91a6093a7501e63..92f7755519799a773cde5e89730af97bc2b4c931 100644 (file)
@@ -373,6 +373,7 @@ typedef unsigned smalluint;
 #define HAVE_STRSIGNAL 1
 #define HAVE_STRVERSCMP 1
 #define HAVE_VASPRINTF 1
+#define HAVE_USLEEP 1
 #define HAVE_UNLOCKED_STDIO 1
 #define HAVE_UNLOCKED_LINE_OPS 1
 #define HAVE_GETLINE 1
@@ -381,8 +382,15 @@ typedef unsigned smalluint;
 #define HAVE_NET_ETHERNET_H 1
 #define HAVE_SYS_STATFS_H 1
 
-#if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
-# undef HAVE_STRVERSCMP
+#if defined(__UCLIBC__)
+# if UCLIBC_VERSION < KERNEL_VERSION(0, 9, 32)
+#  undef HAVE_STRVERSCMP
+# endif
+# if UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
+#  ifndef __UCLIBC_SUSV3_LEGACY__
+#   undef HAVE_USLEEP
+#  endif
+# endif
 #endif
 
 #if defined(__WATCOMC__)
@@ -519,6 +527,10 @@ extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
 # define strsignal(sig) get_signame(sig)
 #endif
 
+#ifndef HAVE_USLEEP
+extern int usleep(unsigned) FAST_FUNC;
+#endif
+
 #ifndef HAVE_VASPRINTF
 extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
 #endif
index 19734517b13bddfc7c606301019e2f93ae9ffb5d..8d90ca4e9aa3729fbb34e8f0ed58e173ee8579aa 100644 (file)
@@ -17,6 +17,24 @@ char* FAST_FUNC strchrnul(const char *s, int c)
 }
 #endif
 
+#ifndef HAVE_USLEEP
+int FAST_FUNC usleep(unsigned usec)
+{
+       struct timespec ts;
+       ts.tv_sec = usec / 1000000u;
+       ts.tv_nsec = (usec % 1000000u) * 1000u;
+       /*
+        * If a signal has non-default handler, nanosleep returns early.
+        * Our version of usleep doesn't return early
+        * if interrupted by such signals:
+        *
+        */
+       while (nanosleep(&ts, &ts) != 0)
+               continue;
+       return 0;
+}
+#endif
+
 #ifndef HAVE_VASPRINTF
 int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
 {