* fstab.c: Added; wrap <fstab.h> functions: getfsent(3), getfsfile(3),
[mono.git] / support / fstab.c
1 /*
2  * <fstab.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004 Jonathan Pryor
8  */
9
10 #include <fstab.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include "mph.h"
17
18 G_BEGIN_DECLS
19
20 struct Mono_Posix_Syscall__Fstab {
21         char  *fs_spec;     /* block device name */
22         char  *fs_file;     /* mount point */
23         char  *fs_vfstype;      /* filesystem type */
24         char  *fs_mntops;   /* mount options */
25         char  *fs_type;     /* rw/rq/ro/sw/xx option */
26         int    fs_freq;     /* dump frequency, in days */
27         int    fs_passno;   /* pass number on parallel dump */
28
29         char  *_fs_buf_;
30 };
31
32 static const size_t
33 fstab_offsets[] = {
34         offsetof (struct fstab, fs_spec),
35         offsetof (struct fstab, fs_file),
36         offsetof (struct fstab, fs_vfstype),
37         offsetof (struct fstab, fs_mntops),
38         offsetof (struct fstab, fs_type)
39 };
40
41 static const size_t
42 mph_fstab_offsets[] = {
43         offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec),
44         offsetof (struct Mono_Posix_Syscall__Fstab, fs_file),
45         offsetof (struct Mono_Posix_Syscall__Fstab, fs_vfstype),
46         offsetof (struct Mono_Posix_Syscall__Fstab, fs_mntops),
47         offsetof (struct Mono_Posix_Syscall__Fstab, fs_type)
48 };
49
50 /*
51  * Copy the native `passwd' structure to it's managed representation.
52  *
53  * To minimize separate mallocs, all the strings are allocated within the same
54  * memory block (stored in _fs_buf_).
55  */
56 static int
57 copy_fstab (struct Mono_Posix_Syscall__Fstab *to, struct fstab *from)
58 {
59         char *buf;
60         buf = _mph_copy_structure_strings (to, mph_fstab_offsets,
61                         from, fstab_offsets, sizeof(fstab_offsets)/sizeof(fstab_offsets[0]));
62
63         to->fs_freq   = from->fs_freq;
64         to->fs_passno = from->fs_passno;
65
66         to->_fs_buf_ = buf;
67         if (buf == NULL) {
68                 return -1;
69         }
70
71         return 0;
72 }
73
74 gint32
75 Mono_Posix_Syscall_getfsent (struct Mono_Posix_Syscall__Fstab *fsbuf)
76 {
77         struct fstab *fs;
78
79         if (fsbuf == NULL) {
80                 errno = EFAULT;
81                 return -1;
82         }
83
84         fs = getfsent ();
85         if (fs == NULL)
86                 return -1;
87
88         if (copy_fstab (fsbuf, fs) == -1) {
89                 errno = ENOMEM;
90                 return -1;
91         }
92         return 0;
93 }
94
95 gint32
96 Mono_Posix_Syscall_getfsfile (const char *mount_point, 
97                 struct Mono_Posix_Syscall__Fstab *fsbuf)
98 {
99         struct fstab *fs;
100
101         if (fsbuf == NULL) {
102                 errno = EFAULT;
103                 return -1;
104         }
105
106         fs = getfsfile (mount_point);
107         if (fs == NULL)
108                 return -1;
109
110         if (copy_fstab (fsbuf, fs) == -1) {
111                 errno = ENOMEM;
112                 return -1;
113         }
114         return 0;
115 }
116
117 gint32
118 Mono_Posix_Syscall_getfsspec (const char *special_file, 
119                 struct Mono_Posix_Syscall__Fstab *fsbuf)
120 {
121         struct fstab *fs;
122
123         if (fsbuf == NULL) {
124                 errno = EFAULT;
125                 return -1;
126         }
127
128         fs = getfsspec (special_file);
129         if (fs == NULL)
130                 return -1;
131
132         if (copy_fstab (fsbuf, fs) == -1) {
133                 errno = ENOMEM;
134                 return -1;
135         }
136         return 0;
137 }
138
139 G_END_DECLS
140
141 /*
142  * vim: noexpandtab
143  */