xref: /openbsd/regress/sys/kern/fcntl_dup/fcntl_dup.c (revision 3cab2bb3)
1 /*	$OpenBSD: fcntl_dup.c,v 1.3 2017/07/27 10:42:24 bluhm Exp $	*/
2 /*
3  *	Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <err.h>
9 #include <fcntl.h>
10 
11 int
12 main(int argc, char *argv[])
13 {
14 	int orgfd, fd1, fd2;
15 	char temp[] = "/tmp/dup2XXXXXXXXX";
16 
17 	if ((orgfd = mkstemp(temp)) < 0)
18 		err(1, "mkstemp");
19 	remove(temp);
20 
21 	if (ftruncate(orgfd, 1024) != 0)
22 		err(1, "ftruncate");
23 
24 	if ((fd1 = dup(orgfd)) < 0)
25 		err(1, "dup");
26 
27 	/* Set close-on-exec */
28 	if (fcntl(fd1, F_SETFD, 1) != 0)
29 		err(1, "fcntl(F_SETFD)");
30 
31 	if ((fd2 = fcntl(fd1, F_DUPFD, 0)) < 0)
32 		err(1, "fcntl(F_DUPFD)");
33 
34 	/* Test 2: Was close-on-exec cleared? */
35 	if (fcntl(fd2, F_GETFD) != 0)
36 		errx(1, "fcntl(F_DUPFD) didn't clear close-on-exec");
37 
38 	return 0;
39 }
40