#ifndef DONE_SPAWN
#define DONE_SPAWN 1 
/* Prototypes etc for routines in lib_spawn.c */
/* free thread mutex */
int spawn_program_and_exit(char *cmd, char *param, char *reason);

void lib_spawn_free(void);
/* Prototypes */
int lib_spawn_noclose(int x);



#ifdef WIN32

#ifdef __BORLANDC__ // [
typedef int IHANDLE;
typedef int pid_t;
typedef signed int intptr_t;
#else // __BORLANDC__ ][
typedef intptr_t IHANDLE;
typedef int pid_t;
#endif // __BORLANDC__ ]

#else

typedef long IHANDLE;

#endif





int spawn_set_mutex(int (*cb)(int vm, int isget, int seconds), int m);
char *spawn_status(char *bf);
int spawn_set_handle_cb(int (*cb)(IHANDLE handle, int isopen, int logonly, char *filename, int line, char *whence));
int spawn_log_closes(int v);
int lib_detach_handle_env(char *xcmd, char *msg, IHANDLE *phandle,char *env[]);
int lib_detach_prog(char *xcmd, char *msg);
int lib_spawn_freeenv(char *env[], int n);
int lib_spawn_logfile(char *fname);
int spawn_kill_later(void);
int spawn_gets_timed(IHANDLE h, char *line, char *excess, int bfsz, int *read, int timeout) ;
int spawn_isalive(IHANDLE x);
int lib_spawn_wait(IHANDLE x);
int lib_sp_write(IHANDLE  f, char *bf);
int lib_sp_bread(IHANDLE f, char *bf, unsigned int maxsz);
int lib_sp_read(IHANDLE f, char *bf, int sz);
IHANDLE lib_spawn(char *cmd, char *env[], IHANDLE *hin, IHANDLE *hout,char *msg );
IHANDLE lib_spawn_getpid(char *cmd, char *env[], IHANDLE *xin, IHANDLE *xout, char *msg, pid_t *ProcessID);
IHANDLE lib_spawn_user(char *cmd, char *env[], int uid, int gid, char *home, IHANDLE *hin, IHANDLE *hout,char *msg );
int spawn_printf(IHANDLE h, char *format, ...);
int spawn_write(IHANDLE h, char *bf, int len);
int spawn_read_timed(IHANDLE h, char *bf, int len, int msecs);
int spawn_kill(IHANDLE x);
int spawn_close_pipex(IHANDLE h, char *src, int line);
#define spawn_close_pipe(a) spawn_close_pipex(a,__FILE__,__LINE__)
int spawn_isalive_rcode(IHANDLE x, int *rcode);

int lib_spawn_cmd(char *cmd);
int spawn_release_timed(IHANDLE pid, int t, int *rcode);
int spawn_release(IHANDLE pid);
//int spawn_kill_h(int x);
IHANDLE lib_spawn_noenv(char *cmd, IHANDLE *hin, IHANDLE *hout,char *msg );
IHANDLE lib_spawn_noenv_getpid(char *cmd, IHANDLE *hin, IHANDLE*hout, char *msg, pid_t *ProcessID);
int spawn_nready(IHANDLE f);
int spawn_read(IHANDLE h, char *bf, int len);
int lib_spawn_loadenv(char *fname, char *env[], int maxn);
int lib_spawn_setenv(char *env[]);
int lib_spawn_addenv_real(char *env[],int *nenv,char *bf);
int lib_spawn_addenv(char *env[],int *nenv,char *var, char *val);
int spawn_write_timed(IHANDLE h, char *bf, int len,int msec);
int spawn_silent(int x);

int lib_detach_wait(IHANDLE x, int seconds, int *ret);
int lib_detach_handle(char *xcmd, char *msg, IHANDLE *phandle);
int lib_detach_free(IHANDLE phandle);
int lib_detach_kill(IHANDLE phandle);
int lib_detach_prog_env(char *cmd, char *env[], char *msg, IHANDLE *phandle);
IHANDLE lib_spawn_user_getpid(char* cmd, char* env[], int uid, int gid, char* home, IHANDLE* hin, IHANDLE* hout, IHANDLE* herr, char* msg, pid_t* ProcessID);


int spawn_gets(IHANDLE h, char *line, char *excess, int bfsz, int *read);
int spawn_gets_timed(IHANDLE h, char *line, char *excess, int bfsz, int *read, int timeout);
/* spawn_gets

WARNING: initialize excess to "" for FIRST CALL. line and excess MUST be same sized buffers.

FRIST CALL: reads a line into 'line', placing excess into 'excess'.
NEXT CALL : copies 'excess' into 'line', find a line OR reads more, placing excess into 'excess'.

RETURNS: TRUE on success, FALSE at eof, or on error, or if buffer is too small
         sets 'read' to 0 on eof, -1 on error, x on success or buffer too small.
*/

/* spawn_error MUST be called directly after the spawn fn that fails,
   otherwise the error may not be accurate */
/* bf MUST be 100 chars or bigger */
char *spawn_error(char *bf);
#ifdef WIN32
int run_win32_process_and_wait(char *program, char *args);
#else
int lib_spawnv_wait_no_path(char *xcmd, char *arg[], char *reason, char *change_dir);
#endif
#endif 