2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/libamu/mtab.c
46 #endif /* HAVE_CONFIG_H */
52 * Firewall /etc/mtab entries
55 mnt_free(mntent_t *mp)
57 XFREE(mp->mnt_fsname);
62 #ifdef HAVE_MNTENT_T_MNT_TIME
63 # ifdef HAVE_MNTENT_T_MNT_TIME_STRING
65 # endif /* HAVE_MNTENT_T_MNT_TIME_STRING */
66 #endif /* HAVE_MNTENT_T_MNT_TIME */
73 * Discard memory allocated for mount list
76 discard_mntlist(mntlist *mp)
90 * Throw away a mount list
93 free_mntlist(mntlist *mp)
96 #ifdef MOUNT_TABLE_ON_FILE
98 #endif /* MOUNT_TABLE_ON_FILE */
103 * Utility routine which returns a pointer to whatever follows an = in a
104 * string. Returns null if = is not found in the string.
110 char *eq = strchr(instr, '=');
118 * Utility routine which returns a pointer to whatever
119 * follows an = in a mount option. Returns null if option
120 * doesn't exist or doesn't have an '='. Won't fail for opt,foo=.
123 hasmnteq(mntent_t *mnt, char *opt)
125 if (mnt && opt) { /* disallow null input pointers */
126 if ( *opt ) { /* disallow the null string as an opt */
127 char *str = amu_hasmntopt(mnt, opt);
128 if ( str ) { /* option was there */
129 char *eq = str + strlen(opt); /* Look at char just after option */
130 if (*eq == '=') /* Is it '=' ? */
131 return ++eq; /* If so, return pointer to remaining str */
140 * Wrapper around hasmntvalerr(), which retains backwards compatibiliy with
141 * older use of hasmntval().
143 * XXX: eventually, all use of hasmntval() should be replaced with
147 hasmntval(mntent_t *mnt, char *opt)
151 err = hasmntvalerr(mnt, opt, &val);
152 if (err) /* if there was an error (hasmntvalerr returned 1) */
153 return 0; /* redundant: val==0 above, but leave here for clarity */
154 /* otherwise there was no error */
160 * Utility routine which determines the value of a numeric option in the
161 * mount options (such as port=%d), and fills in the value in the argument
162 * valp (argument won't be touched if no value is set, for example due to an
165 * Returns non-zero (1) on error; returns 0 on success.
167 * XXX: eventually, all use of hasmntval() should be replaced with
171 hasmntvalerr(mntent_t *mnt, char *opt, int *valp)
173 char *str = amu_hasmntopt(mnt, opt);
174 int err = 1; /* 1 means no good value was set (an error) */
178 /* exit if no option specificed */
183 eq = hasmnteq(mnt, opt);
185 if (!eq) { /* no argument to option ('=' sign was missing) */
186 plog(XLOG_MAP, "numeric option to \"%s\" missing", opt);
190 /* if got here, then we had an '=' after option name */
192 i = strtol(eq, &endptr, 0); /* hex and octal allowed ;-) */
194 (endptr != eq && (*endptr == ',' || *endptr == '\0'))) {
196 * endptr set means strtol saw a non-digit. If the non-digit is a
197 * comma, it's probably the start of the next option. If the comma is
198 * the first char though, complain about it (foo=,bar is made
199 * noticeable by this).
201 * Similar reasoning for '\0' instead of comma, it's the end of the
204 *valp = (int) i; /* set good value */
205 err = 0; /* no error */
207 /* whatever was after the '=' sign wasn't a number */
208 plog(XLOG_MAP, "invalid numeric option in \"%s\": \"%s\"", opt, str);
209 /* fall through to error/exit processing */
218 * Utility routine which returns the string value of
219 * an option in the mount options (such as proto=udp).
220 * Returns NULL if the option is not specified.
221 * Returns malloc'ed string (caller must free!)
224 hasmntstr(mntent_t *mnt, char *opt)
226 char *str = amu_hasmntopt(mnt, opt);
228 if (str) { /* The option was there */
230 char *eq = hasmnteq(mnt, opt);
232 if (eq) { /* and had an = after it */
234 char *endptr = strchr(eq, ',');
236 /* if saw no comma, return strdup'd string */
240 /* else we need to copy only the chars needed */
241 int len = endptr - eq;
242 char *buf = xmalloc(len + 1);
243 strncpy(buf, eq, len);