/* coding: utf-8; */
#include <sys/types.h>
#include <stdbool.h>

#ifndef __SOUNDIO_UTIL_H__
#define __SOUNDIO_UTIL_H__

#ifdef __cplusplus
extern "C" {
#endif

#define WHITESPACE " \t\n\r"

typedef enum
{
	SPAWN_DEFAULT                = 0,
	SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
	SPAWN_SEARCH_PATH            = 1 << 2,
	/* Dump output to /dev/null */
	SPAWN_STDOUT_TO_DEV_NULL     = 1 << 3,
	SPAWN_STDERR_TO_DEV_NULL     = 1 << 4,
	SPAWN_CHILD_INHERITS_STDIN   = 1 << 5,
	SPAWN_FILE_AND_ARGV_ZERO     = 1 << 6
} SpawnFlags;

int ignore_signals(int sig, ...);
int default_signals(int sig, ...);

ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
ssize_t loop_read_all(int fd, void **buf, size_t *nbytes, bool do_poll);

int startswith(const char *s, const char *prefix);
char *strstrip(char *s);

int fd_nonblock(int fd, bool nonblock);
int fd_cloexec(int fd, bool cloexec);

int close_nointr(int fd);
int close_and_invalidate(int *fd);

int sane_dup2(int fd1, int fd2);
int sane_open(const char *path, int mode);

int spawn_async_with_pipes(char *working_directory,
			   char **argv,
			   char **envp,
			   SpawnFlags flags,
			   pid_t *pid,
			   int *in,
			   int *out,
			   int *err);

#ifdef __cplusplus
}
#endif

#endif /* __SOUNDIO_UTIL_H__ */
