summary refs log tree commit diff
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1995-09-14 09:00:06 +0000
committerRoland McGrath <roland@gnu.org>1995-09-14 09:00:06 +0000
commit7a770247ab47579223da3ba873ca8236e5760b74 (patch)
tree855a3637f7eda31fc9a0977f7d15b9b02bb47560
parenteef089c2ab974053d6346632c5aae7f2f61e947f (diff)
downloadglibc-7a770247ab47579223da3ba873ca8236e5760b74.tar.gz
glibc-7a770247ab47579223da3ba873ca8236e5760b74.tar.xz
glibc-7a770247ab47579223da3ba873ca8236e5760b74.zip
Tue Sep 12 14:30:07 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
	* misc/mntent.c: New file.
	* misc/mntent.h: New file.
	* misc/Makefile (headers): Added mntent.h.
	(routines): Added mntent.
	* misc/fstab.c: Rewritten using mntent functions.

Mon Sep 11 14:00:14 1995  Roland McGrath  <roland@whiz-bang.gnu.ai.mit.edu>

	* posix/glob.c (glob): Comment fix.
-rw-r--r--ChangeLog12
-rw-r--r--misc/Makefile4
-rw-r--r--misc/fstab.c260
-rw-r--r--misc/mntent.c111
-rw-r--r--misc/mntent.h77
5 files changed, 284 insertions, 180 deletions
diff --git a/ChangeLog b/ChangeLog
index b194b5a45b..adc6644b74 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Tue Sep 12 14:30:07 1995  Roland McGrath  <roland@churchy.gnu.ai.mit.edu>
+
+	* misc/mntent.c: New file.
+	* misc/mntent.h: New file.
+	* misc/Makefile (headers): Added mntent.h.
+	(routines): Added mntent.
+	* misc/fstab.c: Rewritten using mntent functions.
+
+Mon Sep 11 14:00:14 1995  Roland McGrath  <roland@whiz-bang.gnu.ai.mit.edu>
+
+	* posix/glob.c (glob): Comment fix.
+
 Fri Sep  8 16:25:22 1995  Roland McGrath  <roland@churchy.gnu.ai.mit.edu>
 
 	* libc-symbols.h [HAVE_ELF] (symbol_set_declare): Declare the
diff --git a/misc/Makefile b/misc/Makefile
index 53489c68cd..c626dd7a33 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -25,7 +25,7 @@ subdir	:= misc
 headers	:= sys/uio.h sys/ioctl.h sys/ptrace.h ioctls.h sys/file.h	\
 	   a.out.h nlist.h stab.h stab.def sgtty.h sys/dir.h sys/cdefs.h \
 	   ttyent.h syscall.h syslog.h sys/syslog.h paths.h sys/reboot.h \
-	   sys/mman.h sys/param.h fstab.h search.h utmp.h
+	   sys/mman.h sys/param.h fstab.h mntent.h search.h utmp.h
 
 routines := brk sbrk sstk ioctl \
 	    readv writev \
@@ -42,7 +42,7 @@ routines := brk sbrk sstk ioctl \
 	    ualarm usleep \
 	    gtty stty \
 	    ptrace \
-	    nlist fstab \
+	    nlist fstab mntent \
 	    utimes \
 	    truncate ftruncate \
 	    chflags fchflags \
diff --git a/misc/fstab.c b/misc/fstab.c
index ac59cab861..c3c27df6ef 100644
--- a/misc/fstab.c
+++ b/misc/fstab.c
@@ -1,205 +1,109 @@
-/*
- * Copyright (c) 1980, 1988, 1993
- *	The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
+/* 
+Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)fstab.c	8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
 
-#include <errno.h>
-#include <fstab.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
 
-#ifndef EFTYPE
-#define EFTYPE EINVAL
-#endif
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
 
-static FILE *_fs_fp;
-static struct fstab _fs_fstab;
+#include <fstab.h>
+#include <mntent.h>
+#include <stdio.h>
 
-static void error __P((int));
+static FILE *fstab;
 
-static int
-fstabscan __P((void))
+static FILE *
+fstab_stream (void)
 {
-	register char *cp;
-#define	MAXLINELENGTH	1024
-	static char line[MAXLINELENGTH];
-	char subline[MAXLINELENGTH];
-	int typexx;
-
-	for (;;) {
-		if (!(cp = fgets(line, sizeof(line), _fs_fp)))
-			return(0);
-/* OLD_STYLE_FSTAB */
-		if (!strpbrk(cp, " \t")) {
-			_fs_fstab.fs_spec = strtok(cp, ":\n");
-			_fs_fstab.fs_file = strtok((char *)NULL, ":\n");
-			_fs_fstab.fs_type = strtok((char *)NULL, ":\n");
-			if (_fs_fstab.fs_type) {
-				if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
-					continue;
-				_fs_fstab.fs_mntops = _fs_fstab.fs_type;
-				_fs_fstab.fs_vfstype =
-				    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
-				    "ufs" : "swap";
-				if (cp = strtok((char *)NULL, ":\n")) {
-					_fs_fstab.fs_freq = atoi(cp);
-					if (cp = strtok((char *)NULL, ":\n")) {
-						_fs_fstab.fs_passno = atoi(cp);
-						return(1);
-					}
-				}
-			}
-			goto bad;
-		}
-/* OLD_STYLE_FSTAB */
-		_fs_fstab.fs_spec = strtok(cp, " \t\n");
-		if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
-			continue;
-		_fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
-		_fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
-		_fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
-		if (_fs_fstab.fs_mntops == NULL)
-			goto bad;
-		_fs_fstab.fs_freq = 0;
-		_fs_fstab.fs_passno = 0;
-		if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
-			_fs_fstab.fs_freq = atoi(cp);
-			if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
-				_fs_fstab.fs_passno = atoi(cp);
-		}
-		strcpy(subline, _fs_fstab.fs_mntops);
-		for (typexx = 0, cp = strtok(subline, ","); cp;
-		     cp = strtok((char *)NULL, ",")) {
-			if (strlen(cp) != 2)
-				continue;
-			if (!strcmp(cp, FSTAB_RW)) {
-				_fs_fstab.fs_type = FSTAB_RW;
-				break;
-			}
-			if (!strcmp(cp, FSTAB_RQ)) {
-				_fs_fstab.fs_type = FSTAB_RQ;
-				break;
-			}
-			if (!strcmp(cp, FSTAB_RO)) {
-				_fs_fstab.fs_type = FSTAB_RO;
-				break;
-			}
-			if (!strcmp(cp, FSTAB_SW)) {
-				_fs_fstab.fs_type = FSTAB_SW;
-				break;
-			}
-			if (!strcmp(cp, FSTAB_XX)) {
-				_fs_fstab.fs_type = FSTAB_XX;
-				typexx++;
-				break;
-			}
-		}
-		if (typexx)
-			continue;
-		if (cp != NULL)
-			return(1);
-
-bad:		/* no way to distinguish between EOF and syntax error */
-		error(EFTYPE);
-	}
-	/* NOTREACHED */
+  if (! fstab)
+    fstab = setmntent (_PATH_FSTAB, "r");
+  return fstab;
 }
 
-struct fstab *
-getfsent()
+int
+setfsent (void)
 {
-	if (!_fs_fp && !setfsent() || !fstabscan())
-		return((struct fstab *)NULL);
-	return(&_fs_fstab);
+  if (fstab)
+    {
+      rewind (fstab);
+      return 1;
+    }
+  else
+    fstab = setmntent (_PATH_FSTAB, "r");
+  return fstab ? 0 : 1;
 }
 
-struct fstab *
-getfsspec(name)
-	register const char *name;
+static struct fstab *
+mnt2fs (struct mntent *m)
 {
-	if (setfsent())
-		while (fstabscan())
-			if (!strcmp(_fs_fstab.fs_spec, name))
-				return(&_fs_fstab);
-	return((struct fstab *)NULL);
+  static struct fstab f;
+  f.fs_spec = m->mnt_fsname;
+  f.fs_file = m->mnt_dir;
+  f.fs_vfstype = m->mnt_type;
+  f.fs_mntops = m->mnt_opts;
+  f.fs_type = (hasmntopt (m, FSTAB_RW) ? (char *) FSTAB_RW :
+	       hasmntopt (m, FSTAB_RQ) ? (char *) FSTAB_RQ :
+	       hasmntopt (m, FSTAB_RO) ? (char *) FSTAB_RO :
+	       hasmntopt (m, FSTAB_SW) ? (char *) FSTAB_SW :
+	       hasmntopt (m, FSTAB_XX) ? (char *) FSTAB_XX :
+	       (char *) "??");
+  f.fs_freq = m->mnt_freq;
+  f.fs_passno = m->mnt_passno;
+  return &f;
 }
 
 struct fstab *
-getfsfile(name)
-	register const char *name;
+getfsent (void)
 {
-	if (setfsent())
-		while (fstabscan())
-			if (!strcmp(_fs_fstab.fs_file, name))
-				return(&_fs_fstab);
-	return((struct fstab *)NULL);
+  FILE *s = fstab_stream ();
+
+  if (! s)
+    return NULL;
+  
+  return mnt2fs (getmntent (s));
 }
 
-setfsent()
+struct fstab *
+getfsspec (name)
+     register const char *name;
 {
-	if (_fs_fp) {
-		rewind(_fs_fp);
-		return(1);
-	}
-	if (_fs_fp = fopen(_PATH_FSTAB, "r"))
-		return(1);
-	error(errno);
-	return(0);
+  struct mntent *m;
+  if (setfsent ())
+    while (m = getmntent (fstab))
+      if (!strcmp (m->mnt_fsname, name))
+	return mnt2fs (m);
+  return NULL;
 }
 
-void
-endfsent()
+struct fstab *
+getfsfile (name)
+	register const char *name;
 {
-	if (_fs_fp) {
-		(void)fclose(_fs_fp);
-		_fs_fp = NULL;
-	}
+  struct mntent *m;
+  if (setfsent ())
+    while (m = getmntent (fstab))
+      if (!strcmp (m->mnt_dir, name))
+	return mnt2fs (m);
+  return NULL;
 }
 
-static void
-error(err)
-	int err;
+void
+endfsent ()
 {
-	char *p;
-
-	(void)write(STDERR_FILENO, "fstab: ", 7);
-	(void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
-	(void)write(STDERR_FILENO, ": ", 1);
-	p = strerror(err);
-	(void)write(STDERR_FILENO, p, strlen(p));
-	(void)write(STDERR_FILENO, "\n", 1);
+  if (fstab)
+    {
+      (void) endmntent (fstab);
+      fstab = NULL;
+    }
 }
diff --git a/misc/mntent.c b/misc/mntent.c
new file mode 100644
index 0000000000..42c20817ba
--- /dev/null
+++ b/misc/mntent.c
@@ -0,0 +1,111 @@
+/* Utilities for reading/writing fstab, mtab, etc.
+Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#include <mntent.h>
+#include <stdio.h>
+#include <string.h>
+
+/* Prepare to begin reading and/or writing mount table entries from the
+   beginning of FILE.  MODE is as for `fopen'.  */
+FILE *
+setmntent (const char *file, const char *mode)
+{
+  return fopen (file, mode);
+}
+
+/* Close a stream opened with `setmntent'.  */
+int
+endmntent (FILE *stream)
+{
+  return fclose (stream);
+}
+
+
+/* Read one mount table entry from STREAM.  Returns a pointer to storage
+   reused on the next call, or null for EOF or error (use feof/ferror to
+   check).  */
+struct mntent *
+getmntent (FILE *stream)
+{
+  static char *buf;
+  static size_t bufsiz;
+  static struct mntent m;
+  char *head;
+
+  do
+    {
+      if (getline (&buf, &bufsiz, stream) < 0)
+	return NULL;
+
+      head = buf;
+    } while (head[0] == '#');	/* Skip comment lines.  */
+    
+  m.mnt_fsname = strsep (&head, " \t") ?: (char *) "";
+  m.mnt_dir = strsep (&head, " \t") ?: (char *) "";
+  m.mnt_type = strsep (&head, " \t") ?: (char *) "";
+  m.mnt_opts = strsep (&head, " \t") ?: (char *) "";
+  switch (sscanf (head, "%d %d\n", &m.mnt_freq, &m.mnt_passno))
+    {
+    case 0:
+      m.mnt_freq = 0;
+    case 1:
+      m.mnt_passno = 0;
+    case 2:
+    }
+
+  return &m;
+}
+
+/* Write the mount table entry described by MNT to STREAM.
+   Return zero on success, nonzero on failure.  */
+int
+addmntent (FILE *stream, const struct mntent *mnt)
+{
+  return (fprintf (stream, "%s %s %s %s %d %d\n",
+		   mnt->mnt_fsname,
+		   mnt->mnt_dir,
+		   mnt->mnt_type,
+		   mnt->mnt_opts,
+		   mnt->mnt_freq,
+		   mnt->mnt_passno)
+	  < 0 ? -1 : 0);
+}
+
+/* Search MNT->mnt_opts for an option matching OPT.
+   Returns the address of the substring, or null if none found.  */
+char *
+hasmntopt (const struct mntent *mnt, const char *opt)
+{
+  const size_t optlen = strlen (opt);
+  char *rest = mnt->mnt_opts, *p;
+
+  while ((p = strstr (rest, opt)) != NULL)
+    {
+      if (p == rest || p[-1] == ',' &&
+	  (p[optlen] == '\0' ||
+	   p[optlen] == '=' ||
+	   p[optlen] == ','))
+	return p;
+
+      rest = strchr (rest, ',');
+    }
+
+  return NULL;
+}
+
diff --git a/misc/mntent.h b/misc/mntent.h
new file mode 100644
index 0000000000..99fa56ded4
--- /dev/null
+++ b/misc/mntent.h
@@ -0,0 +1,77 @@
+/* <mntent.h> -- Utilities for reading/writing fstab, mtab, etc.
+Copyright (C) 1995 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB.  If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA.  */
+
+#ifndef	_MNTENT_H
+#define	_MNTENT_H	1
+
+#include <features.h>
+#define __need_FILE
+#include <stdio.h>
+
+
+/* File listing canonical interesting mount points.  */
+#define	_PATH_MNTTAB	"/etc/fstab"
+#define	MNTTAB		_PATH_MNTTAB /* Deprecated alias.  */
+
+/* File listing currently active mount points.  */
+#define	_PATH_MOUNTED	"/var/run/mtab"
+#define	MOUNTED		_PATH_MOUNTED /* Deprecated alias.  */
+
+
+__BEGIN_DECLS
+
+/* Structure describing a mount table entry.  */
+struct mntent
+  {
+    char *mnt_fsname;		/* Device or server for filesystem.  */
+    char *mnt_dir;		/* Directory mounted on.  */
+    char *mnt_type;		/* Type of filesystem: ufs, nfs, etc.  */
+    char *mnt_opts;		/* Comma-separated options for fs.  */
+    int mnt_freq;		/* Dump frequency (in days).  */
+    int mnt_passno;		/* Pass number for `fsck'.  */
+  };
+
+
+/* Prepare to begin reading and/or writing mount table entries from the
+   beginning of FILE.  MODE is as for `fopen'.  */
+extern FILE *setmntent __P ((__const char *__file,
+			     __const char *__mode));
+
+/* Read one mount table entry from STREAM.  Returns a pointer to storage
+   reused on the next call, or null for EOF or error (use feof/ferror to
+   check).  */
+extern struct mntent *getmntent __P ((FILE *__stream));
+
+/* Write the mount table entry described by MNT to STREAM.
+   Return zero on success, nonzero on failure.  */
+extern int addmntent __P ((FILE *__stream,
+			   __const struct mntent *__mnt));
+
+/* Close a stream opened with `setmntent'.  */
+extern int endmntent __P ((FILE *__stream));
+
+/* Search MNT->mnt_opts for an option matching OPT.
+   Returns the address of the substring, or null if none found.  */
+extern char *hasmntopt __P ((__const struct mntent *__mnt,
+			     __const char *__opt));
+
+
+__END_DECLS
+
+#endif	/* mntent.h */