1 /* Utilities for reading/writing fstab, mtab, etc.
2    Copyright (C) 1995-2000, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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.  */
19 
20 #include <alloca.h>
21 #include <mntent.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <machine/weakalias.h>
26 
27 /* Prepare to begin reading and/or writing mount table entries from the
28    beginning of FILE.  MODE is as for `fopen'.  */
29 FILE *
__setmntent(const char * file,const char * mode)30 __setmntent (const char *file, const char *mode)
31 {
32   FILE *result = fopen (file, mode);
33 
34   return result;
35 }
weak_alias(__setmntent,setmntent)36 weak_alias (__setmntent, setmntent)
37 
38 
39 /* Close a stream opened with `setmntent'.  */
40 int
41 __endmntent (FILE *stream)
42 {
43   if (stream)		/* SunOS 4.x allows for NULL stream */
44     fclose (stream);
45   return 1;		/* SunOS 4.x says to always return 1 */
46 }
weak_alias(__endmntent,endmntent)47 weak_alias (__endmntent, endmntent)
48 
49 
50 /* Since the values in a line are separated by spaces, a name cannot
51    contain a space.  Therefore some programs encode spaces in names
52    by the strings "\040".  We undo the encoding when reading an entry.
53    The decoding happens in place.  */
54 static char *
55 decode_name (char *buf)
56 {
57   char *rp = buf;
58   char *wp = buf;
59 
60   do
61     if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '4' && rp[3] == '0')
62       {
63 	/* \040 is a SPACE.  */
64 	*wp++ = ' ';
65 	rp += 3;
66       }
67     else if (rp[0] == '\\' && rp[1] == '0' && rp[2] == '1' && rp[3] == '2')
68       {
69 	/* \012 is a TAB.  */
70 	*wp++ = '\t';
71 	rp += 3;
72       }
73     else if (rp[0] == '\\' && rp[1] == '\\')
74       {
75 	/* We have to escape \\ to be able to represent all characters.  */
76 	*wp++ = '\\';
77 	rp += 1;
78       }
79     else
80       *wp++ = *rp;
81   while (*rp++ != '\0');
82 
83   return buf;
84 }
85 
86 
87 /* Read one mount table entry from STREAM.  Returns a pointer to storage
88    reused on the next call, or null for EOF or error (use feof/ferror to
89    check).  */
90 struct mntent *
__getmntent_r(FILE * stream,struct mntent * mp,char * buffer,int bufsiz)91 __getmntent_r (FILE *stream, struct mntent *mp, char *buffer, int bufsiz)
92 {
93   char *cp;
94   char *head;
95 
96   flockfile (stream);
97   do
98     {
99       char *end_ptr;
100 
101       if (fgets (buffer, bufsiz, stream) == NULL)
102 	{
103 	  funlockfile (stream);
104 	  return NULL;
105 	}
106 
107       end_ptr = strchr (buffer, '\n');
108       if (end_ptr != NULL)	/* chop newline */
109 	*end_ptr = '\0';
110       else
111 	{
112 	  /* Not the whole line was read.  Do it now but forget it.  */
113 	  char tmp[1024];
114 	  while (fgets (tmp, sizeof tmp, stream) != NULL)
115 	    if (strchr (tmp, '\n') != NULL)
116 	      break;
117 	}
118 
119       head = buffer + strspn (buffer, " \t");
120       /* skip empty lines and comment lines:  */
121     }
122   while (head[0] == '\0' || head[0] == '#');
123 
124   cp = strsep (&head, " \t");
125   mp->mnt_fsname = cp != NULL ? decode_name (cp) : (char *) "";
126   if (head)
127     head += strspn (head, " \t");
128   cp = strsep (&head, " \t");
129   mp->mnt_dir = cp != NULL ? decode_name (cp) : (char *) "";
130   if (head)
131     head += strspn (head, " \t");
132   cp = strsep (&head, " \t");
133   mp->mnt_type = cp != NULL ? decode_name (cp) : (char *) "";
134   if (head)
135     head += strspn (head, " \t");
136   cp = strsep (&head, " \t");
137   mp->mnt_opts = cp != NULL ? decode_name (cp) : (char *) "";
138   switch (head ? sscanf (head, " %d %d ", &mp->mnt_freq, &mp->mnt_passno) : 0)
139     {
140     case 0:
141       mp->mnt_freq = 0;
142     case 1:
143       mp->mnt_passno = 0;
144     case 2:
145       break;
146     }
147   funlockfile (stream);
148 
149   return mp;
150 }
weak_alias(__getmntent_r,getmntent_r)151 weak_alias (__getmntent_r, getmntent_r)
152 
153 
154 /* We have to use an encoding for names if they contain spaces or tabs.
155    To be able to represent all characters we also have to escape the
156    backslash itself.  This "function" must be a macro since we use
157    `alloca'.  */
158 #define encode_name(name) \
159   do {									      \
160     const char *rp = name;						      \
161 									      \
162     while (*rp != '\0')							      \
163       if (*rp == ' ' || *rp == '\t' || *rp == '\\')			      \
164 	break;								      \
165       else								      \
166 	++rp;								      \
167 									      \
168     if (*rp != '\0')							      \
169       {									      \
170 	/* In the worst case the length of the string can increase to	      \
171 	   founr times the current length.  */				      \
172 	char *wp;							      \
173 									      \
174 	rp = name;							      \
175 	name = wp = (char *) alloca (strlen (name) * 4 + 1);		      \
176 									      \
177 	do								      \
178 	  if (*rp == ' ')						      \
179 	    {								      \
180 	      *wp++ = '\\';						      \
181 	      *wp++ = '0';						      \
182 	      *wp++ = '4';						      \
183 	      *wp++ = '0';						      \
184 	    }								      \
185 	  else if (*rp == '\t')						      \
186 	    {								      \
187 	      *wp++ = '\\';						      \
188 	      *wp++ = '0';						      \
189 	      *wp++ = '1';						      \
190 	      *wp++ = '2';						      \
191 	    }								      \
192 	  else if (*rp == '\\')						      \
193 	    {								      \
194 	      *wp++ = '\\';						      \
195 	      *wp++ = '\\';						      \
196 	    }								      \
197 	  else								      \
198 	    *wp++ = *rp;						      \
199 	while (*rp++ != '\0');						      \
200       }									      \
201   } while (0)
202 
203 
204 /* Write the mount table entry described by MNT to STREAM.
205    Return zero on success, nonzero on failure.  */
206 int
207 __addmntent (FILE *stream, const struct mntent *mnt)
208 {
209   struct mntent mntcopy = *mnt;
210   if (fseek (stream, 0, SEEK_END))
211     return 1;
212 
213   /* Encode spaces and tabs in the names.  */
214   encode_name (mntcopy.mnt_fsname);
215   encode_name (mntcopy.mnt_dir);
216   encode_name (mntcopy.mnt_type);
217   encode_name (mntcopy.mnt_opts);
218 
219   return (fprintf (stream, "%s %s %s %s %d %d\n",
220 		   mntcopy.mnt_fsname,
221 		   mntcopy.mnt_dir,
222 		   mntcopy.mnt_type,
223 		   mntcopy.mnt_opts,
224 		   mntcopy.mnt_freq,
225 		   mntcopy.mnt_passno)
226 	  < 0 ? 1 : 0);
227 }
weak_alias(__addmntent,addmntent)228 weak_alias (__addmntent, addmntent)
229 
230 
231 /* Search MNT->mnt_opts for an option matching OPT.
232    Returns the address of the substring, or null if none found.  */
233 char *
234 __hasmntopt (const struct mntent *mnt, const char *opt)
235 {
236   const size_t optlen = strlen (opt);
237   char *rest = mnt->mnt_opts, *p;
238 
239   while ((p = strstr (rest, opt)) != NULL)
240     {
241       if (p == rest
242 	  || (p[-1] == ','
243 	      && (p[optlen] == '\0' ||
244 		  p[optlen] == '='  ||
245 		  p[optlen] == ',')))
246 	return p;
247 
248       rest = strchr (rest, ',');
249       if (rest == NULL)
250 	break;
251       ++rest;
252     }
253 
254   return NULL;
255 }
256 weak_alias (__hasmntopt, hasmntopt)
257