summaryrefslogtreecommitdiff
path: root/src/unistd/dup2.c
blob: 2755fbc750cc6a0358aa2bc888da6d0017105f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stddef.h"
#include "sys/types.h"
#include <unistd.h>
#include "fcntl.h"
#include "limits.h"
#include "errno.h"

int dup2(int fildes, int fildes2)
{
	if (fildes2 < 0 || fildes2 >= _POSIX_OPEN_MAX) {
		errno = EBADF;
		return -1;
	}

	if (fildes == fildes2) {
		return fildes2;
	}

	if (fcntl(fildes, F_GETFD) == -1) {
		errno = EBADF;
		return -1;
	}

	close(fildes2);
	return fcntl(fildes, F_DUPFD, fildes2);
}
/*
POSIX(1)
*/