getmntopts.c revision 52055
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 341558Srgrimes#ifndef lint 3528671Ssteve#if 0 3623678Speterstatic char sccsid[] = "@(#)getmntopts.c 8.3 (Berkeley) 3/29/95"; 3728671Ssteve#else 3828671Sstevestatic const char rcsid[] = 3950476Speter "$FreeBSD: head/sbin/mount/getmntopts.c 52055 1999-10-09 11:54:14Z phk $"; 4028671Ssteve#endif 411558Srgrimes#endif /* not lint */ 421558Srgrimes 431558Srgrimes#include <sys/param.h> 4452055Sphk#include <sys/stat.h> 451558Srgrimes 461558Srgrimes#include <err.h> 4752055Sphk#include <errno.h> 481558Srgrimes#include <stdlib.h> 491558Srgrimes#include <string.h> 5052055Sphk#include <sysexits.h> 511558Srgrimes 521558Srgrimes#include "mntopts.h" 531558Srgrimes 544065Swollmanint getmnt_silent = 0; 554065Swollman 561558Srgrimesvoid 574065Swollmangetmntopts(options, m0, flagp, altflagp) 581558Srgrimes const char *options; 591558Srgrimes const struct mntopt *m0; 601558Srgrimes int *flagp; 614065Swollman int *altflagp; 621558Srgrimes{ 631558Srgrimes const struct mntopt *m; 643202Sache int negative, len; 6523678Speter char *opt, *optbuf, *p; 664065Swollman int *thisflagp; 671558Srgrimes 681558Srgrimes /* Copy option string, since it is about to be torn asunder... */ 691558Srgrimes if ((optbuf = strdup(options)) == NULL) 701558Srgrimes err(1, NULL); 711558Srgrimes 721558Srgrimes for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 731558Srgrimes /* Check for "no" prefix. */ 741558Srgrimes if (opt[0] == 'n' && opt[1] == 'o') { 751558Srgrimes negative = 1; 761558Srgrimes opt += 2; 771558Srgrimes } else 781558Srgrimes negative = 0; 791558Srgrimes 8023678Speter /* 8123678Speter * for options with assignments in them (ie. quotas) 8223678Speter * ignore the assignment as it's handled elsewhere 8323678Speter */ 8423678Speter p = strchr(opt, '='); 8523678Speter if (p) 8625301Smsmith *++p = '\0'; 8723678Speter 881558Srgrimes /* Scan option table. */ 893202Sache for (m = m0; m->m_option != NULL; ++m) { 903202Sache len = strlen(m->m_option); 913202Sache if (strncasecmp(opt, m->m_option, len) == 0) 923202Sache if ( m->m_option[len] == '\0' 933202Sache || m->m_option[len] == '=' 943202Sache ) 951558Srgrimes break; 963202Sache } 971558Srgrimes 9837425Scharnier /* Save flag, or fail if option is not recognized. */ 991558Srgrimes if (m->m_option) { 1004065Swollman thisflagp = m->m_altloc ? altflagp : flagp; 1011558Srgrimes if (negative == m->m_inverse) 1024065Swollman *thisflagp |= m->m_flag; 1031558Srgrimes else 1044065Swollman *thisflagp &= ~m->m_flag; 10523678Speter } else if (!getmnt_silent) { 1061558Srgrimes errx(1, "-o %s: option not supported", opt); 1074065Swollman } 1081558Srgrimes } 1091558Srgrimes 1101558Srgrimes free(optbuf); 1111558Srgrimes} 11252055Sphk 11352055Sphkvoid 11452055Sphkrmslashes(rrpin, rrpout) 11552055Sphk char *rrpin; 11652055Sphk char *rrpout; 11752055Sphk{ 11852055Sphk char *rrpoutstart; 11952055Sphk 12052055Sphk *rrpout = *rrpin; 12152055Sphk for (rrpoutstart = rrpout; *rrpin != '\0'; *rrpout++ = *rrpin++) { 12252055Sphk 12352055Sphk /* skip all double slashes */ 12452055Sphk while (*rrpin == '/' && *(rrpin + 1) == '/') 12552055Sphk rrpin++; 12652055Sphk } 12752055Sphk 12852055Sphk /* remove trailing slash if necessary */ 12952055Sphk if (rrpout - rrpoutstart > 1 && *(rrpout - 1) == '/') 13052055Sphk *(rrpout - 1) = '\0'; 13152055Sphk else 13252055Sphk *rrpout = '\0'; 13352055Sphk} 13452055Sphk 13552055Sphkvoid 13652055Sphkcheckpath(path, resolved) 13752055Sphk const char *path; 13852055Sphk char *resolved; 13952055Sphk{ 14052055Sphk struct stat sb; 14152055Sphk 14252055Sphk if (realpath(path, resolved) != NULL && stat(resolved, &sb) == 0) { 14352055Sphk if (!S_ISDIR(sb.st_mode)) 14452055Sphk errx(EX_USAGE, "%s: not a directory", resolved); 14552055Sphk } else 14652055Sphk errx(EX_USAGE, "%s: %s", resolved, strerror(errno)); 14752055Sphk} 148