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
23 #include <stdio_ext.h>
25 #include <sys/types.h>
28 # define flockfile(s) _IO_flockfile (s)
29 # define funlockfile(s) _IO_funlockfile (s)
36 /* Prepare to begin reading and/or writing mount table entries from the
37 beginning of FILE. MODE is as for `fopen'. */
39 __setmntent (const char *file
, const char *mode
)
41 /* Extend the mode parameter with "c" to disable cancellation in the
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
);
49 /* We do the locking ourselves. */
50 __fsetlocking (result
, FSETLOCKING_BYCALLER
);
55 weak_alias (__setmntent
, setmntent
)
58 /* Close a stream opened with `setmntent'. */
60 __endmntent (FILE *stream
)
62 if (stream
) /* SunOS 4.x allows for NULL stream */
64 return 1; /* SunOS 4.x says to always return 1 */
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. */
75 decode_name (char *buf
)
81 if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '4' && rp
[3] == '0')
83 /* \040 is a SPACE. */
87 else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' && rp
[3] == '1')
93 else if (rp
[0] == '\\' && rp
[1] == '0' && rp
[2] == '1' && rp
[3] == '2')
95 /* \012 is a NEWLINE. */
99 else if (rp
[0] == '\\' && rp
[1] == '\\')
101 /* We have to escape \\ to be able to represent all characters. */
105 else if (rp
[0] == '\\' && rp
[1] == '1' && rp
[2] == '3' && rp
[3] == '4')
107 /* \134 is also \\. */
113 while (*rp
++ != '\0');
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
123 __getmntent_r (FILE *stream
, struct mntent
*mp
, char *buffer
, int bufsiz
)
133 if (fgets_unlocked (buffer
, bufsiz
, stream
) == NULL
)
135 funlockfile (stream
);
139 end_ptr
= strchr (buffer
, '\n');
140 if (end_ptr
!= NULL
) /* chop newline */
144 /* Not the whole line was read. Do it now but forget it. */
146 while (fgets_unlocked (tmp
, sizeof tmp
, stream
) != NULL
)
147 if (strchr (tmp
, '\n') != NULL
)
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 *) "";
159 head
+= strspn (head
, " \t");
160 cp
= __strsep (&head
, " \t");
161 mp
->mnt_dir
= cp
!= NULL
? decode_name (cp
) : (char *) "";
163 head
+= strspn (head
, " \t");
164 cp
= __strsep (&head
, " \t");
165 mp
->mnt_type
= cp
!= NULL
? decode_name (cp
) : (char *) "";
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)
179 funlockfile (stream
);
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
191 #define encode_name(name) \
193 const char *rp = name; \
195 while (*rp != '\0') \
196 if (*rp == ' ' || *rp == '\t' || *rp == '\\') \
203 /* In the worst case the length of the string can increase to \
204 founr times the current length. */ \
208 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
218 else if (*rp == '\t') \
225 else if (*rp == '\n') \
232 else if (*rp == '\\') \
239 while (*rp++ != '\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
))
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",
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. */
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
)
283 && (p
[optlen
] == '\0' ||
288 rest
= strchr (rest
, ',');
296 weak_alias (__hasmntopt
, hasmntopt
)