]>
diplodocus.org Git - nmh/blob - sbr/crawl_folders.c
b7901b23147d6c1eb7a7b8c4a938e6b5ade3c075
1 /* crawl_folders.c -- crawl folder hierarchy
3 * This code is Copyright (c) 2008, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/crawl_folders.h>
13 struct crawl_context
{
14 int max
; /* how many folders we currently can hold in
15 * the array `folders', increased by
16 * CRAWL_NUMFOLDERS at a time */
17 char **folders
; /* the array of folders */
23 * Add the folder name into the
24 * list in a sorted fashion.
28 add_folder (char *fold
, struct crawl_context
*crawl
)
32 /* if necessary, reallocate the space for folder names */
33 if (crawl
->foldp
>= crawl
->max
) {
34 crawl
->max
+= CRAWL_NUMFOLDERS
;
35 crawl
->folders
= mh_xrealloc (crawl
->folders
,
36 crawl
->max
* sizeof(char *));
39 for (i
= crawl
->start
; i
< crawl
->foldp
; i
++)
40 if (strcmp (fold
, crawl
->folders
[i
]) < 0) {
41 for (j
= crawl
->foldp
- 1; j
>= i
; j
--)
42 crawl
->folders
[j
+ 1] = crawl
->folders
[j
];
44 crawl
->folders
[i
] = fold
;
48 crawl
->folders
[crawl
->foldp
++] = fold
;
52 add_children (char *name
, struct crawl_context
*crawl
)
60 if (!(dd
= opendir (name
))) {
61 admonish (name
, "unable to read directory ");
65 if (strcmp (name
, ".") == 0) {
66 prefix
= mh_xstrdup("");
68 prefix
= concat(name
, "/", NULL
);
71 while ((dp
= readdir (dd
))) {
72 /* If the system supports it, try to skip processing of children we
73 * know are not directories or symlinks. */
75 #if defined(HAVE_STRUCT_DIRENT_D_TYPE)
76 if (dp
->d_type
== DT_DIR
) {
78 } else if (dp
->d_type
!= DT_LNK
&& dp
->d_type
!= DT_UNKNOWN
) {
82 if (!strcmp (dp
->d_name
, ".") || !strcmp (dp
->d_name
, "..")) {
85 child
= concat(prefix
, dp
->d_name
, NULL
);
86 /* If we have no d_type or d_type is DT_LNK or DT_UNKNOWN, stat the
87 * child to see what it is. */
88 if (child_is_folder
== -1) {
89 child_is_folder
= (stat (child
, &st
) != -1 && S_ISDIR(st
.st_mode
));
91 if (child_is_folder
) {
92 /* add_folder saves child in the list, don't free it */
93 add_folder (child
, crawl
);
104 crawl_folders_body (struct crawl_context
*crawl
,
105 char *dir
, crawl_callback_t
*callback
, void *baton
)
108 int os
= crawl
->start
;
109 int of
= crawl
->foldp
;
111 crawl
->start
= crawl
->foldp
;
113 add_children (dir
, crawl
);
115 for (i
= crawl
->start
; i
< crawl
->foldp
; i
++) {
116 char *fold
= crawl
->folders
[i
];
117 int crawl_children
= 1;
119 if (callback
!= NULL
) {
120 crawl_children
= callback (fold
, baton
);
123 if (crawl_children
) {
124 crawl_folders_body (crawl
, fold
, callback
, baton
);
133 crawl_folders (char *dir
, crawl_callback_t
*callback
, void *baton
)
135 struct crawl_context
*crawl
;
137 crawl
->max
= CRAWL_NUMFOLDERS
;
138 crawl
->start
= crawl
->foldp
= 0;
139 crawl
->folders
= mh_xmalloc (crawl
->max
* sizeof(*crawl
->folders
));
141 crawl_folders_body (crawl
, dir
, callback
, baton
);
143 /* Note that we "leak" the folder names, on the assumption that the caller
145 free (crawl
->folders
);