OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [tests/] [psxtests/] [psxreaddir/] [test.c] - Blame information for rev 484

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.