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.
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.
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.
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 <https://www.gnu.org/licenses/>. */
23 #include <stdio_ext.h>
25 #include <sys/types.h>
27 #define flockfile(s) _IO_flockfile (s)
28 #define funlockfile(s) _IO_funlockfile (s)
34 /* Prepare to begin reading and/or writing mount table entries from the
35 beginning of FILE. MODE is as for `fopen'. */
37 __setmntent (const char *file
, const char *mode
)
39 /* Extend the mode parameter with "c" to disable cancellation in the
40 I/O functions and "e" to set FD_CLOEXEC. */
41 size_t modelen
= strlen (mode
);
42 char newmode
[modelen
+ 3];
43 memcpy (mempcpy (newmode
, mode
, modelen
), "ce", 3);
44 FILE *result
= fopen (file
, newmode
);
47 /* We do the locking ourselves. */
48 __fsetlocking (result
, FSETLOCKING_BYCALLER
);
52 libc_hidden_def (__setmntent
)
53 weak_alias (__setmntent
, setmntent
)
56 /* Close a stream opened with `setmntent'. */
58 __endmntent (FILE *stream
)
60 if (stream
) /* SunOS 4.x allows for NULL stream */
62 return 1; /* SunOS 4.x says to always return 1 */
64 libc_hidden_def (__endmntent
)
65 weak_alias (__endmntent
, endmntent
)
68 /* Since the values in a line are separated by spaces, a name cannot
69 contain a space. Therefore some programs encode spaces in names
70 by the strings "\040". We undo the encoding when reading an entry.
71 The decoding happens in place. */
73 decode_name (char *buf
)
79 if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '4' && rp
[3] == '0')
81 /* \040 is a SPACE. */
85 else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' && rp
[3] == '1')
91 else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' && rp
[3] == '2')
93 /* \012 is a NEWLINE. */
97 else if (rp
[0] == '\\' && rp
[1] == '\\')
99 /* We have to escape \\ to be able to represent all characters. */
103 else if (rp
[0] == '\\' && rp
[1] == '1' && rp
[2] == '3' && rp
[3] == '4')
105 /* \134 is also \\. */
111 while (*rp
++ != '\0');
117 get_mnt_entry (FILE *stream
, struct mntent
*mp
, char *buffer
, int bufsiz
)
126 if (__fgets_unlocked (buffer
, bufsiz
, stream
) == NULL
)
129 end_ptr
= strchr (buffer
, '\n');
130 if (end_ptr
!= NULL
) /* chop newline */
132 /* Do not walk past the start of buffer if it's all whitespace. */
133 while (end_ptr
!= buffer
134 && (end_ptr
[-1] == ' ' || end_ptr
[-1] == '\t'))
140 /* Not the whole line was read. Do it now but forget it. */
142 while (__fgets_unlocked (tmp
, sizeof tmp
, stream
) != NULL
)
143 if (strchr (tmp
, '\n') != NULL
)
147 head
= buffer
+ strspn (buffer
, " \t");
148 /* skip empty lines and comment lines: */
150 while (head
[0] == '\0' || head
[0] == '#');
152 cp
= __strsep (&head
, " \t");
153 mp
->mnt_fsname
= cp
!= NULL
? decode_name (cp
) : (char *) "";
155 head
+= strspn (head
, " \t");
156 cp
= __strsep (&head
, " \t");
157 mp
->mnt_dir
= cp
!= NULL
? decode_name (cp
) : (char *) "";
159 head
+= strspn (head
, " \t");
160 cp
= __strsep (&head
, " \t");
161 mp
->mnt_type
= cp
!= NULL
? decode_name (cp
) : (char *) "";
163 head
+= strspn (head
, " \t");
164 cp
= __strsep (&head
, " \t");
165 mp
->mnt_opts
= cp
!= NULL
? decode_name (cp
) : (char *) "";
166 switch (head
? sscanf (head
, " %d %d ", &mp
->mnt_freq
, &mp
->mnt_passno
) : 0)
181 /* Read one mount table entry from STREAM. Returns a pointer to storage
182 reused on the next call, or null for EOF or error (use feof/ferror to
185 __getmntent_r (FILE *stream
, struct mntent
*mp
, char *buffer
, int bufsiz
)
187 struct mntent
*result
;
191 if (get_mnt_entry (stream
, mp
, buffer
, bufsiz
))
193 /* If the file system is autofs look for a mount option hint
194 ("ignore") to skip the entry. */
195 if (strcmp (mp
->mnt_type
, "autofs") == 0 && __hasmntopt (mp
, "ignore"))
196 memset (mp
, 0, sizeof (*mp
));
208 funlockfile (stream
);
212 libc_hidden_def (__getmntent_r
)
213 weak_alias (__getmntent_r
, getmntent_r
)
216 /* We have to use an encoding for names if they contain spaces or tabs.
217 To be able to represent all characters we also have to escape the
218 backslash itself. This "function" must be a macro since we use
220 #define encode_name(name) \
222 const char *rp = name; \
224 while (*rp != '\0') \
225 if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\') \
232 /* In the worst case the length of the string can increase to \
233 four times the current length. */ \
237 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
247 else if (*rp == '\t') \
254 else if (*rp == '\n') \
261 else if (*rp == '\\') \
268 while (*rp++ != '\0'); \
273 /* Write the mount table entry described by MNT to STREAM.
274 Return zero on success, nonzero on failure. */
276 __addmntent (FILE *stream
, const struct mntent
*mnt
)
278 struct mntent mntcopy
= *mnt
;
279 if (fseek (stream
, 0, SEEK_END
))
282 /* Encode spaces and tabs in the names. */
283 encode_name (mntcopy
.mnt_fsname
);
284 encode_name (mntcopy
.mnt_dir
);
285 encode_name (mntcopy
.mnt_type
);
286 encode_name (mntcopy
.mnt_opts
);
288 return (fprintf (stream
, "%s %s %s %s %d %d\n",
294 mntcopy
.mnt_passno
) < 0
295 || fflush (stream
) != 0);
297 weak_alias (__addmntent
, addmntent
)
300 /* Search MNT->mnt_opts for an option matching OPT.
301 Returns the address of the substring, or null if none found. */
303 __hasmntopt (const struct mntent
*mnt
, const char *opt
)
305 const size_t optlen
= strlen (opt
);
306 char *rest
= mnt
->mnt_opts
, *p
;
308 while ((p
= strstr (rest
, opt
)) != NULL
)
310 if ((p
== rest
|| p
[-1] == ',')
311 && (p
[optlen
] == '\0' || p
[optlen
] == '=' || p
[optlen
] == ','))
314 rest
= strchr (p
, ',');
322 libc_hidden_def (__hasmntopt
)
323 weak_alias (__hasmntopt
, hasmntopt
)