Ken Arnold's version from UNIX Review
[unix-history] / usr / src / lib / libc / gen / fstab.c
CommitLineData
bb0cfa24 1/*
eb8edec4 2 * Copyright (c) 1980, 1988 Regents of the University of California.
19bfcb20
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
bb0cfa24
DF
16 */
17
2ce81398 18#if defined(LIBC_SCCS) && !defined(lint)
eb8edec4 19static char sccsid[] = "@(#)fstab.c 5.4 (Berkeley) %G%";
19bfcb20 20#endif /* LIBC_SCCS and not lint */
02ed2510 21
5b16589f
BJ
22#include <fstab.h>
23#include <stdio.h>
5b16589f 24
02ed2510 25static struct fstab fs;
eb8edec4 26static FILE *fs_file = NULL;
02ed2510 27static char line[BUFSIZ+1];
5b16589f 28
eb8edec4
KB
29static
30fstabscan(fsp)
31 register struct fstab *fsp;
5b16589f 32{
02ed2510 33 register char *cp;
eb8edec4
KB
34 char *fgets(), *strtok();
35
36 for (;;) {
37 if (!(cp = fgets(line, sizeof(line), fs_file)))
38 return(1);
39 fsp->fs_spec = strtok(cp, ":\n");
40 fsp->fs_file = strtok((char *)NULL, ":\n");
41 fsp->fs_type = strtok((char *)NULL, ":\n");
42 if (fsp->fs_type && strcmp(fsp->fs_type, FSTAB_XX)) {
43 if (!(cp = strtok((char *)NULL, ":\n")))
44 continue;
45 fsp->fs_freq = atoi(cp);
46 if (!(cp = strtok((char *)NULL, ":\n")))
47 continue;
48 fsp->fs_passno = atoi(cp);
49 return(0);
50 }
5b16589f 51 }
eb8edec4 52 /* NOTREACHED */
5b16589f
BJ
53}
54
02ed2510 55setfsent()
5b16589f
BJ
56{
57 if (fs_file)
eb8edec4 58 (void)endfsent();
78d4750f 59 if ((fs_file = fopen(FSTAB, "r")) == NULL) {
eb8edec4
KB
60 fs_file = NULL;
61 return(0);
5b16589f 62 }
eb8edec4 63 return(1);
5b16589f
BJ
64}
65
02ed2510 66endfsent()
5b16589f 67{
78d4750f 68 if (fs_file) {
eb8edec4
KB
69 (void)fclose(fs_file);
70 fs_file = NULL;
78d4750f 71 }
eb8edec4 72 return(1);
5b16589f
BJ
73}
74
02ed2510
SL
75struct fstab *
76getfsent()
5b16589f 77{
eb8edec4
KB
78 if (fs_file == NULL && !setfsent() || fstabscan(&fs))
79 return((struct fstab *)NULL);
80 return(&fs);
5b16589f 81}
02ed2510
SL
82
83struct fstab *
84getfsspec(name)
eb8edec4 85 register char *name;
5b16589f 86{
02ed2510
SL
87 register struct fstab *fsp;
88
eb8edec4
KB
89 if (!setfsent()) /* start from the beginning */
90 return((struct fstab *)NULL);
91 while (fsp = getfsent())
92 if (!strcmp(fsp->fs_spec, name))
93 return(fsp);
94 return((struct fstab *)NULL);
5b16589f 95}
02ed2510
SL
96
97struct fstab *
98getfsfile(name)
eb8edec4 99 register char *name;
5b16589f 100{
02ed2510
SL
101 register struct fstab *fsp;
102
eb8edec4
KB
103 if (!setfsent()) /* start from the beginning */
104 return((struct fstab *)NULL);
105 while (fsp = getfsent())
106 if (!strcmp(fsp->fs_file, name))
107 return(fsp);
108 return((struct fstab *)NULL);
5b16589f 109}