* marshal.c: Iter usage.
[mono-project.git] / support / fstab.c
blobc9ff50547a84f6dae119375892199b9fc249110e
1 /*
2 * <fstab.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2005 Jonathan Pryor
8 */
10 #include <errno.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
16 #include "mph.h"
18 #if defined (HAVE_CHECKLIST_H)
19 #include <checklist.h>
20 #elif defined (HAVE_FSTAB_H)
21 #include <fstab.h>
22 #endif /* def HAVE_FSTAB_H */
24 #ifdef HAVE_SYS_VFSTAB_H
25 #include <sys/vfstab.h>
26 #endif /* def HAVE_SYS_VFSTAB_H */
28 G_BEGIN_DECLS
30 struct Mono_Posix_Syscall__Fstab {
31 char *fs_spec; /* block device name */
32 char *fs_file; /* mount point */
33 char *fs_vfstype; /* filesystem type */
34 char *fs_mntops; /* mount options */
35 char *fs_type; /* rw/rq/ro/sw/xx option */
36 int fs_freq; /* dump frequency, in days */
37 int fs_passno; /* pass number on parallel dump */
39 char *_fs_buf_;
42 #ifdef HAVE_CHECKLIST_H
44 typedef struct checklist mph_fstab;
46 static const size_t
47 fstab_offsets[] = {
48 offsetof (struct checklist, fs_spec),
49 offsetof (struct checklist, fs_dir),
50 offsetof (struct checklist, fs_type)
53 static const size_t
54 mph_fstab_offsets[] = {
55 offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec),
56 offsetof (struct Mono_Posix_Syscall__Fstab, fs_file),
57 offsetof (struct Mono_Posix_Syscall__Fstab, fs_type)
60 #elif defined (HAVE_FSTAB_H)
62 typedef struct fstab mph_fstab;
64 static const size_t
65 fstab_offsets[] = {
66 offsetof (struct fstab, fs_spec),
67 offsetof (struct fstab, fs_file),
68 offsetof (struct fstab, fs_vfstype),
69 offsetof (struct fstab, fs_mntops),
70 offsetof (struct fstab, fs_type)
73 static const size_t
74 mph_fstab_offsets[] = {
75 offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec),
76 offsetof (struct Mono_Posix_Syscall__Fstab, fs_file),
77 offsetof (struct Mono_Posix_Syscall__Fstab, fs_vfstype),
78 offsetof (struct Mono_Posix_Syscall__Fstab, fs_mntops),
79 offsetof (struct Mono_Posix_Syscall__Fstab, fs_type)
82 #endif /* def HAVE_FSTAB_H */
84 #if defined (HAVE_CHECKLIST_H) || defined (HAVE_FSTAB_H)
87 * Copy the native `fstab' structure to it's managed representation.
89 * To minimize separate mallocs, all the strings are allocated within the same
90 * memory block (stored in _fs_buf_).
92 static int
93 copy_fstab (struct Mono_Posix_Syscall__Fstab *to, mph_fstab *from)
95 char *buf;
97 memset (to, 0, sizeof(*to));
99 buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
100 from, fstab_offsets, sizeof(fstab_offsets)/sizeof(fstab_offsets[0]));
102 to->fs_freq = from->fs_freq;
103 to->fs_passno = from->fs_passno;
105 to->_fs_buf_ = buf;
106 if (buf == NULL) {
107 return -1;
110 return 0;
113 #endif /* def HAVE_CHECKLIST_H || def HAVE_FSTAB_H */
115 #ifdef HAVE_SYS_VFSTAB_H
118 * Solaris doesn't provide <fstab.h> but has equivalent functionality in
119 * <sys/fstab.h> via getvfsent(3C) and company.
122 typedef struct vfstab mph_fstab;
124 static const size_t
125 vfstab_offsets[] = {
126 offsetof (struct vfstab, vfs_special),
127 offsetof (struct vfstab, vfs_mountp),
128 offsetof (struct vfstab, vfs_fstype),
129 offsetof (struct vfstab, vfs_mntopts)
132 static const size_t
133 mph_fstab_offsets[] = {
134 offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec),
135 offsetof (struct Mono_Posix_Syscall__Fstab, fs_file),
136 offsetof (struct Mono_Posix_Syscall__Fstab, fs_vfstype),
137 offsetof (struct Mono_Posix_Syscall__Fstab, fs_mntops)
141 * Copy the native `vfstab' structure to it's managed representation.
143 * To minimize separate mallocs, all the strings are allocated within the same
144 * memory block (stored in _fs_buf_).
146 static int
147 copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct vfstab *from)
149 char *buf;
151 memset (to, 0, sizeof(*to));
153 buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
154 from, vfstab_offsets, sizeof(vfstab_offsets)/sizeof(vfstab_offsets[0]));
156 to->fs_type = NULL;
157 to->fs_freq = -1;
158 to->fs_passno = -1;
160 to->_fs_buf_ = buf;
161 if (buf == NULL) {
162 return -1;
165 return 0;
169 * Implement Linux/BSD getfsent(3) in terms of Solaris getvfsent(3C)...
171 static FILE*
172 etc_fstab;
174 static int
175 setfsent (void)
177 etc_fstab = fopen ("/etc/vfstab", "r");
178 if (etc_fstab != NULL)
179 return 1;
180 return 0;
183 static void
184 endfsent (void)
186 fclose (etc_fstab);
187 etc_fstab = NULL;
190 static struct vfstab
191 cur_vfstab_entry;
193 static struct vfstab*
194 getfsent (void)
196 int r;
197 r = getvfsent (etc_fstab, &cur_vfstab_entry);
198 if (r == 0)
199 return &cur_vfstab_entry;
200 return NULL;
203 static struct vfstab*
204 getfsfile (const char *mount_point)
206 int r;
207 int close = 0;
208 if (etc_fstab == 0) {
209 close = 1;
210 if (setfsent () != 1)
211 return NULL;
213 rewind (etc_fstab);
214 r = getvfsfile (etc_fstab, &cur_vfstab_entry, (char*) mount_point);
215 if (close)
216 endfsent ();
217 if (r == 0)
218 return &cur_vfstab_entry;
219 return NULL;
222 static struct vfstab*
223 getfsspec (const char *special_file)
225 int r;
226 int close = 0;
227 if (etc_fstab == 0) {
228 close = 1;
229 if (setfsent () != 1)
230 return NULL;
232 rewind (etc_fstab);
233 r = getvfsspec (etc_fstab, &cur_vfstab_entry, (char*) special_file);
234 if (close)
235 endfsent ();
236 if (r == 0)
237 return &cur_vfstab_entry;
238 return NULL;
241 #endif /* def HAVE_SYS_VFSTAB_H */
243 #if defined (HAVE_FSTAB_H) || defined (HAVE_CHECKPOINT_H) || defined (HAVE_SYS_VFSTAB_H)
245 void
246 Mono_Posix_Syscall_endfsent (void)
248 endfsent ();
251 gint32
252 Mono_Posix_Syscall_getfsent (struct Mono_Posix_Syscall__Fstab *fsbuf)
254 mph_fstab *fs;
256 if (fsbuf == NULL) {
257 errno = EFAULT;
258 return -1;
261 fs = getfsent ();
262 if (fs == NULL)
263 return -1;
265 if (copy_fstab (fsbuf, fs) == -1) {
266 errno = ENOMEM;
267 return -1;
269 return 0;
272 gint32
273 Mono_Posix_Syscall_getfsfile (const char *mount_point,
274 struct Mono_Posix_Syscall__Fstab *fsbuf)
276 mph_fstab *fs;
278 if (fsbuf == NULL) {
279 errno = EFAULT;
280 return -1;
283 fs = getfsfile (mount_point);
284 if (fs == NULL)
285 return -1;
287 if (copy_fstab (fsbuf, fs) == -1) {
288 errno = ENOMEM;
289 return -1;
291 return 0;
294 gint32
295 Mono_Posix_Syscall_getfsspec (const char *special_file,
296 struct Mono_Posix_Syscall__Fstab *fsbuf)
298 mph_fstab *fs;
300 if (fsbuf == NULL) {
301 errno = EFAULT;
302 return -1;
305 fs = getfsspec (special_file);
306 if (fs == NULL)
307 return -1;
309 if (copy_fstab (fsbuf, fs) == -1) {
310 errno = ENOMEM;
311 return -1;
313 return 0;
316 gint32
317 Mono_Posix_Syscall_setfsent (void)
319 return setfsent ();
322 #endif /* def HAVE_FSTAB_H || def HAVE_CHECKPOINT_H || def HAVE_SYS_VFSTAB_H */
324 G_END_DECLS
327 * vim: noexpandtab