2007-05-30 Chris Toshok <toshok@ximian.com>
[mono.git] / support / dirent.c
index 999a34126a60dcbef63eafe4925507607797b3ee..ae13bf2ea529bd4a9dfbe053221d5c2712b5a75b 100644 (file)
@@ -4,80 +4,69 @@
  * Authors:
  *   Jonathan Pryor (jonpryor@vt.edu)
  *
- * Copyright (C) 2004 Jonathan Pryor
+ * Copyright (C) 2004-2005 Jonathan Pryor
  */
 
 #include <dirent.h>
 #include <errno.h>
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
 
+#include "map.h"
 #include "mph.h"
 
 G_BEGIN_DECLS
 
-struct Mono_Posix_Syscall__Dirent {
-       /* ino_t  */ mph_ino_t      d_ino;
-       /* off_t  */ mph_off_t      d_off;
-       /* ushort */ unsigned short d_reclen;
-       /* byte   */ unsigned char  d_type;
-       /* string */ char          *d_name;
-};
-
 gint32
-Mono_Posix_Syscall_seekdir (DIR *dir, mph_off_t offset)
+Mono_Posix_Syscall_seekdir (void *dir, mph_off_t offset)
 {
        mph_return_if_off_t_overflow (offset);
 
        errno = 0;
 
-       seekdir (dir, (off_t) offset);
+       seekdir ((DIR*) dir, (off_t) offset);
 
        return errno != 0;
 }
 
 mph_off_t
-Mono_Posix_Syscall_telldir (DIR *dir)
+Mono_Posix_Syscall_telldir (void *dir)
 {
-       return telldir (dir);
+       return telldir ((DIR*) dir);
 }
 
 static void
-copy_dirent (
-       struct Mono_Posix_Syscall__Dirent *to, 
-#ifdef MPH_USE_64_API
-       struct dirent64 *from
-#else
-       struct dirent *from
-#endif
-       )
+copy_dirent (struct Mono_Posix_Syscall__Dirent *to, struct dirent *from)
 {
+       memset (to, 0, sizeof(*to));
+
        to->d_ino    = from->d_ino;
+       to->d_name   = strdup (from->d_name);
+
+#ifdef HAVE_STRUCT_DIRENT_D_OFF
        to->d_off    = from->d_off;
+#endif
+#ifdef HAVE_STRUCT_DIRENT_D_RECLEN
        to->d_reclen = from->d_reclen;
+#endif
+#ifdef HAVE_STRUCT_DIRENT_D_TYPE
        to->d_type   = from->d_type;
-       to->d_name   = strdup (from->d_name);
+#endif
 }
 
 gint32
-Mono_Posix_Syscall_readdir (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry)
+Mono_Posix_Syscall_readdir (void *dirp, struct Mono_Posix_Syscall__Dirent *entry)
 {
-#ifdef MPH_USE_64_API
-       struct dirent64 *d;
-#else
        struct dirent *d;
-#endif
 
        if (entry == NULL) {
                errno = EFAULT;
                return -1;
        }
 
-#ifdef MPH_USE_64_API
-       d = readdir64 (dirp);
-#else
        d = readdir (dirp);
-#endif
+
        if (d == NULL) {
                return -1;
        }
@@ -88,20 +77,12 @@ Mono_Posix_Syscall_readdir (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry)
 }
 
 gint32
-Mono_Posix_Syscall_readdir_r (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
+Mono_Posix_Syscall_readdir_r (void *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
 {
-#ifdef MPH_USE_64_API
-       struct dirent64 _entry;
-#else
        struct dirent _entry;
-#endif
        int r;
 
-#ifdef MPH_USE_64_API
-       r = readdir64_r (dirp, &_entry, (struct dirent64**) result);
-#else
        r = readdir_r (dirp, &_entry, (struct dirent**) result);
-#endif
 
        if (r == 0 && result != NULL) {
                copy_dirent (entry, &_entry);
@@ -110,6 +91,14 @@ Mono_Posix_Syscall_readdir_r (DIR *dirp, struct Mono_Posix_Syscall__Dirent *entr
        return r;
 }
 
+int
+Mono_Posix_Syscall_rewinddir (void* dir)
+{
+       errno = 0;
+       rewinddir (dir);
+       return errno == 0 ? 0 : -1;
+}
+
 G_END_DECLS
 
 /*