1 /* $OpenBSD: getmntopts.c,v 1.9 2004/07/05 18:33:38 otto Exp $ */
2 /* $NetBSD: getmntopts.c,v 1.3 1995/03/18 14:56:58 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/mount.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <fstab.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "mntopts.h"
43
44 __SCCSID("@(#)getmntopts.c 8.1 (Berkeley) 3/27/94");
45 __RCSID("$MirOS: src/sbin/mount/getmntopts.c,v 1.3 2005/10/18 18:51:44 tg Exp $");
46
47 int
getmntopts(const char * optionp,const struct mntopt * m0,int * flagp)48 getmntopts(const char *optionp, const struct mntopt *m0, int *flagp)
49 {
50 char *p, *q;
51 union mntval val;
52 int ret = 0;
53
54 p = q = strdup(optionp);
55 if (p == NULL)
56 err(1, NULL);
57 while (p != NULL) {
58 ret |= getmntopt(&p, &val, m0, flagp);
59 }
60 free(q);
61 return (ret);
62 }
63
64 int
getmntopt(char ** optionp,union mntval * valuep,const struct mntopt * m0,int * flagp)65 getmntopt(char **optionp, union mntval *valuep, const struct mntopt *m0,
66 int *flagp)
67 {
68 const struct mntopt *m;
69 char *opt, *value, *endp;
70 long l;
71 int inverse, negative, needval, ret = 0;
72
73 /* Pull out the next option. */
74 do {
75 if (*optionp == NULL)
76 return (0);
77 opt = strsep(optionp, ",");
78 } while (opt == NULL || *opt == '\0');
79
80 /* Check for "no" prefix. */
81 if (opt[0] == 'n' && opt[1] == 'o') {
82 negative = 1;
83 opt += 2;
84 } else
85 negative = 0;
86
87 /* Stash the value for options with assignments in them. */
88 if ((value = strchr(opt, '=')) != NULL)
89 *value++ = '\0';
90
91 /* Scan option table. */
92 for (m = m0; m->m_option != NULL; ++m)
93 if (strcasecmp(opt, m->m_option) == 0)
94 break;
95
96 /* Save flag, or fail if option is not recognised. */
97 if (m->m_option) {
98 needval = (m->m_oflags & (MFLAG_INTVAL|MFLAG_STRVAL)) != 0;
99 if (needval != (value != NULL) && !(m->m_oflags & MFLAG_OPT))
100 errx(1, "-o %s: option %s a value", opt,
101 needval ? "needs" : "does not need");
102 inverse = (m->m_oflags & MFLAG_INVERSE) ? 1 : 0;
103 if (m->m_oflags & MFLAG_SET) {
104 if (negative == inverse)
105 *flagp |= m->m_flag;
106 else
107 *flagp &= ~m->m_flag;
108 }
109 else if (negative == inverse)
110 ret = m->m_flag;
111 } else
112 errx(1, "-o %s: option not supported", opt);
113
114 /* Store the value for options with assignments in them. */
115 if (value != NULL) {
116 if (m->m_oflags & MFLAG_INTVAL) {
117 l = strtol(value, &endp, 10);
118 if (endp == value || l < 0 || l > INT_MAX ||
119 (l == LONG_MAX && errno == ERANGE))
120 errx(1, "%s: illegal value '%s'",
121 opt, value);
122 valuep->ival = (int)l;
123 } else
124 valuep->strval = value;
125 } else
126 memset(valuep, 0, sizeof(*valuep));
127 return (ret);
128 }
129