]> git.wh0rd.org - dump.git/blame - compat/lib/fstab.c
Added CVS Id.
[dump.git] / compat / lib / fstab.c
CommitLineData
1227625a
SP
1/*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
b45f51d6
SP
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
1227625a
SP
6 *
7 */
8
9/*
10 * Copyright (c) 1980, 1988, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
ec387a12
SP
40 *
41 * $Id: fstab.c,v 1.4 1999/10/11 13:31:10 stelian Exp $
1227625a
SP
42 */
43
1227625a
SP
44#include <errno.h>
45#include <fstab.h>
46#include <mntent.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51
52static FILE *_fs_fp;
53static struct fstab _fs_fstab;
54
55static void error __P((int));
56static int fstabscan __P((void));
57
58static
ddd2ef55 59int fstabscan(void)
1227625a
SP
60{
61 struct mntent *mnt;
62 register char *cp;
63 int typexx;
64#define MAXLINELENGTH 1024
65 char subline[MAXLINELENGTH];
66
67 for (;;) {
68 if (!(mnt = getmntent(_fs_fp)))
69 return 0;
70
71 _fs_fstab.fs_spec = mnt->mnt_fsname;
72 _fs_fstab.fs_file = mnt->mnt_dir;
73 _fs_fstab.fs_vfstype = mnt->mnt_type;
74 _fs_fstab.fs_mntops = mnt->mnt_opts;
75 _fs_fstab.fs_type = FSTAB_RW; /* rw by default under Linux */
76 _fs_fstab.fs_freq = mnt->mnt_freq;
77 _fs_fstab.fs_passno = mnt->mnt_passno;
78
79 strcpy(subline, _fs_fstab.fs_mntops);
80 for (typexx = 0, cp = strtok(subline, ","); cp;
81 cp = strtok((char *)NULL, ",")) {
82 if (!strcmp(cp, FSTAB_RW)) {
83 _fs_fstab.fs_type = FSTAB_RW;
84 break;
85 }
86 if (!strcmp(cp, FSTAB_RQ)) {
87 _fs_fstab.fs_type = FSTAB_RQ;
88 break;
89 }
90 if (!strcmp(cp, FSTAB_RO)) {
91 _fs_fstab.fs_type = FSTAB_RO;
92 break;
93 }
94 if (!strcmp(cp, FSTAB_SW)) {
95 _fs_fstab.fs_type = FSTAB_SW;
96 break;
97 }
98 if (!strcmp(cp, FSTAB_XX)) {
99 _fs_fstab.fs_type = FSTAB_XX;
100 typexx++;
101 break;
102 }
103 }
104 if (typexx)
105 continue;
106
107 return 1;
108 }
109}
110
111struct fstab *
ddd2ef55 112getfsent(void)
1227625a 113{
b45f51d6 114 if ((!_fs_fp && !setfsent()) || !fstabscan())
1227625a
SP
115 return((struct fstab *)NULL);
116 return(&_fs_fstab);
117}
118
119struct fstab *
ddd2ef55 120getfsspec(const char *name)
1227625a
SP
121{
122 if (setfsent())
123 while (fstabscan())
124 if (!strcmp(_fs_fstab.fs_spec, name))
125 return(&_fs_fstab);
126 return((struct fstab *)NULL);
127}
128
129struct fstab *
ddd2ef55 130getfsfile(const char *name)
1227625a
SP
131{
132 if (setfsent())
133 while (fstabscan())
134 if (!strcmp(_fs_fstab.fs_file, name))
135 return(&_fs_fstab);
136 return((struct fstab *)NULL);
137}
138
ddd2ef55
SP
139int
140setfsent(void)
1227625a
SP
141{
142 if (_fs_fp) {
143 rewind(_fs_fp);
144 return(1);
145 }
146 if ((_fs_fp = setmntent(_PATH_FSTAB, "r")))
147 return(1);
148 error(errno);
149 return(0);
150}
151
152void
ddd2ef55 153endfsent(void)
1227625a
SP
154{
155 if (_fs_fp) {
156 (void)endmntent(_fs_fp);
157 _fs_fp = NULL;
158 }
159}
160
161static
ddd2ef55 162void error(int err)
1227625a
SP
163{
164 char *p;
165
166 (void)write(STDERR_FILENO, "fstab: ", 7);
167 (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
168 (void)write(STDERR_FILENO, ": ", 1);
169 p = strerror(err);
170 (void)write(STDERR_FILENO, p, strlen(p));
171 (void)write(STDERR_FILENO, "\n", 1);
172}