summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBerke Viktor <bviktor@hexchat.org>2012-10-18 20:09:15 +0200
committerBerke Viktor <bviktor@hexchat.org>2012-10-18 20:09:15 +0200
commitb686a24d3b39d08d0ff7515fbbe58003708cd964 (patch)
tree4368ad33774fe826922f9ff0a390c97fda69e81e
parent06226c0799b3aa65da095ef5d698ca44ea6726e0 (diff)
Add function for listing subdirs
-rw-r--r--src/common/util.c58
-rw-r--r--src/common/util.h1
2 files changed, 59 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 35c768d2..54ad4f78 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1908,3 +1908,61 @@ hextray_mode ()
 	return 0;
 #endif
 }
+
+/* Routine for listing subfolders of a given folder. ALWAYS free correctly after use, e.g.
+void display_list (GSList *list)
+{
+	GSList *iterator = NULL;
+	for (iterator = list; iterator; iterator = iterator->next)
+	{
+		printf ("%s\t", (char *) iterator->data);
+	}
+}
+
+int main (int argc, char *argv[])
+{
+	GSList *list;
+	list = get_subdirs ("foo");
+	display_list (list);
+#if GLIB_CHECK_VERSION(2,28,0)
+	g_slist_free_full (list, (GFunc) g_free);
+#else
+	g_slist_foreach (list, (GFunc) g_free, NULL);
+	g_slist_free (list);
+#endif
+	return 0;
+}
+*/
+GSList *
+get_subdirs (const char *path)
+{
+	DIR *dir;
+	struct dirent *entry;
+	GSList *dirlist = NULL;
+
+	if (!path)
+	{
+		path = ".";
+	}
+
+	dir = opendir (path);
+
+	if (!dir)
+	{
+		return NULL;
+	}
+
+	entry = readdir (dir);
+
+	while (entry != NULL)
+	{
+		if (entry->d_type == DT_DIR && strcmp (entry->d_name, ".") != 0 && strcmp (entry->d_name, "..") != 0)
+		{
+			dirlist = g_slist_append (dirlist, g_strdup (entry->d_name));
+		}
+
+		entry = readdir (dir);
+	}
+
+	return dirlist;
+}
diff --git a/src/common/util.h b/src/common/util.h
index e7e22524..a2799832 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -60,5 +60,6 @@ void safe_strcpy (char *dest, const char *src, int bytes_left);
 void canonalize_key (char *key);
 int portable_mode ();
 int hextray_mode ();
+GSList *get_subdirs (const char *path);
 
 #endif