mtab.c revision 82794
138494Sobrien/*
282794Sobrien * Copyright (c) 1997-2001 Erez Zadok
338494Sobrien * Copyright (c) 1989 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1989 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042629Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *      %W% (Berkeley) %G%
4038494Sobrien *
4182794Sobrien * $Id: mtab.c,v 1.3.2.3 2001/04/14 21:08:25 ezk Exp $
4238494Sobrien *
4338494Sobrien */
4438494Sobrien
4538494Sobrien#ifdef HAVE_CONFIG_H
4638494Sobrien# include <config.h>
4738494Sobrien#endif /* HAVE_CONFIG_H */
4838494Sobrien#include <am_defs.h>
4938494Sobrien#include <amu.h>
5038494Sobrien
5138494Sobrien
5238494Sobrien/*
5338494Sobrien * Firewall /etc/mtab entries
5438494Sobrien */
5538494Sobrienvoid
5638494Sobrienmnt_free(mntent_t *mp)
5738494Sobrien{
5838494Sobrien  XFREE(mp->mnt_fsname);
5938494Sobrien  XFREE(mp->mnt_dir);
6038494Sobrien  XFREE(mp->mnt_type);
6138494Sobrien  XFREE(mp->mnt_opts);
6238494Sobrien
6338494Sobrien#ifdef HAVE_FIELD_MNTENT_T_MNT_TIME
6438494Sobrien# ifdef HAVE_FIELD_MNTENT_T_MNT_TIME_STRING
6538494Sobrien  XFREE(mp->mnt_time);
6638494Sobrien# endif /* HAVE_FIELD_MNTENT_T_MNT_TIME_STRING */
6738494Sobrien#endif /* HAVE_FIELD_MNTENT_T_MNT_TIME */
6838494Sobrien
6938494Sobrien  XFREE(mp);
7038494Sobrien}
7138494Sobrien
7238494Sobrien
7338494Sobrien/*
7438494Sobrien * Discard memory allocated for mount list
7538494Sobrien */
7638494Sobrienvoid
7738494Sobriendiscard_mntlist(mntlist *mp)
7838494Sobrien{
7938494Sobrien  mntlist *mp2;
8038494Sobrien
8138494Sobrien  while ((mp2 = mp)) {
8238494Sobrien    mp = mp->mnext;
8338494Sobrien    if (mp2->mnt)
8438494Sobrien      mnt_free(mp2->mnt);
8538494Sobrien    XFREE(mp2);
8638494Sobrien  }
8738494Sobrien}
8838494Sobrien
8938494Sobrien
9038494Sobrien/*
9138494Sobrien * Throw away a mount list
9238494Sobrien */
9338494Sobrienvoid
9438494Sobrienfree_mntlist(mntlist *mp)
9538494Sobrien{
9638494Sobrien  discard_mntlist(mp);
9738494Sobrien#ifdef MOUNT_TABLE_ON_FILE
9838494Sobrien  unlock_mntlist();
9938494Sobrien#endif /* MOUNT_TABLE_ON_FILE */
10038494Sobrien}
10138494Sobrien
10238494Sobrien
10338494Sobrien/*
10482794Sobrien * Utility routine which returns a pointer to whatever follows an = in a
10582794Sobrien * string.  Returns null if = is not found in the string.
10682794Sobrien */
10782794Sobrienchar *
10882794Sobrienhaseq(char *instr)
10982794Sobrien{
11082794Sobrien  if (instr) {
11182794Sobrien    char *eq = strchr(instr, '=');
11282794Sobrien    if (eq) return ++eq;
11382794Sobrien  }
11482794Sobrien  return NULL;
11582794Sobrien}
11682794Sobrien
11782794Sobrien
11882794Sobrien/*
11982794Sobrien * Utility routine which returns a pointer to whatever
12082794Sobrien * follows an = in a mount option.  Returns null if option
12182794Sobrien * doesn't exist or doesn't have an '='.  Won't fall for opt,foo=.
12282794Sobrien */
12382794Sobrienchar *
12482794Sobrienhasmnteq(mntent_t *mnt, char *opt)
12582794Sobrien{
12682794Sobrien  if (mnt && opt) {		/* disallow null input pointers */
12782794Sobrien    if ( *opt ) {		/* disallow the null string as an opt */
12882794Sobrien      char *str = hasmntopt(mnt, opt);
12982794Sobrien      if ( str ) {		/* option was there */
13082794Sobrien	char *eq = str + strlen(opt); /* Look at char just after option */
13182794Sobrien	if (*eq == '=')		/* Is it '=' ? */
13282794Sobrien	  return ++eq;		/* If so, return pointer to remaining str */
13382794Sobrien      }
13482794Sobrien    }
13582794Sobrien  }
13682794Sobrien  return NULL;
13782794Sobrien}
13882794Sobrien
13982794Sobrien
14082794Sobrien/*
14138494Sobrien * Utility routine which determines the value of a
14238494Sobrien * numeric option in the mount options (such as port=%d).
14338494Sobrien * Returns 0 if the option is not specified.
14438494Sobrien */
14538494Sobrienint
14638494Sobrienhasmntval(mntent_t *mnt, char *opt)
14738494Sobrien{
14838494Sobrien  char *str = hasmntopt(mnt, opt);
14938494Sobrien
15082794Sobrien  if (str) { /* The option was there */
15182794Sobrien
15282794Sobrien    char *eq = hasmnteq(mnt, opt);
15382794Sobrien
15482794Sobrien    if (eq) { /* and had an = after it */
15582794Sobrien
15682794Sobrien      char *endptr = NULL;
15782794Sobrien      long int i = strtol(eq,&endptr,0); /* hex and octal allowed ;-) */
15882794Sobrien
15982794Sobrien      if ( (! endptr) || /* endptr == NULL means all chars valid */
16082794Sobrien	  /*
16182794Sobrien	   * endptr set means strtol saw a non-digit.  If the
16282794Sobrien	   * non-digit is a comma, it's probably the start of the next
16382794Sobrien	   * option.  If the comma is the first char though, complain about
16482794Sobrien	   * it (foo=,bar is made noticeable by this).
16582794Sobrien	   */
16682794Sobrien	   ((endptr == strchr(eq, ',')) && (endptr != eq))
16782794Sobrien	   )
16882794Sobrien	  return((int) i);
16982794Sobrien      /* whatever was after = wasn't a number */
17082794Sobrien      plog(XLOG_MAP, "invalid numeric option in \"%s\": \"%s\"", opt, str);
17182794Sobrien    } else {
17282794Sobrien      /* No argument to option (= was missing) */
17382794Sobrien      plog(XLOG_MAP, "numeric option to \"%s\" missing", opt);
17482794Sobrien    }
17538494Sobrien  }
17638494Sobrien  return 0;
17738494Sobrien}
178