]>
git.saurik.com Git - apple/libutil.git/blob - getmntopts.c
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>
51 int getmnt_silent
= 0;
53 static const char errmsg
[] = "-o %s: option not supported";
57 const struct mntopt
*mopts
;
63 getmntoptstr(mntoptparse_t mp
, const char *opt
)
65 const struct mntopt
*m
;
67 for (m
= mp
->mopts
; m
->m_option
!= NULL
; m
++)
68 if (strcasecmp(opt
, m
->m_option
) == 0)
71 if (m
->m_option
== NULL
) {
72 if (getmnt_silent
== 0)
78 return mp
->optarg
[m
- mp
->mopts
];
82 getmntoptnum(mntoptparse_t mp
, const char *opt
)
86 void (*fun
)(int, const char *, ...) = NULL
;
87 const char *val
= getmntoptstr(mp
, opt
);
90 if (getmnt_silent
== 0)
91 errx(1, "Missing %s argument", opt
);
97 rv
= strtol(val
, &ep
, 0);
102 if (errno
== ERANGE
&& (rv
== LONG_MAX
|| rv
== LONG_MIN
))
106 if (getmnt_silent
!= 0)
108 (*fun
)(1, "Invalid %s argument `%s'", opt
, val
);
114 freemntopts(mntoptparse_t mp
)
122 getmntopts(const char *options
, const struct mntopt
*m0
, int *flagp
,
125 const struct mntopt
*m
;
127 char *opt
, *p
, *ctx
= NULL
;
132 for (nopts
= 0, m
= m0
; m
->m_option
!= NULL
; ++m
, nopts
++)
135 if ((mp
= malloc(sizeof(struct mntoptparse
))) == NULL
)
138 /* Copy option string, since it is about to be torn asunder... */
139 if ((mp
->optbuf
= strdup(options
)) == NULL
) {
144 if ((mp
->optarg
= calloc(nopts
, sizeof(char *))) == NULL
) {
151 mp
->options
= options
;
153 for (opt
= mp
->optbuf
; (opt
= strtok_r(opt
, ",", &ctx
)) != NULL
; opt
= NULL
) {
154 /* Check for "no" prefix. */
155 if (opt
[0] == 'n' && opt
[1] == 'o') {
162 * for options with assignments in them (ie. quotas)
163 * ignore the assignment as it's handled elsewhere
165 p
= strchr(opt
, '=');
170 /* Scan option table. */
171 for (m
= m0
; m
->m_option
!= NULL
; ++m
)
172 if (strcasecmp(opt
, m
->m_option
) == 0)
175 /* Save flag, or fail if option is not recognised. */
177 mp
->optarg
[m
- m0
] = p
;
178 thisflagp
= m
->m_altloc
? altflagp
: flagp
;
179 if (negative
== m
->m_inverse
)
180 *thisflagp
|= m
->m_flag
;
182 *thisflagp
&= ~m
->m_flag
;
183 } else if (!getmnt_silent
) {
184 errx(1, errmsg
, opt
);