d0d73dc5243e0e57a877521fd97928ba988d0024
[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         seekdir ((DIR*) dir, (off_t) offset);
27
28         return 0;
29 }
30
31 mph_off_t
32 Mono_Posix_Syscall_telldir (void *dir)
33 {
34         return telldir ((DIR*) dir);
35 }
36
37 static void
38 copy_dirent (struct Mono_Posix_Syscall__Dirent *to, struct dirent *from)
39 {
40         memset (to, 0, sizeof(*to));
41
42         to->d_ino    = from->d_ino;
43         to->d_name   = strdup (from->d_name);
44
45 #ifdef HAVE_STRUCT_DIRENT_D_OFF
46         to->d_off    = from->d_off;
47 #endif
48 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
49         to->d_reclen = from->d_reclen;
50 #endif
51 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
52         to->d_type   = from->d_type;
53 #endif
54 }
55
56 gint32
57 Mono_Posix_Syscall_readdir (void *dirp, struct Mono_Posix_Syscall__Dirent *entry)
58 {
59         struct dirent *d;
60
61         if (entry == NULL) {
62                 errno = EFAULT;
63                 return -1;
64         }
65
66         d = readdir (dirp);
67
68         if (d == NULL) {
69                 return -1;
70         }
71
72         copy_dirent (entry, d);
73
74         return 0;
75 }
76
77 gint32
78 Mono_Posix_Syscall_readdir_r (void *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
79 {
80         struct dirent _entry;
81         int r;
82
83         r = readdir_r (dirp, &_entry, (struct dirent**) result);
84
85         if (r == 0 && result != NULL) {
86                 copy_dirent (entry, &_entry);
87         }
88
89         return r;
90 }
91
92 int
93 Mono_Posix_Syscall_rewinddir (void* dir)
94 {
95         rewinddir (dir);
96         return 0;
97 }
98
99 G_END_DECLS
100
101 /*
102  * vim: noexpandtab
103  */