/* 
 * $Id: utils.h,v 1.4 1994/11/08 20:36:14 ejb Exp $
 * $Source: /home/ejb/source/util/include/RCS/utils.h,v $
 * $Author: ejb $
 *
 * This is the header file for all utilites.  It contains abstractions
 * to make the code portable.  It uses both preprocessor symbols
 * that can be defined by programs that include this file and
 * symbols defined by autoconf.
 */

#ifndef __UTIL_H__
#define __UTIL_H__

#if !defined(lint) && !defined(CODECENTER) || defined(RCS_HDRS)
/* Define a static function and call it.  No warnings this way. */
static void rcsid_utils_h(char *s)
{rcsid_utils_h("@(#)$Id: utils.h,v 1.4 1994/11/08 20:36:14 ejb Exp $");}
#endif /* !lint && !CODECENTER || RCS_HDRS */

/* This must be first */
#include "config.h"

/*
 * Set things up here so that other files don't have to be cluttered with
 * checking for autoconf definitions.
 */

/*
 * First, include files and make definitions needed by all programs.
 */
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#if STDC_HEADERS || HAVE_STDLIB_H
# include <stdlib.h>
#endif

#if HAVE_STRING_H
# include <string.h>
#else
# include <strings.h>
# define strrchr rindex
# define strchr index
#endif

#if (! STDC_HEADERS) && HAVE_MEMORY_H
# include <memory.h>
#endif

#if HAVE_MEMORY_H || STDC_HEADERS
# define mem_zero(s, l) memset(s, 0, l)
#else
# define mem_zero(s, l) bzero(s, l)
#endif

#if HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <errno.h>
#if HAVE_STRERROR
# define error_string(e) strerror(e)
#else
# if SYS_ERRLIST_DEFINED
#  ifndef SYS_ERRLIST_DECLARED
    extern char *sys_errlist[];
#  endif
#  define error_string(e) sys_errlist[e]
# else
#  error "Sorry, don't know how to define error_string."
# endif
#endif

#ifndef ERRNO_DECLARED
extern int errno;
#endif

/*
 * Now define things not needed by everyone.  Programs that include this file
 * should define the appropriate symbols to get the proper code here included.
 */

#ifdef NEED_STDARG
# if STDC_HEADERS || HAVE_STDARG_H
#  include <stdarg.h>
#  define USE_VARARGS 0
# else
#  include <varargs.h>
#  define USE_VARARGS 1
# endif
#endif

#ifdef NEED_SYS_PARAM
# if HAVE_SYS_PARAM_H
#  include <sys/param.h>
# else
#  ifndef MAXPATHNEN
#   define MAXPATHLEN 1024
#  endif
# endif
#endif

#ifdef NEED_DIR
/* unistd.h defines _POSIX_VERSION on POSIX.1 systems.  */
# if defined(DIRENT) || defined(_POSIX_VERSION)
#  include <dirent.h>
# else /* not (DIRENT or _POSIX_VERSION) */
#  define dirent direct
#  ifdef SYSNDIR
#   include <sys/ndir.h>
#  endif /* SYSNDIR */
#  ifdef SYSDIR
#   include <sys/dir.h>
#  endif /* SYSDIR */
#  ifdef NDIR
#   include <ndir.h>
#  endif /* NDIR */
# endif /* not (DIRENT or _POSIX_VERSION) */
#endif

#ifdef NEED_WD
# if ! HAVE_GETCWD
#  define getcwd(s, l) getwd(s)
# endif
#endif

#ifdef NEED_STAT
# include <sys/stat.h>
# ifdef STAT_MACROS_BROKEN
#  ifdef S_ISDIR
#   undef S_ISDIR
#  endif
#  ifdef S_ISLNK
#   undef S_ISLNK
#  endif
#  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#  define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
# endif
#endif

#if defined(NEED_SELECT) || defined(NEED_TIME)
# ifdef TIME_WITH_SYS_TIME
#  include <sys/time.h>
#  include <time.h>
# else
#  ifdef HAVE_SYS_TIME_H
#   include <sys/time.h>
#  else
#   include <time.h>
#  endif
# endif
#endif

#ifdef NEED_SELECT
# if HAVE_SYS_SELECT_H 
#  include <sys/select.h>
# endif
# if HAVE_BSTRING_H
#  include <bstring.h>
# endif
#endif

#ifdef NEED_KILLPG
# if ! HAVE_KILLPG
#  define killpg(x,y) kill(-(x), (y))
# endif
#endif

#if HAVE_SIGADDSET
# define POSIX_SIGNALS
#endif

#endif /* __UTIL_H__ */
