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