getmntopts.c revision 114589
11558Srgrimes/*-
21558Srgrimes * Copyright (c) 1994
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
34114589Sobrien#if 0
351558Srgrimes#ifndef lint
3623678Speterstatic char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
37114589Sobrien#endif /* not lint */
3828671Ssteve#endif
39114589Sobrien#include <sys/cdefs.h>
40114589Sobrien__FBSDID("$FreeBSD: head/sbin/mount/getmntopts.c 114589 2003-05-03 18:41:59Z obrien $");
411558Srgrimes
421558Srgrimes#include <sys/param.h>
4352055Sphk#include <sys/stat.h>
441558Srgrimes
451558Srgrimes#include <err.h>
4652055Sphk#include <errno.h>
471558Srgrimes#include <stdlib.h>
481558Srgrimes#include <string.h>
4952055Sphk#include <sysexits.h>
501558Srgrimes
511558Srgrimes#include "mntopts.h"
521558Srgrimes
534065Swollmanint getmnt_silent = 0;
544065Swollman
551558Srgrimesvoid
564065Swollmangetmntopts(options, m0, flagp, altflagp)
571558Srgrimes	const char *options;
581558Srgrimes	const struct mntopt *m0;
591558Srgrimes	int *flagp;
604065Swollman	int *altflagp;
611558Srgrimes{
621558Srgrimes	const struct mntopt *m;
633202Sache	int negative, len;
6423678Speter	char *opt, *optbuf, *p;
654065Swollman	int *thisflagp;
661558Srgrimes
671558Srgrimes	/* Copy option string, since it is about to be torn asunder... */
681558Srgrimes	if ((optbuf = strdup(options)) == NULL)
691558Srgrimes		err(1, NULL);
701558Srgrimes
711558Srgrimes	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
721558Srgrimes		/* Check for "no" prefix. */
731558Srgrimes		if (opt[0] == 'n' && opt[1] == 'o') {
741558Srgrimes			negative = 1;
751558Srgrimes			opt += 2;
761558Srgrimes		} else
771558Srgrimes			negative = 0;
781558Srgrimes
7923678Speter		/*
8023678Speter		 * for options with assignments in them (ie. quotas)
8123678Speter		 * ignore the assignment as it's handled elsewhere
8223678Speter		 */
8323678Speter		p = strchr(opt, '=');
8423678Speter		if (p)
8525301Smsmith			 *++p = '\0';
8623678Speter
871558Srgrimes		/* Scan option table. */
883202Sache		for (m = m0; m->m_option != NULL; ++m) {
893202Sache			len = strlen(m->m_option);
903202Sache			if (strncasecmp(opt, m->m_option, len) == 0)
913202Sache				if (   m->m_option[len]	== '\0'
923202Sache				    || m->m_option[len]	== '='
933202Sache				   )
941558Srgrimes				break;
953202Sache		}
961558Srgrimes
9737425Scharnier		/* Save flag, or fail if option is not recognized. */
981558Srgrimes		if (m->m_option) {
994065Swollman			thisflagp = m->m_altloc ? altflagp : flagp;
1001558Srgrimes			if (negative == m->m_inverse)
1014065Swollman				*thisflagp |= m->m_flag;
1021558Srgrimes			else
1034065Swollman				*thisflagp &= ~m->m_flag;
10423678Speter		} else if (!getmnt_silent) {
1051558Srgrimes			errx(1, "-o %s: option not supported", opt);
1064065Swollman		}
1071558Srgrimes	}
1081558Srgrimes
1091558Srgrimes	free(optbuf);
1101558Srgrimes}
11152055Sphk
11252055Sphkvoid
11352055Sphkrmslashes(rrpin, rrpout)
11452055Sphk	char *rrpin;
11552055Sphk	char *rrpout;
11652055Sphk{
11752055Sphk	char *rrpoutstart;
11852055Sphk
11952055Sphk	*rrpout = *rrpin;
12052055Sphk	for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
12152055Sphk
12252055Sphk		/* skip all double slashes */
12352055Sphk		while (*rrpin == '/' && *(rrpin + 1) == '/')
12452055Sphk			 rrpin++;
12552055Sphk	}
12652055Sphk
12752055Sphk	/* remove trailing slash if necessary */
12852055Sphk	if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
12952055Sphk		*(rrpout - 1) = '\0';
13052055Sphk	else
13152055Sphk		*rrpout = '\0';
13252055Sphk}
13352055Sphk
13452055Sphkvoid
13552055Sphkcheckpath(path, resolved)
13652055Sphk	const char *path;
13752055Sphk	char *resolved;
13852055Sphk{
13952055Sphk	struct stat sb;
14052055Sphk
14152055Sphk	if (realpath(path, resolved) != NULL && stat(resolved, &sb) == 0) {
14252055Sphk		if (!S_ISDIR(sb.st_mode))
14352055Sphk			errx(EX_USAGE, "%s: not a directory", resolved);
14452055Sphk	} else
14552055Sphk		errx(EX_USAGE, "%s: %s", resolved, strerror(errno));
14652055Sphk}
147