Blame lib/libcompat.h

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#ifndef LIBCOMPAT_H
Packit 0b5880
#define LIBCOMPAT_H
Packit 0b5880
Packit 0b5880
#if HAVE_CONFIG_H
Packit 0b5880
#include <config.h>
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
Packit 0b5880
#define GCC_VERSION_AT_LEAST(major, minor) \
Packit 0b5880
((__GNUC__ > (major)) || \
Packit 0b5880
 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
Packit 0b5880
#else
Packit 0b5880
#define GCC_VERSION_AT_LEAST(major, minor) 0
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#if GCC_VERSION_AT_LEAST(2,95)
Packit 0b5880
#define CK_ATTRIBUTE_UNUSED __attribute__ ((unused))
Packit 0b5880
#else
Packit 0b5880
#define CK_ATTRIBUTE_UNUSED
Packit 0b5880
#endif /* GCC 2.95 */
Packit 0b5880
Packit 0b5880
#if GCC_VERSION_AT_LEAST(2,5)
Packit 0b5880
#define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
Packit 0b5880
#else
Packit 0b5880
#define CK_ATTRIBUTE_NORETURN
Packit 0b5880
#endif /* GCC 2.5 */
Packit 0b5880
Packit 0b5880
/*
Packit 0b5880
 * Used for MSVC to create the export attribute
Packit 0b5880
 * CK_DLL_EXP is defined during the compilation of the library
Packit 0b5880
 * on the command line.
Packit 0b5880
 */
Packit 0b5880
#ifndef CK_DLL_EXP
Packit 0b5880
#define CK_DLL_EXP
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#if defined(_MSC_VER)
Packit 0b5880
#include <WinSock2.h>           /* struct timeval, API used in gettimeofday implementation */
Packit 0b5880
#include <io.h>                 /* read, write */
Packit 0b5880
#include <process.h>            /* getpid */
Packit 0b5880
#endif /* _MSC_VER */
Packit 0b5880
Packit 0b5880
/* defines size_t */
Packit 0b5880
#include <sys/types.h>
Packit 0b5880
Packit 0b5880
/* provides assert */
Packit 0b5880
#include <assert.h>
Packit 0b5880
Packit 0b5880
/* defines FILE */
Packit 0b5880
#include <stdio.h>
Packit 0b5880
Packit 0b5880
/* defines exit() */
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
Packit 0b5880
/* defines NAN, INFINITY, isnan(), isinf(), isfinite() */
Packit 0b5880
#include <math.h>
Packit 0b5880
Packit 0b5880
/* However, some older Visual Studio Versions do not */
Packit 0b5880
#if !defined(INFINITY) || !defined(NAN)
Packit 0b5880
extern double DOUBLE_ZERO;
Packit 0b5880
#define INFINITY (1.0/DOUBLE_ZERO)
Packit 0b5880
#define NAN (DOUBLE_ZERO/DOUBLE_ZERO)
Packit 0b5880
#endif
Packit 0b5880
#if !defined(isnan) || !defined(isinf) || !defined(isfinite)
Packit 0b5880
#define NEED_fpclassify
Packit 0b5880
extern int fpclassify(double d);
Packit 0b5880
#define FP_INFINITE (1)
Packit 0b5880
#define FP_NAN (2)
Packit 0b5880
#define FP_ZERO (4)
Packit 0b5880
#define FP_NORMAL (8)
Packit 0b5880
#define FP_SUBNORMAL (16)
Packit 0b5880
#define isnan(x) ((fpclassify((double)(x)) & FP_NAN) == FP_NAN)
Packit 0b5880
#define isinf(x) ((fpclassify((double)(x)) & FP_INFINITE) == FP_INFINITE)
Packit 0b5880
#define isfinite(x) ((fpclassify((double)(x)) & (FP_NAN|FP_INFINITE)) == 0)
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
Packit 0b5880
/* provides localtime and struct tm */
Packit 0b5880
#ifdef HAVE_SYS_TIME_H
Packit 0b5880
#include <sys/time.h>
Packit 0b5880
#endif /* !HAVE_SYS_TIME_H */
Packit 0b5880
#include <time.h>
Packit 0b5880
Packit 0b5880
/* declares fork(), _POSIX_VERSION.  according to Autoconf.info,
Packit 0b5880
   unistd.h defines _POSIX_VERSION if the system is POSIX-compliant,
Packit 0b5880
   so we will use this as a test for all things uniquely provided by
Packit 0b5880
   POSIX like sigaction() and fork() */
Packit 0b5880
#ifdef HAVE_UNISTD_H
Packit 0b5880
#include <unistd.h>
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#ifdef HAVE_SYS_WAIT_H
Packit 0b5880
#include <sys/wait.h>
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
/* declares pthread_create and friends */
Packit 0b5880
#ifdef HAVE_PTHREAD
Packit 0b5880
#include <pthread.h>
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#ifdef HAVE_STDINT_H
Packit 0b5880
#include <stdint.h>
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
/* replacement functions for broken originals */
Packit 0b5880
#if !HAVE_DECL_ALARM
Packit 0b5880
CK_DLL_EXP unsigned int alarm(unsigned int seconds);
Packit 0b5880
#endif /* !HAVE_DECL_ALARM */
Packit 0b5880
Packit 0b5880
#if !HAVE_MALLOC
Packit 0b5880
CK_DLL_EXP void *rpl_malloc(size_t n);
Packit 0b5880
#endif /* !HAVE_MALLOC */
Packit 0b5880
Packit 0b5880
#if !HAVE_REALLOC
Packit 0b5880
CK_DLL_EXP void *rpl_realloc(void *p, size_t n);
Packit 0b5880
#endif /* !HAVE_REALLOC */
Packit 0b5880
Packit 0b5880
#if !HAVE_GETPID && HAVE__GETPID
Packit 0b5880
#define getpid _getpid
Packit 0b5880
#endif /* !HAVE_GETPID && HAVE__GETPID */
Packit 0b5880
Packit 0b5880
#if !HAVE_GETTIMEOFDAY
Packit 0b5880
CK_DLL_EXP int gettimeofday(struct timeval *tv, void *tz);
Packit 0b5880
#endif /* !HAVE_GETTIMEOFDAY */
Packit 0b5880
Packit 0b5880
#if !HAVE_DECL_LOCALTIME_R
Packit 0b5880
#if !defined(localtime_r)
Packit 0b5880
CK_DLL_EXP struct tm *localtime_r(const time_t * clock, struct tm *result);
Packit 0b5880
#endif
Packit 0b5880
#endif /* !HAVE_DECL_LOCALTIME_R */
Packit 0b5880
Packit 0b5880
#if !HAVE_DECL_STRDUP && !HAVE__STRDUP
Packit 0b5880
CK_DLL_EXP char *strdup(const char *str);
Packit 0b5880
#elif !HAVE_DECL_STRDUP && HAVE__STRDUP
Packit 0b5880
#define strdup _strdup
Packit 0b5880
#endif /* !HAVE_DECL_STRDUP && HAVE__STRDUP */
Packit 0b5880
Packit 0b5880
#if !HAVE_DECL_STRSIGNAL
Packit 0b5880
CK_DLL_EXP char *strsignal(int sig);
Packit 0b5880
#endif /* !HAVE_DECL_STRSIGNAL */
Packit 0b5880
Packit 0b5880
/*
Packit 0b5880
 * On systems where clock_gettime() is not available, or
Packit 0b5880
 * on systems where some clocks may not be supported, the
Packit 0b5880
 * definition for CLOCK_MONOTONIC and CLOCK_REALTIME may not
Packit 0b5880
 * be available. These should define which type of clock
Packit 0b5880
 * clock_gettime() should use. We define it here if it is
Packit 0b5880
 * not defined simply so the reimplementation can ignore it.
Packit 0b5880
 *
Packit 0b5880
 * We set the values of these clocks to some (hopefully)
Packit 0b5880
 * invalid value, to avoid the case where we define a
Packit 0b5880
 * clock with a valid value, and unintentionally use
Packit 0b5880
 * an actual good clock by accident.
Packit 0b5880
 */
Packit 0b5880
#ifndef CLOCK_MONOTONIC
Packit 0b5880
#define CLOCK_MONOTONIC -1
Packit 0b5880
#endif
Packit 0b5880
#ifndef CLOCK_REALTIME
Packit 0b5880
#define CLOCK_REALTIME -1
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#ifndef HAVE_LIBRT
Packit 0b5880
Packit 0b5880
#ifdef STRUCT_TIMESPEC_DEFINITION_MISSING
Packit 0b5880
/*
Packit 0b5880
 * The following structure is defined in POSIX 1003.1 for times
Packit 0b5880
 * specified in seconds and nanoseconds. If it is not defined in
Packit 0b5880
 * time.g, then we need to define it here
Packit 0b5880
 */
Packit 0b5880
struct timespec
Packit 0b5880
{
Packit 0b5880
    time_t tv_sec;
Packit 0b5880
    long tv_nsec;
Packit 0b5880
};
Packit 0b5880
#endif /* STRUCT_TIMESPEC_DEFINITION_MISSING */
Packit 0b5880
Packit 0b5880
#ifdef STRUCT_ITIMERSPEC_DEFINITION_MISSING
Packit 0b5880
/* 
Packit 0b5880
 * The following structure is defined in POSIX.1b for timer start values and intervals.
Packit 0b5880
 * If it is not defined in time.h, then we need to define it here.
Packit 0b5880
 */
Packit 0b5880
struct itimerspec
Packit 0b5880
{
Packit 0b5880
    struct timespec it_interval;
Packit 0b5880
    struct timespec it_value;
Packit 0b5880
};
Packit 0b5880
#endif /* STRUCT_ITIMERSPEC_DEFINITION_MISSING */
Packit 0b5880
Packit 0b5880
/* 
Packit 0b5880
 * Do a simple forward declaration in case the struct is not defined.
Packit 0b5880
 * In the versions of timer_create in libcompat, sigevent is never
Packit 0b5880
 * used.
Packit 0b5880
 */
Packit 0b5880
struct sigevent;
Packit 0b5880
Packit 0b5880
CK_DLL_EXP int clock_gettime(clockid_t clk_id, struct timespec *ts);
Packit 0b5880
CK_DLL_EXP int timer_create(clockid_t clockid, struct sigevent *sevp,
Packit 0b5880
                            timer_t * timerid);
Packit 0b5880
CK_DLL_EXP int timer_settime(timer_t timerid, int flags,
Packit 0b5880
                             const struct itimerspec *new_value,
Packit 0b5880
                             struct itimerspec *old_value);
Packit 0b5880
CK_DLL_EXP int timer_delete(timer_t timerid);
Packit 0b5880
#endif /* HAVE_LIBRT */
Packit 0b5880
Packit 0b5880
/*
Packit 0b5880
 * The following checks are to determine if the system's
Packit 0b5880
 * snprintf (or its variants) should be replaced with
Packit 0b5880
 * the C99 compliant version in libcompat.
Packit 0b5880
 */
Packit 0b5880
#if HAVE_CONFIG_H
Packit 0b5880
#include <config.h>
Packit 0b5880
#endif
Packit 0b5880
#if HAVE_STDARG_H
Packit 0b5880
#include <stdarg.h>
Packit 0b5880
Packit 0b5880
#if !HAVE_VSNPRINTF
Packit 0b5880
CK_DLL_EXP int rpl_vsnprintf(char *, size_t, const char *, va_list);
Packit 0b5880
Packit 0b5880
#define vsnprintf rpl_vsnprintf
Packit 0b5880
#endif
Packit 0b5880
#if !HAVE_SNPRINTF
Packit 0b5880
CK_DLL_EXP int rpl_snprintf(char *, size_t, const char *, ...);
Packit 0b5880
Packit 0b5880
#define snprintf rpl_snprintf
Packit 0b5880
#endif
Packit 0b5880
#endif /* HAVE_STDARG_H */
Packit 0b5880
Packit 0b5880
#if !HAVE_GETLINE
Packit 0b5880
CK_DLL_EXP ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
/* silence warnings about an empty library */
Packit 0b5880
CK_DLL_EXP void ck_do_nothing(void) CK_ATTRIBUTE_NORETURN;
Packit 0b5880
Packit 0b5880
#endif /* !LIBCOMPAT_H */