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 sccsid[] = "@(#)fstab.c 8.1 (Berkeley) 6/4/93"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <errno.h> 39 #include <fstab.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 static FILE *_fs_fp; 46 static struct fstab _fs_fstab; 47 static int LineNo = 0; 48 49 static void error __P((int)); 50 static int fstabscan __P((void)); 51 52 static int 53 fstabscan() 54 { 55 char *cp, *p; 56 #define MAXLINELENGTH 1024 57 static char line[MAXLINELENGTH]; 58 char subline[MAXLINELENGTH]; 59 int typexx; 60 61 for (;;) { 62 63 if (!(p = fgets(line, sizeof(line), _fs_fp))) 64 return(0); 65 /* OLD_STYLE_FSTAB */ 66 ++LineNo; 67 if (*line == '#' || *line == '\n') 68 continue; 69 if (!strpbrk(p, " \t")) { 70 _fs_fstab.fs_spec = strsep(&p, ":\n"); 71 _fs_fstab.fs_file = strsep(&p, ":\n"); 72 _fs_fstab.fs_type = strsep(&p, ":\n"); 73 if (_fs_fstab.fs_type) { 74 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 75 continue; 76 _fs_fstab.fs_mntops = _fs_fstab.fs_type; 77 _fs_fstab.fs_vfstype = 78 strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 79 "ufs" : "swap"; 80 if ((cp = strsep(&p, ":\n")) != NULL) { 81 _fs_fstab.fs_freq = atoi(cp); 82 if ((cp = strsep(&p, ":\n")) != NULL) { 83 _fs_fstab.fs_passno = atoi(cp); 84 return(1); 85 } 86 } 87 } 88 goto bad; 89 } 90 /* OLD_STYLE_FSTAB */ 91 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 92 ; 93 _fs_fstab.fs_spec = cp; 94 if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 95 continue; 96 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 97 ; 98 _fs_fstab.fs_file = cp; 99 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 100 ; 101 _fs_fstab.fs_vfstype = cp; 102 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 103 ; 104 _fs_fstab.fs_mntops = cp; 105 if (_fs_fstab.fs_mntops == NULL) 106 goto bad; 107 _fs_fstab.fs_freq = 0; 108 _fs_fstab.fs_passno = 0; 109 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 110 ; 111 if (cp != NULL) { 112 _fs_fstab.fs_freq = atoi(cp); 113 while ((cp = strsep(&p, " \t\n")) != NULL && *cp == '\0') 114 ; 115 if (cp != NULL) 116 _fs_fstab.fs_passno = atoi(cp); 117 } 118 strcpy(subline, _fs_fstab.fs_mntops); 119 p = subline; 120 for (typexx = 0, cp = strsep(&p, ","); cp; 121 cp = strsep(&p, ",")) { 122 if (strlen(cp) != 2) 123 continue; 124 if (!strcmp(cp, FSTAB_RW)) { 125 _fs_fstab.fs_type = FSTAB_RW; 126 break; 127 } 128 if (!strcmp(cp, FSTAB_RQ)) { 129 _fs_fstab.fs_type = FSTAB_RQ; 130 break; 131 } 132 if (!strcmp(cp, FSTAB_RO)) { 133 _fs_fstab.fs_type = FSTAB_RO; 134 break; 135 } 136 if (!strcmp(cp, FSTAB_SW)) { 137 _fs_fstab.fs_type = FSTAB_SW; 138 break; 139 } 140 if (!strcmp(cp, FSTAB_XX)) { 141 _fs_fstab.fs_type = FSTAB_XX; 142 typexx++; 143 break; 144 } 145 } 146 if (typexx) 147 continue; 148 if (cp != NULL) 149 return(1); 150 151 bad: /* no way to distinguish between EOF and syntax error */ 152 error(EFTYPE); 153 } 154 /* NOTREACHED */ 155 } 156 157 struct fstab * 158 getfsent() 159 { 160 if ((!_fs_fp && !setfsent()) || !fstabscan()) 161 return((struct fstab *)NULL); 162 return(&_fs_fstab); 163 } 164 165 struct fstab * 166 getfsspec(name) 167 register const char *name; 168 { 169 if (setfsent()) 170 while (fstabscan()) 171 if (!strcmp(_fs_fstab.fs_spec, name)) 172 return(&_fs_fstab); 173 return((struct fstab *)NULL); 174 } 175 176 struct fstab * 177 getfsfile(name) 178 register const char *name; 179 { 180 if (setfsent()) 181 while (fstabscan()) 182 if (!strcmp(_fs_fstab.fs_file, name)) 183 return(&_fs_fstab); 184 return((struct fstab *)NULL); 185 } 186 187 int 188 setfsent() 189 { 190 if (_fs_fp) { 191 rewind(_fs_fp); 192 LineNo = 0; 193 return(1); 194 } 195 if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL) { 196 LineNo = 0; 197 return(1); 198 } 199 error(errno); 200 return(0); 201 } 202 203 void 204 endfsent() 205 { 206 if (_fs_fp) { 207 (void)fclose(_fs_fp); 208 _fs_fp = NULL; 209 } 210 } 211 212 static void 213 error(err) 214 int err; 215 { 216 char *p; 217 char num[30]; 218 219 (void)write(STDERR_FILENO, "fstab: ", 7); 220 (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); 221 (void)write(STDERR_FILENO, ":", 1); 222 sprintf(num, "%d: ", LineNo); 223 (void)write(STDERR_FILENO, num, strlen(num)); 224 p = strerror(err); 225 (void)write(STDERR_FILENO, p, strlen(p)); 226 (void)write(STDERR_FILENO, "\n", 1); 227 } 228