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)
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;
}
}
free(nodes);
- free_mounted_devices(mounted);
+ free_device_names(mounted);
if(devices)
{