Fix our <mntent.h> implementation.

Used by toybox.

Change-Id: I36a5053423e5cc54ae02a68f4fe110d75134accd
diff --git a/libc/bionic/mntent.cpp b/libc/bionic/mntent.cpp
index 93b6915..f5a8eaa 100644
--- a/libc/bionic/mntent.cpp
+++ b/libc/bionic/mntent.cpp
@@ -28,11 +28,42 @@
 
 #include <mntent.h>
 
-mntent* getmntent(FILE*) {
-  return NULL;
+#include "private/ThreadLocalBuffer.h"
+
+GLOBAL_INIT_THREAD_LOCAL_BUFFER(getmntent_mntent);
+GLOBAL_INIT_THREAD_LOCAL_BUFFER(getmntent_strings);
+
+mntent* getmntent(FILE* fp) {
+  LOCAL_INIT_THREAD_LOCAL_BUFFER(mntent*, getmntent_mntent, sizeof(mntent));
+  LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, getmntent_strings, BUFSIZ);
+  return getmntent_r(fp, getmntent_mntent_tls_buffer,
+                     getmntent_strings_tls_buffer, getmntent_strings_tls_buffer_size);
 }
 
-mntent* getmntent_r(FILE*, struct mntent*, char*, int) {
+mntent* getmntent_r(FILE* fp, struct mntent* e, char* buf, int buf_len) {
+  memset(e, 0, sizeof(*e));
+  while (fgets(buf, buf_len, fp) != NULL) {
+    // Entries look like "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0".
+    // That is: mnt_fsname mnt_dir mnt_type mnt_opts 0 0.
+    int fsname0, fsname1, dir0, dir1, type0, type1, opts0, opts1;
+    if (sscanf(buf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
+               &fsname0, &fsname1, &dir0, &dir1, &type0, &type1, &opts0, &opts1,
+               &e->mnt_freq, &e->mnt_passno) == 2) {
+      e->mnt_fsname = &buf[fsname0];
+      buf[fsname1] = '\0';
+
+      e->mnt_dir = &buf[dir0];
+      buf[dir1] = '\0';
+
+      e->mnt_type = &buf[type0];
+      buf[type1] = '\0';
+
+      e->mnt_opts = &buf[opts0];
+      buf[opts1] = '\0';
+
+      return e;
+    }
+  }
   return NULL;
 }