1 /* $NetBSD: fstab.c,v 1.27 2005/12/24 21:11:16 perry Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1988, 1993 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 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; 36 #else 37 __RCSID("$NetBSD: fstab.c,v 1.27 2005/12/24 21:11:16 perry Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include "namespace.h" 42 #include <sys/types.h> 43 44 #include <assert.h> 45 #include <err.h> 46 #include <errno.h> 47 #include <fstab.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #ifdef __weak_alias 54 __weak_alias(endfsent,_endfsent) 55 __weak_alias(getfsent,_getfsent) 56 __weak_alias(getfsfile,_getfsfile) 57 __weak_alias(getfsspec,_getfsspec) 58 __weak_alias(setfsent,_setfsent) 59 #endif 60 61 static FILE *_fs_fp; 62 static size_t _fs_lineno = 0; 63 static const char *_fs_file = _PATH_FSTAB; 64 static struct fstab _fs_fstab; 65 66 static int fstabscan __P((void)); 67 68 static inline char *nextfld __P((char **, const char *)); 69 70 71 static inline char * 72 nextfld(str, sep) 73 char **str; 74 const char *sep; 75 { 76 char *ret; 77 78 _DIAGASSERT(str != NULL); 79 _DIAGASSERT(sep != NULL); 80 81 while ((ret = strsep(str, sep)) != NULL && *ret == '\0') 82 continue; 83 return ret; 84 } 85 86 87 static int 88 fstabscan() 89 { 90 char *cp, *lp, *sp; 91 #define MAXLINELENGTH 1024 92 static char line[MAXLINELENGTH]; 93 char subline[MAXLINELENGTH]; 94 static const char sep[] = ":\n"; 95 static const char ws[] = " \t\n"; 96 static const char *fstab_type[] = { 97 FSTAB_RW, FSTAB_RQ, FSTAB_RO, FSTAB_SW, FSTAB_DP, FSTAB_XX, NULL 98 }; 99 100 (void)memset(&_fs_fstab, 0, sizeof(_fs_fstab)); 101 for (;;) { 102 if (!(lp = fgets(line, sizeof(line), _fs_fp))) 103 return 0; 104 _fs_lineno++; 105 /* OLD_STYLE_FSTAB */ 106 if (!strpbrk(lp, " \t")) { 107 _fs_fstab.fs_spec = nextfld(&lp, sep); 108 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 109 continue; 110 _fs_fstab.fs_file = nextfld(&lp, sep); 111 _fs_fstab.fs_type = nextfld(&lp, sep); 112 if (_fs_fstab.fs_type) { 113 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 114 continue; 115 _fs_fstab.fs_mntops = _fs_fstab.fs_type; 116 _fs_fstab.fs_vfstype = 117 __UNCONST( 118 strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 119 "ufs" : "swap"); 120 if ((cp = nextfld(&lp, sep)) != NULL) { 121 _fs_fstab.fs_freq = atoi(cp); 122 if ((cp = nextfld(&lp, sep)) != NULL) { 123 _fs_fstab.fs_passno = atoi(cp); 124 return 1; 125 } 126 } 127 } 128 goto bad; 129 } 130 /* OLD_STYLE_FSTAB */ 131 _fs_fstab.fs_spec = nextfld(&lp, ws); 132 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 133 continue; 134 _fs_fstab.fs_file = nextfld(&lp, ws); 135 _fs_fstab.fs_vfstype = nextfld(&lp, ws); 136 _fs_fstab.fs_mntops = nextfld(&lp, ws); 137 if (_fs_fstab.fs_mntops == NULL) 138 goto bad; 139 _fs_fstab.fs_freq = 0; 140 _fs_fstab.fs_passno = 0; 141 if ((cp = nextfld(&lp, ws)) != NULL) { 142 _fs_fstab.fs_freq = atoi(cp); 143 if ((cp = nextfld(&lp, ws)) != NULL) 144 _fs_fstab.fs_passno = atoi(cp); 145 } 146 147 /* subline truncated iff line truncated */ 148 (void)strlcpy(subline, _fs_fstab.fs_mntops, sizeof(subline)); 149 sp = subline; 150 151 while ((cp = nextfld(&sp, ",")) != NULL) { 152 const char **tp; 153 154 if (strlen(cp) != 2) 155 continue; 156 157 for (tp = fstab_type; *tp; tp++) 158 if (strcmp(cp, *tp) == 0) { 159 _fs_fstab.fs_type = __UNCONST(*tp); 160 break; 161 } 162 if (*tp) 163 break; 164 } 165 if (_fs_fstab.fs_type == NULL) 166 goto bad; 167 if (strcmp(_fs_fstab.fs_type, FSTAB_XX) == 0) 168 continue; 169 if (cp != NULL) 170 return 1; 171 172 bad: 173 warnx("%s, %lu: Missing fields", _fs_file, (u_long)_fs_lineno); 174 } 175 /* NOTREACHED */ 176 } 177 178 struct fstab * 179 getfsent() 180 { 181 if ((!_fs_fp && !setfsent()) || !fstabscan()) 182 return NULL; 183 return &_fs_fstab; 184 } 185 186 struct fstab * 187 getfsspec(name) 188 const char *name; 189 { 190 191 _DIAGASSERT(name != NULL); 192 193 if (setfsent()) 194 while (fstabscan()) 195 if (!strcmp(_fs_fstab.fs_spec, name)) 196 return &_fs_fstab; 197 return NULL; 198 } 199 200 struct fstab * 201 getfsfile(name) 202 const char *name; 203 { 204 205 _DIAGASSERT(name != NULL); 206 207 if (setfsent()) 208 while (fstabscan()) 209 if (!strcmp(_fs_fstab.fs_file, name)) 210 return &_fs_fstab; 211 return NULL; 212 } 213 214 int 215 setfsent() 216 { 217 _fs_lineno = 0; 218 if (_fs_fp) { 219 rewind(_fs_fp); 220 return 1; 221 } 222 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) == NULL) { 223 warn("Cannot open `%s'", _PATH_FSTAB); 224 return 0; 225 } 226 return 1; 227 } 228 229 void 230 endfsent() 231 { 232 if (_fs_fp) { 233 (void)fclose(_fs_fp); 234 _fs_fp = NULL; 235 } 236 } 237