1/* Utilities for reading/writing fstab, mtab, etc.
2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <alloca.h>
20#include <mntent.h>
21#include <stdio.h>
22#include <stdio_ext.h>
23#include <string.h>
24#include <sys/types.h>
25
26#define flockfile(s) _IO_flockfile (s)
27#define funlockfile(s) _IO_funlockfile (s)
28
29#undef __setmntent
30#undef __endmntent
31#undef __getmntent_r
32
33/* Prepare to begin reading and/or writing mount table entries from the
34 beginning of FILE. MODE is as for `fopen'. */
35FILE *
36__setmntent (const char *file, const char *mode)
37{
38 /* Extend the mode parameter with "c" to disable cancellation in the
39 I/O functions and "e" to set FD_CLOEXEC. */
40 size_t modelen = strlen (mode);
41 char newmode[modelen + 3];
42 memcpy (mempcpy (newmode, mode, modelen), "ce", 3);
43 FILE *result = fopen (file, newmode);
44
45 if (result != NULL)
46 /* We do the locking ourselves. */
47 __fsetlocking (result, FSETLOCKING_BYCALLER);
48
49 return result;
50}
51libc_hidden_def (__setmntent)
52weak_alias (__setmntent, setmntent)
53
54
55/* Close a stream opened with `setmntent'. */
56int
57__endmntent (FILE *stream)
58{
59 if (stream) /* SunOS 4.x allows for NULL stream */
60 fclose (stream);
61 return 1; /* SunOS 4.x says to always return 1 */
62}
63libc_hidden_def (__endmntent)
64weak_alias (__endmntent, endmntent)
65
66
67/* Since the values in a line are separated by spaces, a name cannot
68 contain a space. Therefore some programs encode spaces in names
69 by the strings "\040". We undo the encoding when reading an entry.
70 The decoding happens in place. */
71static char *
72decode_name (char *buf)
73{
74 char *rp = buf;
75 char *wp = buf;
76
77 do
78 if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
79 {
80 /* \040 is a SPACE. */
81 *wp++ = ' ';
82 rp += 3;
83 }
84 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
85 {
86 /* \011 is a TAB. */
87 *wp++ = '\t';
88 rp += 3;
89 }
90 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
91 {
92 /* \012 is a NEWLINE. */
93 *wp++ = '\n';
94 rp += 3;
95 }
96 else if (rp[0] == '\\' && rp[1] == '\\')
97 {
98 /* We have to escape \\ to be able to represent all characters. */
99 *wp++ = '\\';
100 rp += 1;
101 }
102 else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
103 {
104 /* \134 is also \\. */
105 *wp++ = '\\';
106 rp += 3;
107 }
108 else
109 *wp++ = *rp;
110 while (*rp++ != '\0');
111
112 return buf;
113}
114
115
116/* Read one mount table entry from STREAM. Returns a pointer to storage
117 reused on the next call, or null for EOF or error (use feof/ferror to
118 check). */
119struct mntent *
120__getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
121{
122 char *cp;
123 char *head;
124
125 flockfile (stream);
126 do
127 {
128 char *end_ptr;
129
130 if (__fgets_unlocked (buffer, bufsiz, stream) == NULL)
131 {
132 funlockfile (stream);
133 return NULL;
134 }
135
136 end_ptr = strchr (buffer, '\n');
137 if (end_ptr != NULL) /* chop newline */
138 {
139 /* Do not walk past the start of buffer if it's all whitespace. */
140 while (end_ptr != buffer
141 && (end_ptr[-1] == ' ' || end_ptr[-1] == '\t'))
142 end_ptr--;
143 *end_ptr = '\0';
144 }
145 else
146 {
147 /* Not the whole line was read. Do it now but forget it. */
148 char tmp[1024];
149 while (__fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
150 if (strchr (tmp, '\n') != NULL)
151 break;
152 }
153
154 head = buffer + strspn (buffer, " \t");
155 /* skip empty lines and comment lines: */
156 }
157 while (head[0] == '\0' || head[0] == '#');
158
159 cp = __strsep (&head, " \t");
160 mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
161 if (head)
162 head += strspn (head, " \t");
163 cp = __strsep (&head, " \t");
164 mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
165 if (head)
166 head += strspn (head, " \t");
167 cp = __strsep (&head, " \t");
168 mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
169 if (head)
170 head += strspn (head, " \t");
171 cp = __strsep (&head, " \t");
172 mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
173 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
174 {
175 case 0:
176 mp->mnt_freq = 0;
177 /* Fall through. */
178 case 1:
179 mp->mnt_passno = 0;
180 /* Fall through. */
181 case 2:
182 break;
183 }
184 funlockfile (stream);
185
186 return mp;
187}
188libc_hidden_def (__getmntent_r)
189weak_alias (__getmntent_r, getmntent_r)
190
191
192/* We have to use an encoding for names if they contain spaces or tabs.
193 To be able to represent all characters we also have to escape the
194 backslash itself. This "function" must be a macro since we use
195 `alloca'. */
196#define encode_name(name) \
197 do { \
198 const char *rp = name; \
199 \
200 while (*rp != '\0') \
201 if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\') \
202 break; \
203 else \
204 ++rp; \
205 \
206 if (*rp != '\0') \
207 { \
208 /* In the worst case the length of the string can increase to \
209 four times the current length. */ \
210 char *wp; \
211 \
212 rp = name; \
213 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
214 \
215 do \
216 if (*rp == ' ') \
217 { \
218 *wp++ = '\\'; \
219 *wp++ = '0'; \
220 *wp++ = '4'; \
221 *wp++ = '0'; \
222 } \
223 else if (*rp == '\t') \
224 { \
225 *wp++ = '\\'; \
226 *wp++ = '0'; \
227 *wp++ = '1'; \
228 *wp++ = '1'; \
229 } \
230 else if (*rp == '\n') \
231 { \
232 *wp++ = '\\'; \
233 *wp++ = '0'; \
234 *wp++ = '1'; \
235 *wp++ = '2'; \
236 } \
237 else if (*rp == '\\') \
238 { \
239 *wp++ = '\\'; \
240 *wp++ = '\\'; \
241 } \
242 else \
243 *wp++ = *rp; \
244 while (*rp++ != '\0'); \
245 } \
246 } while (0)
247
248
249/* Write the mount table entry described by MNT to STREAM.
250 Return zero on success, nonzero on failure. */
251int
252__addmntent (FILE *stream, const struct mntent *mnt)
253{
254 struct mntent mntcopy = *mnt;
255 if (fseek (stream, 0, SEEK_END))
256 return 1;
257
258 /* Encode spaces and tabs in the names. */
259 encode_name (mntcopy.mnt_fsname);
260 encode_name (mntcopy.mnt_dir);
261 encode_name (mntcopy.mnt_type);
262 encode_name (mntcopy.mnt_opts);
263
264 return (fprintf (stream, "%s %s %s %s %d %d\n",
265 mntcopy.mnt_fsname,
266 mntcopy.mnt_dir,
267 mntcopy.mnt_type,
268 mntcopy.mnt_opts,
269 mntcopy.mnt_freq,
270 mntcopy.mnt_passno) < 0
271 || fflush (stream) != 0);
272}
273weak_alias (__addmntent, addmntent)
274
275
276/* Search MNT->mnt_opts for an option matching OPT.
277 Returns the address of the substring, or null if none found. */
278char *
279__hasmntopt (const struct mntent *mnt, const char *opt)
280{
281 const size_t optlen = strlen (opt);
282 char *rest = mnt->mnt_opts, *p;
283
284 while ((p = strstr (rest, opt)) != NULL)
285 {
286 if ((p == rest || p[-1] == ',')
287 && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
288 return p;
289
290 rest = strchr (p, ',');
291 if (rest == NULL)
292 break;
293 ++rest;
294 }
295
296 return NULL;
297}
298libc_hidden_def (__hasmntopt)
299weak_alias (__hasmntopt, hasmntopt)
300