# include <stdio.h>
# include <fcntl.h>
# include <malloc.h>
# include <math.h>
# include <errno.h>
# include <time.h>
# include <memory.h>
# include <stdlib.h>
# include <string.h>
# include <sys/socket.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/uio.h>
# include <sys/un.h>
# include <unistd.h>
# include <stdarg.h> 
# include <readline/history.h>
# include <readline/readline.h>

/* OHANA included stuff */
# include <fitsio.h>              

# ifndef OHANA_H
# define OHANA_H

extern double hypot();

# define TRUE (1)
# define FALSE (0)
# define SIGN(X)  (((X) == 0) ? 0 : ((fabs((double)(X))) / (X)))
# define ROUND(X) ((int) ((X) + 0.5*SIGN(X)))
# define SQR(X)   (double) (((double)(X))*((double)(X)))
# define SQ(X)    (double) (((double)(X))*((double)(X)))
# define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
# define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
# define SWAP(X,Y) {double tmp=(X); (X) = (Y); (Y) = tmp;}

# define LCK_UNLOCK  0   /* file is unlocked */
# define LCK_ACCESS  1   /* can't get access to file */
# define LCK_TIMEOUT 2   /* timeout setting lock */
# define LCK_HARDLCK 3   /* error setting hard lockfile */
# define LCK_HUNLOCK 4   /* error clearing hard lockfile */

# define LCK_EMPTY   5   /* locked file is empty */
# define LCK_FULL    6   /* locked file is not empty */
# define LCK_UNKNOWN 7   /* can't stat file to get size */

# define LCK_SOFT    10  /* soft lock */
# define LCK_HARD    11  /* hard lock */
# define LCK_XCLD    12  /* exclusive soft lock */

# ifndef M_PI
# define M_PI 3.14159265358979323846264
# endif

int setlockfile2 (char *filename, double timeout, int type, int *state);
int clearlockfile2 (char *filename, int fd, int type, int *state);

FILE *fsetlockfile (char *filename, double timeout, int type, int *state);
int fclearlockfile (char *filename, FILE *f, int type, int *state);

# ifndef PROTO
#   define PROTO(A) A
# endif

/* 
# else
#   ifndef PROTO
#   define PROTO(A) ()
#   endif
# include <varargs.h>
# endif
*/

# ifndef ALLOCATE
# define ALLOCATE(X,T,S)  \
  X=(T *)malloc((unsigned) (MAX(((S)*sizeof(T)),1)));\
  if(X==NULL) \
    { \
      fprintf(stderr,"failed to malloc X\n");\
        exit (10);\
    } 
# define REALLOCATE(X,T,S) \
  X=(T *)realloc(X,(unsigned) (MAX(((S)*sizeof(T)),1))); \
  if(X==NULL) \
    { \
       fprintf(stderr,"failed to realloc X\n"); \
       exit (10); \
    }
# endif /* ALLOCATE */

# ifndef FOPEN 
# define FOPEN(F,NAME) \
  F = fopen (NAME, "r"); \
  if (F == NULL) { \
    fprintf (stderr, "failed to open %s\n", NAME); \
    exit (0); \
  }
# endif /* FOPEN */

# define DEG_RAD 57.295779513082322
# define RAD_DEG  0.017453292519943

# define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))

/* in string.c */
int   stripwhite (char *string);
int   strnumcmp (char *str1, char *str2);
char *strcreate (char *string);
char *strncreate (char *string, int n);
int   scan_line (FILE *f, char *line); 
int   dparse (double *X, int NX, char *line);
int   fparse (float *X, int NX, char *line);
int   get_argument (int argc, char **argv, char *arg);
int   remove_argument (int N, int *argc, char **argv);

int   Fread (void *ptr, int size, int nitems, FILE *f, char *type);
int   Fwrite (void *ptr, int size, int nitems, FILE *f, char *type);
int   ByteSwap (char *ptr, int size, int nitems, char *type);
int   ConvertStruct (char *buffer, int size, int Nbytes, char *type);

char *pathname (char *name);
char *filebasename (char *name);
char *filerootname (char *name);
char *findexec (int argc, char **argv);
int mkdirhier (char *path);

FILE *fsetlockfile (char *filename, double timeout, int type, int *state);
int fclearlockfile (char *filename, FILE *f, int type, int *state);
int setlockfile2 (char *filename, double timeout, int type, int *state);
int clearlockfile2 (char *filename, int fd, int type, int *state);

char *SelectConfigFile (int *argc, char **argv, char *progname);
char *LoadConfigFile (char *filename);
char *ScanConfig (char *config, char *field, char *mode, int N,...);
char *expandline (char *line, char *config);
char *fileextname (char *file);
char *LoadRawConfigFile (char *, int);

/*
#   define F_SETFL         4   
#   define O_NONBLOCK      0200000  
#   define AF_UNIX         1          
#   define SOCK_STREAM     1
*/

# endif
