1 /* $NetBSD: getmntopts.c,v 1.3 2003/08/07 16:44:58 agc Exp $ */
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)getmntopts.c 8.3 (Berkeley) 3/29/95";
37 __RCSID("$NetBSD: getmntopts.c,v 1.3 2003/08/07 16:44:58 agc Exp $");
41 #include <sys/param.h>
42 #include <sys/mount.h>
52 int getmnt_silent
= 0;
54 static const char errmsg
[] = "-o %s: option not supported";
58 const struct mntopt
*mopts
;
64 getmntoptstr(mntoptparse_t mp
, const char *opt
)
66 const struct mntopt
*m
;
68 for (m
= mp
->mopts
; m
->m_option
!= NULL
; m
++)
69 if (strcasecmp(opt
, m
->m_option
) == 0)
72 if (m
->m_option
== NULL
) {
73 if (getmnt_silent
== 0)
79 return mp
->optarg
[m
- mp
->mopts
];
83 getmntoptnum(mntoptparse_t mp
, const char *opt
)
87 void (*fun
)(int, const char *, ...) = NULL
;
88 const char *val
= getmntoptstr(mp
, opt
);
91 if (getmnt_silent
== 0)
92 errx(1, "Missing %s argument", opt
);
98 rv
= strtol(val
, &ep
, 0);
103 if (errno
== ERANGE
&& (rv
== LONG_MAX
|| rv
== LONG_MIN
))
107 if (getmnt_silent
!= 0)
109 (*fun
)(1, "Invalid %s argument `%s'", opt
, val
);
115 freemntopts(mntoptparse_t mp
)
123 getmntopts(const char *options
, const struct mntopt
*m0
, int *flagp
,
126 const struct mntopt
*m
;
133 for (nopts
= 0, m
= m0
; m
->m_option
!= NULL
; ++m
, nopts
++)
136 if ((mp
= malloc(sizeof(struct mntoptparse
))) == NULL
)
139 /* Copy option string, since it is about to be torn asunder... */
140 if ((mp
->optbuf
= strdup(options
)) == NULL
) {
145 if ((mp
->optarg
= calloc(nopts
, sizeof(char *))) == NULL
) {
152 mp
->options
= options
;
154 for (opt
= mp
->optbuf
; (opt
= strtok(opt
, ",")) != NULL
; opt
= NULL
) {
155 /* Check for "no" prefix. */
156 if (opt
[0] == 'n' && opt
[1] == 'o') {
163 * for options with assignments in them (ie. quotas)
164 * ignore the assignment as it's handled elsewhere
166 p
= strchr(opt
, '=');
171 /* Scan option table. */
172 for (m
= m0
; m
->m_option
!= NULL
; ++m
)
173 if (strcasecmp(opt
, m
->m_option
) == 0)
176 /* Save flag, or fail if option is not recognised. */
178 mp
->optarg
[m
- m0
] = p
;
179 thisflagp
= m
->m_altloc
? altflagp
: flagp
;
180 if (negative
== m
->m_inverse
)
181 *thisflagp
|= m
->m_flag
;
183 *thisflagp
&= ~m
->m_flag
;
184 } else if (!getmnt_silent
) {
185 errx(1, errmsg
, opt
);