<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: list code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 15 Oct 2008 17:13:47 GMT</pubDate>
    <description>DZone Snippets: list code</description>
    <item>
      <title>Listing the files and subdirectories in C - Linux</title>
      <link>http://snippets.dzone.com/posts/show/5734</link>
      <description>// program lists the files and subdirectories within a given directory in full path&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;#include &lt;dirent.h&gt;&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2);&lt;br /&gt;&lt;br /&gt;int main () {&lt;br /&gt;	struct dirent *dp;&lt;br /&gt;&lt;br /&gt;        // enter existing path to directory below&lt;br /&gt;	const char *dir_path="/path/to/directory/to/list";&lt;br /&gt;	DIR *dir = opendir(dir_path);&lt;br /&gt;	while ((dp=readdir(dir)) != NULL) {&lt;br /&gt;		char *tmp;&lt;br /&gt;		tmp = path_cat(dir_path, dp-&gt;d_name);&lt;br /&gt;		printf("%s\n", tmp);&lt;br /&gt;		free(tmp);&lt;br /&gt;		tmp=NULL;&lt;br /&gt;	}&lt;br /&gt;	closedir(dir);&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2) {&lt;br /&gt;	size_t str1_len = strlen(str1);&lt;br /&gt;	size_t str2_len = strlen(str2);&lt;br /&gt;	char *result;&lt;br /&gt;	result = malloc((str1_len+str2_len+1)*sizeof *result);&lt;br /&gt;	strcpy (result,str1);&lt;br /&gt;	int i,j;&lt;br /&gt;	for(i=str1_len, j=0; ((i&lt;(str1_len+str2_len)) &amp;&amp; (j&lt;str2_len));i++, j++) {&lt;br /&gt;		result[i]=str2[j];&lt;br /&gt;	}&lt;br /&gt;	result[str1_len+str2_len]='\0';&lt;br /&gt;	return result;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Jul 2008 01:13:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5734</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>Put mounted drives list into Glist</title>
      <link>http://snippets.dzone.com/posts/show/5350</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;glib.h&gt;&lt;br /&gt;#include &lt;mntent.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;GList * g_get_drives_list(GList * g) {&lt;br /&gt;	FILE *fstab = NULL;&lt;br /&gt;	struct mntent *part = NULL;&lt;br /&gt;	gchar *mntp = NULL;&lt;br /&gt;	&lt;br /&gt;	if ((fstab = setmntent( "/etc/fstab", "r" )) != NULL) {&lt;br /&gt;		while ((part = getmntent(fstab))  != NULL) {&lt;br /&gt;			if((strcmp(part-&gt;mnt_type, "proc")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "devpts")) != 0 &lt;br /&gt;																					&amp;&amp; (strcmp(part-&gt;mnt_type, "swap")) != 0) {&lt;br /&gt;				mntp = g_strdup(part-&gt;mnt_dir);&lt;br /&gt;				g=g_list_append(g, mntp);&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		endmntent(fstab);&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	if ((fstab = setmntent( "/etc/mtab", "r")) != NULL) {&lt;br /&gt;		while ((part = getmntent(fstab)) != NULL) {&lt;br /&gt;			if (part-&gt;mnt_type != NULL) {&lt;br /&gt;				if((strcmp(part-&gt;mnt_type, "proc")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "devpts")) != 0&lt;br /&gt;						&amp;&amp; (strcmp(part-&gt;mnt_type, "swap")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "sysfs")) != 0&lt;br /&gt;						&amp;&amp; (strcmp(part-&gt;mnt_type, "tmpfs")) != 0 &amp;&amp; (strcmp(part-&gt;mnt_type, "fuseblk")) != 0&lt;br /&gt;						&amp;&amp; (strcmp(part-&gt;mnt_type, "securityfs")) != 0) {&lt;br /&gt;					if((g_list_find_custom(g, part-&gt;mnt_dir, (GCompareFunc)strcmp)) == 0) {&lt;br /&gt;						mntp=g_strdup(part-&gt;mnt_dir);&lt;br /&gt;						g=g_list_append(g, mntp);&lt;br /&gt;					}&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		endmntent(fstab);&lt;br /&gt;	}&lt;br /&gt;	return g;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 12 Apr 2008 13:22:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5350</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
  </channel>
</rss>
