getmntopts.c revision 50476
1283625Sdim/*-
2283625Sdim * Copyright (c) 1994
3283625Sdim *	The Regents of the University of California.  All rights reserved.
4283625Sdim *
5283625Sdim * Redistribution and use in source and binary forms, with or without
6283625Sdim * modification, are permitted provided that the following conditions
7283625Sdim * are met:
8283625Sdim * 1. Redistributions of source code must retain the above copyright
9283625Sdim *    notice, this list of conditions and the following disclaimer.
10283625Sdim * 2. Redistributions in binary form must reproduce the above copyright
11283625Sdim *    notice, this list of conditions and the following disclaimer in the
12283625Sdim *    documentation and/or other materials provided with the distribution.
13283625Sdim * 3. All advertising materials mentioning features or use of this software
14283625Sdim *    must display the following acknowledgement:
15284236Sdim *	This product includes software developed by the University of
16284236Sdim *	California, Berkeley and its contributors.
17283625Sdim * 4. Neither the name of the University nor the names of its contributors
18296417Sdim *    may be used to endorse or promote products derived from this software
19283625Sdim *    without specific prior written permission.
20284236Sdim *
21296417Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22284236Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23284236Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24284236Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25283625Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26296417Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27296417Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28296417Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29296417Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30296417Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31296417Sdim * SUCH DAMAGE.
32296417Sdim */
33296417Sdim
34296417Sdim#ifndef lint
35296417Sdim#if 0
36296417Sdimstatic char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
37296417Sdim#else
38296417Sdimstatic const char rcsid[] =
39296417Sdim  "$FreeBSD: head/sbin/mount/getmntopts.c 50476 1999-08-28 00:22:10Z peter $";
40296417Sdim#endif
41296417Sdim#endif /* not lint */
42296417Sdim
43296417Sdim#include <sys/param.h>
44296417Sdim
45284236Sdim#include <err.h>
46284236Sdim#include <stdlib.h>
47284236Sdim#include <string.h>
48296417Sdim
49283625Sdim#include "mntopts.h"
50283625Sdim
51284236Sdimint getmnt_silent = 0;
52284236Sdim
53284236Sdimvoid
54284236Sdimgetmntopts(options, m0, flagp, altflagp)
55284236Sdim	const char *options;
56284236Sdim	const struct mntopt *m0;
57284236Sdim	int *flagp;
58296417Sdim	int *altflagp;
59296417Sdim{
60296417Sdim	const struct mntopt *m;
61296417Sdim	int negative, len;
62296417Sdim	char *opt, *optbuf, *p;
63296417Sdim	int *thisflagp;
64296417Sdim
65296417Sdim	/* Copy option string, since it is about to be torn asunder... */
66296417Sdim	if ((optbuf = strdup(options)) == NULL)
67296417Sdim		err(1, NULL);
68296417Sdim
69296417Sdim	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
70296417Sdim		/* Check for "no" prefix. */
71296417Sdim		if (opt[0] == 'n' && opt[1] == 'o') {
72296417Sdim			negative = 1;
73296417Sdim			opt += 2;
74296417Sdim		} else
75296417Sdim			negative = 0;
76296417Sdim
77296417Sdim		/*
78296417Sdim		 * for options with assignments in them (ie. quotas)
79296417Sdim		 * ignore the assignment as it's handled elsewhere
80296417Sdim		 */
81296417Sdim		p = strchr(opt, '=');
82296417Sdim		if (p)
83296417Sdim			 *++p = '\0';
84296417Sdim
85296417Sdim		/* Scan option table. */
86296417Sdim		for (m = m0; m->m_option != NULL; ++m) {
87296417Sdim			len = strlen(m->m_option);
88296417Sdim			if (strncasecmp(opt, m->m_option, len) == 0)
89296417Sdim				if (   m->m_option[len]	== '\0'
90296417Sdim				    || m->m_option[len]	== '='
91296417Sdim				   )
92284734Sdim				break;
93284734Sdim		}
94284734Sdim
95284734Sdim		/* Save flag, or fail if option is not recognized. */
96284734Sdim		if (m->m_option) {
97284734Sdim			thisflagp = m->m_altloc ? altflagp : flagp;
98284734Sdim			if (negative == m->m_inverse)
99296417Sdim				*thisflagp |= m->m_flag;
100296417Sdim			else
101284734Sdim				*thisflagp &= ~m->m_flag;
102309124Sdim		} else if (!getmnt_silent) {
103309124Sdim			errx(1, "-o %s: option not supported", opt);
104284734Sdim		}
105284734Sdim	}
106284734Sdim
107296417Sdim	free(optbuf);
108296417Sdim}
109296417Sdim