Skip to content

Commit

Permalink
Cleans up some ordering in os_compat.c.
Browse files Browse the repository at this point in the history
This rearranges the order of some conditionals and includes, in order
to avoid some gratuitous includes where unnecessary.

Due to the fact that some actual definitions for the clock_gettime
fallback are embedded directly in gpsd_config.h, it's necessary to
include <time.h> prior to including it.  This will be fixed once the
relevant definitions are moved to the upcoming os_compat.h.

This also replaces the "UNUSED" in clock_gettime with a void cast,
eliminating the need for compiler.h.

TESTED:
Ran "scons build-all check" on OSX 10.9, OSX 10.12 (which has real
clock_gettime) and Ubuntu 14.  Also verified that the clock_gettime
fallback is present in OSX 10.9, but absent in OSX 10.12 and in Ubuntu
14.
  • Loading branch information
fhgwright committed Jan 22, 2017
1 parent 390b32f commit 0fbb941
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions os_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
* in the histories of those files.
*/

/* Determine which of these functions we need */
#include <time.h> /* For time_t (temp until we fix gpsd_config.h) */
#include "gpsd_config.h"

#ifndef HAVE_CLOCK_GETTIME

/* Simulate ANSI/POSIX clock_gettime() on platforms that don't have it */

#include <time.h>
#include <sys/time.h>

#include "compiler.h"

#ifndef HAVE_CLOCK_GETTIME

/*
* Note that previous versions of this code made use of clock_get_time()
* on OSX, as a way to get time of day with nanosecond resolution. But
Expand All @@ -30,8 +32,9 @@
* gettimeofday(). Thus, it makes no sense to do anything special for OSX.
*/

int clock_gettime(clockid_t clk_id UNUSED, struct timespec *ts)
int clock_gettime(clockid_t clk_id, struct timespec *ts)
{
(void) clk_id;
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0)
return -1;
Expand All @@ -43,6 +46,8 @@ int clock_gettime(clockid_t clk_id UNUSED, struct timespec *ts)

/* End of clock_gettime section */

#ifndef HAVE_DAEMON

/* Simulate Linux/BSD daemon() on platforms that don't have it */

#include <stdlib.h>
Expand All @@ -51,8 +56,6 @@ int clock_gettime(clockid_t clk_id UNUSED, struct timespec *ts)
#include <fcntl.h>
#include <unistd.h>

#include "gpsd_config.h"
#ifndef HAVE_DAEMON
#if defined (HAVE_PATH_H)
#include <paths.h>
#else
Expand Down Expand Up @@ -96,17 +99,16 @@ int daemon(int nochdir, int noclose)

/* Provide BSD strlcat()/strlcpy() on platforms that don't have it */

#include <string.h>
#include <time.h> /* for time_t */
#include "gpsd_config.h"

/*
* These versions use memcpy and strlen() because they are often
* heavily optimized down to assembler level. Thus, likely to be
* faster even with the function call overhead.
*/

#ifndef HAVE_STRLCAT

#include <string.h>

/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
Expand Down Expand Up @@ -178,6 +180,9 @@ size_t strlcat(char *dst, const char *src, size_t siz)
#endif /* HAVE_STRLCAT */

#ifndef HAVE_STRLCPY

#include <string.h>

/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
Expand Down

0 comments on commit 0fbb941

Please sign in to comment.