remount: use mntent files
authorAlan Cox <alan@linux.intel.com>
Sun, 31 Dec 2017 22:24:33 +0000 (22:24 +0000)
committerAlan Cox <alan@linux.intel.com>
Sun, 31 Dec 2017 22:24:33 +0000 (22:24 +0000)
This isn't sufficient for a proper remount but is a straight port of the
current usage model.

Applications/util/remount.c

index 0b846a9..bfaf4f8 100644 (file)
@@ -2,36 +2,28 @@
 #include <string.h>
 #include <stdlib.h>
 #include <sys/mount.h>
+#include <mntent.h>
 
-/* Assumed length of a line in /etc/mtab */
-#define MTAB_LINE 160
-
-char *getdev(char *arg)
+static char *getdev(char *arg)
 {
        FILE *f;
-       char tmp[MTAB_LINE];
-       char* dev;
-       char* mntpt;
+       struct mntent *mnt;
 
-       f = fopen("/etc/mtab", "r");
+       f = setmntent("/etc/mtab", "r");
        if (f) {
-               while (fgets(tmp, sizeof(tmp), f)) {
-                       dev = strtok(tmp, " \t");
-                       if (*tmp == '#' || dev == NULL)
-                               continue;
-                       mntpt = strtok(NULL, " \t");
-                       if (mntpt == NULL)
-                               continue;
-                       if ((strcmp(dev, arg) == 0) || (strcmp(mntpt, arg) == 0)) {
-                               fclose(f);
-                               return strdup(dev);
+               while (mnt = getmntent(f)) {
+                       if ((strcmp(mnt->mnt_fsname, arg) == 0) || (strcmp(mnt->mnt_dir, arg) == 0)) {
+                               endmntent(f);
+                               return strdup(mnt->mnt_fsname);
                        }
                }
-               fclose(f);
+               endmntent(f);
        }
        return NULL;
 }
 
+/* FIXME: needs to update mtab entry / support nosuid etc ? */
+
 int main(int argc, char *argv[])
 {
        char *dev;