diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common.h | 8 | ||||
-rw-r--r-- | src/pipeutils.c | 46 | ||||
-rw-r--r-- | src/signals.c | 21 | ||||
-rw-r--r-- | src/systemtime.c | 23 |
4 files changed, 52 insertions, 46 deletions
diff --git a/src/common.h b/src/common.h index 2633c83..0e0fd3b 100644 --- a/src/common.h +++ b/src/common.h @@ -20,6 +20,12 @@ #ifndef REDSHIFT_COMMON_H #define REDSHIFT_COMMON_H +#ifndef WINDOWS +# if defined(__WIN32__) || defined(_WIN32) +# define WINDOWS +# endif +#endif + #include <sys/stat.h> #include <sys/types.h> #include <dirent.h> @@ -37,7 +43,7 @@ #ifdef _POSIX_TIMERS # include <sys/time.h> #endif -#if defined(__WIN32__) || defined(_WIN32) +#ifdef WINDOWS # include <windows.h> #else # include <pwd.h> diff --git a/src/pipeutils.c b/src/pipeutils.c index 200fb6d..9202910 100644 --- a/src/pipeutils.c +++ b/src/pipeutils.c @@ -19,19 +19,40 @@ #include "common.h" -#ifndef _WIN32 +#ifdef WINDOWS + +/* Create non-blocking set of pipe fds. + + Not supported on Windows! Always fails. */ +int +pipeutils_create_nonblocking(int pipefds[2]) +{ + return -1; +} + +#else /* Create non-blocking set of pipe fds. */ int pipeutils_create_nonblocking(int pipefds[2]) { - int r = pipe(pipefds); - if (r == -1) { + int flags; + +#if defined(__linux__) && !defined(MISSING_PIPE2) + if (!pipe2(pipefds, O_NONBLOCK)) { + return 0; + } else if (errno != ENOSYS) { + perror("pipe2 O_NONBLOCK"); + return -1; + } +#endif + + if (pipe(pipefds)) { perror("pipe"); return -1; } - int flags = fcntl(pipefds[0], F_GETFL); + flags = fcntl(pipefds[0], F_GETFL); if (flags == -1) { perror("fcntl"); close(pipefds[0]); @@ -39,8 +60,7 @@ pipeutils_create_nonblocking(int pipefds[2]) return -1; } - r = fcntl(pipefds[0], F_SETFL, flags | O_NONBLOCK); - if (r == -1) { + if (fcntl(pipefds[0], F_SETFL, flags | O_NONBLOCK)) { perror("fcntl"); close(pipefds[0]); close(pipefds[1]); @@ -55,8 +75,7 @@ pipeutils_create_nonblocking(int pipefds[2]) return -1; } - r = fcntl(pipefds[1], F_SETFL, flags | O_NONBLOCK); - if (r == -1) { + if (fcntl(pipefds[1], F_SETFL, flags | O_NONBLOCK)) { perror("fcntl"); close(pipefds[0]); close(pipefds[1]); @@ -66,17 +85,6 @@ pipeutils_create_nonblocking(int pipefds[2]) return 0; } -#else /* _WIN32 */ - -/* Create non-blocking set of pipe fds. - - Not supported on Windows! Always fails. */ -int -pipeutils_create_nonblocking(int pipefds[2]) -{ - return -1; -} - #endif /* Signal on write-end of pipe. */ diff --git a/src/signals.c b/src/signals.c index 8cdb9e7..70addf8 100644 --- a/src/signals.c +++ b/src/signals.c @@ -51,31 +51,28 @@ int exiting = 0; int signals_install_handlers(void) { -#if !defined(__WIN32__) +#ifndef WINDOWS struct sigaction sigact; sigset_t sigset; - int r; - sigemptyset(&sigset); /* Install signal handler for INT and TERM signals */ + memset(&sigact, 0, sizeof(sigact)); + sigemptyset(&sigset); sigact.sa_handler = sigexit; sigact.sa_mask = sigset; sigact.sa_flags = 0; - r = sigaction(SIGINT, &sigact, NULL); - if (r < 0) { + if (sigaction(SIGINT, &sigact, NULL)) { perror("sigaction"); return -1; } - r = sigaction(SIGTERM, &sigact, NULL); - if (r < 0) { + if (sigaction(SIGTERM, &sigact, NULL)) { perror("sigaction"); return -1; } - r = sigaction(SIGQUIT, &sigact, NULL); - if (r < 0) { + if (sigaction(SIGQUIT, &sigact, NULL)) { perror("sigaction"); return -1; } @@ -85,8 +82,7 @@ signals_install_handlers(void) sigact.sa_mask = sigset; sigact.sa_flags = 0; - r = sigaction(SIGUSR1, &sigact, NULL); - if (r < 0) { + if (sigaction(SIGUSR1, &sigact, NULL)) { perror("sigaction"); return -1; } @@ -97,8 +93,7 @@ signals_install_handlers(void) sigact.sa_mask = sigset; sigact.sa_flags = 0; - r = sigaction(SIGCHLD, &sigact, NULL); - if (r < 0) { + if (sigaction(SIGCHLD, &sigact, NULL)) { perror("sigaction"); return -1; } diff --git a/src/systemtime.c b/src/systemtime.c index c440c36..2675fea 100644 --- a/src/systemtime.c +++ b/src/systemtime.c @@ -23,32 +23,29 @@ int systemtime_get_time(double *t) { -#if defined(_WIN32) /* Windows */ +#if defined(WINDOWS) /* Windows */ FILETIME now; ULARGE_INTEGER i; GetSystemTimeAsFileTime(&now); i.LowPart = now.dwLowDateTime; i.HighPart = now.dwHighDateTime; - /* FILETIME is tenths of microseconds since 1601-01-01 UTC */ *t = (i.QuadPart / 10000000.0) - 11644473600.0; + #elif defined(_POSIX_TIMERS) /* POSIX timers */ struct timespec now; - int r = clock_gettime(CLOCK_REALTIME, &now); - if (r < 0) { + if (clock_gettime(CLOCK_REALTIME, &now)) { perror("clock_gettime"); return -1; } - *t = now.tv_sec + (now.tv_nsec / 1000000000.0); + #else /* other platforms */ struct timeval now; - int r = gettimeofday(&now, NULL); - if (r < 0) { + if (gettimeofday(&now, NULL)) { perror("gettimeofday"); return -1; } - *t = now.tv_sec + (now.tv_usec / 1000000.0); #endif @@ -59,12 +56,12 @@ systemtime_get_time(double *t) void systemtime_msleep(unsigned int msecs) { -#ifndef _WIN32 +#ifdef WINDOWS + Sleep(msecs); +#else struct timespec sleep; - sleep.tv_sec = msecs / 1000; - sleep.tv_nsec = (msecs % 1000)*1000000; + sleep.tv_sec = (time_t)(msecs / 1000U); + sleep.tv_nsec = (long)(msecs % 1000U) * 1000000L; nanosleep(&sleep, NULL); -#else - Sleep(msecs); #endif } |