121341Sdist /* 235370Sbostic * Copyright (c) 1980, 1988 Regents of the University of California. 335368Sbostic * All rights reserved. 435368Sbostic * 542624Sbostic * %sccs.include.redist.c% 621341Sdist */ 721341Sdist 826552Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*46597Sdonn static char sccsid[] = "@(#)fstab.c 5.15 (Berkeley) 02/23/91"; 1035368Sbostic #endif /* LIBC_SCCS and not lint */ 1112597Ssam 1245646Sbostic #include <sys/errno.h> 132010Swnj #include <fstab.h> 1438110Sbostic #include <unistd.h> 152010Swnj #include <stdio.h> 16*46597Sdonn #include <stdlib.h> 1745646Sbostic #include <string.h> 182010Swnj 1936214Sbostic static FILE *_fs_fp; 2036214Sbostic static struct fstab _fs_fstab; 21*46597Sdonn static error(); 222010Swnj 2335370Sbostic static 2436214Sbostic fstabscan() 252010Swnj { 2612597Ssam register char *cp; 2740563Smckusick #define MAXLINELENGTH 1024 2836214Sbostic static char line[MAXLINELENGTH]; 2938671Smckusick char subline[MAXLINELENGTH]; 3038697Smckusick int typexx; 3112597Ssam 3235370Sbostic for (;;) { 3336214Sbostic if (!(cp = fgets(line, sizeof(line), _fs_fp))) 3436214Sbostic return(0); 3545245Sbostic /* OLD_STYLE_FSTAB */ 3645245Sbostic if (!strpbrk(cp, " \t")) { 3745245Sbostic _fs_fstab.fs_spec = strtok(cp, ":\n"); 3845245Sbostic _fs_fstab.fs_file = strtok((char *)NULL, ":\n"); 3945245Sbostic _fs_fstab.fs_type = strtok((char *)NULL, ":\n"); 4045245Sbostic if (_fs_fstab.fs_type) { 4145245Sbostic if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 4245245Sbostic continue; 4345245Sbostic _fs_fstab.fs_mntops = _fs_fstab.fs_type; 4445245Sbostic _fs_fstab.fs_vfstype = 4545245Sbostic strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 4645245Sbostic "ufs" : "swap"; 4745245Sbostic if (cp = strtok((char *)NULL, ":\n")) { 4845245Sbostic _fs_fstab.fs_freq = atoi(cp); 4945245Sbostic if (cp = strtok((char *)NULL, ":\n")) { 5045245Sbostic _fs_fstab.fs_passno = atoi(cp); 5145245Sbostic return(1); 5245245Sbostic } 5345245Sbostic } 5445245Sbostic } 5545245Sbostic goto bad; 5645245Sbostic } 5745245Sbostic /* OLD_STYLE_FSTAB */ 5838671Smckusick _fs_fstab.fs_spec = strtok(cp, " \t\n"); 5941578Sbostic if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 6041578Sbostic continue; 6138671Smckusick _fs_fstab.fs_file = strtok((char *)NULL, " \t\n"); 6238671Smckusick _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n"); 6338671Smckusick _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n"); 6438671Smckusick if (_fs_fstab.fs_mntops == NULL) 6538671Smckusick goto bad; 6638671Smckusick _fs_fstab.fs_freq = 0; 6738671Smckusick _fs_fstab.fs_passno = 0; 6838671Smckusick if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { 6938671Smckusick _fs_fstab.fs_freq = atoi(cp); 7038671Smckusick if ((cp = strtok((char *)NULL, " \t\n")) != NULL) 7138671Smckusick _fs_fstab.fs_passno = atoi(cp); 7238671Smckusick } 7338671Smckusick strcpy(subline, _fs_fstab.fs_mntops); 7438697Smckusick for (typexx = 0, cp = strtok(subline, ","); cp; 7538671Smckusick cp = strtok((char *)NULL, ",")) { 7638671Smckusick if (strlen(cp) != 2) 7735370Sbostic continue; 7838671Smckusick if (!strcmp(cp, FSTAB_RW)) { 7938671Smckusick _fs_fstab.fs_type = FSTAB_RW; 8038671Smckusick break; 8138110Sbostic } 8238671Smckusick if (!strcmp(cp, FSTAB_RQ)) { 8338671Smckusick _fs_fstab.fs_type = FSTAB_RQ; 8438671Smckusick break; 8538671Smckusick } 8638671Smckusick if (!strcmp(cp, FSTAB_RO)) { 8738671Smckusick _fs_fstab.fs_type = FSTAB_RO; 8838671Smckusick break; 8938671Smckusick } 9038671Smckusick if (!strcmp(cp, FSTAB_SW)) { 9138671Smckusick _fs_fstab.fs_type = FSTAB_SW; 9238671Smckusick break; 9338671Smckusick } 9438671Smckusick if (!strcmp(cp, FSTAB_XX)) { 9538671Smckusick _fs_fstab.fs_type = FSTAB_XX; 9638697Smckusick typexx++; 9738671Smckusick break; 9838671Smckusick } 9935370Sbostic } 10038697Smckusick if (typexx) 10138697Smckusick continue; 10238671Smckusick if (cp != NULL) 10338671Smckusick return(1); 10445245Sbostic 10545245Sbostic bad: /* no way to distinguish between EOF and syntax error */ 106*46597Sdonn error(EFTYPE); 1072010Swnj } 10835370Sbostic /* NOTREACHED */ 1092010Swnj } 1102010Swnj 11112597Ssam struct fstab * 11212597Ssam getfsent() 1132010Swnj { 11436214Sbostic if (!_fs_fp && !setfsent() || !fstabscan()) 11535370Sbostic return((struct fstab *)NULL); 11636214Sbostic return(&_fs_fstab); 1172010Swnj } 11812597Ssam 11912597Ssam struct fstab * 12012597Ssam getfsspec(name) 121*46597Sdonn register const char *name; 1222010Swnj { 12336214Sbostic if (setfsent()) 12436214Sbostic while (fstabscan()) 12536214Sbostic if (!strcmp(_fs_fstab.fs_spec, name)) 12636214Sbostic return(&_fs_fstab); 12735370Sbostic return((struct fstab *)NULL); 1282010Swnj } 12912597Ssam 13012597Ssam struct fstab * 13112597Ssam getfsfile(name) 132*46597Sdonn register const char *name; 1332010Swnj { 13436214Sbostic if (setfsent()) 13536214Sbostic while (fstabscan()) 13636214Sbostic if (!strcmp(_fs_fstab.fs_file, name)) 13736214Sbostic return(&_fs_fstab); 13835370Sbostic return((struct fstab *)NULL); 1392010Swnj } 14036214Sbostic 14136214Sbostic setfsent() 14236214Sbostic { 14336214Sbostic if (_fs_fp) { 14436214Sbostic rewind(_fs_fp); 14536214Sbostic return(1); 14636214Sbostic } 14745646Sbostic if (_fs_fp = fopen(_PATH_FSTAB, "r")) 14845646Sbostic return(1); 14945646Sbostic error(errno); 15045646Sbostic return(0); 15136214Sbostic } 15236214Sbostic 15336214Sbostic void 15436214Sbostic endfsent() 15536214Sbostic { 15636214Sbostic if (_fs_fp) { 15736214Sbostic (void)fclose(_fs_fp); 15836214Sbostic _fs_fp = NULL; 15936214Sbostic } 16036214Sbostic } 16145646Sbostic 16245646Sbostic static 16345646Sbostic error(err) 16445646Sbostic int err; 16545646Sbostic { 16645646Sbostic char *p; 16745646Sbostic 16845646Sbostic (void)write(STDERR_FILENO, "fstab: ", 7); 16945646Sbostic (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); 17045646Sbostic p = strerror(err); 17145646Sbostic (void)write(STDERR_FILENO, p, strlen(p)); 17245646Sbostic (void)write(STDERR_FILENO, "\n", 1); 17345646Sbostic } 174