source: rtems/c/src/tests/psxtests/psxreaddir/test.c @ 01a2e18

4.104.114.84.9
Last change on this file since 01a2e18 was 01a2e18, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 31, 1999 at 8:52:35 PM

Removed unneeded include of libio_.h.

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