source: rtems/c/src/tests/psxtests/psxreaddir/test.c @ 0895bdb

4.104.114.84.95
Last change on this file since 0895bdb was 0895bdb, checked in by Joel Sherrill <joel.sherrill@…>, on 11/23/98 at 18:57:48

Added tests in support of the file system infrastructure.

  • Property mode set to 100644
File size: 8.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  status = mkdir( "/many", 0x1c0 );
209  status = chdir( "/many" );
210  for (i = 0; i<=44; i++) {
211    fd = open (many_files[i], O_CREAT);
212    close (fd);
213  }
214  directory_not = opendir( "/many" );
215  printdir ( directory_not );
216  d_not = readdir( directory_not );
217
218  fd = open ("/b/my_file", O_CREAT);
219  assert( fd != -1 );
220  close (fd);
221
222  status = scandir(
223     "/b/my_file",
224     &namelist,
225     select1,
226     NULL
227  );
228
229  fd  = open( "/b/new_file", O_CREAT );
230  assert( fd != -1 );
231
232  status = fcntl( fd, F_SETFD, 1 );
233  assert( status == 0 );
234  status = fcntl( fd, F_GETFD, 1 );
235  assert( status == 1 );
236
237  status = fcntl( fd, F_DUPFD, 1 );
238  assert ( status == -1 );
239
240  status = fcntl( fd, F_GETFL, 1 );
241  assert ( status == -1 );
242
243  status = fcntl( fd, F_SETFL, 1 );
244  assert ( status == -1 );
245
246  status = fcntl( fd, F_GETLK, 1 );
247  assert ( status == -1 );
248
249  status = fcntl( fd, F_SETLK, 1 );
250  assert ( status == -1 );
251
252  status = fcntl( fd, F_SETLKW, 1 );
253  assert ( status == -1 );
254
255  status = fcntl( fd, F_SETOWN, 1 );
256  assert ( status == -1 );
257
258  status = fcntl( fd, F_GETOWN, 1 );
259  assert ( status == -1 );
260
261  status = fcntl( fd, 0xb, 1 );
262  assert( status == -1 );
263
264  directory_not = opendir ("/b/my_file");
265  d_not = readdir(directory_not);
266
267  directory_not = opendir ("/a");
268  d_not = readdir (directory_not);
269
270  status = chdir ("/b/my_file");
271  assert (status == -1);
272
273  printf( "\nPerforming stat of directory /\n");
274  status = stat( "/", &s );
275  printf("status for stat : %d, size of directory: %d\n\n",
276         status,(int)s.st_size);
277
278  puts( "\nOpening directory /" );
279  directory = opendir("/");
280
281  assert( directory );
282
283  printdir(directory);
284
285  printf("\nmkdir /d/my_dir\n");
286  status = mkdir( "/d/my_dir", 0x1c0 );
287  printf("Open /d/my_dir\n");
288  directory_not = opendir( "/d/my_dir" );
289  assert( directory_not );
290
291  printf( "remove /d/my_dir.\n" );
292  status = rmdir( "/d/my_dir" );
293  assert( status == 0 );
294
295  printf( "close /d/my_dir.\n" );
296  closedir( directory_not );
297
298  printf( "\nOpening directory /c\n" );
299  directory2 = opendir("/c");
300
301  assert( directory2 );
302
303  printdir(directory2);
304  status = closedir( directory2 );
305
306  printf( "\nOpening directory /c/y\n" );
307  directory3 = opendir("/c/y");
308
309  assert( directory3 );
310
311  printdir(directory3);
312  status = closedir( directory3 );
313
314  printf( "\nLSEEK to the start of the open directory\n" );
315  lseek( directory->dd_fd, 0, SEEK_SET );
316  printdir(directory);
317
318  lseek( directory->dd_fd, 0, SEEK_CUR );
319
320  lseek( directory->dd_fd, 0, SEEK_END );
321
322  lseek( directory->dd_fd, 0, -99 );
323
324  printf( "\nRewinding directory\n" );
325  rewinddir( directory );
326  printdir(directory);
327
328/* Don't know how to check this one automatically. */
329  printf( "Send rewinddir a NULL pointer\n");
330  rewinddir( NULL );
331
332  printf( "\nSeek directory\n" );
333  printf( "telldir() should report only sizeof(struct dirent) increments \n" );
334  printf( "in position. Sizeof(struct dirent): %d\n", sizeof(struct dirent) );
335  rewinddir( directory );
336  for( off=0 ; off<=200 ; off=off + sizeof(struct dirent) / 4 ) {
337    seekdir( directory, off );
338    printf(
339       "seeked to %2d -- currently at %2d\n",
340       (int)off,
341       (int)telldir(directory)
342    );
343  }
344
345  seekdir( NULL, off );
346
347  printf( "\nClosing directory\n" );
348  status = closedir( directory );
349
350  printf( "\nSCANDIR TEST\n");
351  printf( "\nselection rule 1\n");
352  printf( "scanning for any entry under directory /c\n\n");
353  status = scandir(
354     "/c",
355     &namelist,
356     select1,
357     NULL
358  );
359  printf("\nscandir status: %d\n", status );
360  for ( i=0; i<status; i++)
361  {
362     printf("Selected Node Name: %s\n", namelist[i]->d_name );
363  }
364
365  printf( "\nselection rule 2\n");
366  printf( "scanning for any entry under directory /c whose name = y\n\n");
367  status = scandir(
368     "/c",
369     &namelist,
370     select2,
371     NULL
372  );
373  printf("\nscandir status: %d\n", status );
374  for ( i=0; i<status; i++)
375  {
376     printf("Selected Node Name: %s\n", namelist[i]->d_name );
377  }
378
379  printf( "\nSCANDIR with sorting\n" );
380  printf( "\nselection rule 1\n");
381  printf( "scanning for any entry under directory /c\n");
382  printf( "sort in ascending order\n\n");
383  status = scandir(
384     "/c",
385     &namelist,
386     select1,
387     compare_ascending
388  );
389  printf("\nscandir status: %d\n", status );
390  for ( i=0; i<status; i++)
391  {
392     printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name );
393  }
394
395
396  printf( "\nSCANDIR with sorting\n" );
397  printf( "\nselection rule 1\n");
398  printf( "scanning for any entry under directory /c\n");
399  printf( "sort in descending order\n\n");
400  status = scandir(
401     "/c",
402     &namelist,
403     select1,
404     compare_descending
405  );
406  printf("scandir status: %d\n", status );
407  for ( i=0; i<status; i++)
408  {
409     printf("Selected and Sorted Node Name: %s\n", namelist[i]->d_name );
410  }
411
412
413  printf( "\n\n*** END OF READDIR TEST ***\n" );
414  exit(0);
415}
416
Note: See TracBrowser for help on using the repository browser.