Merge pull request #5675 from mono/glib-debug-symbols
[mono.git] / support / dirent.c
index 48c0b0f0e00ed3ad637702eff3452a70146b96b9..bc1b3e238e16730ad5113258b05eb174ec214b6f 100644 (file)
@@ -4,84 +4,83 @@
  * 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 <limits.h>
+#include <unistd.h>
 
+#include "map.h"
 #include "mph.h"
 
-G_BEGIN_DECLS
+#if defined (PATH_MAX) && defined (NAME_MAX)
+       #define MPH_PATH_MAX MAX(PATH_MAX, NAME_MAX)
+#elif defined (PATH_MAX)
+       #define MPH_PATH_MAX PATH_MAX
+#elif defined (NAME_MAX)
+       #define MPH_PATH_MAX NAME_MAX
+#else /* !defined PATH_MAX && !defined NAME_MAX */
+       #define MPH_PATH_MAX 2048
+#endif
 
-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;
-};
+G_BEGIN_DECLS
 
+#if HAVE_SEEKDIR
 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;
+       return 0;
 }
+#endif  /* def HAVE_SEEKDIR */
 
+#if HAVE_TELLDIR
 mph_off_t
-Mono_Posix_Syscall_telldir (DIR *dir)
+Mono_Posix_Syscall_telldir (void *dir)
 {
-       return telldir (dir);
+       return telldir ((DIR*) dir);
 }
+#endif  /* def HAVE_TELLDIR */
 
 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;
-#ifdef MPH_ON_BSD
-       to->d_off    = 0;
-#else
+       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
+       errno = 0;
        d = readdir (dirp);
-#endif
+
        if (d == NULL) {
                return -1;
        }
@@ -92,28 +91,29 @@ 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
+       struct dirent *_entry = malloc (sizeof (struct dirent) + MPH_PATH_MAX + 1);
        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
+       r = readdir_r (dirp, _entry, (struct dirent**) result);
 
-       if (r == 0 && result != NULL) {
-               copy_dirent (entry, &_entry);
+       if (r == 0 && *result != NULL) {
+               copy_dirent (entry, _entry);
        }
 
+       free (_entry);
+
        return r;
 }
 
+int
+Mono_Posix_Syscall_rewinddir (void* dir)
+{
+       rewinddir (dir);
+       return 0;
+}
+
 G_END_DECLS
 
 /*