48c0b0f0e00ed3ad637702eff3452a70146b96b9
[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 #ifdef MPH_ON_BSD
57         to->d_off    = 0;
58 #else
59         to->d_off    = from->d_off;
60 #endif
61         to->d_reclen = from->d_reclen;
62         to->d_type   = from->d_type;
63         to->d_name   = strdup (from->d_name);
64 }
65
66 gint32
67 Mono_Posix_Syscall_readdir (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry)
68 {
69 #ifdef MPH_USE_64_API
70         struct dirent64 *d;
71 #else
72         struct dirent *d;
73 #endif
74
75         if (entry == NULL) {
76                 errno = EFAULT;
77                 return -1;
78         }
79
80 #ifdef MPH_USE_64_API
81         d = readdir64 (dirp);
82 #else
83         d = readdir (dirp);
84 #endif
85         if (d == NULL) {
86                 return -1;
87         }
88
89         copy_dirent (entry, d);
90
91         return 0;
92 }
93
94 gint32
95 Mono_Posix_Syscall_readdir_r (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
96 {
97 #ifdef MPH_USE_64_API
98         struct dirent64 _entry;
99 #else
100         struct dirent _entry;
101 #endif
102         int r;
103
104 #ifdef MPH_USE_64_API
105         r = readdir64_r (dirp, &_entry, (struct dirent64**) result);
106 #else
107         r = readdir_r (dirp, &_entry, (struct dirent**) result);
108 #endif
109
110         if (r == 0 && result != NULL) {
111                 copy_dirent (entry, &_entry);
112         }
113
114         return r;
115 }
116
117 G_END_DECLS
118
119 /*
120  * vim: noexpandtab
121  */