]> git.tdb.fi Git - pmount-gui.git/commitdiff
Generalize mtab handling code
authorMikko Rasa <tdb@tdb.fi>
Sat, 19 Apr 2014 15:09:42 +0000 (18:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 19 Apr 2014 17:11:32 +0000 (20:11 +0300)
main.c

diff --git a/main.c b/main.c
index b1dcb1ccc645e57f470fdcbbc6a671bad381d133..cd95cc943cc78cabd0142b2b881367847bda9567 100644 (file)
--- a/main.c
+++ b/main.c
@@ -180,47 +180,56 @@ void free_properties(Property *props)
        free(props);
 }
 
-char **get_mounted_devices(void)
+char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
 {
-       FILE *mtab;
+       FILE *file;
        struct mntent *me;
-       char **mounted = NULL;
-       int n_mounted = 0;
+       char **devices = NULL;
+       int n_devices = 0;
 
-       mtab = setmntent("/etc/mtab", "r");
-       if(!mtab)
+       file = setmntent(filename, "r");
+       if(!file)
                return NULL;
 
-       while((me = getmntent(mtab)))
-       {
-               mounted = (char **)realloc(mounted, (n_mounted+2)*sizeof(char *));
-               mounted[n_mounted] = strdup(me->mnt_fsname);
-               ++n_mounted;
-       }
+       while((me = getmntent(file)))
+               if(!predicate || predicate(me))
+               {
+                       devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
+                       devices[n_devices] = strdup(me->mnt_fsname);
+                       ++n_devices;
+               }
 
-       endmntent(mtab);
-       mounted[n_mounted] = NULL;
+       endmntent(file);
+       if(devices)
+               devices[n_devices] = NULL;
 
-       return mounted;
+       return devices;
 }
 
-int is_mounted(char **mounted, char *devname)
+char **get_mounted_devices(void)
+{
+       return get_mount_entries("/etc/mtab", NULL);
+}
+
+int is_in_array(char **names, char *devname)
 {
        int i;
-       for(i=0; mounted[i]; ++i)
-               if(!strcmp(devname, mounted[i]))
+       if(!names || !devname)
+               return 0;
+       for(i=0; names[i]; ++i)
+               if(!strcmp(devname, names[i]))
                        return 1;
        return 0;
 }
 
-void free_mounted_devices(char **mounted)
+void free_device_names(char **names)
 {
        int i;
-       if(!mounted)
+       if(!names)
                return;
-       for(i=0; mounted[i]; ++i)
-               free(mounted[i]);
-       free(mounted);
+       for(i=0; names[i]; ++i)
+               free(names[i]);
+       free(names);
 }
 
 int is_removable(char *devpath)
@@ -457,7 +466,7 @@ Device *get_devices(void)
                        devices[n_devices].node = nodes[i];
                        devices[n_devices].label = strdup(label);
                        devices[n_devices].description = strdup(buf);
-                       devices[n_devices].mounted = is_mounted(mounted, devname);
+                       devices[n_devices].mounted = is_in_array(mounted, devname);
                        devices[n_devices].time = st.st_mtime;
                        ++n_devices;
                }
@@ -467,7 +476,7 @@ Device *get_devices(void)
        }
 
        free(nodes);
-       free_mounted_devices(mounted);
+       free_device_names(mounted);
 
        if(devices)
        {