Correct incorrect bug number ordering in resolved bug list.
[glibc.git] / misc / mntent_r.c
blob43f3a980a411380e4d7fd257bf4877a273a7c245
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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <alloca.h>
21 #include <mntent.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <string.h>
25 #include <sys/types.h>
27 #define flockfile(s) _IO_flockfile (s)
28 #define funlockfile(s) _IO_funlockfile (s)
30 #undef __setmntent
31 #undef __endmntent
32 #undef __getmntent_r
34 /* Prepare to begin reading and/or writing mount table entries from the
35 beginning of FILE. MODE is as for `fopen'. */
36 FILE *
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);
46 if (result != NULL)
47 /* We do the locking ourselves. */
48 __fsetlocking (result, FSETLOCKING_BYCALLER);
50 return result;
52 INTDEF(__setmntent)
53 weak_alias (__setmntent, setmntent)
56 /* Close a stream opened with `setmntent'. */
57 int
58 __endmntent (FILE *stream)
60 if (stream) /* SunOS 4.x allows for NULL stream */
61 fclose (stream);
62 return 1; /* SunOS 4.x says to always return 1 */
64 INTDEF(__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. */
72 static char *
73 decode_name (char *buf)
75 char *rp = buf;
76 char *wp = buf;
79 if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
81 /* \040 is a SPACE. */
82 *wp++ = ' ';
83 rp += 3;
85 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
87 /* \011 is a TAB. */
88 *wp++ = '\t';
89 rp += 3;
91 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
93 /* \012 is a NEWLINE. */
94 *wp++ = '\n';
95 rp += 3;
97 else if (rp[0] == '\\' && rp[1] == '\\')
99 /* We have to escape \\ to be able to represent all characters. */
100 *wp++ = '\\';
101 rp += 1;
103 else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
105 /* \134 is also \\. */
106 *wp++ = '\\';
107 rp += 3;
109 else
110 *wp++ = *rp;
111 while (*rp++ != '\0');
113 return buf;
117 /* Read one mount table entry from STREAM. Returns a pointer to storage
118 reused on the next call, or null for EOF or error (use feof/ferror to
119 check). */
120 struct mntent *
121 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
123 char *cp;
124 char *head;
126 flockfile (stream);
129 char *end_ptr;
131 if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
133 funlockfile (stream);
134 return NULL;
137 end_ptr = strchr (buffer, '\n');
138 if (end_ptr != NULL) /* chop newline */
139 *end_ptr = '\0';
140 else
142 /* Not the whole line was read. Do it now but forget it. */
143 char tmp[1024];
144 while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
145 if (strchr (tmp, '\n') != NULL)
146 break;
149 head = buffer + strspn (buffer, " \t");
150 /* skip empty lines and comment lines: */
152 while (head[0] == '\0' || head[0] == '#');
154 cp = __strsep (&head, " \t");
155 mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
156 if (head)
157 head += strspn (head, " \t");
158 cp = __strsep (&head, " \t");
159 mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
160 if (head)
161 head += strspn (head, " \t");
162 cp = __strsep (&head, " \t");
163 mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
164 if (head)
165 head += strspn (head, " \t");
166 cp = __strsep (&head, " \t");
167 mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
168 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
170 case 0:
171 mp->mnt_freq = 0;
172 case 1:
173 mp->mnt_passno = 0;
174 case 2:
175 break;
177 funlockfile (stream);
179 return mp;
181 INTDEF(__getmntent_r)
182 weak_alias (__getmntent_r, getmntent_r)
185 /* We have to use an encoding for names if they contain spaces or tabs.
186 To be able to represent all characters we also have to escape the
187 backslash itself. This "function" must be a macro since we use
188 `alloca'. */
189 #define encode_name(name) \
190 do { \
191 const char *rp = name; \
193 while (*rp != '\0') \
194 if (*rp == ' ' || *rp == '\t' || *rp == '\n' || *rp == '\\') \
195 break; \
196 else \
197 ++rp; \
199 if (*rp != '\0') \
201 /* In the worst case the length of the string can increase to \
202 four times the current length. */ \
203 char *wp; \
205 rp = name; \
206 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
208 do \
209 if (*rp == ' ') \
211 *wp++ = '\\'; \
212 *wp++ = '0'; \
213 *wp++ = '4'; \
214 *wp++ = '0'; \
216 else if (*rp == '\t') \
218 *wp++ = '\\'; \
219 *wp++ = '0'; \
220 *wp++ = '1'; \
221 *wp++ = '1'; \
223 else if (*rp == '\n') \
225 *wp++ = '\\'; \
226 *wp++ = '0'; \
227 *wp++ = '1'; \
228 *wp++ = '2'; \
230 else if (*rp == '\\') \
232 *wp++ = '\\'; \
233 *wp++ = '\\'; \
235 else \
236 *wp++ = *rp; \
237 while (*rp++ != '\0'); \
239 } while (0)
242 /* Write the mount table entry described by MNT to STREAM.
243 Return zero on success, nonzero on failure. */
245 __addmntent (FILE *stream, const struct mntent *mnt)
247 struct mntent mntcopy = *mnt;
248 if (fseek (stream, 0, SEEK_END))
249 return 1;
251 /* Encode spaces and tabs in the names. */
252 encode_name (mntcopy.mnt_fsname);
253 encode_name (mntcopy.mnt_dir);
254 encode_name (mntcopy.mnt_type);
255 encode_name (mntcopy.mnt_opts);
257 return (fprintf (stream, "%s %s %s %s %d %d\n",
258 mntcopy.mnt_fsname,
259 mntcopy.mnt_dir,
260 mntcopy.mnt_type,
261 mntcopy.mnt_opts,
262 mntcopy.mnt_freq,
263 mntcopy.mnt_passno) < 0
264 || fflush (stream) != 0);
266 weak_alias (__addmntent, addmntent)
269 /* Search MNT->mnt_opts for an option matching OPT.
270 Returns the address of the substring, or null if none found. */
271 char *
272 __hasmntopt (const struct mntent *mnt, const char *opt)
274 const size_t optlen = strlen (opt);
275 char *rest = mnt->mnt_opts, *p;
277 while ((p = strstr (rest, opt)) != NULL)
279 if ((p == rest || p[-1] == ',')
280 && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
281 return p;
283 rest = strchr (p, ',');
284 if (rest == NULL)
285 break;
286 ++rest;
289 return NULL;
291 weak_alias (__hasmntopt, hasmntopt)