]>
diplodocus.org Git - nmh/blob - sbr/crawl_folders.c
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.
9 #include <h/crawl_folders.h>
12 struct crawl_context
{
13 int max
; /* how many folders we currently can hold in
14 * the array `folders', increased by
15 * CRAWL_NUMFOLDERS at a time */
16 char **folders
; /* the array of folders */
22 * Add the folder name into the
23 * list in a sorted fashion.
27 add_folder (char *fold
, struct crawl_context
*crawl
)
31 /* if necessary, reallocate the space for folder names */
32 if (crawl
->foldp
>= crawl
->max
) {
33 crawl
->max
+= CRAWL_NUMFOLDERS
;
34 crawl
->folders
= mh_xrealloc (crawl
->folders
,
35 crawl
->max
* sizeof(char *));
38 for (i
= crawl
->start
; i
< crawl
->foldp
; i
++)
39 if (strcmp (fold
, crawl
->folders
[i
]) < 0) {
40 for (j
= crawl
->foldp
- 1; j
>= i
; j
--)
41 crawl
->folders
[j
+ 1] = crawl
->folders
[j
];
43 crawl
->folders
[i
] = fold
;
47 crawl
->folders
[crawl
->foldp
++] = fold
;
51 add_children (char *name
, struct crawl_context
*crawl
)
59 if (!(dd
= opendir (name
))) {
60 admonish (name
, "unable to read directory ");
64 if (strcmp (name
, ".") == 0) {
65 prefix
= mh_xstrdup("");
67 prefix
= concat(name
, "/", NULL
);
70 while ((dp
= readdir (dd
))) {
71 /* If the system supports it, try to skip processing of children we
72 * know are not directories or symlinks. */
74 #if defined(HAVE_STRUCT_DIRENT_D_TYPE)
75 if (dp
->d_type
== DT_DIR
) {
77 } else if (dp
->d_type
!= DT_LNK
&& dp
->d_type
!= DT_UNKNOWN
) {
81 if (!strcmp (dp
->d_name
, ".") || !strcmp (dp
->d_name
, "..")) {
84 child
= concat(prefix
, dp
->d_name
, NULL
);
85 /* If we have no d_type or d_type is DT_LNK or DT_UNKNOWN, stat the
86 * child to see what it is. */
87 if (child_is_folder
== -1) {
88 child_is_folder
= (stat (child
, &st
) != -1 && S_ISDIR(st
.st_mode
));
90 if (child_is_folder
) {
91 /* add_folder saves child in the list, don't free it */
92 add_folder (child
, crawl
);
103 crawl_folders_body (struct crawl_context
*crawl
,
104 char *dir
, crawl_callback_t
*callback
, void *baton
)
107 int os
= crawl
->start
;
108 int of
= crawl
->foldp
;
110 crawl
->start
= crawl
->foldp
;
112 add_children (dir
, crawl
);
114 for (i
= crawl
->start
; i
< crawl
->foldp
; i
++) {
115 char *fold
= crawl
->folders
[i
];
116 int crawl_children
= 1;
118 if (callback
!= NULL
) {
119 crawl_children
= callback (fold
, baton
);
122 if (crawl_children
) {
123 crawl_folders_body (crawl
, fold
, callback
, baton
);
132 crawl_folders (char *dir
, crawl_callback_t
*callback
, void *baton
)
134 struct crawl_context
*crawl
;
136 crawl
->max
= CRAWL_NUMFOLDERS
;
137 crawl
->start
= crawl
->foldp
= 0;
138 crawl
->folders
= mh_xmalloc (crawl
->max
* sizeof(*crawl
->folders
));
140 crawl_folders_body (crawl
, dir
, callback
, baton
);
142 /* Note that we "leak" the folder names, on the assumption that the caller
144 free (crawl
->folders
);