summaryrefslogtreecommitdiff
path: root/gpsutils.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2018-12-27 05:39:42 -0500
committerEric S. Raymond <esr@thyrsus.com>2018-12-27 05:39:42 -0500
commit1998c0301f6f9542bf5dd51e8d18f3d7f4e76023 (patch)
tree47b092141978039c773e4a4e9f992eab90ee340b /gpsutils.c
parente81cfb77ab1a8de2e7cac6883e632124bcbe6f35 (diff)
downloadgpsd-1998c0301f6f9542bf5dd51e8d18f3d7f4e76023.tar.gz
Factor most uses of pselect(2) into a new utility function named 'nanowait'.
Also, remove sime header inclusions discovered to be unnecessary during the change.
Diffstat (limited to 'gpsutils.c')
-rw-r--r--gpsutils.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/gpsutils.c b/gpsutils.c
index 0a4d5d40..8b855855 100644
--- a/gpsutils.c
+++ b/gpsutils.c
@@ -10,6 +10,11 @@
* http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02_01_02 */
#define _XOPEN_SOURCE 600
+#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L
+/* pselect() needs _POSIX_C_SOURCE >= 200112L */
+#define _POSIX_C_SOURCE 200112L
+#endif
+
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
@@ -18,10 +23,12 @@
#include <math.h>
#include <errno.h>
#include <ctype.h>
+#include <sys/time.h> /* expected to have a pselect(2) prototype a la SuS */
#include "gps.h"
#include "libgps.h"
#include "os_compat.h"
+#include "timespec.h"
#ifdef USE_QT
#include <QDateTime>
@@ -621,4 +628,16 @@ double earth_distance(double lat1, double lon1, double lat2, double lon2)
return earth_distance_and_bearings(lat1, lon1, lat2, lon2, NULL, NULL);
}
+bool nanowait(int fd, int nanoseconds)
+{
+ fd_set fdset;
+ struct timespec to;
+
+ FD_ZERO(&fdset);
+ FD_SET(fd, &fdset);
+ to.tv_sec = nanoseconds / NS_IN_SEC;
+ to.tv_nsec = nanoseconds % NS_IN_SEC;
+ return pselect(fd + 1, &fdset, NULL, NULL, &to, NULL) == 1;
+}
+
/* end */