2010-04-06 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / support / dirent.c
blob4673ac51439cfeab01f3553fc5ebdc4691990d9d
1 /*
2 * <dirent.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2005 Jonathan Pryor
8 */
10 #include <dirent.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <limits.h>
15 #include <unistd.h>
17 #include "map.h"
18 #include "mph.h"
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
30 G_BEGIN_DECLS
32 gint32
33 Mono_Posix_Syscall_seekdir (void *dir, mph_off_t offset)
35 mph_return_if_off_t_overflow (offset);
37 seekdir ((DIR*) dir, (off_t) offset);
39 return 0;
42 mph_off_t
43 Mono_Posix_Syscall_telldir (void *dir)
45 return telldir ((DIR*) dir);
48 static void
49 copy_dirent (struct Mono_Posix_Syscall__Dirent *to, struct dirent *from)
51 memset (to, 0, sizeof(*to));
53 to->d_ino = from->d_ino;
54 to->d_name = strdup (from->d_name);
56 #ifdef HAVE_STRUCT_DIRENT_D_OFF
57 to->d_off = from->d_off;
58 #endif
59 #ifdef HAVE_STRUCT_DIRENT_D_RECLEN
60 to->d_reclen = from->d_reclen;
61 #endif
62 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
63 to->d_type = from->d_type;
64 #endif
67 gint32
68 Mono_Posix_Syscall_readdir (void *dirp, struct Mono_Posix_Syscall__Dirent *entry)
70 struct dirent *d;
72 if (entry == NULL) {
73 errno = EFAULT;
74 return -1;
77 d = readdir (dirp);
79 if (d == NULL) {
80 return -1;
83 copy_dirent (entry, d);
85 return 0;
88 gint32
89 Mono_Posix_Syscall_readdir_r (void *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
91 struct dirent *_entry = malloc (sizeof (struct dirent) + MPH_PATH_MAX + 1);
92 int r;
94 r = readdir_r (dirp, _entry, (struct dirent**) result);
96 if (r == 0 && *result != NULL) {
97 copy_dirent (entry, _entry);
100 free (_entry);
102 return r;
106 Mono_Posix_Syscall_rewinddir (void* dir)
108 rewinddir (dir);
109 return 0;
112 G_END_DECLS
115 * vim: noexpandtab