Integrate Mono.Posix OEE with Mono.
[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 Jonathan Pryor
8  */
9
10 #include <dirent.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
14
15 #include "mph.h"
16
17 G_BEGIN_DECLS
18
19 struct Mono_Posix_Syscall__Dirent {
20         /* ino_t  */ mph_ino_t      d_ino;
21         /* off_t  */ mph_off_t      d_off;
22         /* ushort */ unsigned short d_reclen;
23         /* byte   */ unsigned char  d_type;
24         /* string */ char          *d_name;
25 };
26
27 gint32
28 Mono_Posix_Syscall_seekdir (DIR *dir, mph_off_t offset)
29 {
30         mph_return_if_off_t_overflow (offset);
31
32         errno = 0;
33
34         seekdir (dir, (off_t) offset);
35
36         return errno != 0;
37 }
38
39 mph_off_t
40 Mono_Posix_Syscall_telldir (DIR *dir)
41 {
42         return telldir (dir);
43 }
44
45 static void
46 copy_dirent (
47         struct Mono_Posix_Syscall__Dirent *to, 
48 #ifdef MPH_USE_64_API
49         struct dirent64 *from
50 #else
51         struct dirent *from
52 #endif
53         )
54 {
55         to->d_ino    = from->d_ino;
56         to->d_off    = from->d_off;
57         to->d_reclen = from->d_reclen;
58         to->d_type   = from->d_type;
59         to->d_name   = strdup (from->d_name);
60 }
61
62 gint32
63 Mono_Posix_Syscall_readdir (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry)
64 {
65 #ifdef MPH_USE_64_API
66         struct dirent64 *d;
67 #else
68         struct dirent *d;
69 #endif
70
71         if (entry == NULL) {
72                 errno = EFAULT;
73                 return -1;
74         }
75
76 #ifdef MPH_USE_64_API
77         d = readdir64 (dirp);
78 #else
79         d = readdir (dirp);
80 #endif
81         if (d == NULL) {
82                 return -1;
83         }
84
85         copy_dirent (entry, d);
86
87         return 0;
88 }
89
90 gint32
91 Mono_Posix_Syscall_readdir_r (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
92 {
93 #ifdef MPH_USE_64_API
94         struct dirent64 _entry;
95 #else
96         struct dirent _entry;
97 #endif
98         int r;
99
100 #ifdef MPH_USE_64_API
101         r = readdir64_r (dirp, &_entry, (struct dirent64**) result);
102 #else
103         r = readdir_r (dirp, &_entry, (struct dirent**) result);
104 #endif
105
106         if (r == 0 && result != NULL) {
107                 copy_dirent (entry, &_entry);
108         }
109
110         return r;
111 }
112
113 G_END_DECLS
114
115 /*
116  * vim: noexpandtab
117  */