r12: Added menu sources files.
[rox-filer.git] / ROX-Filer / src / directory.c
blobe1483afc7579052c4cbcbcaba6e0bce33595d989
1 /* vi: set cindent:
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * By Thomas Leonard, <tal197@ecs.soton.ac.uk>.
6 */
8 /* directory.c - code for handling directories (non-GUI) */
10 #include <stdlib.h>
11 #include <stdio.h>
13 #include <glib.h>
14 #include <sys/types.h>
15 #include <dirent.h>
17 #include "support.h"
18 #include "directory.h"
20 /* Calls the callback function for each object in the directory.
21 * '.' and '..' are ignored.
22 * Returns FALSE on error (errno is set).
24 gboolean directory_scan(Directory *directory,
25 void (*callback)(char *name, gpointer user_data),
26 gpointer user_data)
28 DIR *dir;
29 struct dirent *next;
31 g_return_val_if_fail(directory != NULL, FALSE);
33 dir = opendir(directory->path);
34 if (!dir)
35 return FALSE;
37 while ((next = readdir(dir)))
39 char *name = next->d_name;
41 /* Ignore '.' and '..' */
42 if (*name == '.' && (name[1] == '\0'
43 || (name[1] == '.' && name[2] == '\0')))
44 continue;
46 callback(name, user_data);
49 /* An error here should be ignored - NFS seems to generate
50 * unnecessary errors.
52 closedir(dir);
54 return TRUE;
57 /* Public methods */
59 /* Create a new directory for the given path.
60 * Path will be pathdup'd. The directory is NOT scanned yet.
62 Directory *directory_new(char *path)
64 Directory *dir;
66 g_return_val_if_fail(path != NULL, NULL);
68 dir = g_malloc(sizeof(Directory));
70 dir->path = pathdup(path);
71 dir->files = NULL; /* Mark as unscanned */
72 dir->number_of_files = 0;
74 return dir;
77 void directory_destroy(Directory *dir)
79 g_return_if_fail(dir != NULL);
81 printf("[ destroy '%s' ]\n", dir->path);
82 g_free(dir->path);
83 g_free(dir);