change debugfs finder function

Change the debugfs_locate_mpoint function to be nicer and more
efficient.
Another effect is that fixes a segmentation fault when calling powerdebug --dump

Signed-off-by: Daniel Lezcano <daniel.lezcano@free.fr>
Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
diff --git a/clocks.c b/clocks.c
index c42f589..ff79235 100644
--- a/clocks.c
+++ b/clocks.c
@@ -13,6 +13,9 @@
  *       - initial API and implementation
  *******************************************************************************/
 
+#include <stdio.h>
+#include <mntent.h>
+
 #include "powerdebug.h"
 #include "clocks.h"
 
@@ -20,14 +23,36 @@
 static char clk_name[NAME_MAX];
 static int  bold[MAX_LINES];
 
+static int locate_debugfs(char *clk_path)
+{
+	const char *mtab = "/proc/mounts";
+	struct mntent *mntent;
+	int ret = -1;
+	FILE *file = NULL;
+
+	file = setmntent(mtab, "r");
+	if (!file)
+		return -1;
+
+	while ((mntent = getmntent(file))) {
+
+		if (strcmp(mntent->mnt_type, "debugfs"))
+			continue;
+
+		strcpy(clk_path, mntent->mnt_dir);
+		ret = 0;
+		break;
+	}
+
+	fclose(file);
+	return ret;
+}
+
 int init_clock_details(bool dump, int selectedwindow)
 {
-	char *path = debugfs_locate_mpoint();
 	struct stat buf;
 
-	if (path)
-		strcpy(clk_dir_path, path);
-	else {
+	if (locate_debugfs(clk_dir_path)) {
 		if (!dump) {
 			create_selectedwindow(selectedwindow);
 			sprintf(clock_lines[0], "Unable to locate debugfs "
@@ -43,6 +68,7 @@
 			exit(1);
 		}
 	}
+
 	sprintf(clk_dir_path, "%s/clock", clk_dir_path);
 	//strcpy(clk_dir_path, "/debug/clock"); // Hardcoded for testing..
 	if (stat(clk_dir_path, &buf)) {
@@ -539,37 +565,3 @@
 		}
 	}
 }
-
-char *debugfs_locate_mpoint(void)
-{
-	int ret;
-	FILE *filep;
-	char **path;
-	char fsname[64];
-	struct statfs sfs;
-
-	path = likely_mpoints;
-	while (*path) {
-		ret = statfs(*path, &sfs);
-		if (ret >= 0 && sfs.f_type == (long)DEBUGFS_MAGIC)
-			return *path;
-		path++;
-	}
-
-	filep = fopen("/proc/mounts", "r");
-	if (filep == NULL) {
-		fprintf(stderr, "powerdebug: Error opening /proc/mounts.");
-		exit(1);
-	}
-
-	while (fscanf(filep, "%*s %s %s %*s %*d %*d\n",
-			debugfs_mntpoint, fsname) == 2)
-		if (!strcmp(fsname, "debugfs"))
-			break;
-	fclose(filep);
-
-	if (strcmp(fsname, "debugfs"))
-		return NULL;
-
-	return debugfs_mntpoint;
-}
diff --git a/clocks.h b/clocks.h
index 4b176d0..dd9216a 100644
--- a/clocks.h
+++ b/clocks.h
@@ -36,17 +36,10 @@
 	struct clock_info **children;
 } *clocks_info;
 
-char debugfs_mntpoint[1024];
 char clock_lines[MAX_LINES][128];
 int  clock_line_no;
 int  old_clock_line_no;
 
-char *likely_mpoints[] = {
-	"/sys/kernel/debug",
-	"/debug",
-	NULL
-};
-
 void add_clock_details_recur(struct clock_info *clk, int hrow, int selected);
 void destroy_clocks_info(void);
 void destroy_clocks_info_recur(struct clock_info *clock);
diff --git a/powerdebug.h b/powerdebug.h
index d2bd4ec..f424c1f 100644
--- a/powerdebug.h
+++ b/powerdebug.h
@@ -42,7 +42,6 @@
 extern int  init_clock_details(bool dump, int selectedwindow);
 extern void print_clock_header(void);
 extern void print_one_clock(int line, char *str, int bold, int highlight);
-extern char *debugfs_locate_mpoint(void);
 
 extern void get_sensor_info(char *path, char *name, char *sensor, int verbose);
 extern int  read_and_print_sensor_info(int verbose);