2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)getmntopts.c 8.3 (Berkeley) 3/29/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/mount.h>
54 int getmnt_silent = 0;
57 getmntopts(const char *options, const struct mntopt *m0, int *flagp,
60 const struct mntopt *m;
62 char *opt, *optbuf, *p;
65 /* Copy option string, since it is about to be torn asunder... */
66 if ((optbuf = strdup(options)) == NULL)
69 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
70 /* Check for "no" prefix. */
71 if (opt[0] == 'n' && opt[1] == 'o') {
78 * for options with assignments in them (ie. quotas)
79 * ignore the assignment as it's handled elsewhere
85 /* Scan option table. */
86 for (m = m0; m->m_option != NULL; ++m) {
87 len = strlen(m->m_option);
88 if (strncasecmp(opt, m->m_option, len) == 0)
89 if (opt[len] == '\0' || opt[len] == '=')
93 /* Save flag, or fail if option is not recognized. */
95 thisflagp = m->m_altloc ? altflagp : flagp;
96 if (negative == m->m_inverse)
97 *thisflagp |= m->m_flag;
99 *thisflagp &= ~m->m_flag;
100 } else if (!getmnt_silent) {
101 errx(1, "-o %s: option not supported", opt);
109 rmslashes(char *rrpin, char *rrpout)
114 for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
116 /* skip all double slashes */
117 while (*rrpin == '/' && *(rrpin + 1) == '/')
121 /* remove trailing slash if necessary */
122 if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
123 *(rrpout - 1) = '\0';
129 checkpath(const char *path, char *resolved)
133 if (realpath(path, resolved) == NULL || stat(resolved, &sb) != 0)
135 if (!S_ISDIR(sb.st_mode)) {
143 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
151 *iov = realloc(*iov, sizeof **iov * (i + 2));
156 (*iov)[i].iov_base = strdup(name);
157 (*iov)[i].iov_len = strlen(name) + 1;
159 (*iov)[i].iov_base = val;
160 if (len == (size_t)-1) {
162 len = strlen(val) + 1;
166 (*iov)[i].iov_len = (int)len;
171 * This function is needed for compatibility with parameters
172 * which used to use the mount_argf() command for the old mount() syscall.
175 build_iovec_argf(struct iovec **iov, int *iovlen, const char *name,
176 const char *fmt, ...)
179 char val[255] = { 0 };
182 vsnprintf(val, sizeof(val), fmt, ap);
184 build_iovec(iov, iovlen, name, strdup(val), (size_t)-1);
188 * Free the iovec and reset to NULL with zero length. Useful for calling
192 free_iovec(struct iovec **iov, int *iovlen)
196 for (i = 0; i < *iovlen; i++)
197 free((*iov)[i].iov_base);