#include "unp.h"
#include <stdarg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>

void *
Calloc(size_t n, size_t size)
{
	void	*ptr;

	if ( (ptr = calloc(n, size)) == NULL)
		err_sys("calloc error");
	return(ptr);
}

void
Close(int fd)
{
	if (close(fd) == -1)
		err_sys("close error");
}

void
Dup2(int fd1, int fd2)
{
	if (dup2(fd1, fd2) == -1)
		err_sys("dup2 error");
}

int
Fcntl(int fd, int cmd, int arg)
{
	int	n;

	if ( (n = fcntl(fd, cmd, arg)) == -1)
		err_sys("fcntl error");
	return(n);
}

void
Gettimeofday(struct timeval *tv, void *foo)
{
	if (gettimeofday(tv, foo) == -1)
		err_sys("gettimeofday error");
	return;
}

int
Ioctl(int fd, int request, void *arg)
{
	int		n;

	if ( (n = ioctl(fd, request, arg)) == -1)
		err_sys("ioctl error");
	return(n);	/* streamio of I_LIST returns value */
}

pid_t
Fork(void)
{
	pid_t	pid;

	if ( (pid = fork()) == -1)
		err_sys("fork error");
	return(pid);
}

void *
Malloc(size_t size)
{
	void	*ptr;

	if ( (ptr = malloc(size)) == NULL)
		err_sys("malloc error");
	return(ptr);
}

int
Mkstemp(char *template)
{
	int i;

	if ((i = mkstemp(template)) < 0)
		err_quit("mkstemp error");
	return i;
}

#include	<sys/mman.h>

void *
Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
	void	*ptr;

	if ( (ptr = mmap(addr, len, prot, flags, fd, offset)) == ((void *) -1))
		err_sys("mmap error");
	return(ptr);
}

int
Open(const char *pathname, int oflag, mode_t mode)
{
	int		fd;

	if ( (fd = open(pathname, oflag, mode)) == -1)
		err_sys("open error for %s", pathname);
	return(fd);
}

void
Pipe(int *fds)
{
	if (pipe(fds) < 0)
		err_sys("pipe error");
}

ssize_t
Read(int fd, void *ptr, size_t nbytes)
{
	ssize_t		n;

	if ( (n = read(fd, ptr, nbytes)) == -1)
		err_sys("read error");
	return(n);
}

void
Sigaddset(sigset_t *set, int signo)
{
	if (sigaddset(set, signo) == -1)
		err_sys("sigaddset error");
}

void
Sigdelset(sigset_t *set, int signo)
{
	if (sigdelset(set, signo) == -1)
		err_sys("sigdelset error");
}

void
Sigemptyset(sigset_t *set)
{
	if (sigemptyset(set) == -1)
		err_sys("sigemptyset error");
}

void
Sigfillset(sigset_t *set)
{
	if (sigfillset(set) == -1)
		err_sys("sigfillset error");
}

int
Sigismember(const sigset_t *set, int signo)
{
	int		n;

	if ( (n = sigismember(set, signo)) == -1)
		err_sys("sigismember error");
	return(n);
}

void
Sigpending(sigset_t *set)
{
	if (sigpending(set) == -1)
		err_sys("sigpending error");
}

void
Sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
	if (sigprocmask(how, set, oset) == -1)
		err_sys("sigprocmask error");
}

char *
Strdup(const char *str)
{
	char	*ptr;

	if ( (ptr = strdup(str)) == NULL)
		err_sys("strdup error");
	return(ptr);
}

long
Sysconf(int name)
{
	long	val;

	errno = 0;		/* in case sysconf() does not change this */
	if ( (val = sysconf(name)) == -1)
		err_sys("sysconf error");
	return(val);
}

#ifdef	HAVE_SYS_SYSCTL_H
void
Sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
	   void *newp, size_t newlen)
{
	if (sysctl(name, namelen, oldp, oldlenp, newp, newlen) == -1)
		err_sys("sysctl error");
}
#endif

void
Unlink(const char *pathname)
{
	if (unlink(pathname) == -1)
		err_sys("unlink error for %s", pathname);
}

pid_t
Wait(int *iptr)
{
	pid_t	pid;

	if ( (pid = wait(iptr)) == -1)
		err_sys("wait error");
	return(pid);
}

pid_t
Waitpid(pid_t pid, int *iptr, int options)
{
	pid_t	retpid;

	if ( (retpid = waitpid(pid, iptr, options)) == -1)
		err_sys("waitpid error");
	return(retpid);
}

void
Write(int fd, void *ptr, size_t nbytes)
{
	if (write(fd, ptr, nbytes) != nbytes)
		err_sys("write error");
}

sem_t *
Sem_open(const char *pathname, int oflag, ...)
{
	sem_t   *sem;
	va_list ap;
	mode_t  mode;
	unsigned int    value;

	if (oflag & O_CREAT) {
		va_start(ap, oflag); 
		
		if (sizeof (mode_t) < sizeof (int))
			mode = va_arg(ap, int);
		else
			mode = va_arg(ap, mode_t);
		
		//mode = va_arg(ap, va_mode_t);
		value = va_arg(ap, unsigned int);
		if ( (sem = sem_open(pathname, oflag, mode, value)) == SEM_FAILED)
			err_sys("sem_open error for %s", pathname);
		va_end(ap);
	} else {
		if ( (sem = sem_open(pathname, oflag)) == SEM_FAILED)
			err_sys("sem_open error for %s", pathname);
	}
	return(sem);
}

void
Sem_close(sem_t *sem)
{
	if (sem_close(sem) == -1)
		err_sys("sem_close error");
}

void
Sem_unlink(const char *pathname)
{
	if (sem_unlink(pathname) == -1)
		err_sys("sem_unlink error");
}

void
Sem_init(sem_t *sem, int pshared, unsigned int value)
{
	if (sem_init(sem, pshared, value) == -1)
		err_sys("sem_init error");
}

void
Sem_destroy(sem_t *sem)
{
	if (sem_destroy(sem) == -1)
		err_sys("sem_destroy error");
}

void
Sem_wait(sem_t *sem)
{
	if (sem_wait(sem) == -1)
		err_sys("sem_wait error");
}

int
Sem_trywait(sem_t *sem)
{
	int     rc;

	if ( (rc = sem_trywait(sem)) == -1 && errno != EAGAIN)
		err_sys("sem_trywait error");
	return(rc);
}

/* include Sem_post */
void
Sem_post(sem_t *sem)
{
	if (sem_post(sem) == -1)
		err_sys("sem_post error");
}
/* end Sem_post */

void
Sem_getvalue(sem_t *sem, int *valp)
{
	if (sem_getvalue(sem, valp) == -1)
		err_sys("sem_getvalue error");
}

int
Semget(key_t key, int nsems, int flag)
{
	int     rc;

	if ( (rc = semget(key, nsems, flag)) == -1)
		err_sys("semget error");
	return(rc);
}

void
Semop(int id, struct sembuf *opsptr, size_t nops)
{
	if (semop(id, opsptr, nops) == -1)
		err_sys("semctl error");
}


 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
       /* union semun is defined by including <sys/sem.h> */
       #else
       /* according to X/OPEN we have to define it ourselves */
       union semun {
               int val;                    /* SETVAL을 위한값 */
               struct semid_ds *buf;       /* IPC_STAT, IPC_SET을 위한 버퍼 */
               unsigned short int *array;  /* GETALL, SETALL을 위한 배열 */
               struct seminfo *__buf;      /* IPC_INFO을 위한 버퍼 */
       };
#endif

int
Semctl(int id, int semnum, int cmd, ...)
{
	int     rc;
	va_list ap;
	union semun arg;

	if (cmd == GETALL || cmd == SETALL || cmd == SETVAL || cmd == IPC_STAT || cmd == IPC_SET) 
	{
		va_start(ap, cmd);      // init ap to final named argument 
		arg = va_arg(ap, union semun);
		
		if ( (rc = semctl(id, semnum, cmd, arg)) == -1)
			err_sys("semctl error");
		va_end(ap);
	} 
	else 
	{
		if ( (rc = semctl(id, semnum, cmd)) == -1)
			err_sys("semctl error");
	}
	return(rc);
}


key_t
Ftok(const char *pathname, int id)
{
	key_t   key;

	if ( (key = ftok(pathname, id)) == -1)
		err_sys("ftok error for pathname \"%s\" and id %d", pathname, id);
	return(key);
}


int
Getopt(int argc, char *const *argv, const char *str)
{
	int     opt;

	if ( ( opt = getopt(argc, argv, str)) == '?')
		exit(1);        /* getopt() has already written to stderr */
	return(opt);
}

void
Ftruncate(int fd, off_t length)
{
	if (ftruncate(fd, length) == -1)
		err_sys("ftruncate error");
}

int
Shm_open(const char *pathname, int oflag, mode_t mode)
{
	int     fd;

	if ( (fd = shm_open(pathname, oflag, mode)) == -1)
		err_sys("shm_open error for %s", pathname);
	return(fd);
}

void
Shm_unlink(const char *pathname)
{
	if (shm_unlink(pathname) == -1)
		err_sys("shm_unlink error");
}

void
Fstat(int fd, struct stat *ptr)
{
    if (fstat(fd, ptr) == -1)
	    err_sys("fstat error");
}
