/***************************************************************************
 * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
 * is provided to you without charge, and with no warranty.  You may give  *
 * away copies of JOVE, including sources, provided that this notice is    *
 * included in all the files.                                              *
 ***************************************************************************/

/* The diversity of process management is complicated and difficult to handle.
 * - In some systems (noteably POSIX), a process id has type "pid_t"
 * - V7 only has wait.  POSIX has waitpid (with options).  BSD has
 *   wait3.  Someone has wait2.
 * - The status result set by wait and used by WIF* has type
 *   "union wait" in BSD, but "int" everywhere else.
 * - The WIF* functions are defined in <sys/wait.h> by BSD and POSIX.
 * - WTERMSIG seems to be a creation of POSIX
 * - Some systems have vfork(1) and perform better if it is used
 *   in place of fork(1).
 *
 * This header attempts to span this diversity.  We provide:
 * - POSIX pid_t
 * - wait_opt, to accept options (and use them, if possible).
 * - wait_status_t
 * - WIF*
 * - WTERMSIG
 */

#ifdef	POSIX_PROCS

# include <sys/types.h>	/* defines pid_t */
# include <sys/wait.h>
  typedef int	wait_status_t;
# define wait_opt(stat_loc, options)	waitpid(-1, stat_loc, options)

#else	/*!POSIX_PROCS*/

 typedef int	pid_t;

#ifdef	BSD_WAIT

# include <sys/wait.h>

  typedef union wait	wait_status_t;

# ifndef	WEXITSTATUS
#  define WEXITSTATUS(w)	((w).w_retcode)
# endif

# ifndef	WTERMSIG
#  define WTERMSIG(w)	((w).w_termsig)
# endif

# ifndef	WAIT3
#  define wait_opt(stat_loc, options)	wait2(stat_loc, options)
# else
#  define wait_opt(stat_loc, options)	wait3(stat_loc, options, (struct rusage *)NULL)
# endif

#else	/*!BSD_WAIT*/

  typedef int	wait_status_t;

# define WIFSTOPPED(w)	((w & 0377) == 0177)
# define WIFEXITED(w)	((w & 0377) == 0)
# define WIFSIGNALED(w)	(((w >> 8) & 0377) == 0)
# define WEXITSTATUS(w)	((w >> 8) & 0377)
# define WTERMSIG(w)	(w & 0177)

# define wait_opt(stat_loc, options)		wait(stat_loc)

#endif	/*!BSD_WAIT*/
#endif	/*!POSIX_PROCS*/

#ifndef	POSIX_UNISTD
extern int	kill proto((pid_t /*pid*/, int /*sig*/));	/* signal.h */

extern pid_t	fork proto((void));
extern pid_t	getpid proto((void));
extern int	getuid proto((void));
extern int	setuid proto((int));
#endif

#ifdef	VFORK
extern int	UNMACRO(vfork) proto((void));
#endif

#ifdef	SYSV
extern pid_t	UNMACRO(setpgrp) proto((void));
#else
/*
*/
#endif
