1 /* $NetBSD: fstab.c,v 1.16 1998/10/16 11:24:30 kleink 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; 40 #else 41 __RCSID("$NetBSD: fstab.c,v 1.16 1998/10/16 11:24:30 kleink Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 #include <sys/types.h> 47 #include <sys/uio.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <fstab.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #ifdef __weak_alias 57 __weak_alias(endfsent,_endfsent); 58 __weak_alias(getfsent,_getfsent); 59 __weak_alias(getfsfile,_getfsfile); 60 __weak_alias(getfsspec,_getfsspec); 61 __weak_alias(setfsent,_setfsent); 62 #endif 63 64 static FILE *_fs_fp; 65 static struct fstab _fs_fstab; 66 67 static void error __P((int)); 68 static int fstabscan __P((void)); 69 70 static int 71 fstabscan() 72 { 73 char *cp; 74 #define MAXLINELENGTH 1024 75 static char line[MAXLINELENGTH]; 76 char subline[MAXLINELENGTH]; 77 int typexx; 78 char *lastp; 79 static const char sep[] = ":\n"; 80 static const char ws[] = " \t\n"; 81 82 for (;;) { 83 if (!(cp = fgets(line, sizeof(line), _fs_fp))) 84 return(0); 85 /* OLD_STYLE_FSTAB */ 86 if (!strpbrk(cp, " \t")) { 87 _fs_fstab.fs_spec = strtok_r(cp, sep, &lastp); 88 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 89 continue; 90 _fs_fstab.fs_file = strtok_r(NULL, sep, &lastp); 91 _fs_fstab.fs_type = strtok_r(NULL, sep, &lastp); 92 if (_fs_fstab.fs_type) { 93 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 94 continue; 95 _fs_fstab.fs_mntops = _fs_fstab.fs_type; 96 _fs_fstab.fs_vfstype = 97 strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 98 "ufs" : "swap"; 99 if ((cp = strtok_r(NULL, sep, &lastp)) 100 != NULL) { 101 _fs_fstab.fs_freq = atoi(cp); 102 if ((cp = strtok_r(NULL, sep, &lastp)) 103 != NULL) { 104 _fs_fstab.fs_passno = atoi(cp); 105 return(1); 106 } 107 } 108 } 109 goto bad; 110 } 111 /* OLD_STYLE_FSTAB */ 112 _fs_fstab.fs_spec = strtok_r(cp, ws, &lastp); 113 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 114 continue; 115 _fs_fstab.fs_file = strtok_r(NULL, ws, &lastp); 116 _fs_fstab.fs_vfstype = strtok_r(NULL, ws, &lastp); 117 _fs_fstab.fs_mntops = strtok_r(NULL, ws, &lastp); 118 if (_fs_fstab.fs_mntops == NULL) 119 goto bad; 120 _fs_fstab.fs_freq = 0; 121 _fs_fstab.fs_passno = 0; 122 if ((cp = strtok_r(NULL, ws, &lastp)) != NULL) { 123 _fs_fstab.fs_freq = atoi(cp); 124 if ((cp = strtok_r(NULL, ws, &lastp)) != NULL) 125 _fs_fstab.fs_passno = atoi(cp); 126 } 127 (void)strncpy(subline, _fs_fstab.fs_mntops, sizeof(subline)-1); 128 for (typexx = 0, cp = strtok_r(subline, ",", &lastp); cp; 129 cp = strtok_r((char *)NULL, ",", &lastp)) { 130 if (strlen(cp) != 2) 131 continue; 132 if (!strcmp(cp, FSTAB_RW)) { 133 _fs_fstab.fs_type = FSTAB_RW; 134 break; 135 } 136 if (!strcmp(cp, FSTAB_RQ)) { 137 _fs_fstab.fs_type = FSTAB_RQ; 138 break; 139 } 140 if (!strcmp(cp, FSTAB_RO)) { 141 _fs_fstab.fs_type = FSTAB_RO; 142 break; 143 } 144 if (!strcmp(cp, FSTAB_SW)) { 145 _fs_fstab.fs_type = FSTAB_SW; 146 break; 147 } 148 if (!strcmp(cp, FSTAB_XX)) { 149 _fs_fstab.fs_type = FSTAB_XX; 150 typexx++; 151 break; 152 } 153 } 154 if (typexx) 155 continue; 156 if (cp != NULL) 157 return(1); 158 159 bad: /* no way to distinguish between EOF and syntax error */ 160 error(EFTYPE); 161 } 162 /* NOTREACHED */ 163 } 164 165 struct fstab * 166 getfsent() 167 { 168 if ((!_fs_fp && !setfsent()) || !fstabscan()) 169 return((struct fstab *)NULL); 170 return(&_fs_fstab); 171 } 172 173 struct fstab * 174 getfsspec(name) 175 const char *name; 176 { 177 if (setfsent()) 178 while (fstabscan()) 179 if (!strcmp(_fs_fstab.fs_spec, name)) 180 return(&_fs_fstab); 181 return((struct fstab *)NULL); 182 } 183 184 struct fstab * 185 getfsfile(name) 186 const char *name; 187 { 188 if (setfsent()) 189 while (fstabscan()) 190 if (!strcmp(_fs_fstab.fs_file, name)) 191 return(&_fs_fstab); 192 return((struct fstab *)NULL); 193 } 194 195 int 196 setfsent() 197 { 198 if (_fs_fp) { 199 rewind(_fs_fp); 200 return(1); 201 } 202 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) 203 return(1); 204 error(errno); 205 return(0); 206 } 207 208 void 209 endfsent() 210 { 211 if (_fs_fp) { 212 (void)fclose(_fs_fp); 213 _fs_fp = NULL; 214 } 215 } 216 217 static void 218 error(err) 219 int err; 220 { 221 222 errno = err; 223 warn(_PATH_FSTAB); 224 } 225