xref: /netbsd-src/external/bsd/am-utils/dist/libamu/mtab.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: mtab.c,v 1.1.1.2 2009/03/20 20:26:55 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2009 Erez Zadok
5  * Copyright (c) 1989 Jan-Simon Pendry
6  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7  * Copyright (c) 1989 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry at Imperial College, London.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgment:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *
42  * File: am-utils/libamu/mtab.c
43  *
44  */
45 
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif /* HAVE_CONFIG_H */
49 #include <am_defs.h>
50 #include <amu.h>
51 
52 
53 /*
54  * Firewall /etc/mtab entries
55  */
56 void
57 mnt_free(mntent_t *mp)
58 {
59   XFREE(mp->mnt_fsname);
60   XFREE(mp->mnt_dir);
61   XFREE(mp->mnt_type);
62   XFREE(mp->mnt_opts);
63 
64 #ifdef HAVE_MNTENT_T_MNT_TIME
65 # ifdef HAVE_MNTENT_T_MNT_TIME_STRING
66   XFREE(mp->mnt_time);
67 # endif /* HAVE_MNTENT_T_MNT_TIME_STRING */
68 #endif /* HAVE_MNTENT_T_MNT_TIME */
69 
70   XFREE(mp);
71 }
72 
73 
74 /*
75  * Discard memory allocated for mount list
76  */
77 void
78 discard_mntlist(mntlist *mp)
79 {
80   mntlist *mp2;
81 
82   while ((mp2 = mp)) {
83     mp = mp->mnext;
84     if (mp2->mnt)
85       mnt_free(mp2->mnt);
86     XFREE(mp2);
87   }
88 }
89 
90 
91 /*
92  * Throw away a mount list
93  */
94 void
95 free_mntlist(mntlist *mp)
96 {
97   discard_mntlist(mp);
98 #ifdef MOUNT_TABLE_ON_FILE
99   unlock_mntlist();
100 #endif /* MOUNT_TABLE_ON_FILE */
101 }
102 
103 
104 /*
105  * Utility routine which returns a pointer to whatever follows an = in a
106  * string.  Returns null if = is not found in the string.
107  */
108 char *
109 haseq(char *instr)
110 {
111   if (instr) {
112     char *eq = strchr(instr, '=');
113     if (eq) return ++eq;
114   }
115   return NULL;
116 }
117 
118 
119 /*
120  * Utility routine which returns a pointer to whatever
121  * follows an = in a mount option.  Returns null if option
122  * doesn't exist or doesn't have an '='.  Won't fail for opt,foo=.
123  */
124 char *
125 hasmnteq(mntent_t *mnt, char *opt)
126 {
127   if (mnt && opt) {		/* disallow null input pointers */
128     if ( *opt ) {		/* disallow the null string as an opt */
129       char *str = amu_hasmntopt(mnt, opt);
130       if ( str ) {		/* option was there */
131 	char *eq = str + strlen(opt); /* Look at char just after option */
132 	if (*eq == '=')		/* Is it '=' ? */
133 	  return ++eq;		/* If so, return pointer to remaining str */
134       }
135     }
136   }
137   return NULL;
138 }
139 
140 
141 /*
142  * Wrapper around hasmntvalerr(), which retains backwards compatibiliy with
143  * older use of hasmntval().
144  *
145  * XXX: eventually, all use of hasmntval() should be replaced with
146  * hasmntvalerr().
147  */
148 int
149 hasmntval(mntent_t *mnt, char *opt)
150 {
151   int err, val = 0;
152 
153   err = hasmntvalerr(mnt, opt, &val);
154   if (err)	   /* if there was an error (hasmntvalerr returned 1) */
155     return 0;	   /* redundant: val==0 above, but leave here for clarity */
156   /* otherwise there was no error */
157   return val;
158 }
159 
160 
161 /*
162  * Utility routine which determines the value of a numeric option in the
163  * mount options (such as port=%d), and fills in the value in the argument
164  * valp (argument won't be touched if no value is set, for example due to an
165  * error).
166  *
167  * Returns non-zero (1) on error; returns 0 on success.
168  *
169  * XXX: eventually, all use of hasmntval() should be replaced with
170  * hasmntvalerr().
171  */
172 unsigned int
173 hasmntvalerr(mntent_t *mnt, char *opt, int *valp)
174 {
175   char *str = amu_hasmntopt(mnt, opt);
176   int err = 1;		     /* 1 means no good value was set (an error) */
177   char *eq, *endptr;
178   long int i;
179 
180   /* exit if no option specificed */
181   if (!str) {
182     goto out;
183   }
184 
185   eq = hasmnteq(mnt, opt);
186 
187   if (!eq) {		  /* no argument to option ('=' sign was missing) */
188     plog(XLOG_MAP, "numeric option to \"%s\" missing", opt);
189     goto out;
190   }
191 
192   /* if got here, then we had an '=' after option name */
193   endptr = NULL;
194   i = strtol(eq, &endptr, 0); /* hex and octal allowed ;-) */
195   if (!endptr ||
196       (endptr != eq && (*endptr == ',' || *endptr == '\0'))) {
197       /*
198        * endptr set means strtol saw a non-digit.  If the non-digit is a
199        * comma, it's probably the start of the next option.  If the comma is
200        * the first char though, complain about it (foo=,bar is made
201        * noticeable by this).
202        *
203        * Similar reasoning for '\0' instead of comma, it's the end of the
204        * string.
205        */
206     *valp = (int) i;		/* set good value */
207     err = 0;			/* no error */
208   } else {
209     /* whatever was after the '=' sign wasn't a number */
210     plog(XLOG_MAP, "invalid numeric option in \"%s\": \"%s\"", opt, str);
211     /* fall through to error/exit processing */
212   }
213 
214  out:
215   return err;
216 }
217 
218 
219 /*
220  * Utility routine which returns the string value of
221  * an option in the mount options (such as proto=udp).
222  * Returns NULL if the option is not specified.
223  * Returns malloc'ed string (caller must free!)
224  */
225 char *
226 hasmntstr(mntent_t *mnt, char *opt)
227 {
228   char *str = amu_hasmntopt(mnt, opt);
229 
230   if (str) { /* The option was there */
231 
232     char *eq = hasmnteq(mnt, opt);
233 
234     if (eq) { /* and had an = after it */
235 
236       char *endptr = strchr(eq, ',');
237 
238       /* if saw no comma, return strdup'd string */
239       if (!endptr)
240 	return strdup(eq);
241       else {
242 	/* else we need to copy only the chars needed */
243 	int len = endptr - eq;
244 	char *buf = xmalloc(len + 1);
245 	strncpy(buf, eq, len);
246 	buf[len] = '\0';
247 	return buf;
248       }
249     }
250   }
251   return NULL;
252 }
253