2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/libamu/mtab.c
42 #endif /* HAVE_CONFIG_H */
48 * Firewall /etc/mtab entries
51 mnt_free(mntent_t *mp)
53 XFREE(mp->mnt_fsname);
58 #ifdef HAVE_MNTENT_T_MNT_TIME
59 # ifdef HAVE_MNTENT_T_MNT_TIME_STRING
61 # endif /* HAVE_MNTENT_T_MNT_TIME_STRING */
62 #endif /* HAVE_MNTENT_T_MNT_TIME */
69 * Discard memory allocated for mount list
72 discard_mntlist(mntlist *mp)
86 * Throw away a mount list
89 free_mntlist(mntlist *mp)
92 #ifdef MOUNT_TABLE_ON_FILE
94 #endif /* MOUNT_TABLE_ON_FILE */
99 * Utility routine which returns a pointer to whatever follows an = in a
100 * string. Returns null if = is not found in the string.
106 char *eq = strchr(instr, '=');
114 * Utility routine which returns a pointer to whatever
115 * follows an = in a mount option. Returns null if option
116 * doesn't exist or doesn't have an '='. Won't fail for opt,foo=.
119 hasmnteq(mntent_t *mnt, char *opt)
121 if (mnt && opt) { /* disallow null input pointers */
122 if ( *opt ) { /* disallow the null string as an opt */
123 char *str = amu_hasmntopt(mnt, opt);
124 if ( str ) { /* option was there */
125 char *eq = str + strlen(opt); /* Look at char just after option */
126 if (*eq == '=') /* Is it '=' ? */
127 return ++eq; /* If so, return pointer to remaining str */
136 * Wrapper around hasmntvalerr(), which retains backwards compatibiliy with
137 * older use of hasmntval().
139 * XXX: eventually, all use of hasmntval() should be replaced with
143 hasmntval(mntent_t *mnt, char *opt)
147 err = hasmntvalerr(mnt, opt, &val);
148 if (err) /* if there was an error (hasmntvalerr returned 1) */
149 return 0; /* redundant: val==0 above, but leave here for clarity */
150 /* otherwise there was no error */
156 * Utility routine which determines the value of a numeric option in the
157 * mount options (such as port=%d), and fills in the value in the argument
158 * valp (argument won't be touched if no value is set, for example due to an
161 * Returns non-zero (1) on error; returns 0 on success.
163 * XXX: eventually, all use of hasmntval() should be replaced with
167 hasmntvalerr(mntent_t *mnt, char *opt, int *valp)
169 char *str = amu_hasmntopt(mnt, opt);
170 int err = 1; /* 1 means no good value was set (an error) */
174 /* exit if no option specificed */
179 eq = hasmnteq(mnt, opt);
181 if (!eq) { /* no argument to option ('=' sign was missing) */
182 plog(XLOG_MAP, "numeric option to \"%s\" missing", opt);
186 /* if got here, then we had an '=' after option name */
188 i = strtol(eq, &endptr, 0); /* hex and octal allowed ;-) */
190 (endptr != eq && (*endptr == ',' || *endptr == '\0'))) {
192 * endptr set means strtol saw a non-digit. If the non-digit is a
193 * comma, it's probably the start of the next option. If the comma is
194 * the first char though, complain about it (foo=,bar is made
195 * noticeable by this).
197 * Similar reasoning for '\0' instead of comma, it's the end of the
200 *valp = (int) i; /* set good value */
201 err = 0; /* no error */
203 /* whatever was after the '=' sign wasn't a number */
204 plog(XLOG_MAP, "invalid numeric option in \"%s\": \"%s\"", opt, str);
205 /* fall through to error/exit processing */
214 * Utility routine which returns the string value of
215 * an option in the mount options (such as proto=udp).
216 * Returns NULL if the option is not specified.
217 * Returns malloc'ed string (caller must free!)
220 hasmntstr(mntent_t *mnt, char *opt)
222 char *str = amu_hasmntopt(mnt, opt);
224 if (str) { /* The option was there */
226 char *eq = hasmnteq(mnt, opt);
228 if (eq) { /* and had an = after it */
230 char *endptr = strchr(eq, ',');
232 /* if saw no comma, return xstrdup'd string */
236 /* else we need to copy only the chars needed */
237 int len = endptr - eq;
238 char *buf = xmalloc(len + 1);
239 strncpy(buf, eq, len);