1 /* 2 * Copyright (c) 1980, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char rcsid[] = "$OpenBSD: fstab.c,v 1.9 2001/06/27 00:58:54 lebel Exp $"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/types.h> 39 #include <sys/uio.h> 40 #include <sys/stat.h> 41 42 #include <errno.h> 43 #include <limits.h> 44 #include <fstab.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 static FILE *_fs_fp; 51 static struct fstab _fs_fstab; 52 53 static void error __P((int)); 54 static int fstabscan __P((void)); 55 56 static int 57 fstabscan() 58 { 59 register char *cp; 60 #define MAXLINELENGTH 1024 61 static char line[MAXLINELENGTH]; 62 char subline[MAXLINELENGTH]; 63 char *endp; 64 int typexx; 65 long l; 66 67 for (;;) { 68 if (!(cp = fgets(line, sizeof(line), _fs_fp))) 69 return(0); 70 /* OLD_STYLE_FSTAB */ 71 if (!strpbrk(cp, " \t")) { 72 _fs_fstab.fs_spec = strtok(cp, ":\n"); 73 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 74 continue; 75 _fs_fstab.fs_file = strtok((char *)NULL, ":\n"); 76 _fs_fstab.fs_type = strtok((char *)NULL, ":\n"); 77 if (_fs_fstab.fs_type) { 78 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 79 continue; 80 _fs_fstab.fs_mntops = _fs_fstab.fs_type; 81 _fs_fstab.fs_vfstype = 82 strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 83 "ufs" : "swap"; 84 if ((cp = strtok((char *)NULL, ":\n"))) { 85 l = strtol(cp, &endp, 10); 86 if (endp == cp || *endp != '\0' || 87 l < 0 || l >= INT_MAX) 88 goto bad; 89 _fs_fstab.fs_freq = l; 90 if ((cp = strtok((char *)NULL, ":\n"))) { 91 l = strtol(cp, &endp, 10); 92 if (endp == cp || *endp != '\0' 93 || l < 0 || l >= INT_MAX) 94 goto bad; 95 _fs_fstab.fs_passno = l; 96 return(1); 97 } 98 } 99 } 100 goto bad; 101 } 102 /* OLD_STYLE_FSTAB */ 103 _fs_fstab.fs_spec = strtok(cp, " \t\n"); 104 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 105 continue; 106 _fs_fstab.fs_file = strtok((char *)NULL, " \t\n"); 107 _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n"); 108 _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n"); 109 if (_fs_fstab.fs_mntops == NULL) 110 goto bad; 111 _fs_fstab.fs_freq = 0; 112 _fs_fstab.fs_passno = 0; 113 if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { 114 l = strtol(cp, &endp, 10); 115 if (endp == cp || *endp != '\0' || l < 0 || 116 l >= INT_MAX) 117 goto bad; 118 _fs_fstab.fs_freq = l; 119 if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { 120 l = strtol(cp, &endp, 10); 121 if (endp == cp || *endp != '\0' || l < 0 || 122 l >= INT_MAX) 123 goto bad; 124 _fs_fstab.fs_passno = l; 125 } 126 } 127 strlcpy(subline, _fs_fstab.fs_mntops, sizeof subline); 128 for (typexx = 0, cp = strtok(subline, ","); cp; 129 cp = strtok((char *)NULL, ",")) { 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(NULL); 170 return(&_fs_fstab); 171 } 172 173 struct fstab * 174 getfsspec(name) 175 register 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 register 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 struct stat sbuf; 199 200 if (_fs_fp) { 201 rewind(_fs_fp); 202 return(1); 203 } 204 205 if (stat(_PATH_FSTAB, &sbuf) != 0) 206 goto fail; 207 if ((sbuf.st_size == 0) || ((sbuf.st_mode & S_IFMT) != S_IFREG)) { 208 errno = EFTYPE; 209 goto fail; 210 } 211 212 if ((_fs_fp = fopen(_PATH_FSTAB, "r"))) 213 return(1); 214 215 fail: 216 error(errno); 217 return(0); 218 } 219 220 void 221 endfsent() 222 { 223 if (_fs_fp) { 224 (void)fclose(_fs_fp); 225 _fs_fp = NULL; 226 } 227 } 228 229 static void 230 error(err) 231 int err; 232 { 233 struct iovec iov[5]; 234 235 iov[0].iov_base = "fstab: "; 236 iov[0].iov_len = 7; 237 iov[1].iov_base = _PATH_FSTAB; 238 iov[1].iov_len = sizeof(_PATH_FSTAB) - 1; 239 iov[2].iov_base = ": "; 240 iov[2].iov_len = 2; 241 iov[3].iov_base = strerror(err); 242 iov[3].iov_len = strlen(iov[3].iov_base); 243 iov[4].iov_base = "\n"; 244 iov[4].iov_len = 1; 245 (void)writev(STDERR_FILENO, iov, 5); 246 } 247