Added
Link Here
|
1 |
/* |
2 |
* Copyright (c) 1980, 1989, 1993, 1994 |
3 |
* The Regents of the University of California. All rights reserved. |
4 |
* Copyright (c) 2001 |
5 |
* David Rufino <daverufino@btinternet.com> |
6 |
* Copyright (c) 2006 |
7 |
* Stanislav Sedov <ssedov@mbsd.msk.ru> |
8 |
* |
9 |
* Redistribution and use in source and binary forms, with or without |
10 |
* modification, are permitted provided that the following conditions |
11 |
* are met: |
12 |
* 1. Redistributions of source code must retain the above copyright |
13 |
* notice, this list of conditions and the following disclaimer. |
14 |
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
* notice, this list of conditions and the following disclaimer in the |
16 |
* documentation and/or other materials provided with the distribution. |
17 |
* 3. All advertising materials mentioning features or use of this software |
18 |
* must display the following acknowledgement: |
19 |
* This product includes software developed by the University of |
20 |
* California, Berkeley and its contributors. |
21 |
* 4. Neither the name of the University nor the names of its contributors |
22 |
* may be used to endorse or promote products derived from this software |
23 |
* without specific prior written permission. |
24 |
* |
25 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
26 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
27 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
28 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
29 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
30 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
31 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
32 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
33 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
34 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
35 |
* SUCH DAMAGE. |
36 |
*/ |
37 |
|
38 |
/* most of this was ripped from the mount(3) source */ |
39 |
|
40 |
//#include "config.h" |
41 |
#include "mntent.h" |
42 |
#include <stdlib.h> |
43 |
#include <string.h> |
44 |
#include <sys/param.h> |
45 |
#include <sys/ucred.h> |
46 |
#include <sys/mount.h> |
47 |
|
48 |
static int pos = -1; |
49 |
static int mntsize = -1; |
50 |
static struct mntent _mntent; |
51 |
|
52 |
struct { |
53 |
int m_flag; |
54 |
const char *m_option; |
55 |
} mntoptions[] = { |
56 |
{ MNT_ASYNC, "async" }, |
57 |
{ MNT_NOATIME, "noatime"}, |
58 |
{ MNT_NOEXEC, "noexec"}, |
59 |
{ MNT_NOSUID, "nosuid"}, |
60 |
{ MNT_NOSYMFOLLOW, "nosymfollow"}, |
61 |
{ MNT_SYNCHRONOUS, "sync"}, |
62 |
{ MNT_UNION, "union"}, |
63 |
{ MNT_NOCLUSTERR, "noclusterr"}, |
64 |
{ static_cast<int>(MNT_NOCLUSTERW), "noclusterw"}, |
65 |
{ MNT_SUIDDIR, "suiddir"}, |
66 |
#ifdef MNT_SNAPSHOT |
67 |
{ MNT_SNAPSHOT, "snapshot"}, |
68 |
#endif |
69 |
#ifdef MNT_MULTILABEL |
70 |
{ MNT_MULTILABEL, "multilabel"}, |
71 |
#endif |
72 |
#ifdef MNT_ACLS |
73 |
{ MNT_ACLS, "acls"}, |
74 |
#endif |
75 |
#ifdef MNT_NODEV |
76 |
{ MNT_NODEV, "nodev"}, |
77 |
#endif |
78 |
}; |
79 |
|
80 |
#define N_OPTS (sizeof(mntoptions) / sizeof(*mntoptions)) |
81 |
|
82 |
|
83 |
extern "C" { |
84 |
|
85 |
char * |
86 |
hasmntopt (const struct mntent *mnt, const char *option) |
87 |
{ |
88 |
char *opt, *optbuf; |
89 |
|
90 |
optbuf = strdup(mnt->mnt_opts); |
91 |
for (opt = optbuf; (opt = strtok(opt, " ")) != NULL; opt = NULL) { |
92 |
if (!strcasecmp(opt, option)) { |
93 |
opt = opt - optbuf + mnt->mnt_opts; |
94 |
free (optbuf); |
95 |
return (opt); |
96 |
} |
97 |
} |
98 |
free (optbuf); |
99 |
return (NULL); |
100 |
} |
101 |
|
102 |
static char * |
103 |
catopt (char *s0, const char *s1) |
104 |
{ |
105 |
size_t newlen; |
106 |
char *cp; |
107 |
|
108 |
if (s1 == NULL || *s1 == '\0') |
109 |
return s0; |
110 |
|
111 |
if (s0 != NULL) { |
112 |
newlen = strlen(s0) + strlen(s1) + 1 + 1; |
113 |
if ((cp = (char *)realloc(s0, newlen)) == NULL) |
114 |
return (NULL); |
115 |
|
116 |
(void)strcat(cp, " "); |
117 |
(void)strcat(cp, s1); |
118 |
} else |
119 |
cp = strdup(s1); |
120 |
|
121 |
return (cp); |
122 |
} |
123 |
|
124 |
|
125 |
static char * |
126 |
flags2opts (int flags) |
127 |
{ |
128 |
char *res = NULL; |
129 |
int i; |
130 |
|
131 |
res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw"); |
132 |
|
133 |
for (i = 0; i < N_OPTS; i++) |
134 |
if (flags & mntoptions[i].m_flag) |
135 |
res = catopt(res, mntoptions[i].m_option); |
136 |
return res; |
137 |
} |
138 |
|
139 |
static struct mntent * |
140 |
statfs_to_mntent (struct statfs *mntbuf) |
141 |
{ |
142 |
static char opts_buf[40], *tmp; |
143 |
|
144 |
_mntent.mnt_fsname = mntbuf->f_mntfromname; |
145 |
_mntent.mnt_dir = mntbuf->f_mntonname; |
146 |
_mntent.mnt_type = mntbuf->f_fstypename; |
147 |
tmp = flags2opts (mntbuf->f_flags); |
148 |
if (tmp) { |
149 |
opts_buf[sizeof(opts_buf) - 1] = '\0'; |
150 |
strncpy (opts_buf, tmp, sizeof(opts_buf)-1); |
151 |
free (tmp); |
152 |
} else { |
153 |
*opts_buf = '\0'; |
154 |
} |
155 |
_mntent.mnt_opts = opts_buf; |
156 |
_mntent.mnt_freq = _mntent.mnt_passno = 0; |
157 |
return (&_mntent); |
158 |
} |
159 |
|
160 |
struct mntent * |
161 |
getmntent (FILE *fp) |
162 |
{ |
163 |
static struct statfs *mntbuf; |
164 |
|
165 |
if (pos == -1 || mntsize == -1) |
166 |
mntsize = getmntinfo (&mntbuf, MNT_NOWAIT); |
167 |
|
168 |
++pos; |
169 |
if (pos == mntsize) { |
170 |
pos = mntsize = -1; |
171 |
return (NULL); |
172 |
} |
173 |
|
174 |
return (statfs_to_mntent (&mntbuf[pos])); |
175 |
} |
176 |
|
177 |
}; // extern "C" |