1 /* $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $ */
3 * Written by Artur Grabowski <art@openbsd.org> 2001 Public Domain.
12 main(int argc, char *argv[])
15 char temp[] = "/tmp/dup2XXXXXXXXX";
17 if ((orgfd = mkstemp(temp)) < 0)
21 if (ftruncate(orgfd, 1024) != 0)
24 if ((fd1 = dup(orgfd)) < 0)
27 /* Set close-on-exec */
28 if (fcntl(fd1, F_SETFD, 1) != 0)
29 err(1, "fcntl(F_SETFD)");
31 if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
34 /* Test 1: Do we get the right fd? */
36 errx(1, "dup2 didn't give us the right fd");
38 /* Test 2: Was close-on-exec cleared? */
39 if (fcntl(fd2, F_GETFD) != 0)
40 errx(1, "dup2 didn't clear close-on-exec");