]> git.wh0rd.org - dump.git/blob - compat/lib/fstab.c
cybercable -> noos.
[dump.git] / compat / lib / fstab.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: fstab.c,v 1.9 2000/12/04 15:43:16 stelian Exp $";
45 #endif /* not lint */
46
47 #include <errno.h>
48 #include <fstab.h>
49 #include <mntent.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <bylabel.h>
55
56 static FILE *_fs_fp;
57 static struct fstab _fs_fstab;
58
59 static void error __P((int));
60 static int fstabscan __P((void));
61
62 void msg __P((const char *fmt, ...));
63
64 static
65 int fstabscan(void)
66 {
67 struct mntent *mnt;
68 register char *cp;
69 int typexx;
70 #define MAXLINELENGTH 1024
71 char subline[MAXLINELENGTH];
72 char *device_name;
73
74 for (;;) {
75 if (!(mnt = getmntent(_fs_fp)))
76 return 0;
77
78 device_name = get_device_name(mnt->mnt_fsname);
79 if (!device_name) {
80 msg("Warning: unable to translate %s\n", mnt->mnt_fsname);
81 continue;
82 }
83 _fs_fstab.fs_spec = device_name;
84 _fs_fstab.fs_file = mnt->mnt_dir;
85 _fs_fstab.fs_vfstype = mnt->mnt_type;
86 _fs_fstab.fs_mntops = mnt->mnt_opts;
87 _fs_fstab.fs_type = FSTAB_RW; /* rw by default under Linux */
88 _fs_fstab.fs_freq = mnt->mnt_freq;
89 _fs_fstab.fs_passno = mnt->mnt_passno;
90
91 strcpy(subline, _fs_fstab.fs_mntops);
92 for (typexx = 0, cp = strtok(subline, ","); cp;
93 cp = strtok((char *)NULL, ",")) {
94 if (!strcmp(cp, FSTAB_RW)) {
95 _fs_fstab.fs_type = FSTAB_RW;
96 break;
97 }
98 if (!strcmp(cp, FSTAB_RQ)) {
99 _fs_fstab.fs_type = FSTAB_RQ;
100 break;
101 }
102 if (!strcmp(cp, FSTAB_RO)) {
103 _fs_fstab.fs_type = FSTAB_RO;
104 break;
105 }
106 if (!strcmp(cp, FSTAB_SW)) {
107 _fs_fstab.fs_type = FSTAB_SW;
108 break;
109 }
110 if (!strcmp(cp, FSTAB_XX)) {
111 _fs_fstab.fs_type = FSTAB_XX;
112 typexx++;
113 break;
114 }
115 }
116 if (typexx)
117 continue;
118
119 return 1;
120 }
121 }
122
123 struct fstab *
124 getfsent(void)
125 {
126 if ((!_fs_fp && !setfsent()) || !fstabscan())
127 return((struct fstab *)NULL);
128 return(&_fs_fstab);
129 }
130
131 struct fstab *
132 getfsspec(const char *name)
133 {
134 if (setfsent())
135 while (fstabscan())
136 if (!strcmp(_fs_fstab.fs_spec, name))
137 return(&_fs_fstab);
138 return((struct fstab *)NULL);
139 }
140
141 struct fstab *
142 getfsfile(const char *name)
143 {
144 if (setfsent())
145 while (fstabscan())
146 if (!strcmp(_fs_fstab.fs_file, name))
147 return(&_fs_fstab);
148 return((struct fstab *)NULL);
149 }
150
151 int
152 setfsent(void)
153 {
154 if (_fs_fp) {
155 rewind(_fs_fp);
156 return(1);
157 }
158 if ((_fs_fp = setmntent(_PATH_FSTAB, "r")))
159 return(1);
160 error(errno);
161 return(0);
162 }
163
164 void
165 endfsent(void)
166 {
167 if (_fs_fp) {
168 (void)endmntent(_fs_fp);
169 _fs_fp = NULL;
170 }
171 }
172
173 static
174 void error(int err)
175 {
176 char *p;
177
178 (void)write(STDERR_FILENO, "fstab: ", 7);
179 (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
180 (void)write(STDERR_FILENO, ": ", 1);
181 p = strerror(err);
182 (void)write(STDERR_FILENO, p, strlen(p));
183 (void)write(STDERR_FILENO, "\n", 1);
184 }