]> sourceware.org Git - newlib-cygwin.git/blob - winsup/testsuite/winsup.api/ltp/readdir01.c
* winsup.api/ltp/dup03.c: New test.
[newlib-cygwin.git] / winsup / testsuite / winsup.api / ltp / readdir01.c
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
33 /* $Id$ */
34 /**********************************************************
35 *
36 * OS Test - Silicon Graphics, Inc.
37 *
38 * TEST IDENTIFIER : readdir01
39 *
40 * EXECUTED BY : anyone
41 *
42 * TEST TITLE : write multiple files and try to find them with readdir
43 *
44 * TEST CASE TOTAL :
45 *
46 * WALL CLOCK TIME :
47 *
48 * CPU TYPES : ALL
49 *
50 * AUTHOR : Nate Straz
51 *
52 * CO-PILOT :
53 *
54 * DATE STARTED : 02/16/2001
55 *
56 * INITIAL RELEASE : Linux 2.4.x
57 *
58 * TEST CASES
59 *
60 * 1.) Create n files and check that readdir() finds n files
61 *
62 * INPUT SPECIFICATIONS
63 * The standard options for system call tests are accepted.
64 * (See the parse_opts(3) man page).
65 *
66 * OUTPUT SPECIFICATIONS
67 *
68 * DURATION
69 * Terminates - with frequency and infinite modes.
70 *
71 * SIGNALS
72 * Uses SIGUSR1 to pause before test if option set.
73 * (See the parse_opts(3) man page).
74 *
75 * RESOURCES
76 * None
77 *
78 * ENVIRONMENTAL NEEDS
79 * No run-time environmental needs.
80 *
81 * SPECIAL PROCEDURAL REQUIREMENTS
82 * None
83 *
84 * INTERCASE DEPENDENCIES
85 * None
86 *
87 * DETAILED DESCRIPTION
88 * This is a Phase I test for the readdir(2) system call. It is intended
89 * to provide a limited exposure of the system call, for now. It
90 * should/will be extended when full functional tests are written for
91 * readdir(2).
92 *
93 * Setup:
94 * Setup signal handling.
95 * Pause for SIGUSR1 if option specified.
96 *
97 * Test:
98 * Loop if the proper options are given.
99 * Execute system call
100 * Check return code, if system call failed (return=-1)
101 * Log the errno and Issue a FAIL message.
102 * Otherwise, Issue a PASS message.
103 *
104 * Cleanup:
105 * Print errno log and/or timing stats if options given
106 *
107 *
108 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
109
110 #include <sys/types.h>
111 #include <sys/stat.h>
112 #include <fcntl.h>
113 #include <dirent.h>
114 #include <unistd.h>
115 #include <errno.h>
116 #include <string.h>
117 #include <signal.h>
118 /* test.h and usctest.h are the two header files that are required by the
119 * quickhit package. They contain function and macro declarations which you
120 * can use in your test programs
121 */
122 #include "test.h"
123 #include "usctest.h"
124
125 /* The setup and cleanup functions are basic parts of a test case. These
126 * steps are usually put in separate functions for clarity. The help function
127 * is only needed when you are adding new command line options.
128 */
129 void setup();
130 void help();
131 void cleanup();
132
133 char *TCID="readdir01"; /* Test program identifier. */
134 int TST_TOTAL=2; /* Total number of test cases. */
135 extern int Tst_count; /* Test Case counter for tst_* routines */
136 extern int Tst_nobuf;
137
138 int exp_enos[]={0, 0};
139
140 #define BASENAME "readdirfile"
141
142 char Basename[255];
143 char Fname[255];
144 int Nfiles=0;
145
146 /* To add command line options you need to declare a structure to pass to
147 * parse_opts(). options is the structure used in this example. The format is
148 * the string that should be added to optstring in getopt(3), an integer that
149 * will be used as a flag if the option is given, and a pointer to a string that
150 * should receive the optarg parameter from getopt(3). Here we add a -N
151 * option. Long options are not supported at this time.
152 */
153 char *Nfilearg;
154 int Nflag=0;
155
156 /* for test specific parse_opts options */
157 option_t options[] = {
158 { "N:", &Nflag, &Nfilearg }, /* -N #files */
159 { NULL, NULL, NULL }
160 };
161
162 /***********************************************************************
163 * Main
164 ***********************************************************************/
165 int
166 main(int ac, char **av)
167 {
168 int lc; /* loop counter */
169 const char *msg; /* message returned from parse_opts */
170 int cnt;
171 int nfiles, fd;
172 char fname[255];
173 DIR *test_dir;
174 struct dirent *dptr;
175
176 Tst_nobuf=1;
177
178 /***************************************************************
179 * parse standard options
180 ***************************************************************/
181 /* start off by parsing the command line options. We provide a function
182 * that understands many common options to control looping. If you are not
183 * adding any new options, pass NULL in place of options and &help.
184 */
185 if ( (msg=parse_opts(ac, av, options, &help)) ) {
186 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
187 tst_exit();
188 }
189
190 if ( Nflag ) {
191 if (sscanf(Nfilearg, "%i", &Nfiles) != 1 ) {
192 tst_brkm(TBROK, NULL, "--N option arg is not a number");
193 tst_exit();
194 }
195 }
196
197 /***************************************************************
198 * perform global setup for test
199 ***************************************************************/
200 /* Next you should run a setup routine to make sure your environment is
201 * sane.
202 */
203 setup();
204
205 /* set the expected errnos... */
206 TEST_EXP_ENOS(exp_enos);
207
208 /***************************************************************
209 * check looping state
210 ***************************************************************/
211 /* TEST_LOOPING() is a macro that will make sure the test continues
212 * looping according to the standard command line args.
213 */
214 for (lc=0; TEST_LOOPING(lc); lc++) {
215
216 /* reset Tst_count in case we are looping. */
217 Tst_count=0;
218
219 if ( Nfiles )
220 nfiles = Nfiles;
221 else
222 /* min of 10 links and max of a 100 links */
223 nfiles = (lc%90)+10;
224
225 /* create a bunch of files to look at */
226 for(cnt=0; cnt < nfiles; cnt++) {
227
228 sprintf(fname, "%s%d", Basename, cnt);
229 if ((fd = open(fname, O_RDWR|O_CREAT, 0700)) == -1) {
230 tst_brkm(TBROK, cleanup,
231 "open(%s, O_RDWR|O_CREAT,0700) Failed, errno=%d : %s", fname, errno, strerror(errno));
232 } else if (write(fd, "hello\n", 6) < 0) {
233 tst_brkm(TBROK, cleanup,
234 "write(%s, \"hello\\n\", 6) Failed, errno=%d : %s", fname, errno, strerror(errno));
235 } else if (close(fd) < 0) {
236 tst_res(TWARN, "close(%s) Failed, errno=%d : %s",
237 fname, errno, strerror(errno));
238 }
239 }
240
241 if ((test_dir = opendir(".")) == NULL) {
242 tst_resm(TFAIL, "opendir(\".\") Failed, errno=%d : %s",
243 errno, strerror(errno));
244 } else {
245 /* count the entries we find to see if any are missing */
246 cnt = 0;
247 errno = 0;
248 while ( (dptr = readdir(test_dir)) ) {
249 if (strcmp(dptr->d_name, ".") && strcmp(dptr->d_name, ".."))
250 cnt++;
251 }
252
253 if (errno != 0) {
254 tst_resm(TFAIL, "readir(test_dir) Failed on try %d, errno=%d : %s",
255 cnt, errno, strerror(errno));
256 }
257 if (cnt == nfiles) {
258 tst_resm(TPASS, "found all %d that were created", nfiles);
259 } else if (cnt > nfiles) {
260 tst_resm(TFAIL, "found more files than were created");
261 tst_resm(TINFO, "created: %d, found: %d", nfiles, cnt);
262 } else {
263 tst_resm(TFAIL, "found less files than were created");
264 tst_resm(TINFO, "created: %d, found: %d", nfiles, cnt);
265 }
266 }
267
268 /* Here we clean up after the test case so we can do another iteration.
269 */
270 for(cnt=0; cnt < nfiles; cnt++) {
271
272 sprintf(fname, "%s%d", Basename, cnt);
273
274 if (unlink(fname) == -1) {
275 tst_res(TWARN, "unlink(%s) Failed, errno=%d : %s",
276 Fname, errno, strerror(errno));
277 }
278 }
279
280 } /* End for TEST_LOOPING */
281
282 /***************************************************************
283 * cleanup and exit
284 ***************************************************************/
285 cleanup();
286
287 return 0;
288 } /* End main */
289
290 /***************************************************************
291 * help
292 ***************************************************************/
293 /* The custom help() function is really simple. Just write your help message to
294 * standard out. Your help function will be called after the standard options
295 * have been printed
296 */
297 void
298 help()
299 {
300 printf(" -N #files : create #files files every iteration\n");
301 }
302
303 /***************************************************************
304 * setup() - performs all ONE TIME setup for this test.
305 ***************************************************************/
306 void
307 setup()
308 {
309 /* You will want to enable some signal handling so you can capture
310 * unexpected signals like SIGSEGV.
311 */
312 tst_sig(NOFORK, DEF_HANDLER, cleanup);
313
314 /* Pause if that option was specified */
315 /* One cavet that hasn't been fixed yet. TEST_PAUSE contains the code to
316 * fork the test with the -c option. You want to make sure you do this
317 * before you create your temporary directory.
318 */
319 TEST_PAUSE;
320
321 /* If you are doing any file work, you should use a temporary directory. We
322 * provide tst_tmpdir() which will create a uniquely named temporary
323 * directory and cd into it. You can now create files in the current
324 * directory without worrying.
325 */
326 tst_tmpdir();
327
328 sprintf(Basename, "%s_%d.", BASENAME, getpid());
329 }
330
331 /***************************************************************
332 * cleanup() - performs all ONE TIME cleanup for this test at
333 * completion or premature exit.
334 ***************************************************************/
335 void
336 cleanup()
337 {
338 /*
339 * print timing stats if that option was specified.
340 * print errno log if that option was specified.
341 */
342 TEST_CLEANUP;
343
344 /* If you use a temporary directory, you need to be sure you remove it. Use
345 * tst_rmdir() to do it automatically.
346 */
347 tst_rmdir();
348
349 /* exit with return code appropriate for results */
350 tst_exit();
351 }
This page took 0.053639 seconds and 5 git commands to generate.