getmntopts.c revision 123268
1204076Spjd/*-
2204076Spjd * Copyright (c) 1994
3219351Spjd *	The Regents of the University of California.  All rights reserved.
4204076Spjd *
5204076Spjd * Redistribution and use in source and binary forms, with or without
6204076Spjd * modification, are permitted provided that the following conditions
7204076Spjd * are met:
8204076Spjd * 1. Redistributions of source code must retain the above copyright
9204076Spjd *    notice, this list of conditions and the following disclaimer.
10204076Spjd * 2. Redistributions in binary form must reproduce the above copyright
11204076Spjd *    notice, this list of conditions and the following disclaimer in the
12204076Spjd *    documentation and/or other materials provided with the distribution.
13204076Spjd * 3. All advertising materials mentioning features or use of this software
14204076Spjd *    must display the following acknowledgement:
15204076Spjd *	This product includes software developed by the University of
16204076Spjd *	California, Berkeley and its contributors.
17204076Spjd * 4. Neither the name of the University nor the names of its contributors
18204076Spjd *    may be used to endorse or promote products derived from this software
19204076Spjd *    without specific prior written permission.
20204076Spjd *
21204076Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24204076Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31204076Spjd * SUCH DAMAGE.
32204076Spjd */
33204076Spjd
34204076Spjd#if 0
35204076Spjd#ifndef lint
36204076Spjdstatic char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
37204076Spjd#endif /* not lint */
38204076Spjd#endif
39204076Spjd#include <sys/cdefs.h>
40204076Spjd__FBSDID("$FreeBSD: head/sbin/mount/getmntopts.c 123268 2003-12-07 23:02:16Z trhodes $");
41204076Spjd
42204076Spjd#include <sys/param.h>
43204076Spjd#include <sys/stat.h>
44204076Spjd
45204076Spjd#include <err.h>
46204076Spjd#include <errno.h>
47204076Spjd#include <stdlib.h>
48211982Spjd#include <string.h>
49204076Spjd#include <sysexits.h>
50204076Spjd
51204076Spjd#include "mntopts.h"
52204076Spjd
53204076Spjdint getmnt_silent = 0;
54204076Spjd
55204076Spjdvoid
56204076Spjdgetmntopts(options, m0, flagp, altflagp)
57204076Spjd	const char *options;
58204076Spjd	const struct mntopt *m0;
59204076Spjd	int *flagp;
60212038Spjd	int *altflagp;
61204076Spjd{
62204076Spjd	const struct mntopt *m;
63204076Spjd	int negative, len;
64211886Spjd	char *opt, *optbuf, *p;
65204076Spjd	int *thisflagp;
66204076Spjd
67204076Spjd	/* Copy option string, since it is about to be torn asunder... */
68204076Spjd	if ((optbuf = strdup(options)) == NULL)
69204076Spjd		err(1, NULL);
70204076Spjd
71210886Spjd	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
72210886Spjd		/* Check for "no" prefix. */
73210886Spjd		if (opt[0] == 'n' && opt[1] == 'o') {
74204076Spjd			negative = 1;
75204076Spjd			opt += 2;
76204076Spjd		} else
77204076Spjd			negative = 0;
78204076Spjd
79204076Spjd		/*
80204076Spjd		 * for options with assignments in them (ie. quotas)
81204076Spjd		 * ignore the assignment as it's handled elsewhere
82204076Spjd		 */
83204076Spjd		p = strchr(opt, '=');
84204076Spjd		if (p != NULL)
85204076Spjd			 *++p = '\0';
86204076Spjd
87204076Spjd		/* Scan option table. */
88204076Spjd		for (m = m0; m->m_option != NULL; ++m) {
89204076Spjd			len = strlen(m->m_option);
90204076Spjd			if (strncasecmp(opt, m->m_option, len) == 0)
91204076Spjd				if (   m->m_option[len]	== '\0'
92204076Spjd				    || m->m_option[len]	== '='
93204076Spjd				   )
94204076Spjd				break;
95204076Spjd		}
96204076Spjd
97204076Spjd		/* Save flag, or fail if option is not recognized. */
98204076Spjd		if (m->m_option) {
99204076Spjd			thisflagp = m->m_altloc ? altflagp : flagp;
100204076Spjd			if (negative == m->m_inverse)
101204076Spjd				*thisflagp |= m->m_flag;
102204076Spjd			else
103204076Spjd				*thisflagp &= ~m->m_flag;
104204076Spjd		} else if (!getmnt_silent) {
105204076Spjd			errx(1, "-o %s: option not supported", opt);
106204076Spjd		}
107204076Spjd	}
108204076Spjd
109204076Spjd	free(optbuf);
110204076Spjd}
111204076Spjd
112204076Spjdvoid
113204076Spjdrmslashes(rrpin, rrpout)
114204076Spjd	char *rrpin;
115204076Spjd	char *rrpout;
116204076Spjd{
117204076Spjd	char *rrpoutstart;
118204076Spjd
119204076Spjd	*rrpout = *rrpin;
120204076Spjd	for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
121204076Spjd
122204076Spjd		/* skip all double slashes */
123204076Spjd		while (*rrpin == '/' && *(rrpin + 1) == '/')
124204076Spjd			 rrpin++;
125204076Spjd	}
126204076Spjd
127204076Spjd	/* remove trailing slash if necessary */
128204076Spjd	if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
129204076Spjd		*(rrpout - 1) = '\0';
130204076Spjd	else
131204076Spjd		*rrpout = '\0';
132204076Spjd}
133204076Spjd
134204076Spjdvoid
135204076Spjdcheckpath(path, resolved)
136204076Spjd	const char *path;
137204076Spjd	char *resolved;
138204076Spjd{
139204076Spjd	struct stat sb;
140204076Spjd
141204076Spjd	if (realpath(path, resolved) != NULL && stat(resolved, &sb) == 0) {
142204076Spjd		if (!S_ISDIR(sb.st_mode))
143204076Spjd			errx(EX_USAGE, "%s: not a directory", resolved);
144204076Spjd	} else
145204076Spjd		errx(EX_USAGE, "%s: %s", resolved, strerror(errno));
146204076Spjd}
147204076Spjd