Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libutil / getmntopts.c
blob069e99b60e8ba8e09f48f3780622e3363a4f128b
1 /* $NetBSD: getmntopts.c,v 1.3 2003/08/07 16:44:58 agc Exp $ */
3 /*-
4 * Copyright (c) 1994
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
9 * are met:
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
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)getmntopts.c 8.3 (Berkeley) 3/29/95";
36 #else
37 __RCSID("$NetBSD: getmntopts.c,v 1.3 2003/08/07 16:44:58 agc Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/param.h>
42 #include <sys/mount.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fstab.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #include <mntopts.h>
52 int getmnt_silent = 0;
54 static const char errmsg[] = "-o %s: option not supported";
56 struct mntoptparse {
57 const char *options;
58 const struct mntopt *mopts;
59 char *optbuf;
60 char **optarg;
63 const char *
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)
70 break;
72 if (m->m_option == NULL) {
73 if (getmnt_silent == 0)
74 errx(1, errmsg, opt);
75 else
76 return NULL;
79 return mp->optarg[m - mp->mopts];
82 long
83 getmntoptnum(mntoptparse_t mp, const char *opt)
85 char *ep;
86 long rv;
87 void (*fun)(int, const char *, ...) = NULL;
88 const char *val = getmntoptstr(mp, opt);
90 if (val == NULL) {
91 if (getmnt_silent == 0)
92 errx(1, "Missing %s argument", opt);
93 else
94 return -1;
97 errno = 0;
98 rv = strtol(val, &ep, 0);
100 if (*ep)
101 fun = errx;
103 if (errno == ERANGE && (rv == LONG_MAX || rv == LONG_MIN))
104 fun = err;
106 if (fun) {
107 if (getmnt_silent != 0)
108 return -1;
109 (*fun)(1, "Invalid %s argument `%s'", opt, val);
111 return rv;
114 void
115 freemntopts(mntoptparse_t mp)
117 free(mp->optbuf);
118 free(mp->optarg);
119 free(mp);
122 mntoptparse_t
123 getmntopts(const char *options, const struct mntopt *m0, int *flagp,
124 int *altflagp)
126 const struct mntopt *m;
127 int negative;
128 char *opt, *p;
129 int *thisflagp;
130 size_t nopts;
131 mntoptparse_t mp;
133 for (nopts = 0, m = m0; m->m_option != NULL; ++m, nopts++)
134 continue;
136 if ((mp = malloc(sizeof(struct mntoptparse))) == NULL)
137 return NULL;
139 /* Copy option string, since it is about to be torn asunder... */
140 if ((mp->optbuf = strdup(options)) == NULL) {
141 free(mp);
142 return NULL;
145 if ((mp->optarg = calloc(nopts, sizeof(char *))) == NULL) {
146 free(mp->optbuf);
147 free(mp);
148 return NULL;
151 mp->mopts = m0;
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') {
157 negative = 1;
158 opt += 2;
159 } else
160 negative = 0;
163 * for options with assignments in them (ie. quotas)
164 * ignore the assignment as it's handled elsewhere
166 p = strchr(opt, '=');
167 if (p) {
168 *p++ = '\0';
171 /* Scan option table. */
172 for (m = m0; m->m_option != NULL; ++m)
173 if (strcasecmp(opt, m->m_option) == 0)
174 break;
176 /* Save flag, or fail if option is not recognised. */
177 if (m->m_option) {
178 mp->optarg[m - m0] = p;
179 thisflagp = m->m_altloc ? altflagp : flagp;
180 if (negative == m->m_inverse)
181 *thisflagp |= m->m_flag;
182 else
183 *thisflagp &= ~m->m_flag;
184 } else if (!getmnt_silent) {
185 errx(1, errmsg, opt);
186 } else {
187 free(mp->optbuf);
188 free(mp);
190 return NULL;
193 return mp;