2007-10-19 Marek Habersack <mhabersack@novell.com>
[mono.git] / support / fstab.c
index f6ab2cc27d9d046bf2d3bbd20aafc432e9c7583e..e81821b18c6c11a48c8253c7646d3229aab9fcac 100644 (file)
 #include <stdlib.h>
 #include <stddef.h>
 
+#include "map.h"
 #include "mph.h"
 
-#ifdef HAVE_FSTAB_H
+#if defined (HAVE_CHECKLIST_H)
+#include <checklist.h>
+#elif defined (HAVE_FSTAB_H)
 #include <fstab.h>
 #endif /* def HAVE_FSTAB_H */
 
 
 G_BEGIN_DECLS
 
-struct Mono_Posix_Syscall__Fstab {
-       char  *fs_spec;     /* block device name */
-       char  *fs_file;     /* mount point */
-       char  *fs_vfstype;      /* filesystem type */
-       char  *fs_mntops;   /* mount options */
-       char  *fs_type;     /* rw/rq/ro/sw/xx option */
-       int    fs_freq;     /* dump frequency, in days */
-       int    fs_passno;   /* pass number on parallel dump */
+#ifdef HAVE_CHECKLIST_H
 
-       char  *_fs_buf_;
+typedef struct checklist mph_fstab;
+
+static const size_t
+fstab_offsets[] = {
+       offsetof (struct checklist, fs_spec),
+       offsetof (struct checklist, fs_dir),
+       offsetof (struct checklist, fs_type)
+};
+
+static const size_t
+mph_fstab_offsets[] = {
+       offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec),
+       offsetof (struct Mono_Posix_Syscall__Fstab, fs_file),
+       offsetof (struct Mono_Posix_Syscall__Fstab, fs_type)
 };
 
-#ifdef HAVE_FSTAB_H
+#elif defined (HAVE_FSTAB_H)
 
 typedef struct fstab mph_fstab;
 
@@ -59,6 +68,10 @@ mph_fstab_offsets[] = {
        offsetof (struct Mono_Posix_Syscall__Fstab, fs_type)
 };
 
+#endif /* def HAVE_FSTAB_H */
+
+#if defined (HAVE_CHECKLIST_H) || defined (HAVE_FSTAB_H)
+
 /*
  * Copy the native `fstab' structure to it's managed representation.
  *
@@ -66,9 +79,12 @@ mph_fstab_offsets[] = {
  * memory block (stored in _fs_buf_).
  */
 static int
-copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct fstab *from)
+copy_fstab (struct Mono_Posix_Syscall__Fstab *to, mph_fstab *from)
 {
        char *buf;
+
+       memset (to, 0, sizeof(*to));
+
        buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
                        from, fstab_offsets, sizeof(fstab_offsets)/sizeof(fstab_offsets[0]));
 
@@ -83,7 +99,7 @@ copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct fstab *from)
        return 0;
 }
 
-#endif /* def HAVE_FSTAB_H */
+#endif /* def HAVE_CHECKLIST_H || def HAVE_FSTAB_H */
 
 #ifdef HAVE_SYS_VFSTAB_H
 
@@ -120,6 +136,9 @@ static int
 copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct vfstab *from)
 {
        char *buf;
+
+       memset (to, 0, sizeof(*to));
+
        buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
                        from, vfstab_offsets, sizeof(vfstab_offsets)/sizeof(vfstab_offsets[0]));
 
@@ -144,6 +163,9 @@ etc_fstab;
 static int
 setfsent (void)
 {
+       /* protect from bad users calling setfsent(), setfsent(), ... endfsent() */
+       if (etc_fstab != NULL)
+               fclose (etc_fstab);
        etc_fstab = fopen ("/etc/vfstab", "r");
        if (etc_fstab != NULL)
                return 1;
@@ -210,12 +232,13 @@ getfsspec (const char *special_file)
 
 #endif /* def HAVE_SYS_VFSTAB_H */
 
-#if defined (HAVE_FSTAB_H) || defined (HAVE_SYS_VFSTAB_H)
+#if defined (HAVE_FSTAB_H) || defined (HAVE_CHECKPOINT_H) || defined (HAVE_SYS_VFSTAB_H)
 
-void
+int
 Mono_Posix_Syscall_endfsent (void)
 {
        endfsent ();
+       return 0;
 }
 
 gint32
@@ -289,7 +312,7 @@ Mono_Posix_Syscall_setfsent (void)
        return setfsent ();
 }
 
-#endif /* def HAVE_FSTAB_H || def HAVE_SYS_VFSTAB_H */
+#endif /* def HAVE_FSTAB_H || def HAVE_CHECKPOINT_H || def HAVE_SYS_VFSTAB_H */
 
 G_END_DECLS