source: rtems/c/src/tests/psxtests/psxreaddir/test.c @ 1e566bbb

4.104.114.84.95
Last change on this file since 1e566bbb was 1e566bbb, checked in by Jennifer Averett <Jennifer.Averett@…>, on 11/17/00 at 19:06:40

2000-11-17 Jennifer Averret <jennifer@…>

  • psxmount/test.c, psxmount/psxmount.scn: Improve output to report expected error condition in one case.
  • psxreaddir/test.c, psxreaddir.scn: Added test cases to exercise readdir() of root of mounted filesystem. Also corrected the screen file for some mistakes noticed in this effort.
  • Property mode set to 100644
File size: 11.0 KB
RevLine 
[0895bdb]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 */
[3251b55]22
[0895bdb]23#include <stdio.h>
24#include <sys/types.h>
25#include <fcntl.h>
26#include <dirent.h>
27#include <string.h>
28#include <assert.h>
29#include <unistd.h>
30#include <errno.h>
[1e566bbb]31#include <imfs.h>
32#include <assert.h>
[0895bdb]33
34DIR *directory;
35DIR *directory2;
36DIR *directory3;
37DIR *directory_not;
38
39#ifndef __P
40#define __P(args)()
41#endif
42
43int scandir ( const char *dirname,
44   struct dirent *** namelist,
45   int (*select) __P((struct dirent *)),
46   int (*dcomp) __P((const void *, const void *))
47);
48
49#if defined(__rtems__)
50#define d_type d_reclen
51#endif
52
53void printdir( DIR *directory )
54{
55  struct dirent *d;
56
57  printf( "    %-20s %8s %8s %8s %4s\n",
58     "     name", "inode", " offset", "reclen", " type" );
59  d = readdir(directory);
60 
61  while (d) {
62    printf( "    %-20s %8d %8d %6d   0x%04x\n",
63       d->d_name, (int)d->d_ino, (int)d->d_off, d->d_reclen, d->d_type );
64    d = readdir(directory);
65
66  }
67}
68
[1e566bbb]69void complete_printdir( char *path )
70{
71  DIR *the_dir;
72  int status;
73
74  the_dir = opendir( path );
75  assert( the_dir );
76  printdir( the_dir );
77  status = closedir( the_dir );
78}
79
[0895bdb]80char *many_files[] = {
81        "a",
82        "b",
83        "c",
84        "d",
85        "e",
86        "f",
87        "g",
88        "h",
89        "i",
90        "j",
91        "k",
92        "l",
93        "m",
94        "n",
95        "o",
96        "p",
97        "q",
98        "r",
99        "s",
100        "t",
101        "u",
102        "v",
103        "w",
104        "x",
105        "y",
106        "z",
107        "aa",
108        "ab",
109        "ac",
110        "ad",
111        "ae",
112        "af",
113        "ag",
114        "ah",
115        "ai",
116        "aj",
117        "ak",
118        "al",
119        "am",
120        "an",
121        "ao",
122        "ap",
123        "aq",
124        "ar"
125};
126
127char *dnames[] = {
128        "a",
129        "b",
130        "c",
131        "d",
132        "e",
133        "f",
134        "c/y",
135        "c/z",
136        "c/x",
137        "c/y/a3333",
138        "c/y/j123",
139        "END"
140};
141
142int select1 ( struct dirent *entry )
143{
144   printf("SCANDIR SELECT1 accepts  nodename: %s\n", entry->d_name );
145   return 1;
146}
147
148int select2 ( struct dirent *entry )
149{
150   if( strcmp( entry->d_name, "y") == 0 ) {
151      printf("SCANDIR SELECT accepted nodename: %s\n", entry->d_name );
152      return 1;
153   }
154   printf("SCANDIR SELECT rejected nodename: %s\n", entry->d_name );
155   return 0;
156}
157
158int compare_ascending( struct dirent **a, struct dirent **b )
159{
160   int i;
161
162   i = strcmp (
163      (char *)((struct dirent *)(*a)->d_name),
164      (char *)((struct dirent *)(*b)->d_name)
165   );
166   return i;
167}
168
169
170int compare_descending( struct dirent **a, struct dirent **b )
171{
172   int i;
173
174   i = strcmp (
175      (char *)((struct dirent *)(*b)->d_name),
176      (char *)((struct dirent *)(*a)->d_name)
177   );
178
179   return i;
180}
181
[1e566bbb]182int test_across_mount()
183{
184  rtems_filesystem_mount_table_entry_t *mt_entry;
185  int                                  status;
186
187  /*
188   * Verify Readdir works correctly over mount points.
189   */
190
191  printf("Validate readdir across mount point\n");
192  assert( mkdir( "/imfs", 0777 ) == 0 );
193  assert( mkdir( "/imfs/should_be_hidden", 0777 ) == 0 );
194  complete_printdir("/imfs" );
195  printf("Attempting to mount IMFS file system at /imfs \n");
196  status = mount(
197     &mt_entry,
198     &IMFS_ops,
199     RTEMS_FILESYSTEM_READ_WRITE,
200     NULL,
201     "/imfs" );
202  assert( status == 0 );
203  if( mt_entry == NULL ){
204     printf(" NULL mount table entry was returned\n");
205  }
206  printf( "create /imfs/testdir and /imfs/testdir/testsubdir\n");
207
208  status = mkdir( "/imfs/testdir", 0777 );
209  assert( status == 0 );
210  status = mkdir( "/imfs/testdir/testsubdir", 0777 );
211  assert( status == 0 );
212 
213  complete_printdir("/imfs" );
214  complete_printdir("/imfs/" );
215  complete_printdir("/imfs/." );
216  complete_printdir("/imfs/testdir" );
217  complete_printdir("/imfs/testdir/.." );
218}
219
[0895bdb]220#if defined(__rtems__)
221int test_main(void)
222#else
223int main(
224  int argc,
225  char **argv
226)
227#endif
228{
229  int fd;
230  int i;
231  int status;
232  off_t off;
233  struct dirent *d_not;
234  struct dirent **namelist;
235  struct stat s;
236
237
238  printf( "\n\n*** READDIR TEST ***\n" );
239
240  printf( "\nchdir to the root directory\n" );
241  status = chdir( "/" );
242  printf( "chdir() status : %d\n\n", status );
243
244  printf( "\nCreating a series of directories under /\n" );
245  i=0;
246  while ( strcmp(dnames[i], "END") != 0 )
247  {
248     status = mkdir( dnames[i], 0x1c0 );
249     printf("Creating directory: %s      %d %d   ", dnames[i], status, errno );
250     if ( errno == 0 )
251        printf(" Success\n");
252     else
253        printf(" Failure\n");
254
255     i++;
256  }
257
[c2f9b97]258  /*
259   * Create files under many and open the directory.
260   */
261
262  printf("Create a lot of files\n");
[0895bdb]263  status = mkdir( "/many", 0x1c0 );
264  status = chdir( "/many" );
[c2f9b97]265  for (i = 0; i<44; i++) {
266    printf("  Create %s\n", many_files[i]);
267    fd = open (many_files[i], O_CREAT, S_IRWXU);
[0895bdb]268    close (fd);
269  }
[c2f9b97]270  printf("Open /many and print the directory\n");
[0895bdb]271  directory_not = opendir( "/many" );
272  printdir ( directory_not );
273  d_not = readdir( directory_not );
274
[c2f9b97]275  printf("open /b/myfile\n");
276  fd = open ("/b/my_file", O_CREAT, S_IRWXU);
[0895bdb]277  assert( fd != -1 );
278  close (fd);
279
[c2f9b97]280  printf("scandir a file status: ");
[0895bdb]281  status = scandir(
282     "/b/my_file",
283     &namelist,
284     select1,
285     NULL
286  );
[c2f9b97]287  printf("%d\n", status);
[0895bdb]288
[c2f9b97]289  printf("Open /b/new_file\n");
290  fd  = open( "/b/new_file", O_CREAT, S_IRWXU );
[0895bdb]291  assert( fd != -1 );
292
[c2f9b97]293  printf("fcntl F_SETFD should return 0\n");
[0895bdb]294  status = fcntl( fd, F_SETFD, 1 );
295  assert( status == 0 );
[c2f9b97]296
297  printf("fcntl F_SETFD should return 1\n");
[0895bdb]298  status = fcntl( fd, F_GETFD, 1 );
299  assert( status == 1 );
300
[3251b55]301#if 0
[c2f9b97]302  printf("fcntl F_DUPFD should return 0\n");
303  status = fcntl( fd, F_DUPFD, 0 );
304  assert ( status == 0 );
[3251b55]305#else
306  printf("fcntl F_DUPFD should return 0 -- skip until implemented\n");
307#endif
[0895bdb]308
[3251b55]309  printf("fcntl F_GETFL returns current flags\n");
[0895bdb]310  status = fcntl( fd, F_GETFL, 1 );
[3251b55]311  printf("fcntl F_GETFL returned 0x%x\n", status );
312  assert( status != -1 );
[0895bdb]313
[3251b55]314  printf("fcntl F_SETFL to add O_APPEND and O_NONBLOCK\n");
315  status = fcntl( fd, F_SETFL, O_APPEND|O_NONBLOCK );
316  assert ( status != -1 );
317
318  printf("fcntl F_GETFL return current flags to see changes\n");
319  status = fcntl( fd, F_GETFL, 1 );
320  printf("fcntl F_GETFL returned 0x%x\n", status );
321  assert( status != -1 );
[0895bdb]322
[c2f9b97]323  printf("fcntl F_GETLK should return -1\n");
[0895bdb]324  status = fcntl( fd, F_GETLK, 1 );
325  assert ( status == -1 );
326
[c2f9b97]327  printf("fcntl F_SETLK should return -1\n");
[0895bdb]328  status = fcntl( fd, F_SETLK, 1 );
329  assert ( status == -1 );
330
[c2f9b97]331  printf("fcntl F_SETLKW should return -1\n");
[0895bdb]332  status = fcntl( fd, F_SETLKW, 1 );
333  assert ( status == -1 );
334
[c2f9b97]335  printf("fcntl F_SETOWN should return -1\n");
[0895bdb]336  status = fcntl( fd, F_SETOWN, 1 );
337  assert ( status == -1 );
338
[c2f9b97]339  printf("fcntl F_GETOWN should return -1\n");
[0895bdb]340  status = fcntl( fd, F_GETOWN, 1 );
341  assert ( status == -1 );
342
[c2f9b97]343  printf("fcntl invalid argument should return -1\n");
[0895bdb]344  status = fcntl( fd, 0xb, 1 );
[c2f9b97]345  printf("Status %d\n",status);
[0895bdb]346  assert( status == -1 );
347
[c2f9b97]348  printf("opendir and readdir /b/myfile\n");
[0895bdb]349  directory_not = opendir ("/b/my_file");
350  d_not = readdir(directory_not);
351
[c2f9b97]352  printf("opendir and readdir\n");
[0895bdb]353  directory_not = opendir ("/a");
354  d_not = readdir (directory_not);
355
[c2f9b97]356  printf("chdir to /b/myfile\n");
[0895bdb]357  status = chdir ("/b/my_file");
358  assert (status == -1);
359
360  printf( "\nPerforming stat of directory /\n");
361  status = stat( "/", &s );
362  printf("status for stat : %d, size of directory: %d\n\n",
363         status,(int)s.st_size);
364
[c2f9b97]365  puts( "\nOpen and print directory /" );
[0895bdb]366  directory = opendir("/");
367  assert( directory );
368  printdir(directory);
369
370  printf("\nmkdir /d/my_dir\n");
371  status = mkdir( "/d/my_dir", 0x1c0 );
372  printf("Open /d/my_dir\n");
373  directory_not = opendir( "/d/my_dir" );
374  assert( directory_not );
375
376  printf( "remove /d/my_dir.\n" );
377  status = rmdir( "/d/my_dir" );
378  assert( status == 0 );
379
380  printf( "close /d/my_dir.\n" );
381  closedir( directory_not );
382
383  printf( "\nOpening directory /c\n" );
384  directory2 = opendir("/c");
385
386  assert( directory2 );
387
388  printdir(directory2);
389  status = closedir( directory2 );
390
391  printf( "\nOpening directory /c/y\n" );
392  directory3 = opendir("/c/y");
393  assert( directory3 );
394  printdir(directory3);
395  status = closedir( directory3 );
396
397  printf( "\nLSEEK to the start of the open directory\n" );
398  lseek( directory->dd_fd, 0, SEEK_SET );
399  printdir(directory);
400
401  lseek( directory->dd_fd, 0, SEEK_CUR );
402
403  lseek( directory->dd_fd, 0, SEEK_END );
404
405  lseek( directory->dd_fd, 0, -99 );
406
407  printf( "\nRewinding directory\n" );
408  rewinddir( directory );
409  printdir(directory);
410
411/* Don't know how to check this one automatically. */
412  printf( "Send rewinddir a NULL pointer\n");
413  rewinddir( NULL );
414
415  printf( "\nSeek directory\n" );
416  printf( "telldir() should report only sizeof(struct dirent) increments \n" );
417  printf( "in position. Sizeof(struct dirent): %d\n", sizeof(struct dirent) );
418  rewinddir( directory );
419  for( off=0 ; off<=200 ; off=off + sizeof(struct dirent) / 4 ) {
420    seekdir( directory, off );
421    printf(
422       "seeked to %2d -- currently at %2d\n",
423       (int)off,
424       (int)telldir(directory)
425    );
426  }
427
[3251b55]428  printf( "Send seekdir a NULL pointer\n");
[0895bdb]429  seekdir( NULL, off );
430
431  printf( "\nClosing directory\n" );
432  status = closedir( directory );
433
434  printf( "\nSCANDIR TEST\n");
435  printf( "\nselection rule 1\n");
436  printf( "scanning for any entry under directory /c\n\n");
437  status = scandir(
438     "/c",
439     &namelist,
440     select1,
441     NULL
442  );
443  printf("\nscandir status: %d\n", status );
444  for ( i=0; i<status; i++)
445  {
446     printf("Selected Node Name: %s\n", namelist[i]->d_name );
447  }
448
449  printf( "\nselection rule 2\n");
450  printf( "scanning for any entry under directory /c whose name = y\n\n");
451  status = scandir(
452     "/c",
453     &namelist,
454     select2,
455     NULL
456  );
457  printf("\nscandir status: %d\n", status );
458  for ( i=0; i<status; i++)
459  {
460     printf("Selected Node Name: %s\n", namelist[i]->d_name );
461  }
462
463  printf( "\nSCANDIR with sorting\n" );
464  printf( "\nselection rule 1\n");
465  printf( "scanning for any entry under directory /c\n");
466  printf( "sort in ascending order\n\n");
467  status = scandir(
468     "/c",
469     &namelist,
470     select1,
471     compare_ascending
472  );
473  printf("\nscandir status: %d\n", status );
474  for ( i=0; i<status; i++)
475  {
476     printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name );
477  }
478
479
480  printf( "\nSCANDIR with sorting\n" );
481  printf( "\nselection rule 1\n");
482  printf( "scanning for any entry under directory /c\n");
483  printf( "sort in descending order\n\n");
484  status = scandir(
485     "/c",
486     &namelist,
487     select1,
488     compare_descending
489  );
490  printf("scandir status: %d\n", status );
491  for ( i=0; i<status; i++)
492  {
493     printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name );
494  }
495
[1e566bbb]496  test_across_mount();
[0895bdb]497  printf( "\n\n*** END OF READDIR TEST ***\n" );
498  exit(0);
499}
500
Note: See TracBrowser for help on using the repository browser.