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