summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2004-09-28 14:42:50 +0000
committerEric S. Raymond <esr@thyrsus.com>2004-09-28 14:42:50 +0000
commitde431beb6189b024112549b0dbe82d664b819322 (patch)
tree604bb530a213b65da227ded3c8f31f073dd93311
parent575f20073e1930fe525ee1303272a69f473acc62 (diff)
downloadgpsd-de431beb6189b024112549b0dbe82d664b819322.tar.gz
Better handling of network errors.
-rw-r--r--gps.c11
-rw-r--r--libgps.c4
-rw-r--r--netlib.c2
-rw-r--r--xgpsspeed.c9
4 files changed, 14 insertions, 12 deletions
diff --git a/gps.c b/gps.c
index 3a00bc73..15b5eec4 100644
--- a/gps.c
+++ b/gps.c
@@ -419,15 +419,12 @@ int main(int argc, char *argv[])
XtAppContext app;
extern char *optarg;
int option;
- char *device_name = "localhost:2947";
+ char *server = NULL;
while ((option = getopt(argc, argv, "hp:")) != -1) {
switch (option) {
case 'p':
- if (device_name)
- free(device_name);
- device_name = malloc(strlen(optarg) + 1);
- strcpy(device_name, optarg);
+ server = strdup(optarg);
break;
case 'h':
case '?':
@@ -444,10 +441,10 @@ int main(int argc, char *argv[])
/*
* Essentially all the interface to libgps happens below here
*/
- gpsdata = gps_open(NULL, NULL);
+ gpsdata = gps_open(server, DEFAULT_GPSD_PORT);
if (!gpsdata)
{
- fprintf(stderr, "gps: no gpsd running.\n");
+ fprintf(stderr, "gps: no gpsd running or network error (%d).\n", errno);
exit(2);
}
diff --git a/libgps.c b/libgps.c
index acd341bf..322f90f1 100644
--- a/libgps.c
+++ b/libgps.c
@@ -3,6 +3,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
#include "gps.h"
#include "gpsd.h"
@@ -22,7 +23,10 @@ struct gps_data_t *gps_open(char *host, char *port)
port = DEFAULT_GPSD_PORT;
if ((gpsdata->gps_fd = netlib_connectsock(host, port, "tcp")) < 0)
+ {
+ errno = gpsdata->gps_fd;
return NULL;
+ }
now = time(NULL);
INIT(gpsdata->online_stamp, now);
diff --git a/netlib.c b/netlib.c
index 620c1caf..499b0f71 100644
--- a/netlib.c
+++ b/netlib.c
@@ -40,7 +40,7 @@ int netlib_connectsock(char *host, char *service, char *protocol)
if ( (phe = gethostbyname(host)) )
bcopy(phe->h_addr, (char *) &sin.sin_addr, phe->h_length);
else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
- return -NL_NOHOST;
+ return NL_NOHOST;
if ((ppe = getprotobyname(protocol)) == 0)
return NL_NOPROTO;
if (strcmp(protocol, "udp") == 0)
diff --git a/xgpsspeed.c b/xgpsspeed.c
index 3fd8d23c..f45b24db 100644
--- a/xgpsspeed.c
+++ b/xgpsspeed.c
@@ -10,6 +10,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
+#include <errno.h>
#include <stdio.h>
#include <math.h>
@@ -74,12 +75,12 @@ int main(int argc, char **argv)
Cardinal i;
extern char *optarg;
int option;
- char *device_name = "/dev/gps";
+ char *server = NULL;
while ((option = getopt(argc, argv, "hp:")) != -1) {
switch (option) {
case 'p':
- device_name = strdup(optarg);
+ server = strdup(optarg);
break;
case 'h':
case '?':
@@ -137,10 +138,10 @@ int main(int argc, char **argv)
/*
* Essentially all the interface to libgps happens below here
*/
- gpsdata = gps_open(NULL, NULL);
+ gpsdata = gps_open(server, DEFAULT_GPSD_PORT);
if (!gpsdata)
{
- fprintf(stderr, "xgpsspeed: no gpsd running.\n");
+ fprintf(stderr, "xgpsspeed: no gpsd running or network error (%d).\n", errno);
exit(2);
}