1 |
|
|
/* $OpenBSD: getmntopts.c,v 1.12 2015/01/16 06:39:59 deraadt 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/types.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 |
|
|
#include <limits.h> |
42 |
|
|
|
43 |
|
|
#include "mntopts.h" |
44 |
|
|
|
45 |
|
|
int |
46 |
|
|
getmntopts(const char *optionp, const struct mntopt *m0, int *flagp) |
47 |
|
|
{ |
48 |
|
|
char *p, *q; |
49 |
|
|
union mntval val; |
50 |
|
|
int ret = 0; |
51 |
|
|
|
52 |
|
|
p = q = strdup(optionp); |
53 |
|
|
if (p == NULL) |
54 |
|
|
err(1, NULL); |
55 |
|
|
while (p != NULL) { |
56 |
|
|
ret |= getmntopt(&p, &val, m0, flagp); |
57 |
|
|
} |
58 |
|
|
free(q); |
59 |
|
|
return (ret); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
int |
63 |
|
|
getmntopt(char **optionp, union mntval *valuep, const struct mntopt *m0, |
64 |
|
|
int *flagp) |
65 |
|
|
{ |
66 |
|
|
const struct mntopt *m; |
67 |
|
|
char *opt, *value, *endp; |
68 |
|
|
long l; |
69 |
|
|
int inverse, negative, needval, ret = 0; |
70 |
|
|
|
71 |
|
|
/* Pull out the next option. */ |
72 |
|
|
do { |
73 |
|
|
if (*optionp == NULL) |
74 |
|
|
return (0); |
75 |
|
|
opt = strsep(optionp, ","); |
76 |
|
|
} while (opt == NULL || *opt == '\0'); |
77 |
|
|
|
78 |
|
|
/* Check for "no" prefix. */ |
79 |
|
|
if (opt[0] == 'n' && opt[1] == 'o') { |
80 |
|
|
negative = 1; |
81 |
|
|
opt += 2; |
82 |
|
|
} else |
83 |
|
|
negative = 0; |
84 |
|
|
|
85 |
|
|
/* Stash the value for options with assignments in them. */ |
86 |
|
|
if ((value = strchr(opt, '=')) != NULL) |
87 |
|
|
*value++ = '\0'; |
88 |
|
|
|
89 |
|
|
/* Scan option table. */ |
90 |
|
|
for (m = m0; m->m_option != NULL; ++m) |
91 |
|
|
if (strcasecmp(opt, m->m_option) == 0) |
92 |
|
|
break; |
93 |
|
|
|
94 |
|
|
/* Save flag, or fail if option is not recognised. */ |
95 |
|
|
if (m->m_option) { |
96 |
|
|
needval = (m->m_oflags & (MFLAG_INTVAL|MFLAG_STRVAL)) != 0; |
97 |
|
|
if (needval != (value != NULL) && !(m->m_oflags & MFLAG_OPT)) |
98 |
|
|
errx(1, "-o %s: option %s a value", opt, |
99 |
|
|
needval ? "needs" : "does not need"); |
100 |
|
|
inverse = (m->m_oflags & MFLAG_INVERSE) ? 1 : 0; |
101 |
|
|
if (m->m_oflags & MFLAG_SET) { |
102 |
|
|
if (negative == inverse) |
103 |
|
|
*flagp |= m->m_flag; |
104 |
|
|
else |
105 |
|
|
*flagp &= ~m->m_flag; |
106 |
|
|
} |
107 |
|
|
else if (negative == inverse) |
108 |
|
|
ret = m->m_flag; |
109 |
|
|
} else |
110 |
|
|
errx(1, "-o %s: option not supported", opt); |
111 |
|
|
|
112 |
|
|
/* Store the value for options with assignments in them. */ |
113 |
|
|
if (value != NULL) { |
114 |
|
|
if (m->m_oflags & MFLAG_INTVAL) { |
115 |
|
|
errno = 0; |
116 |
|
|
l = strtol(value, &endp, 10); |
117 |
|
|
if (endp == value || l < 0 || l > INT_MAX || |
118 |
|
|
(l == LONG_MAX && errno == ERANGE)) |
119 |
|
|
errx(1, "%s: illegal value '%s'", |
120 |
|
|
opt, value); |
121 |
|
|
valuep->ival = (int)l; |
122 |
|
|
} else |
123 |
|
|
valuep->strval = value; |
124 |
|
|
} else |
125 |
|
|
memset(valuep, 0, sizeof(*valuep)); |
126 |
|
|
return (ret); |
127 |
|
|
} |