[PATCH] Bionic lacks setmntent, getmntent_r, endmntent; provide workarounds

Matt Whitlock busybox at mattwhitlock.name
Sat Apr 25 00:16:03 UTC 2015


Note: This commit subsumes a previous workaround (908d6b70, a0e17f7d) for the lack of getmntent_r in dietlibc.
---
 include/platform.h  | 39 +++++++++++++++++++++++++++++++++++++++
 libbb/platform.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 util-linux/mount.c  | 12 ------------
 util-linux/umount.c | 12 ------------
 4 files changed, 86 insertions(+), 24 deletions(-)

diff --git a/include/platform.h b/include/platform.h
index df95945..aa7fddd 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -384,6 +384,11 @@ typedef unsigned smalluint;
 #define HAVE_UNLOCKED_LINE_OPS 1
 #define HAVE_GETLINE 1
 #define HAVE_XTABS 1
+#define HAVE_SETMNTENT 1
+#define HAVE_GETMNTENT 1
+#define HAVE_GETMNTENT_R 1
+#define HAVE_ADDMNTENT 1
+#define HAVE_ENDMNTENT 1
 #define HAVE_MNTENT_H 1
 #define HAVE_NET_ETHERNET_H 1
 #define HAVE_SYS_STATFS_H 1
@@ -446,6 +451,7 @@ typedef unsigned smalluint;
 
 #if defined(__dietlibc__)
 # undef HAVE_STRCHRNUL
+# undef HAVE_GETMNTENT_R
 #endif
 
 #if defined(__APPLE__)
@@ -485,6 +491,11 @@ typedef unsigned smalluint;
 # undef HAVE_STRCHRNUL
 # undef HAVE_STRVERSCMP
 # undef HAVE_UNLOCKED_LINE_OPS
+# undef HAVE_SETMNTENT
+# undef HAVE_GETMNTENT
+# undef HAVE_GETMNTENT_R
+# undef HAVE_ADDMNTENT
+# undef HAVE_ENDMNTENT
 # undef HAVE_NET_ETHERNET_H
 #endif
 
@@ -561,4 +572,32 @@ extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC
 extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
 #endif
 
+#ifndef HAVE_SETMNTENT
+# include <stdio.h> /* for FILE */
+# define setmntent bb_setmntent
+extern FILE * setmntent(const char *filename, const char *type);
+#endif
+
+#ifndef HAVE_GETMNTENT
+# include <stdio.h> /* for FILE */
+extern struct mntent * getmntent(FILE *stream);
+#endif
+
+#ifndef HAVE_GETMNTENT_R
+# include <stdio.h> /* for FILE */
+# define getmntent_r bb_getmntent_r
+extern struct mntent * getmntent_r(FILE *streamp, struct mntent *mntbuf, char *buf, int buflen);
+#endif
+
+#ifndef HAVE_ADDMNTENT
+# include <stdio.h> /* for FILE */
+extern int addmntent(FILE *stream, const struct mntent *mnt);
+#endif
+
+#ifndef HAVE_ENDMNTENT
+# include <stdio.h> /* for FILE */
+# define endmntent bb_endmntent
+extern int endmntent(FILE *streamp);
+#endif
+
 #endif
diff --git a/libbb/platform.c b/libbb/platform.c
index 8d90ca4..c6c2526 100644
--- a/libbb/platform.c
+++ b/libbb/platform.c
@@ -194,3 +194,50 @@ ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
 	return len;
 }
 #endif
+
+#ifndef HAVE_SETMNTENT
+FILE *setmntent(const char *filename, const char *type)
+{
+	return fopen(filename, type);
+}
+#endif
+
+#ifndef HAVE_GETMNTENT_R
+# ifdef HAVE_GETMNTENT
+struct mntent * getmntent_r(FILE *streamp, struct mntent *mntbuf,
+		char *buf UNUSED_PARAM, int buflen UNUSED_PARAM)
+{
+	struct mntent *ent = getmntent(streamp);
+	return ent ? memcpy(mntbuf, ent, sizeof *mntbuf) : NULL;
+}
+# else
+struct mntent * getmntent_r(FILE *streamp, struct mntent *mntbuf, char *buf, int buflen)
+{
+	static const char delims[] = " \t";
+	char *saveptr;
+	while (fgets(buf, buflen, streamp)) {
+		if ((mntbuf->mnt_fsname = strtok_r(buf, delims, &saveptr)) &&
+				mntbuf->mnt_fsname[0] != '#' &&
+				(mntbuf->mnt_dir = strtok_r(NULL, delims, &saveptr)) &&
+				(mntbuf->mnt_type = strtok_r(NULL, delims, &saveptr)) &&
+				(mntbuf->mnt_opts = strtok_r(NULL, delims, &saveptr))) {
+			mntbuf->mnt_passno = mntbuf->mnt_freq = 0;
+			if ((buf = strtok_r(NULL, delims, &saveptr))) {
+				mntbuf->mnt_freq = atoi(buf);
+				if ((buf = strtok_r(NULL, delims, &saveptr)))
+					mntbuf->mnt_passno = atoi(buf);
+			}
+			return mntbuf;
+		}
+	}
+	return NULL;
+}
+# endif
+#endif
+
+#ifndef HAVE_ENDMNTENT
+int endmntent(FILE *streamp)
+{
+	return fclose(streamp);
+}
+#endif
diff --git a/util-linux/mount.c b/util-linux/mount.c
index cb40c80..3d179a7 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -245,18 +245,6 @@
 #endif
 
 
-#if defined(__dietlibc__)
-// 16.12.2006, Sampo Kellomaki (sampo at iki.fi)
-// dietlibc-0.30 does not have implementation of getmntent_r()
-static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
-		char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
-{
-	struct mntent* ment = getmntent(stream);
-	return memcpy(result, ment, sizeof(*ment));
-}
-#endif
-
-
 // Not real flags, but we want to be able to check for this.
 enum {
 	MOUNT_USERS  = (1 << 28) * ENABLE_DESKTOP,
diff --git a/util-linux/umount.c b/util-linux/umount.c
index 4c2e882..2afdf59 100644
--- a/util-linux/umount.c
+++ b/util-linux/umount.c
@@ -32,18 +32,6 @@
 #include <sys/mount.h>
 #include "libbb.h"
 
-#if defined(__dietlibc__)
-// TODO: This does not belong here.
-/* 16.12.2006, Sampo Kellomaki (sampo at iki.fi)
- * dietlibc-0.30 does not have implementation of getmntent_r() */
-static struct mntent *getmntent_r(FILE* stream, struct mntent* result,
-		char* buffer UNUSED_PARAM, int bufsize UNUSED_PARAM)
-{
-	struct mntent* ment = getmntent(stream);
-	return memcpy(result, ment, sizeof(*ment));
-}
-#endif
-
 /* Ignored: -v -t -i
  * bbox always acts as if -d is present.
  * -D can be used to suppress it (bbox extension).
-- 
2.0.5



More information about the busybox mailing list