* sysdeps/m68k/dl-machine.h (RTLD_START): Readd _dl_start_user
[glibc/pb-stable.git] / misc / mntent_r.c
blob78b1c2dceaa43b75ec7cfde4bd3ffaabc97d258d
1 /* Utilities for reading/writing fstab, mtab, etc.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <alloca.h>
21 #include <mntent.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
26 #ifdef USE_IN_LIBIO
27 # define flockfile(s) _IO_flockfile (s)
28 # define funlockfile(s) _IO_funlockfile (s)
29 #endif
31 /* Prepare to begin reading and/or writing mount table entries from the
32 beginning of FILE. MODE is as for `fopen'. */
33 FILE *
34 __setmntent (const char *file, const char *mode)
36 return fopen (file, mode);
38 weak_alias (__setmntent, setmntent)
41 /* Close a stream opened with `setmntent'. */
42 int
43 __endmntent (FILE *stream)
45 if (stream) /* SunOS 4.x allows for NULL stream */
46 fclose (stream);
47 return 1; /* SunOS 4.x says to always return 1 */
49 weak_alias (__endmntent, endmntent)
52 /* Since the values in a line are separated by spaces, a name cannot
53 contain a space. Therefore some programs encode spaces in names
54 by the strings "\040". We undo the encoding when reading an entry.
55 The decoding happens in place. */
56 static char *
57 decode_name (char *buf)
59 char *rp = buf;
60 char *wp = buf;
63 if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
65 /* \040 is a SPACE. */
66 *wp++ = ' ';
67 rp += 3;
69 else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
71 /* \012 is a TAB. */
72 *wp++ = '\t';
73 rp += 3;
75 else if (rp[0] == '\\' && rp[1] == '\\')
77 /* We have to escape \\ to be able to represent all characters. */
78 *wp++ = '\\';
79 rp += 1;
81 else
82 *wp++ = *rp;
83 while (*rp++ != '\0');
85 return buf;
89 /* Read one mount table entry from STREAM. Returns a pointer to storage
90 reused on the next call, or null for EOF or error (use feof/ferror to
91 check). */
92 struct mntent *
93 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
95 char *cp;
96 char *head;
98 flockfile (stream);
101 char *end_ptr;
103 if (fgets_unlocked (buffer, bufsiz, stream) == NULL)
105 funlockfile (stream);
106 return NULL;
109 end_ptr = strchr (buffer, '\n');
110 if (end_ptr != NULL) /* chop newline */
111 *end_ptr = '\0';
112 else
114 /* Not the whole line was read. Do it now but forget it. */
115 char tmp[1024];
116 while (fgets_unlocked (tmp, sizeof tmp, stream) != NULL)
117 if (strchr (tmp, '\n') != NULL)
118 break;
121 head = buffer + strspn (buffer, " \t");
122 /* skip empty lines and comment lines: */
124 while (head[0] == '\0' || head[0] == '#');
126 cp = __strsep (&head, " \t");
127 mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
128 if (head)
129 head += strspn (head, " \t");
130 cp = __strsep (&head, " \t");
131 mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
132 if (head)
133 head += strspn (head, " \t");
134 cp = __strsep (&head, " \t");
135 mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
136 if (head)
137 head += strspn (head, " \t");
138 cp = __strsep (&head, " \t");
139 mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
140 switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
142 case 0:
143 mp->mnt_freq = 0;
144 case 1:
145 mp->mnt_passno = 0;
146 case 2:
148 funlockfile (stream);
150 return mp;
152 weak_alias (__getmntent_r, getmntent_r)
155 /* We have to use an encoding for names if they contain spaces or tabs.
156 To be able to represent all characters we also have to escape the
157 backslash itself. This "function" must be a macro since we use
158 `alloca'. */
159 #define encode_name(name) \
160 do { \
161 const char *rp = name; \
163 while (*rp != '\0') \
164 if (*rp == ' ' || *rp == '\t' || *rp == '\\') \
165 break; \
166 else \
167 ++rp; \
169 if (*rp != '\0') \
171 /* In the worst case the length of the string can increase to \
172 founr times the current length. */ \
173 char *wp; \
175 rp = name; \
176 name = wp = (char *) alloca (strlen (name) * 4 + 1); \
178 do \
179 if (*rp == ' ') \
181 *wp++ = '\\'; \
182 *wp++ = '0'; \
183 *wp++ = '4'; \
184 *wp++ = '0'; \
186 else if (*rp == '\t') \
188 *wp++ = '\\'; \
189 *wp++ = '0'; \
190 *wp++ = '1'; \
191 *wp++ = '2'; \
193 else if (*rp == '\\') \
195 *wp++ = '\\'; \
196 *wp++ = '\\'; \
198 else \
199 *wp++ = *rp; \
200 while (*rp++ != '\0'); \
202 } while (0)
205 /* Write the mount table entry described by MNT to STREAM.
206 Return zero on success, nonzero on failure. */
208 __addmntent (FILE *stream, const struct mntent *mnt)
210 struct mntent mntcopy = *mnt;
211 if (fseek (stream, 0, SEEK_END))
212 return 1;
214 /* Encode spaces and tabs in the names. */
215 encode_name (mntcopy.mnt_fsname);
216 encode_name (mntcopy.mnt_dir);
217 encode_name (mntcopy.mnt_type);
218 encode_name (mntcopy.mnt_opts);
220 return (fprintf (stream, "%s %s %s %s %d %d\n",
221 mntcopy.mnt_fsname,
222 mntcopy.mnt_dir,
223 mntcopy.mnt_type,
224 mntcopy.mnt_opts,
225 mntcopy.mnt_freq,
226 mntcopy.mnt_passno)
227 < 0 ? 1 : 0);
229 weak_alias (__addmntent, addmntent)
232 /* Search MNT->mnt_opts for an option matching OPT.
233 Returns the address of the substring, or null if none found. */
234 char *
235 __hasmntopt (const struct mntent *mnt, const char *opt)
237 const size_t optlen = strlen (opt);
238 char *rest = mnt->mnt_opts, *p;
240 while ((p = strstr (rest, opt)) != NULL)
242 if (p == rest
243 || (p[-1] == ','
244 && (p[optlen] == '\0' ||
245 p[optlen] == '=' ||
246 p[optlen] == ',')))
247 return p;
249 rest = strchr (rest, ',');
250 if (rest == NULL)
251 break;
252 ++rest;
255 return NULL;
257 weak_alias (__hasmntopt, hasmntopt)