Home | History | Annotate | Line # | Download | only in libutil
getmntopts.c revision 1.2
      1  1.2  christos /*	$NetBSD: getmntopts.c,v 1.2 2003/04/11 17:37:28 christos Exp $	*/
      2  1.1  jdolecek 
      3  1.1  jdolecek /*-
      4  1.1  jdolecek  * Copyright (c) 1994
      5  1.1  jdolecek  *	The Regents of the University of California.  All rights reserved.
      6  1.1  jdolecek  *
      7  1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
      8  1.1  jdolecek  * modification, are permitted provided that the following conditions
      9  1.1  jdolecek  * are met:
     10  1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     15  1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     16  1.1  jdolecek  *    must display the following acknowledgement:
     17  1.1  jdolecek  *	This product includes software developed by the University of
     18  1.1  jdolecek  *	California, Berkeley and its contributors.
     19  1.1  jdolecek  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  jdolecek  *    may be used to endorse or promote products derived from this software
     21  1.1  jdolecek  *    without specific prior written permission.
     22  1.1  jdolecek  *
     23  1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  jdolecek  * SUCH DAMAGE.
     34  1.1  jdolecek  */
     35  1.1  jdolecek 
     36  1.1  jdolecek #include <sys/cdefs.h>
     37  1.1  jdolecek #ifndef lint
     38  1.1  jdolecek #if 0
     39  1.1  jdolecek static char sccsid[] = "@(#)getmntopts.c	8.3 (Berkeley) 3/29/95";
     40  1.1  jdolecek #else
     41  1.2  christos __RCSID("$NetBSD: getmntopts.c,v 1.2 2003/04/11 17:37:28 christos Exp $");
     42  1.1  jdolecek #endif
     43  1.1  jdolecek #endif /* not lint */
     44  1.1  jdolecek 
     45  1.1  jdolecek #include <sys/param.h>
     46  1.1  jdolecek #include <sys/mount.h>
     47  1.1  jdolecek 
     48  1.1  jdolecek #include <err.h>
     49  1.1  jdolecek #include <errno.h>
     50  1.1  jdolecek #include <fstab.h>
     51  1.1  jdolecek #include <stdlib.h>
     52  1.1  jdolecek #include <string.h>
     53  1.1  jdolecek 
     54  1.1  jdolecek #include <mntopts.h>
     55  1.1  jdolecek 
     56  1.1  jdolecek int getmnt_silent = 0;
     57  1.1  jdolecek 
     58  1.2  christos static const char errmsg[] = "-o %s: option not supported";
     59  1.2  christos 
     60  1.2  christos struct mntoptparse {
     61  1.2  christos 	const char *options;
     62  1.2  christos 	const struct mntopt *mopts;
     63  1.2  christos 	char *optbuf;
     64  1.2  christos 	char **optarg;
     65  1.2  christos };
     66  1.2  christos 
     67  1.2  christos const char *
     68  1.2  christos getmntoptstr(mntoptparse_t mp, const char *opt)
     69  1.2  christos {
     70  1.2  christos 	const struct mntopt *m;
     71  1.2  christos 
     72  1.2  christos 	for (m = mp->mopts; m->m_option != NULL; m++)
     73  1.2  christos 		if (strcasecmp(opt, m->m_option) == 0)
     74  1.2  christos 			break;
     75  1.2  christos 
     76  1.2  christos 	if (m->m_option == NULL) {
     77  1.2  christos 		if (getmnt_silent == 0)
     78  1.2  christos 			errx(1, errmsg, opt);
     79  1.2  christos 		else
     80  1.2  christos 			return NULL;
     81  1.2  christos 	}
     82  1.2  christos 
     83  1.2  christos 	return mp->optarg[m - mp->mopts];
     84  1.2  christos }
     85  1.2  christos 
     86  1.2  christos long
     87  1.2  christos getmntoptnum(mntoptparse_t mp, const char *opt)
     88  1.2  christos {
     89  1.2  christos 	char *ep;
     90  1.2  christos 	long rv;
     91  1.2  christos 	void (*fun)(int, const char *, ...) = NULL;
     92  1.2  christos 	const char *val = getmntoptstr(mp, opt);
     93  1.2  christos 
     94  1.2  christos 	if (val == NULL) {
     95  1.2  christos 		if (getmnt_silent == 0)
     96  1.2  christos 			errx(1, "Missing %s argument", opt);
     97  1.2  christos 		else
     98  1.2  christos 			return -1;
     99  1.2  christos 	}
    100  1.2  christos 
    101  1.2  christos 	errno = 0;
    102  1.2  christos 	rv = strtol(val, &ep, 0);
    103  1.2  christos 
    104  1.2  christos 	if (*ep)
    105  1.2  christos 		fun = errx;
    106  1.2  christos 
    107  1.2  christos 	if (errno == ERANGE && (rv == LONG_MAX || rv == LONG_MIN))
    108  1.2  christos 		fun = err;
    109  1.2  christos 
    110  1.2  christos 	if (fun) {
    111  1.2  christos 		if (getmnt_silent != 0)
    112  1.2  christos 			return -1;
    113  1.2  christos 		(*fun)(1, "Invalid %s argument `%s'", opt, val);
    114  1.2  christos 	}
    115  1.2  christos 	return rv;
    116  1.2  christos }
    117  1.2  christos 
    118  1.1  jdolecek void
    119  1.2  christos freemntopts(mntoptparse_t mp)
    120  1.2  christos {
    121  1.2  christos 	free(mp->optbuf);
    122  1.2  christos 	free(mp->optarg);
    123  1.2  christos 	free(mp);
    124  1.2  christos }
    125  1.2  christos 
    126  1.2  christos mntoptparse_t
    127  1.2  christos getmntopts(const char *options, const struct mntopt *m0, int *flagp,
    128  1.2  christos     int *altflagp)
    129  1.1  jdolecek {
    130  1.1  jdolecek 	const struct mntopt *m;
    131  1.1  jdolecek 	int negative;
    132  1.2  christos 	char *opt, *p;
    133  1.1  jdolecek 	int *thisflagp;
    134  1.2  christos 	size_t nopts;
    135  1.2  christos 	mntoptparse_t mp;
    136  1.2  christos 
    137  1.2  christos 	for (nopts = 0, m = m0; m->m_option != NULL; ++m, nopts++)
    138  1.2  christos 		continue;
    139  1.2  christos 
    140  1.2  christos 	if ((mp = malloc(sizeof(struct mntoptparse))) == NULL)
    141  1.2  christos 		return NULL;
    142  1.1  jdolecek 
    143  1.1  jdolecek 	/* Copy option string, since it is about to be torn asunder... */
    144  1.2  christos 	if ((mp->optbuf = strdup(options)) == NULL) {
    145  1.2  christos 		free(mp);
    146  1.2  christos 		return NULL;
    147  1.2  christos 	}
    148  1.2  christos 
    149  1.2  christos 	if ((mp->optarg = calloc(nopts, sizeof(char *))) == NULL) {
    150  1.2  christos 		free(mp->optbuf);
    151  1.2  christos 		free(mp);
    152  1.2  christos 		return NULL;
    153  1.2  christos 	}
    154  1.1  jdolecek 
    155  1.2  christos 	mp->mopts = m0;
    156  1.2  christos 	mp->options = options;
    157  1.2  christos 
    158  1.2  christos 	for (opt = mp->optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    159  1.1  jdolecek 		/* Check for "no" prefix. */
    160  1.1  jdolecek 		if (opt[0] == 'n' && opt[1] == 'o') {
    161  1.1  jdolecek 			negative = 1;
    162  1.1  jdolecek 			opt += 2;
    163  1.1  jdolecek 		} else
    164  1.1  jdolecek 			negative = 0;
    165  1.1  jdolecek 
    166  1.1  jdolecek 		/*
    167  1.1  jdolecek 		 * for options with assignments in them (ie. quotas)
    168  1.1  jdolecek 		 * ignore the assignment as it's handled elsewhere
    169  1.1  jdolecek 		 */
    170  1.1  jdolecek 		p = strchr(opt, '=');
    171  1.2  christos 		if (p) {
    172  1.2  christos 			 *p++ = '\0';
    173  1.2  christos 		}
    174  1.1  jdolecek 
    175  1.1  jdolecek 		/* Scan option table. */
    176  1.1  jdolecek 		for (m = m0; m->m_option != NULL; ++m)
    177  1.1  jdolecek 			if (strcasecmp(opt, m->m_option) == 0)
    178  1.1  jdolecek 				break;
    179  1.1  jdolecek 
    180  1.1  jdolecek 		/* Save flag, or fail if option is not recognised. */
    181  1.1  jdolecek 		if (m->m_option) {
    182  1.2  christos 			mp->optarg[m - m0] = p;
    183  1.1  jdolecek 			thisflagp = m->m_altloc ? altflagp : flagp;
    184  1.1  jdolecek 			if (negative == m->m_inverse)
    185  1.1  jdolecek 				*thisflagp |= m->m_flag;
    186  1.1  jdolecek 			else
    187  1.1  jdolecek 				*thisflagp &= ~m->m_flag;
    188  1.1  jdolecek 		} else if (!getmnt_silent) {
    189  1.2  christos 			errx(1, errmsg, opt);
    190  1.1  jdolecek 		}
    191  1.1  jdolecek 	}
    192  1.2  christos 	return mp;
    193  1.1  jdolecek }
    194