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