getmntopts.c revision 114589
1272343Sngie/*-
2272343Sngie * Copyright (c) 1994
3272343Sngie *	The Regents of the University of California.  All rights reserved.
4272343Sngie *
5272343Sngie * Redistribution and use in source and binary forms, with or without
6272343Sngie * modification, are permitted provided that the following conditions
7272343Sngie * are met:
8272343Sngie * 1. Redistributions of source code must retain the above copyright
9272343Sngie *    notice, this list of conditions and the following disclaimer.
10272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer in the
12272343Sngie *    documentation and/or other materials provided with the distribution.
13272343Sngie * 3. All advertising materials mentioning features or use of this software
14272343Sngie *    must display the following acknowledgement:
15272343Sngie *	This product includes software developed by the University of
16272343Sngie *	California, Berkeley and its contributors.
17272343Sngie * 4. Neither the name of the University nor the names of its contributors
18272343Sngie *    may be used to endorse or promote products derived from this software
19272343Sngie *    without specific prior written permission.
20272343Sngie *
21272343Sngie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31272343Sngie * SUCH DAMAGE.
32272343Sngie */
33272343Sngie
34272343Sngie#if 0
35272343Sngie#ifndef lint
36272343Sngiestatic char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
37272343Sngie#endif /* not lint */
38272343Sngie#endif
39272343Sngie#include <sys/cdefs.h>
40272343Sngie__FBSDID("$FreeBSD: head/sbin/mount/getmntopts.c 114589 2003-05-03 18:41:59Z obrien $");
41272343Sngie
42272343Sngie#include <sys/param.h>
43272343Sngie#include <sys/stat.h>
44272343Sngie
45272343Sngie#include <err.h>
46272343Sngie#include <errno.h>
47272343Sngie#include <stdlib.h>
48272343Sngie#include <string.h>
49272343Sngie#include <sysexits.h>
50272343Sngie
51272343Sngie#include "mntopts.h"
52272343Sngie
53272343Sngieint getmnt_silent = 0;
54272343Sngie
55272343Sngievoid
56272343Sngiegetmntopts(options, m0, flagp, altflagp)
57272343Sngie	const char *options;
58272343Sngie	const struct mntopt *m0;
59272343Sngie	int *flagp;
60272343Sngie	int *altflagp;
61272343Sngie{
62272343Sngie	const struct mntopt *m;
63272343Sngie	int negative, len;
64272343Sngie	char *opt, *optbuf, *p;
65272343Sngie	int *thisflagp;
66272343Sngie
67272343Sngie	/* Copy option string, since it is about to be torn asunder... */
68272343Sngie	if ((optbuf = strdup(options)) == NULL)
69272343Sngie		err(1, NULL);
70272343Sngie
71272343Sngie	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
72272343Sngie		/* Check for "no" prefix. */
73272343Sngie		if (opt[0] == 'n' && opt[1] == 'o') {
74272343Sngie			negative = 1;
75272343Sngie			opt += 2;
76272343Sngie		} else
77272343Sngie			negative = 0;
78272343Sngie
79272343Sngie		/*
80272343Sngie		 * for options with assignments in them (ie. quotas)
81272343Sngie		 * ignore the assignment as it's handled elsewhere
82272343Sngie		 */
83272343Sngie		p = strchr(opt, '=');
84272343Sngie		if (p)
85272343Sngie			 *++p = '\0';
86272343Sngie
87272343Sngie		/* Scan option table. */
88272343Sngie		for (m = m0; m->m_option != NULL; ++m) {
89272343Sngie			len = strlen(m->m_option);
90272343Sngie			if (strncasecmp(opt, m->m_option, len) == 0)
91272343Sngie				if (   m->m_option[len]	== '\0'
92272343Sngie				    || m->m_option[len]	== '='
93272343Sngie				   )
94272343Sngie				break;
95272343Sngie		}
96272343Sngie
97272343Sngie		/* Save flag, or fail if option is not recognized. */
98272343Sngie		if (m->m_option) {
99272343Sngie			thisflagp = m->m_altloc ? altflagp : flagp;
100272343Sngie			if (negative == m->m_inverse)
101272343Sngie				*thisflagp |= m->m_flag;
102272343Sngie			else
103272343Sngie				*thisflagp &= ~m->m_flag;
104272343Sngie		} else if (!getmnt_silent) {
105272343Sngie			errx(1, "-o %s: option not supported", opt);
106272343Sngie		}
107272343Sngie	}
108272343Sngie
109272343Sngie	free(optbuf);
110272343Sngie}
111272343Sngie
112272343Sngievoid
113272343Sngiermslashes(rrpin, rrpout)
114272343Sngie	char *rrpin;
115272343Sngie	char *rrpout;
116272343Sngie{
117272343Sngie	char *rrpoutstart;
118272343Sngie
119272343Sngie	*rrpout = *rrpin;
120272343Sngie	for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
121272343Sngie
122272343Sngie		/* skip all double slashes */
123272343Sngie		while (*rrpin == '/' && *(rrpin + 1) == '/')
124272343Sngie			 rrpin++;
125272343Sngie	}
126272343Sngie
127272343Sngie	/* remove trailing slash if necessary */
128272343Sngie	if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
129272343Sngie		*(rrpout - 1) = '\0';
130272343Sngie	else
131272343Sngie		*rrpout = '\0';
132272343Sngie}
133272343Sngie
134272343Sngievoid
135272343Sngiecheckpath(path, resolved)
136272343Sngie	const char *path;
137272343Sngie	char *resolved;
138272343Sngie{
139272343Sngie	struct stat sb;
140272343Sngie
141272343Sngie	if (realpath(path, resolved) != NULL && stat(resolved, &sb) == 0) {
142272343Sngie		if (!S_ISDIR(sb.st_mode))
143272343Sngie			errx(EX_USAGE, "%s: not a directory", resolved);
144272343Sngie	} else
145272343Sngie		errx(EX_USAGE, "%s: %s", resolved, strerror(errno));
146272343Sngie}
147272343Sngie