source: rtems/c/src/tests/psxtests/psxreaddir/test.c @ 7b158663

4.104.114.84.95
Last change on this file since 7b158663 was c2f9b97, checked in by Jennifer Averett <Jennifer.Averett@…>, on 12/03/98 at 22:41:57

Cleaned up test.
Updated scn files to match present expected test output.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 *  This is a native test to explore how the readdir() family works.
3 *  Newlib supports the following readdir() family members:
4 *
5 *    closedir()   -
6 *    readdir()    -
7 *    scandir()    -
8 *    opendir()    -
9 *    rewinddir()  -
10 *    telldir()    - BSD not in POSIX
11 *    seekdir()    - BSD not in POSIX
12 *
13 *
14 *  seekdir() takes an offset which is a byte offset.  The Linux
15 *  implementation of this appears to seek to the ((off/DIRENT_SIZE) + 1)
16 *  record where DIRENT_SIZE seems to be 12 bytes.
17 *
18 *
19 *
20 *  $Id$
21 */
22#include <stdio.h>
23#include <sys/types.h>
24#include <fcntl.h>
25#include <dirent.h>
26#include <string.h>
27#include <assert.h>
28#include <unistd.h>
29#include <errno.h>
30
31#include "/usr1/rtems/mg10/rtems-mg10/c/src/lib/libc/libio_.h"
32
33DIR *directory;
34DIR *directory2;
35DIR *directory3;
36DIR *directory_not;
37
38#ifndef __P
39#define __P(args)()
40#endif
41
42int scandir ( const char *dirname,
43   struct dirent *** namelist,
44   int (*select) __P((struct dirent *)),
45   int (*dcomp) __P((const void *, const void *))
46);
47
48#if defined(__rtems__)
49#define d_type d_reclen
50#endif
51
52void printdir( DIR *directory )
53{
54  struct dirent *d;
55
56  printf( "    %-20s %8s %8s %8s %4s\n",
57     "     name", "inode", " offset", "reclen", " type" );
58  d = readdir(directory);
59 
60  while (d) {
61    printf( "    %-20s %8d %8d %6d   0x%04x\n",
62       d->d_name, (int)d->d_ino, (int)d->d_off, d->d_reclen, d->d_type );
63    d = readdir(directory);
64
65  }
66}
67
68char *many_files[] = {
69        "a",
70        "b",
71        "c",
72        "d",
73        "e",
74        "f",
75        "g",
76        "h",
77        "i",
78        "j",
79        "k",
80        "l",
81        "m",
82        "n",
83        "o",
84        "p",
85        "q",
86        "r",
87        "s",
88        "t",
89        "u",
90        "v",
91        "w",
92        "x",
93        "y",
94        "z",
95        "aa",
96        "ab",
97        "ac",
98        "ad",
99        "ae",
100        "af",
101        "ag",
102        "ah",
103        "ai",
104        "aj",
105        "ak",
106        "al",
107        "am",
108        "an",
109        "ao",
110        "ap",
111        "aq",
112        "ar"
113};
114
115char *dnames[] = {
116        "a",
117        "b",
118        "c",
119        "d",
120        "e",
121        "f",
122        "c/y",
123        "c/z",
124        "c/x",
125        "c/y/a3333",
126        "c/y/j123",
127        "END"
128};
129
130int select1 ( struct dirent *entry )
131{
132   printf("SCANDIR SELECT1 accepts  nodename: %s\n", entry->d_name );
133   return 1;
134}
135
136int select2 ( struct dirent *entry )
137{
138   if( strcmp( entry->d_name, "y") == 0 ) {
139      printf("SCANDIR SELECT accepted nodename: %s\n", entry->d_name );
140      return 1;
141   }
142   printf("SCANDIR SELECT rejected nodename: %s\n", entry->d_name );
143   return 0;
144}
145
146int compare_ascending( struct dirent **a, struct dirent **b )
147{
148   int i;
149
150   i = strcmp (
151      (char *)((struct dirent *)(*a)->d_name),
152      (char *)((struct dirent *)(*b)->d_name)
153   );
154   return i;
155}
156
157
158int compare_descending( struct dirent **a, struct dirent **b )
159{
160   int i;
161
162   i = strcmp (
163      (char *)((struct dirent *)(*b)->d_name),
164      (char *)((struct dirent *)(*a)->d_name)
165   );
166
167   return i;
168}
169
170#if defined(__rtems__)
171int test_main(void)
172#else
173int main(
174  int argc,
175  char **argv
176)
177#endif
178{
179  int fd;
180  int i;
181  int status;
182  off_t off;
183  struct dirent *d_not;
184  struct dirent **namelist;
185  struct stat s;
186
187
188  printf( "\n\n*** READDIR TEST ***\n" );
189
190  printf( "\nchdir to the root directory\n" );
191  status = chdir( "/" );
192  printf( "chdir() status : %d\n\n", status );
193
194  printf( "\nCreating a series of directories under /\n" );
195  i=0;
196  while ( strcmp(dnames[i], "END") != 0 )
197  {
198     status = mkdir( dnames[i], 0x1c0 );
199     printf("Creating directory: %s      %d %d   ", dnames[i], status, errno );
200     if ( errno == 0 )
201        printf(" Success\n");
202     else
203        printf(" Failure\n");
204
205     i++;
206  }
207
208  /*
209   * Create files under many and open the directory.
210   */
211
212  printf("Create a lot of files\n");
213  status = mkdir( "/many", 0x1c0 );
214  status = chdir( "/many" );
215  for (i = 0; i<44; i++) {
216    printf("  Create %s\n", many_files[i]);
217    fd = open (many_files[i], O_CREAT, S_IRWXU);
218    close (fd);
219  }
220  printf("Open /many and print the directory\n");
221  directory_not = opendir( "/many" );
222  printdir ( directory_not );
223  d_not = readdir( directory_not );
224
225  printf("open /b/myfile\n");
226  fd = open ("/b/my_file", O_CREAT, S_IRWXU);
227  assert( fd != -1 );
228  close (fd);
229
230  printf("scandir a file status: ");
231  status = scandir(
232     "/b/my_file",
233     &namelist,
234     select1,
235     NULL
236  );
237  printf("%d\n", status);
238
239  printf("Open /b/new_file\n");
240  fd  = open( "/b/new_file", O_CREAT, S_IRWXU );
241  assert( fd != -1 );
242
243  printf("fcntl F_SETFD should return 0\n");
244  status = fcntl( fd, F_SETFD, 1 );
245  assert( status == 0 );
246
247  printf("fcntl F_SETFD should return 1\n");
248  status = fcntl( fd, F_GETFD, 1 );
249  assert( status == 1 );
250
251  printf("fcntl F_DUPFD should return 0\n");
252  status = fcntl( fd, F_DUPFD, 0 );
253  assert ( status == 0 );
254
255  printf("fcntl F_GETFL should return -1\n");
256  status = fcntl( fd, F_GETFL, 1 );
257  assert ( status == -1 );
258
259  printf("fcntl F_SETFL should return -1\n");
260  status = fcntl( fd, F_SETFL, 1 );
261  assert ( status == -1 );
262
263  printf("fcntl F_GETLK should return -1\n");
264  status = fcntl( fd, F_GETLK, 1 );
265  assert ( status == -1 );
266
267  printf("fcntl F_SETLK should return -1\n");
268  status = fcntl( fd, F_SETLK, 1 );
269  assert ( status == -1 );
270
271  printf("fcntl F_SETLKW should return -1\n");
272  status = fcntl( fd, F_SETLKW, 1 );
273  assert ( status == -1 );
274
275  printf("fcntl F_SETOWN should return -1\n");
276  status = fcntl( fd, F_SETOWN, 1 );
277  assert ( status == -1 );
278
279  printf("fcntl F_GETOWN should return -1\n");
280  status = fcntl( fd, F_GETOWN, 1 );
281  assert ( status == -1 );
282
283  printf("fcntl invalid argument should return -1\n");
284  status = fcntl( fd, 0xb, 1 );
285  printf("Status %d\n",status);
286  assert( status == -1 );
287
288  printf("opendir and readdir /b/myfile\n");
289  directory_not = opendir ("/b/my_file");
290  d_not = readdir(directory_not);
291
292  printf("opendir and readdir\n");
293  directory_not = opendir ("/a");
294  d_not = readdir (directory_not);
295
296  printf("chdir to /b/myfile\n");
297  status = chdir ("/b/my_file");
298  assert (status == -1);
299
300  printf( "\nPerforming stat of directory /\n");
301  status = stat( "/", &s );
302  printf("status for stat : %d, size of directory: %d\n\n",
303         status,(int)s.st_size);
304
305  puts( "\nOpen and print directory /" );
306  directory = opendir("/");
307  assert( directory );
308  printdir(directory);
309
310  printf("\nmkdir /d/my_dir\n");
311  status = mkdir( "/d/my_dir", 0x1c0 );
312  printf("Open /d/my_dir\n");
313  directory_not = opendir( "/d/my_dir" );
314  assert( directory_not );
315
316  printf( "remove /d/my_dir.\n" );
317  status = rmdir( "/d/my_dir" );
318  assert( status == 0 );
319
320  printf( "close /d/my_dir.\n" );
321  closedir( directory_not );
322
323  printf( "\nOpening directory /c\n" );
324  directory2 = opendir("/c");
325
326  assert( directory2 );
327
328  printdir(directory2);
329  status = closedir( directory2 );
330
331  printf( "\nOpening directory /c/y\n" );
332  directory3 = opendir("/c/y");
333  assert( directory3 );
334  printdir(directory3);
335  status = closedir( directory3 );
336
337  printf( "\nLSEEK to the start of the open directory\n" );
338  lseek( directory->dd_fd, 0, SEEK_SET );
339  printdir(directory);
340
341  lseek( directory->dd_fd, 0, SEEK_CUR );
342
343  lseek( directory->dd_fd, 0, SEEK_END );
344
345  lseek( directory->dd_fd, 0, -99 );
346
347  printf( "\nRewinding directory\n" );
348  rewinddir( directory );
349  printdir(directory);
350
351/* Don't know how to check this one automatically. */
352  printf( "Send rewinddir a NULL pointer\n");
353  rewinddir( NULL );
354
355  printf( "\nSeek directory\n" );
356  printf( "telldir() should report only sizeof(struct dirent) increments \n" );
357  printf( "in position. Sizeof(struct dirent): %d\n", sizeof(struct dirent) );
358  rewinddir( directory );
359  for( off=0 ; off<=200 ; off=off + sizeof(struct dirent) / 4 ) {
360    seekdir( directory, off );
361    printf(
362       "seeked to %2d -- currently at %2d\n",
363       (int)off,
364       (int)telldir(directory)
365    );
366  }
367
368  seekdir( NULL, off );
369
370  printf( "\nClosing directory\n" );
371  status = closedir( directory );
372
373  printf( "\nSCANDIR TEST\n");
374  printf( "\nselection rule 1\n");
375  printf( "scanning for any entry under directory /c\n\n");
376  status = scandir(
377     "/c",
378     &namelist,
379     select1,
380     NULL
381  );
382  printf("\nscandir status: %d\n", status );
383  for ( i=0; i<status; i++)
384  {
385     printf("Selected Node Name: %s\n", namelist[i]->d_name );
386  }
387
388  printf( "\nselection rule 2\n");
389  printf( "scanning for any entry under directory /c whose name = y\n\n");
390  status = scandir(
391     "/c",
392     &namelist,
393     select2,
394     NULL
395  );
396  printf("\nscandir status: %d\n", status );
397  for ( i=0; i<status; i++)
398  {
399     printf("Selected Node Name: %s\n", namelist[i]->d_name );
400  }
401
402  printf( "\nSCANDIR with sorting\n" );
403  printf( "\nselection rule 1\n");
404  printf( "scanning for any entry under directory /c\n");
405  printf( "sort in ascending order\n\n");
406  status = scandir(
407     "/c",
408     &namelist,
409     select1,
410     compare_ascending
411  );
412  printf("\nscandir status: %d\n", status );
413  for ( i=0; i<status; i++)
414  {
415     printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name );
416  }
417
418
419  printf( "\nSCANDIR with sorting\n" );
420  printf( "\nselection rule 1\n");
421  printf( "scanning for any entry under directory /c\n");
422  printf( "sort in descending order\n\n");
423  status = scandir(
424     "/c",
425     &namelist,
426     select1,
427     compare_descending
428  );
429  printf("scandir status: %d\n", status );
430  for ( i=0; i<status; i++)
431  {
432     printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name );
433  }
434
435
436  printf( "\n\n*** END OF READDIR TEST ***\n" );
437  exit(0);
438}
439
Note: See TracBrowser for help on using the repository browser.