xref: /minix3/lib/libutil/getmntopts.c (revision 0c3983b25a88161cf074524e5c94585a2582ae82)
1*0c3983b2SBen Gras /*	$NetBSD: getmntopts.c,v 1.4 2007/08/26 22:46:15 pooka Exp $	*/
2*0c3983b2SBen Gras 
3*0c3983b2SBen Gras /*-
4*0c3983b2SBen Gras  * Copyright (c) 1994
5*0c3983b2SBen Gras  *	The Regents of the University of California.  All rights reserved.
6*0c3983b2SBen Gras  *
7*0c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
8*0c3983b2SBen Gras  * modification, are permitted provided that the following conditions
9*0c3983b2SBen Gras  * are met:
10*0c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
11*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
12*0c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*0c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*0c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
15*0c3983b2SBen Gras  * 3. Neither the name of the University nor the names of its contributors
16*0c3983b2SBen Gras  *    may be used to endorse or promote products derived from this software
17*0c3983b2SBen Gras  *    without specific prior written permission.
18*0c3983b2SBen Gras  *
19*0c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*0c3983b2SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*0c3983b2SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*0c3983b2SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*0c3983b2SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*0c3983b2SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*0c3983b2SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*0c3983b2SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*0c3983b2SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*0c3983b2SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0c3983b2SBen Gras  * SUCH DAMAGE.
30*0c3983b2SBen Gras  */
31*0c3983b2SBen Gras 
32*0c3983b2SBen Gras #include <sys/cdefs.h>
33*0c3983b2SBen Gras #ifndef lint
34*0c3983b2SBen Gras #if 0
35*0c3983b2SBen Gras static char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
36*0c3983b2SBen Gras #else
37*0c3983b2SBen Gras __RCSID("$NetBSD: getmntopts.c,v 1.4 2007/08/26 22:46:15 pooka Exp $");
38*0c3983b2SBen Gras #endif
39*0c3983b2SBen Gras #endif /* not lint */
40*0c3983b2SBen Gras 
41*0c3983b2SBen Gras #include <sys/param.h>
42*0c3983b2SBen Gras #include <sys/mount.h>
43*0c3983b2SBen Gras 
44*0c3983b2SBen Gras #include <err.h>
45*0c3983b2SBen Gras #include <errno.h>
46*0c3983b2SBen Gras #include <fstab.h>
47*0c3983b2SBen Gras #include <stdlib.h>
48*0c3983b2SBen Gras #include <string.h>
49*0c3983b2SBen Gras 
50*0c3983b2SBen Gras #include <mntopts.h>
51*0c3983b2SBen Gras 
52*0c3983b2SBen Gras int getmnt_silent = 0;
53*0c3983b2SBen Gras 
54*0c3983b2SBen Gras static const char errmsg[] = "-o %s: option not supported";
55*0c3983b2SBen Gras 
56*0c3983b2SBen Gras struct mntoptparse {
57*0c3983b2SBen Gras 	const char *options;
58*0c3983b2SBen Gras 	const struct mntopt *mopts;
59*0c3983b2SBen Gras 	char *optbuf;
60*0c3983b2SBen Gras 	char **optarg;
61*0c3983b2SBen Gras };
62*0c3983b2SBen Gras 
63*0c3983b2SBen Gras const char *
getmntoptstr(mntoptparse_t mp,const char * opt)64*0c3983b2SBen Gras getmntoptstr(mntoptparse_t mp, const char *opt)
65*0c3983b2SBen Gras {
66*0c3983b2SBen Gras 	const struct mntopt *m;
67*0c3983b2SBen Gras 
68*0c3983b2SBen Gras 	for (m = mp->mopts; m->m_option != NULL; m++)
69*0c3983b2SBen Gras 		if (strcasecmp(opt, m->m_option) == 0)
70*0c3983b2SBen Gras 			break;
71*0c3983b2SBen Gras 
72*0c3983b2SBen Gras 	if (m->m_option == NULL) {
73*0c3983b2SBen Gras 		if (getmnt_silent == 0)
74*0c3983b2SBen Gras 			errx(1, errmsg, opt);
75*0c3983b2SBen Gras 		else
76*0c3983b2SBen Gras 			return NULL;
77*0c3983b2SBen Gras 	}
78*0c3983b2SBen Gras 
79*0c3983b2SBen Gras 	return mp->optarg[m - mp->mopts];
80*0c3983b2SBen Gras }
81*0c3983b2SBen Gras 
82*0c3983b2SBen Gras long
getmntoptnum(mntoptparse_t mp,const char * opt)83*0c3983b2SBen Gras getmntoptnum(mntoptparse_t mp, const char *opt)
84*0c3983b2SBen Gras {
85*0c3983b2SBen Gras 	char *ep;
86*0c3983b2SBen Gras 	long rv;
87*0c3983b2SBen Gras 	void (*fun)(int, const char *, ...) = NULL;
88*0c3983b2SBen Gras 	const char *val = getmntoptstr(mp, opt);
89*0c3983b2SBen Gras 
90*0c3983b2SBen Gras 	if (val == NULL) {
91*0c3983b2SBen Gras 		if (getmnt_silent == 0)
92*0c3983b2SBen Gras 			errx(1, "Missing %s argument", opt);
93*0c3983b2SBen Gras 		else
94*0c3983b2SBen Gras 			return -1;
95*0c3983b2SBen Gras 	}
96*0c3983b2SBen Gras 
97*0c3983b2SBen Gras 	errno = 0;
98*0c3983b2SBen Gras 	rv = strtol(val, &ep, 0);
99*0c3983b2SBen Gras 
100*0c3983b2SBen Gras 	if (*ep)
101*0c3983b2SBen Gras 		fun = errx;
102*0c3983b2SBen Gras 
103*0c3983b2SBen Gras 	if (errno == ERANGE && (rv == LONG_MAX || rv == LONG_MIN))
104*0c3983b2SBen Gras 		fun = err;
105*0c3983b2SBen Gras 
106*0c3983b2SBen Gras 	if (fun) {
107*0c3983b2SBen Gras 		if (getmnt_silent != 0)
108*0c3983b2SBen Gras 			return -1;
109*0c3983b2SBen Gras 		(*fun)(1, "Invalid %s argument `%s'", opt, val);
110*0c3983b2SBen Gras 	}
111*0c3983b2SBen Gras 	return rv;
112*0c3983b2SBen Gras }
113*0c3983b2SBen Gras 
114*0c3983b2SBen Gras void
freemntopts(mntoptparse_t mp)115*0c3983b2SBen Gras freemntopts(mntoptparse_t mp)
116*0c3983b2SBen Gras {
117*0c3983b2SBen Gras 	free(mp->optbuf);
118*0c3983b2SBen Gras 	free(mp->optarg);
119*0c3983b2SBen Gras 	free(mp);
120*0c3983b2SBen Gras }
121*0c3983b2SBen Gras 
122*0c3983b2SBen Gras mntoptparse_t
getmntopts(const char * options,const struct mntopt * m0,int * flagp,int * altflagp)123*0c3983b2SBen Gras getmntopts(const char *options, const struct mntopt *m0, int *flagp,
124*0c3983b2SBen Gras     int *altflagp)
125*0c3983b2SBen Gras {
126*0c3983b2SBen Gras 	const struct mntopt *m;
127*0c3983b2SBen Gras 	int negative;
128*0c3983b2SBen Gras 	char *opt, *p;
129*0c3983b2SBen Gras 	int *thisflagp;
130*0c3983b2SBen Gras 	size_t nopts;
131*0c3983b2SBen Gras 	mntoptparse_t mp;
132*0c3983b2SBen Gras 
133*0c3983b2SBen Gras 	for (nopts = 0, m = m0; m->m_option != NULL; ++m, nopts++)
134*0c3983b2SBen Gras 		continue;
135*0c3983b2SBen Gras 
136*0c3983b2SBen Gras 	if ((mp = malloc(sizeof(struct mntoptparse))) == NULL)
137*0c3983b2SBen Gras 		return NULL;
138*0c3983b2SBen Gras 
139*0c3983b2SBen Gras 	/* Copy option string, since it is about to be torn asunder... */
140*0c3983b2SBen Gras 	if ((mp->optbuf = strdup(options)) == NULL) {
141*0c3983b2SBen Gras 		free(mp);
142*0c3983b2SBen Gras 		return NULL;
143*0c3983b2SBen Gras 	}
144*0c3983b2SBen Gras 
145*0c3983b2SBen Gras 	if ((mp->optarg = calloc(nopts, sizeof(char *))) == NULL) {
146*0c3983b2SBen Gras 		free(mp->optbuf);
147*0c3983b2SBen Gras 		free(mp);
148*0c3983b2SBen Gras 		return NULL;
149*0c3983b2SBen Gras 	}
150*0c3983b2SBen Gras 
151*0c3983b2SBen Gras 	mp->mopts = m0;
152*0c3983b2SBen Gras 	mp->options = options;
153*0c3983b2SBen Gras 
154*0c3983b2SBen Gras 	for (opt = mp->optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
155*0c3983b2SBen Gras 		/* Check for "no" prefix. */
156*0c3983b2SBen Gras 		if (opt[0] == 'n' && opt[1] == 'o') {
157*0c3983b2SBen Gras 			negative = 1;
158*0c3983b2SBen Gras 			opt += 2;
159*0c3983b2SBen Gras 		} else
160*0c3983b2SBen Gras 			negative = 0;
161*0c3983b2SBen Gras 
162*0c3983b2SBen Gras 		/*
163*0c3983b2SBen Gras 		 * for options with assignments in them (ie. quotas)
164*0c3983b2SBen Gras 		 * ignore the assignment as it's handled elsewhere
165*0c3983b2SBen Gras 		 */
166*0c3983b2SBen Gras 		p = strchr(opt, '=');
167*0c3983b2SBen Gras 		if (p) {
168*0c3983b2SBen Gras 			 *p++ = '\0';
169*0c3983b2SBen Gras 		}
170*0c3983b2SBen Gras 
171*0c3983b2SBen Gras 		/* Scan option table. */
172*0c3983b2SBen Gras 		for (m = m0; m->m_option != NULL; ++m)
173*0c3983b2SBen Gras 			if (strcasecmp(opt, m->m_option) == 0)
174*0c3983b2SBen Gras 				break;
175*0c3983b2SBen Gras 
176*0c3983b2SBen Gras 		/* Save flag, or fail if option is not recognised. */
177*0c3983b2SBen Gras 		if (m->m_option) {
178*0c3983b2SBen Gras 			mp->optarg[m - m0] = p;
179*0c3983b2SBen Gras 			thisflagp = m->m_altloc ? altflagp : flagp;
180*0c3983b2SBen Gras 			if (negative == m->m_inverse)
181*0c3983b2SBen Gras 				*thisflagp |= m->m_flag;
182*0c3983b2SBen Gras 			else
183*0c3983b2SBen Gras 				*thisflagp &= ~m->m_flag;
184*0c3983b2SBen Gras 		} else if (!getmnt_silent) {
185*0c3983b2SBen Gras 			errx(1, errmsg, opt);
186*0c3983b2SBen Gras 		} else {
187*0c3983b2SBen Gras 			free(mp->optbuf);
188*0c3983b2SBen Gras 			free(mp);
189*0c3983b2SBen Gras 
190*0c3983b2SBen Gras 			return NULL;
191*0c3983b2SBen Gras 		}
192*0c3983b2SBen Gras 	}
193*0c3983b2SBen Gras 	return mp;
194*0c3983b2SBen Gras }
195