Merge pull request #5714 from alexischr/update_bockbuild
[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 <limits.h>
15 #include <unistd.h>
16
17 #include "map.h"
18 #include "mph.h"
19
20 #if defined (PATH_MAX) && defined (NAME_MAX)
21         #define MPH_PATH_MAX MAX(PATH_MAX, NAME_MAX)
22 #elif defined (PATH_MAX)
23         #define MPH_PATH_MAX PATH_MAX
24 #elif defined (NAME_MAX)
25         #define MPH_PATH_MAX NAME_MAX
26 #else /* !defined PATH_MAX && !defined NAME_MAX */
27         #define MPH_PATH_MAX 2048
28 #endif
29
30 G_BEGIN_DECLS
31
32 #if HAVE_SEEKDIR
33 gint32
34 Mono_Posix_Syscall_seekdir (void *dir, mph_off_t offset)
35 {
36         mph_return_if_off_t_overflow (offset);
37
38         seekdir ((DIR*) dir, (off_t) offset);
39
40         return 0;
41 }
42 #endif  /* def HAVE_SEEKDIR */
43
44 #if HAVE_TELLDIR
45 mph_off_t
46 Mono_Posix_Syscall_telldir (void *dir)
47 {
48         return telldir ((DIR*) dir);
49 }
50 #endif  /* def HAVE_TELLDIR */
51
52 static void
53 copy_dirent (struct Mono_Posix_Syscall__Dirent *to, struct dirent *from)
54 {
55         memset (to, 0, sizeof(*to));
56
57         to->d_ino    = from->d_ino;
58         to->d_name   = strdup (from->d_name);
59
60 #ifdef HAVE_STRUCT_DIRENT_D_OFF
61         to->d_off    = from->d_off;
62 #endif
63 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
64         to->d_reclen = from->d_reclen;
65 #endif
66 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
67         to->d_type   = from->d_type;
68 #endif
69 }
70
71 gint32
72 Mono_Posix_Syscall_readdir (void *dirp, struct Mono_Posix_Syscall__Dirent *entry)
73 {
74         struct dirent *d;
75
76         if (entry == NULL) {
77                 errno = EFAULT;
78                 return -1;
79         }
80
81         errno = 0;
82         d = readdir (dirp);
83
84         if (d == NULL) {
85                 return -1;
86         }
87
88         copy_dirent (entry, d);
89
90         return 0;
91 }
92
93 gint32
94 Mono_Posix_Syscall_readdir_r (void *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
95 {
96         struct dirent *_entry = malloc (sizeof (struct dirent) + MPH_PATH_MAX + 1);
97         int r;
98
99         r = readdir_r (dirp, _entry, (struct dirent**) result);
100
101         if (r == 0 && *result != NULL) {
102                 copy_dirent (entry, _entry);
103         }
104
105         free (_entry);
106
107         return r;
108 }
109
110 int
111 Mono_Posix_Syscall_rewinddir (void* dir)
112 {
113         rewinddir (dir);
114         return 0;
115 }
116
117 G_END_DECLS
118
119 /*
120  * vim: noexpandtab
121  */