Remove posix/glob directory (standalone glob build support).
[glibc.git] / misc / mntent_r.c
blob3b5418a193736854df9ad8a08e3380367c4fc767
1 /* Utilities for reading/writing fstab, mtab, etc.
2 Copyright (C) 1995-2000, 2001, 2002, 2003, 2006, 2010, 2011
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <alloca.h>
22 #include <mntent.h>
23 #include <stdio.h>
24 #include <stdio_ext.h>
25 #include <string.h>
26 #include <sys/types.h>
28 #define flockfile(s) _IO_flockfile (s)
29 #define funlockfile(s) _IO_funlockfile (s)
31 #undef __setmntent
32 #undef __endmntent
33 #undef __getmntent_r
35 /* Prepare to begin reading and/or writing mount table entries from the
36 beginning of FILE. MODE is as for `fopen'. */
37 FILE *
38 __setmntent (const char *file, const char *mode)
40 /* Extend the mode parameter with "c" to disable cancellation in the
41 I/O functions and "e" to set FD_CLOEXEC. */
42 size_t modelen = strlen (mode);
43 char newmode[modelen + 3];
44 memcpy (mempcpy (newmode, mode, modelen), "ce", 3);
45 FILE *result = fopen (file, newmode);
47 if (result != NULL)
48 /* We do the locking ourselves. */
49 __fsetlocking (result, FSETLOCKING_BYCALLER);
51 return result;
53 INTDEF(__setmntent)
54 weak_alias (__setmntent, setmntent)
57 /* Close a stream opened with `setmntent'. */
58 int
59 __endmntent (FILE *stream)
61 if (stream) /* SunOS 4.x allows for NULL stream */
62 fclose (stream);
63 return 1; /* SunOS 4.x says to always return 1 */
65 INTDEF(__endmntent)
66 weak_alias (__endmntent, endmntent)
69 /* Since the values in a line are separated by spaces, a name cannot
70 contain a space. Therefore some programs encode spaces in names
71 by the strings "\040". We undo the encoding when reading an entry.
72 The decoding happens in place. */
73 static char *
74 decode_name (char *buf)
76 char *rp = buf;
77 char *wp = buf;
80 if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
82 /* \040 is a SPACE. */
83 *wp++ = ' ';
84 rp += 3;
86 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
88 /* \011 is a TAB. */
89 *wp++ = '\t';
90 rp += 3;
92 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
94 /* \012 is a NEWLINE. */
95 *wp++ = '\n';
96 rp += 3;
98 else if (rp[0] == '\\' && rp[1] == '\\')
100 /* We have to escape \\ to be able to represent all characters. */
101 *wp++ = '\\';
102 rp += 1;
104 else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
106 /* \134 is also \\. */
107 *wp++ = '\\';
108 rp += 3;
110 else
111 *wp++ = *rp;
112 while (*rp++ != '\0');
114 return buf;
118 /* Read one mount table entry from STREAM. Returns a pointer to storage
119 reused on the next call, or null for EOF or error (use feof/ferror to
120 check). */
121 struct mntent *
122 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
124 char *cp;
125 char *head;
127 flockfile (stream);
130 char *end_ptr;
132 if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
134 funlockfile (stream);
135 return NULL;
138 end_ptr = strchr (buffer, '\n');
139 if (end_ptr != NULL) /* chop newline */
140 *end_ptr = '\0';
141 else
143 /* Not the whole line was read. Do it now but forget it. */
144 char tmp[1024];
145 while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
146 if (strchr (tmp, '\n') != NULL)
147 break;
150 head = buffer + strspn (buffer, " \t");
151 /* skip empty lines and comment lines: */
153 while (head[0] == '\0' || head[0] == '#');
155 cp = __strsep (&head, " \t");
156 mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
157 if (head)
158 head += strspn (head, " \t");
159 cp = __strsep (&head, " \t");
160 mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
161 if (head)
162 head += strspn (head, " \t");
163 cp = __strsep (&head, " \t");
164 mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
165 if (head)
166 head += strspn (head, " \t");
167 cp = __strsep (&head, " \t");
168 mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
169 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
171 case 0:
172 mp->mnt_freq = 0;
173 case 1:
174 mp->mnt_passno = 0;
175 case 2:
176 break;
178 funlockfile (stream);
180 return mp;
182 INTDEF(__getmntent_r)
183 weak_alias (__getmntent_r, getmntent_r)
186 /* We have to use an encoding for names if they contain spaces or tabs.
187 To be able to represent all characters we also have to escape the
188 backslash itself. This "function" must be a macro since we use
189 `alloca'. */
190 #define encode_name(name) \
191 do { \
192 const char *rp = name; \
194 while (*rp != '\0') \
195 if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\') \
196 break; \
197 else \
198 ++rp; \
200 if (*rp != '\0') \
202 /* In the worst case the length of the string can increase to \
203 four times the current length. */ \
204 char *wp; \
206 rp = name; \
207 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
209 do \
210 if (*rp == ' ') \
212 *wp++ = '\\'; \
213 *wp++ = '0'; \
214 *wp++ = '4'; \
215 *wp++ = '0'; \
217 else if (*rp == '\t') \
219 *wp++ = '\\'; \
220 *wp++ = '0'; \
221 *wp++ = '1'; \
222 *wp++ = '1'; \
224 else if (*rp == '\n') \
226 *wp++ = '\\'; \
227 *wp++ = '0'; \
228 *wp++ = '1'; \
229 *wp++ = '2'; \
231 else if (*rp == '\\') \
233 *wp++ = '\\'; \
234 *wp++ = '\\'; \
236 else \
237 *wp++ = *rp; \
238 while (*rp++ != '\0'); \
240 } while (0)
243 /* Write the mount table entry described by MNT to STREAM.
244 Return zero on success, nonzero on failure. */
246 __addmntent (FILE *stream, const struct mntent *mnt)
248 struct mntent mntcopy = *mnt;
249 if (fseek (stream, 0, SEEK_END))
250 return 1;
252 /* Encode spaces and tabs in the names. */
253 encode_name (mntcopy.mnt_fsname);
254 encode_name (mntcopy.mnt_dir);
255 encode_name (mntcopy.mnt_type);
256 encode_name (mntcopy.mnt_opts);
258 return (fprintf (stream, "%s %s %s %s %d %d\n",
259 mntcopy.mnt_fsname,
260 mntcopy.mnt_dir,
261 mntcopy.mnt_type,
262 mntcopy.mnt_opts,
263 mntcopy.mnt_freq,
264 mntcopy.mnt_passno) < 0
265 || fflush (stream) != 0);
267 weak_alias (__addmntent, addmntent)
270 /* Search MNT->mnt_opts for an option matching OPT.
271 Returns the address of the substring, or null if none found. */
272 char *
273 __hasmntopt (const struct mntent *mnt, const char *opt)
275 const size_t optlen = strlen (opt);
276 char *rest = mnt->mnt_opts, *p;
278 while ((p = strstr (rest, opt)) != NULL)
280 if ((p == rest || p[-1] == ',')
281 && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
282 return p;
284 rest = strchr (p, ',');
285 if (rest == NULL)
286 break;
287 ++rest;
290 return NULL;
292 weak_alias (__hasmntopt, hasmntopt)