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