1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
3f08c3bdfSopenharmony_ci *    ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
4f08c3bdfSopenharmony_ci *   Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
7f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
8f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
9f08c3bdfSopenharmony_ci *   (at your option) any later version.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
12f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
17f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
18f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci/*
22f08c3bdfSopenharmony_ci  WHAT:  Does dup return -1 on the 21st file?
23f08c3bdfSopenharmony_ci  HOW:   Create up to _NFILE (20) files and check for -1 return on the
24f08c3bdfSopenharmony_ci         next attempt
25f08c3bdfSopenharmony_ci         Should check NOFILE as well as _NFILE.  19-Jun-84 Dale.
26f08c3bdfSopenharmony_ci*/
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#include <stdio.h>
29f08c3bdfSopenharmony_ci#include <fcntl.h>
30f08c3bdfSopenharmony_ci#include <errno.h>
31f08c3bdfSopenharmony_ci#include <unistd.h>
32f08c3bdfSopenharmony_ci#include <sys/types.h>
33f08c3bdfSopenharmony_ci#include <sys/stat.h>
34f08c3bdfSopenharmony_ci#include <sys/param.h>
35f08c3bdfSopenharmony_ci#include "test.h"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cichar *TCID = "dup06";
38f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic int cnt_free_fds(int maxfd)
41f08c3bdfSopenharmony_ci{
42f08c3bdfSopenharmony_ci	int freefds = 0;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	for (maxfd--; maxfd >= 0; maxfd--)
45f08c3bdfSopenharmony_ci		if (fcntl(maxfd, F_GETFD) == -1 && errno == EBADF)
46f08c3bdfSopenharmony_ci			freefds++;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	return (freefds);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void);
52f08c3bdfSopenharmony_cistatic void cleanup(void);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ciint main(int ac, char **av)
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	int *fildes, i;
57f08c3bdfSopenharmony_ci	int min;
58f08c3bdfSopenharmony_ci	int freefds;
59f08c3bdfSopenharmony_ci	int lc;
60f08c3bdfSopenharmony_ci	const char *pfilname = "dup06";
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	setup();
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	min = getdtablesize();
67f08c3bdfSopenharmony_ci	freefds = cnt_free_fds(min);
68f08c3bdfSopenharmony_ci	fildes = malloc((min + 5) * sizeof(int));
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	for (i = 0; i < min + 5; i++)
71f08c3bdfSopenharmony_ci		fildes[i] = 0;
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
74f08c3bdfSopenharmony_ci		unlink(pfilname);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci		if ((fildes[0] = creat(pfilname, 0666)) == -1) {
77f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "Cannot open first file");
78f08c3bdfSopenharmony_ci		} else {
79f08c3bdfSopenharmony_ci			for (i = 1; i < min + 5; i++) {
80f08c3bdfSopenharmony_ci				if ((fildes[i] = dup(fildes[i - 1])) == -1)
81f08c3bdfSopenharmony_ci					break;
82f08c3bdfSopenharmony_ci			}
83f08c3bdfSopenharmony_ci			if (i < freefds) {
84f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Not enough files duped");
85f08c3bdfSopenharmony_ci			} else if (i > freefds) {
86f08c3bdfSopenharmony_ci				tst_resm(TFAIL, "Too many files duped");
87f08c3bdfSopenharmony_ci			} else {
88f08c3bdfSopenharmony_ci				tst_resm(TPASS, "Test passed.");
89f08c3bdfSopenharmony_ci			}
90f08c3bdfSopenharmony_ci		}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci		unlink(pfilname);
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci		for (i = 0; i < min + 5; i++) {
95f08c3bdfSopenharmony_ci			if (fildes[i] != 0 && fildes[i] != -1)
96f08c3bdfSopenharmony_ci				close(fildes[i]);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci			fildes[i] = 0;
99f08c3bdfSopenharmony_ci		}
100f08c3bdfSopenharmony_ci	}
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	cleanup();
103f08c3bdfSopenharmony_ci	tst_exit();
104f08c3bdfSopenharmony_ci}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_cistatic void setup(void)
107f08c3bdfSopenharmony_ci{
108f08c3bdfSopenharmony_ci	tst_tmpdir();
109f08c3bdfSopenharmony_ci}
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_cistatic void cleanup(void)
112f08c3bdfSopenharmony_ci{
113f08c3bdfSopenharmony_ci	tst_rmdir();
114f08c3bdfSopenharmony_ci}
115