No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / am-utils / dist / libamu / mtab.c
blob4f7f33ff3f7c80335da66ae5ebfcbe35218198aa
1 /* $NetBSD$ */
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.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
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.
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.
42 * File: am-utils/libamu/mtab.c
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif /* HAVE_CONFIG_H */
49 #include <am_defs.h>
50 #include <amu.h>
54 * Firewall /etc/mtab entries
56 void
57 mnt_free(mntent_t *mp)
59 XFREE(mp->mnt_fsname);
60 XFREE(mp->mnt_dir);
61 XFREE(mp->mnt_type);
62 XFREE(mp->mnt_opts);
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 */
70 XFREE(mp);
75 * Discard memory allocated for mount list
77 void
78 discard_mntlist(mntlist *mp)
80 mntlist *mp2;
82 while ((mp2 = mp)) {
83 mp = mp->mnext;
84 if (mp2->mnt)
85 mnt_free(mp2->mnt);
86 XFREE(mp2);
92 * Throw away a mount list
94 void
95 free_mntlist(mntlist *mp)
97 discard_mntlist(mp);
98 #ifdef MOUNT_TABLE_ON_FILE
99 unlock_mntlist();
100 #endif /* MOUNT_TABLE_ON_FILE */
105 * Utility routine which returns a pointer to whatever follows an = in a
106 * string. Returns null if = is not found in the string.
108 char *
109 haseq(char *instr)
111 if (instr) {
112 char *eq = strchr(instr, '=');
113 if (eq) return ++eq;
115 return NULL;
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=.
124 char *
125 hasmnteq(mntent_t *mnt, char *opt)
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 */
137 return NULL;
142 * Wrapper around hasmntvalerr(), which retains backwards compatibiliy with
143 * older use of hasmntval().
145 * XXX: eventually, all use of hasmntval() should be replaced with
146 * hasmntvalerr().
149 hasmntval(mntent_t *mnt, char *opt)
151 int err, val = 0;
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;
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).
167 * Returns non-zero (1) on error; returns 0 on success.
169 * XXX: eventually, all use of hasmntval() should be replaced with
170 * hasmntvalerr().
172 unsigned int
173 hasmntvalerr(mntent_t *mnt, char *opt, int *valp)
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;
180 /* exit if no option specificed */
181 if (!str) {
182 goto out;
185 eq = hasmnteq(mnt, opt);
187 if (!eq) { /* no argument to option ('=' sign was missing) */
188 plog(XLOG_MAP, "numeric option to \"%s\" missing", opt);
189 goto out;
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'))) {
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).
203 * Similar reasoning for '\0' instead of comma, it's the end of the
204 * string.
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 */
214 out:
215 return err;
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!)
225 char *
226 hasmntstr(mntent_t *mnt, char *opt)
228 char *str = amu_hasmntopt(mnt, opt);
230 if (str) { /* The option was there */
232 char *eq = hasmnteq(mnt, opt);
234 if (eq) { /* and had an = after it */
236 char *endptr = strchr(eq, ',');
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;
251 return NULL;