[BZ #406]
[glibc.git] / misc / mntent_r.c
blob1476c86ee2da183006b2a947b8c319afee2fb89a
1 /* Utilities for reading/writing fstab, mtab, etc.
2 Copyright (C) 1995-2000, 2001, 2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 #ifdef USE_IN_LIBIO
28 # define flockfile(s) _IO_flockfile (s)
29 # define funlockfile(s) _IO_funlockfile (s)
30 #endif
32 #undef __setmntent
33 #undef __endmntent
34 #undef __getmntent_r
36 /* Prepare to begin reading and/or writing mount table entries from the
37 beginning of FILE. MODE is as for `fopen'. */
38 FILE *
39 __setmntent (const char *file, const char *mode)
41 /* Extend the mode parameter with "c" to disable cancellation in the
42 I/O functions. */
43 size_t modelen = strlen (mode);
44 char newmode[modelen + 2];
45 memcpy (mempcpy (newmode, mode, modelen), "c", 2);
46 FILE *result = fopen (file, newmode);
48 if (result != NULL)
49 /* We do the locking ourselves. */
50 __fsetlocking (result, FSETLOCKING_BYCALLER);
52 return result;
54 INTDEF(__setmntent)
55 weak_alias (__setmntent, setmntent)
58 /* Close a stream opened with `setmntent'. */
59 int
60 __endmntent (FILE *stream)
62 if (stream) /* SunOS 4.x allows for NULL stream */
63 fclose (stream);
64 return 1; /* SunOS 4.x says to always return 1 */
66 INTDEF(__endmntent)
67 weak_alias (__endmntent, endmntent)
70 /* Since the values in a line are separated by spaces, a name cannot
71 contain a space. Therefore some programs encode spaces in names
72 by the strings "\040". We undo the encoding when reading an entry.
73 The decoding happens in place. */
74 static char *
75 decode_name (char *buf)
77 char *rp = buf;
78 char *wp = buf;
81 if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
83 /* \040 is a SPACE. */
84 *wp++ = ' ';
85 rp += 3;
87 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '1')
89 /* \011 is a TAB. */
90 *wp++ = '\t';
91 rp += 3;
93 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
95 /* \012 is a NEWLINE. */
96 *wp++ = '\n';
97 rp += 3;
99 else if (rp[0] == '\\' && rp[1] == '\\')
101 /* We have to escape \\ to be able to represent all characters. */
102 *wp++ = '\\';
103 rp += 1;
105 else if (rp[0] == '\\' && rp[1] == '1' && rp[2] == '3' && rp[3] == '4')
107 /* \134 is also \\. */
108 *wp++ = '\\';
109 rp += 3;
111 else
112 *wp++ = *rp;
113 while (*rp++ != '\0');
115 return buf;
119 /* Read one mount table entry from STREAM. Returns a pointer to storage
120 reused on the next call, or null for EOF or error (use feof/ferror to
121 check). */
122 struct mntent *
123 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
125 char *cp;
126 char *head;
128 flockfile (stream);
131 char *end_ptr;
133 if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
135 funlockfile (stream);
136 return NULL;
139 end_ptr = strchr (buffer, '\n');
140 if (end_ptr != NULL) /* chop newline */
141 *end_ptr = '\0';
142 else
144 /* Not the whole line was read. Do it now but forget it. */
145 char tmp[1024];
146 while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
147 if (strchr (tmp, '\n') != NULL)
148 break;
151 head = buffer + strspn (buffer, " \t");
152 /* skip empty lines and comment lines: */
154 while (head[0] == '\0' || head[0] == '#');
156 cp = __strsep (&head, " \t");
157 mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
158 if (head)
159 head += strspn (head, " \t");
160 cp = __strsep (&head, " \t");
161 mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
162 if (head)
163 head += strspn (head, " \t");
164 cp = __strsep (&head, " \t");
165 mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
166 if (head)
167 head += strspn (head, " \t");
168 cp = __strsep (&head, " \t");
169 mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
170 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
172 case 0:
173 mp->mnt_freq = 0;
174 case 1:
175 mp->mnt_passno = 0;
176 case 2:
177 break;
179 funlockfile (stream);
181 return mp;
183 INTDEF(__getmntent_r)
184 weak_alias (__getmntent_r, getmntent_r)
187 /* We have to use an encoding for names if they contain spaces or tabs.
188 To be able to represent all characters we also have to escape the
189 backslash itself. This "function" must be a macro since we use
190 `alloca'. */
191 #define encode_name(name) \
192 do { \
193 const char *rp = name; \
195 while (*rp != '\0') \
196 if (*rp == ' ' || *rp == '\t' || *rp == '\\') \
197 break; \
198 else \
199 ++rp; \
201 if (*rp != '\0') \
203 /* In the worst case the length of the string can increase to \
204 founr times the current length. */ \
205 char *wp; \
207 rp = name; \
208 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
210 do \
211 if (*rp == ' ') \
213 *wp++ = '\\'; \
214 *wp++ = '0'; \
215 *wp++ = '4'; \
216 *wp++ = '0'; \
218 else if (*rp == '\t') \
220 *wp++ = '\\'; \
221 *wp++ = '0'; \
222 *wp++ = '1'; \
223 *wp++ = '1'; \
225 else if (*rp == '\n') \
227 *wp++ = '\\'; \
228 *wp++ = '0'; \
229 *wp++ = '1'; \
230 *wp++ = '2'; \
232 else if (*rp == '\\') \
234 *wp++ = '\\'; \
235 *wp++ = '\\'; \
237 else \
238 *wp++ = *rp; \
239 while (*rp++ != '\0'); \
241 } while (0)
244 /* Write the mount table entry described by MNT to STREAM.
245 Return zero on success, nonzero on failure. */
247 __addmntent (FILE *stream, const struct mntent *mnt)
249 struct mntent mntcopy = *mnt;
250 if (fseek (stream, 0, SEEK_END))
251 return 1;
253 /* Encode spaces and tabs in the names. */
254 encode_name (mntcopy.mnt_fsname);
255 encode_name (mntcopy.mnt_dir);
256 encode_name (mntcopy.mnt_type);
257 encode_name (mntcopy.mnt_opts);
259 return (fprintf (stream, "%s %s %s %s %d %d\n",
260 mntcopy.mnt_fsname,
261 mntcopy.mnt_dir,
262 mntcopy.mnt_type,
263 mntcopy.mnt_opts,
264 mntcopy.mnt_freq,
265 mntcopy.mnt_passno)
266 < 0 ? 1 : 0);
268 weak_alias (__addmntent, addmntent)
271 /* Search MNT->mnt_opts for an option matching OPT.
272 Returns the address of the substring, or null if none found. */
273 char *
274 __hasmntopt (const struct mntent *mnt, const char *opt)
276 const size_t optlen = strlen (opt);
277 char *rest = mnt->mnt_opts, *p;
279 while ((p = strstr (rest, opt)) != NULL)
281 if (p == rest
282 || (p[-1] == ','
283 && (p[optlen] == '\0' ||
284 p[optlen] == '=' ||
285 p[optlen] == ',')))
286 return p;
288 rest = strchr (rest, ',');
289 if (rest == NULL)
290 break;
291 ++rest;
294 return NULL;
296 weak_alias (__hasmntopt, hasmntopt)