getmntopts.c revision 138095
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 * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
3223678Speterstatic char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
33114589Sobrien#endif /* not lint */
3428671Ssteve#endif
35114589Sobrien#include <sys/cdefs.h>
36114589Sobrien__FBSDID("$FreeBSD: head/sbin/mount/getmntopts.c 138095 2004-11-25 13:31:46Z phk $");
371558Srgrimes
381558Srgrimes#include <sys/param.h>
3952055Sphk#include <sys/stat.h>
40138095Sphk#include <sys/uio.h>
411558Srgrimes
421558Srgrimes#include <err.h>
4352055Sphk#include <errno.h>
441558Srgrimes#include <stdlib.h>
451558Srgrimes#include <string.h>
4652055Sphk#include <sysexits.h>
471558Srgrimes
481558Srgrimes#include "mntopts.h"
491558Srgrimes
504065Swollmanint getmnt_silent = 0;
514065Swollman
521558Srgrimesvoid
534065Swollmangetmntopts(options, m0, flagp, altflagp)
541558Srgrimes	const char *options;
551558Srgrimes	const struct mntopt *m0;
561558Srgrimes	int *flagp;
574065Swollman	int *altflagp;
581558Srgrimes{
591558Srgrimes	const struct mntopt *m;
603202Sache	int negative, len;
6123678Speter	char *opt, *optbuf, *p;
624065Swollman	int *thisflagp;
631558Srgrimes
641558Srgrimes	/* Copy option string, since it is about to be torn asunder... */
651558Srgrimes	if ((optbuf = strdup(options)) == NULL)
661558Srgrimes		err(1, NULL);
671558Srgrimes
681558Srgrimes	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
691558Srgrimes		/* Check for "no" prefix. */
701558Srgrimes		if (opt[0] == 'n' && opt[1] == 'o') {
711558Srgrimes			negative = 1;
721558Srgrimes			opt += 2;
731558Srgrimes		} else
741558Srgrimes			negative = 0;
751558Srgrimes
7623678Speter		/*
7723678Speter		 * for options with assignments in them (ie. quotas)
7823678Speter		 * ignore the assignment as it's handled elsewhere
7923678Speter		 */
8023678Speter		p = strchr(opt, '=');
81123268Strhodes		if (p != NULL)
8225301Smsmith			 *++p = '\0';
8323678Speter
841558Srgrimes		/* Scan option table. */
853202Sache		for (m = m0; m->m_option != NULL; ++m) {
863202Sache			len = strlen(m->m_option);
873202Sache			if (strncasecmp(opt, m->m_option, len) == 0)
88138094Sphk				if (opt[len] == '\0' || opt[len] == '=')
89138094Sphk					break;
903202Sache		}
911558Srgrimes
9237425Scharnier		/* Save flag, or fail if option is not recognized. */
931558Srgrimes		if (m->m_option) {
944065Swollman			thisflagp = m->m_altloc ? altflagp : flagp;
951558Srgrimes			if (negative == m->m_inverse)
964065Swollman				*thisflagp |= m->m_flag;
971558Srgrimes			else
984065Swollman				*thisflagp &= ~m->m_flag;
9923678Speter		} else if (!getmnt_silent) {
1001558Srgrimes			errx(1, "-o %s: option not supported", opt);
1014065Swollman		}
1021558Srgrimes	}
1031558Srgrimes
1041558Srgrimes	free(optbuf);
1051558Srgrimes}
10652055Sphk
10752055Sphkvoid
10852055Sphkrmslashes(rrpin, rrpout)
10952055Sphk	char *rrpin;
11052055Sphk	char *rrpout;
11152055Sphk{
11252055Sphk	char *rrpoutstart;
11352055Sphk
11452055Sphk	*rrpout = *rrpin;
11552055Sphk	for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) {
11652055Sphk
11752055Sphk		/* skip all double slashes */
11852055Sphk		while (*rrpin == '/' && *(rrpin + 1) == '/')
11952055Sphk			 rrpin++;
12052055Sphk	}
12152055Sphk
12252055Sphk	/* remove trailing slash if necessary */
12352055Sphk	if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/')
12452055Sphk		*(rrpout - 1) = '\0';
12552055Sphk	else
12652055Sphk		*rrpout = '\0';
12752055Sphk}
12852055Sphk
12952055Sphkvoid
13052055Sphkcheckpath(path, resolved)
13152055Sphk	const char *path;
13252055Sphk	char *resolved;
13352055Sphk{
13452055Sphk	struct stat sb;
13552055Sphk
13652055Sphk	if (realpath(path, resolved) != NULL && stat(resolved, &sb) == 0) {
13752055Sphk		if (!S_ISDIR(sb.st_mode))
13852055Sphk			errx(EX_USAGE, "%s: not a directory", resolved);
13952055Sphk	} else
14052055Sphk		errx(EX_USAGE, "%s: %s", resolved, strerror(errno));
14152055Sphk}
142138095Sphk
143138095Sphkvoid
144138095Sphkbuild_iovec(struct iovec **iov, int *iovlen, const char *name, void *val, int len)
145138095Sphk{
146138095Sphk	int i;
147138095Sphk
148138095Sphk	if (iovlen < 0)
149138095Sphk		return;
150138095Sphk	i = *iovlen;
151138095Sphk	*iov = realloc(*iov, sizeof **iov * (i + 2));
152138095Sphk	if (*iov == NULL) {
153138095Sphk		*iovlen = -1;
154138095Sphk		return;
155138095Sphk	}
156138095Sphk	(*iov)[i].iov_base = strdup(name);
157138095Sphk	(*iov)[i].iov_len = strlen(name) + 1;
158138095Sphk	i++;
159138095Sphk	(*iov)[i].iov_base = val;
160138095Sphk	if (len < 0)
161138095Sphk		len = strlen(val) + 1;
162138095Sphk	(*iov)[i].iov_len = len;
163138095Sphk	*iovlen = ++i;
164138095Sphk}
165