DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Tvrtko has posted 6 posts at DZone. View Full User Profile

Put Mounted Drives List Into Glist

04.12.2008
| 3010 views |
  • submit to reddit
        Function that gets list of mounted drives from '/etc/fstab' and '/etc/mtab' files and puts data in the GList object (from GLib library) that can be further used in GTK.

#include <glib.h>
#include <mntent.h>
#include <string.h>

GList * g_get_drives_list(GList * g) {
	FILE *fstab = NULL;
	struct mntent *part = NULL;
	gchar *mntp = NULL;
	
	if ((fstab = setmntent( "/etc/fstab", "r" )) != NULL) {
		while ((part = getmntent(fstab))  != NULL) {
			if((strcmp(part->mnt_type, "proc")) != 0 && (strcmp(part->mnt_type, "devpts")) != 0 
																					&& (strcmp(part->mnt_type, "swap")) != 0) {
				mntp = g_strdup(part->mnt_dir);
				g=g_list_append(g, mntp);
			}
		}
		endmntent(fstab);
	}
	
	if ((fstab = setmntent( "/etc/mtab", "r")) != NULL) {
		while ((part = getmntent(fstab)) != NULL) {
			if (part->mnt_type != NULL) {
				if((strcmp(part->mnt_type, "proc")) != 0 && (strcmp(part->mnt_type, "devpts")) != 0
						&& (strcmp(part->mnt_type, "swap")) != 0 && (strcmp(part->mnt_type, "sysfs")) != 0
						&& (strcmp(part->mnt_type, "tmpfs")) != 0 && (strcmp(part->mnt_type, "fuseblk")) != 0
						&& (strcmp(part->mnt_type, "securityfs")) != 0) {
					if((g_list_find_custom(g, part->mnt_dir, (GCompareFunc)strcmp)) == 0) {
						mntp=g_strdup(part->mnt_dir);
						g=g_list_append(g, mntp);
					}
				}
			}
		}
		endmntent(fstab);
	}
	return g;
}