New test.
[mono.git] / support / dirent.c
1 /*
2  * <dirent.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004-2005 Jonathan Pryor
8  */
9
10 #include <dirent.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #include "map.h"
17 #include "mph.h"
18
19 G_BEGIN_DECLS
20
21 gint32
22 Mono_Posix_Syscall_seekdir (void *dir, mph_off_t offset)
23 {
24         mph_return_if_off_t_overflow (offset);
25
26         errno = 0;
27
28         seekdir ((DIR*) dir, (off_t) offset);
29
30         return errno != 0;
31 }
32
33 mph_off_t
34 Mono_Posix_Syscall_telldir (void *dir)
35 {
36         return telldir ((DIR*) dir);
37 }
38
39 static void
40 copy_dirent (struct Mono_Posix_Syscall__Dirent *to, struct dirent *from)
41 {
42         memset (to, 0, sizeof(*to));
43
44         to->d_ino    = from->d_ino;
45         to->d_name   = strdup (from->d_name);
46
47 #ifdef HAVE_STRUCT_DIRENT_D_OFF
48         to->d_off    = from->d_off;
49 #endif
50 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
51         to->d_reclen = from->d_reclen;
52 #endif
53 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
54         to->d_type   = from->d_type;
55 #endif
56 }
57
58 gint32
59 Mono_Posix_Syscall_readdir (void *dirp, struct Mono_Posix_Syscall__Dirent *entry)
60 {
61         struct dirent *d;
62
63         if (entry == NULL) {
64                 errno = EFAULT;
65                 return -1;
66         }
67
68         d = readdir (dirp);
69
70         if (d == NULL) {
71                 return -1;
72         }
73
74         copy_dirent (entry, d);
75
76         return 0;
77 }
78
79 gint32
80 Mono_Posix_Syscall_readdir_r (void *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
81 {
82         struct dirent _entry;
83         int r;
84
85         r = readdir_r (dirp, &_entry, (struct dirent**) result);
86
87         if (r == 0 && result != NULL) {
88                 copy_dirent (entry, &_entry);
89         }
90
91         return r;
92 }
93
94 int
95 Mono_Posix_Syscall_rewinddir (void* dir)
96 {
97         errno = 0;
98         rewinddir (dir);
99         return errno == 0 ? 0 : -1;
100 }
101
102 G_END_DECLS
103
104 /*
105  * vim: noexpandtab
106  */