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*45646Sbostic static char sccsid[] = "@(#)fstab.c 5.13 (Berkeley) 11/28/90"; 1035368Sbostic #endif /* LIBC_SCCS and not lint */ 1112597Ssam 12*45646Sbostic #include <sys/errno.h> 132010Swnj #include <fstab.h> 1438110Sbostic #include <unistd.h> 152010Swnj #include <stdio.h> 16*45646Sbostic #include <string.h> 172010Swnj 1836214Sbostic static FILE *_fs_fp; 1936214Sbostic static struct fstab _fs_fstab; 202010Swnj 2135370Sbostic static 2236214Sbostic fstabscan() 232010Swnj { 2412597Ssam register char *cp; 2540563Smckusick #define MAXLINELENGTH 1024 2636214Sbostic static char line[MAXLINELENGTH]; 2738671Smckusick char subline[MAXLINELENGTH]; 2838671Smckusick char *fgets(), *strtok(); 2938697Smckusick int typexx; 3012597Ssam 3135370Sbostic for (;;) { 3236214Sbostic if (!(cp = fgets(line, sizeof(line), _fs_fp))) 3336214Sbostic return(0); 3445245Sbostic /* OLD_STYLE_FSTAB */ 3545245Sbostic if (!strpbrk(cp, " \t")) { 3645245Sbostic _fs_fstab.fs_spec = strtok(cp, ":\n"); 3745245Sbostic _fs_fstab.fs_file = strtok((char *)NULL, ":\n"); 3845245Sbostic _fs_fstab.fs_type = strtok((char *)NULL, ":\n"); 3945245Sbostic if (_fs_fstab.fs_type) { 4045245Sbostic if (!strcmp(_fs_fstab.fs_type, FSTAB_XX)) 4145245Sbostic continue; 4245245Sbostic _fs_fstab.fs_mntops = _fs_fstab.fs_type; 4345245Sbostic _fs_fstab.fs_vfstype = 4445245Sbostic strcmp(_fs_fstab.fs_type, FSTAB_SW) ? 4545245Sbostic "ufs" : "swap"; 4645245Sbostic if (cp = strtok((char *)NULL, ":\n")) { 4745245Sbostic _fs_fstab.fs_freq = atoi(cp); 4845245Sbostic if (cp = strtok((char *)NULL, ":\n")) { 4945245Sbostic _fs_fstab.fs_passno = atoi(cp); 5045245Sbostic return(1); 5145245Sbostic } 5245245Sbostic } 5345245Sbostic } 5445245Sbostic goto bad; 5545245Sbostic } 5645245Sbostic /* OLD_STYLE_FSTAB */ 5738671Smckusick _fs_fstab.fs_spec = strtok(cp, " \t\n"); 5841578Sbostic if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#') 5941578Sbostic continue; 6038671Smckusick _fs_fstab.fs_file = strtok((char *)NULL, " \t\n"); 6138671Smckusick _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n"); 6238671Smckusick _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n"); 6338671Smckusick if (_fs_fstab.fs_mntops == NULL) 6438671Smckusick goto bad; 6538671Smckusick _fs_fstab.fs_freq = 0; 6638671Smckusick _fs_fstab.fs_passno = 0; 6738671Smckusick if ((cp = strtok((char *)NULL, " \t\n")) != NULL) { 6838671Smckusick _fs_fstab.fs_freq = atoi(cp); 6938671Smckusick if ((cp = strtok((char *)NULL, " \t\n")) != NULL) 7038671Smckusick _fs_fstab.fs_passno = atoi(cp); 7138671Smckusick } 7238671Smckusick strcpy(subline, _fs_fstab.fs_mntops); 7338697Smckusick for (typexx = 0, cp = strtok(subline, ","); cp; 7438671Smckusick cp = strtok((char *)NULL, ",")) { 7538671Smckusick if (strlen(cp) != 2) 7635370Sbostic continue; 7738671Smckusick if (!strcmp(cp, FSTAB_RW)) { 7838671Smckusick _fs_fstab.fs_type = FSTAB_RW; 7938671Smckusick break; 8038110Sbostic } 8138671Smckusick if (!strcmp(cp, FSTAB_RQ)) { 8238671Smckusick _fs_fstab.fs_type = FSTAB_RQ; 8338671Smckusick break; 8438671Smckusick } 8538671Smckusick if (!strcmp(cp, FSTAB_RO)) { 8638671Smckusick _fs_fstab.fs_type = FSTAB_RO; 8738671Smckusick break; 8838671Smckusick } 8938671Smckusick if (!strcmp(cp, FSTAB_SW)) { 9038671Smckusick _fs_fstab.fs_type = FSTAB_SW; 9138671Smckusick break; 9238671Smckusick } 9338671Smckusick if (!strcmp(cp, FSTAB_XX)) { 9438671Smckusick _fs_fstab.fs_type = FSTAB_XX; 9538697Smckusick typexx++; 9638671Smckusick break; 9738671Smckusick } 9835370Sbostic } 9938697Smckusick if (typexx) 10038697Smckusick continue; 10138671Smckusick if (cp != NULL) 10238671Smckusick return(1); 10345245Sbostic 10445245Sbostic bad: /* no way to distinguish between EOF and syntax error */ 105*45646Sbostic write(EBADFORMAT); 1062010Swnj } 10735370Sbostic /* NOTREACHED */ 1082010Swnj } 1092010Swnj 11012597Ssam struct fstab * 11112597Ssam getfsent() 1122010Swnj { 11336214Sbostic if (!_fs_fp && !setfsent() || !fstabscan()) 11435370Sbostic return((struct fstab *)NULL); 11536214Sbostic return(&_fs_fstab); 1162010Swnj } 11712597Ssam 11812597Ssam struct fstab * 11912597Ssam getfsspec(name) 12035370Sbostic register char *name; 1212010Swnj { 12236214Sbostic if (setfsent()) 12336214Sbostic while (fstabscan()) 12436214Sbostic if (!strcmp(_fs_fstab.fs_spec, name)) 12536214Sbostic return(&_fs_fstab); 12635370Sbostic return((struct fstab *)NULL); 1272010Swnj } 12812597Ssam 12912597Ssam struct fstab * 13012597Ssam getfsfile(name) 13135370Sbostic register char *name; 1322010Swnj { 13336214Sbostic if (setfsent()) 13436214Sbostic while (fstabscan()) 13536214Sbostic if (!strcmp(_fs_fstab.fs_file, name)) 13636214Sbostic return(&_fs_fstab); 13735370Sbostic return((struct fstab *)NULL); 1382010Swnj } 13936214Sbostic 14036214Sbostic setfsent() 14136214Sbostic { 14236214Sbostic if (_fs_fp) { 14336214Sbostic rewind(_fs_fp); 14436214Sbostic return(1); 14536214Sbostic } 146*45646Sbostic if (_fs_fp = fopen(_PATH_FSTAB, "r")) 147*45646Sbostic return(1); 148*45646Sbostic error(errno); 149*45646Sbostic return(0); 15036214Sbostic } 15136214Sbostic 15236214Sbostic void 15336214Sbostic endfsent() 15436214Sbostic { 15536214Sbostic if (_fs_fp) { 15636214Sbostic (void)fclose(_fs_fp); 15736214Sbostic _fs_fp = NULL; 15836214Sbostic } 15936214Sbostic } 160*45646Sbostic 161*45646Sbostic static 162*45646Sbostic error(err) 163*45646Sbostic int err; 164*45646Sbostic { 165*45646Sbostic char *p; 166*45646Sbostic 167*45646Sbostic (void)write(STDERR_FILENO, "fstab: ", 7); 168*45646Sbostic (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1); 169*45646Sbostic p = strerror(err); 170*45646Sbostic (void)write(STDERR_FILENO, p, strlen(p)); 171*45646Sbostic (void)write(STDERR_FILENO, "\n", 1); 172*45646Sbostic } 173