]>
diplodocus.org Git - nmh/blob - sbr/crawl_folders.c
3 * crawl_folders.c -- crawl folder hierarchy
7 * This code is Copyright (c) 2008, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/crawl_folders.h>
16 struct crawl_context
{
17 int max
; /* how many folders we currently can hold in
18 * the array `folders', increased by
19 * CRAWL_NUMFOLDERS at a time */
20 int total
; /* how many `folders' actually has */
21 char **folders
; /* the array of folders */
27 * Add the folder name into the
28 * list in a sorted fashion.
32 add_folder (char *fold
, struct crawl_context
*crawl
)
36 /* if necessary, reallocate the space for folder names */
37 if (crawl
->foldp
>= crawl
->max
) {
38 crawl
->max
+= CRAWL_NUMFOLDERS
;
39 crawl
->folders
= mh_xrealloc (crawl
->folders
,
40 crawl
->max
* sizeof(char *));
43 for (i
= crawl
->start
; i
< crawl
->foldp
; i
++)
44 if (strcmp (fold
, crawl
->folders
[i
]) < 0) {
45 for (j
= crawl
->foldp
- 1; j
>= i
; j
--)
46 crawl
->folders
[j
+ 1] = crawl
->folders
[j
];
48 crawl
->folders
[i
] = fold
;
53 crawl
->folders
[crawl
->foldp
++] = fold
;
57 add_children (char *name
, struct crawl_context
*crawl
)
65 if (!(dd
= opendir (name
))) {
66 admonish (name
, "unable to read directory ");
70 if (strcmp (name
, ".") == 0) {
73 prefix
= concat (name
, "/", (void *)NULL
);
76 while ((dp
= readdir (dd
))) {
77 /* If the system supports it, try to skip processing of children we
78 * know are not directories or symlinks. */
80 #if defined(HAVE_STRUCT_DIRENT_D_TYPE)
81 if (dp
->d_type
== DT_DIR
) {
83 } else if (dp
->d_type
!= DT_LNK
&& dp
->d_type
!= DT_UNKNOWN
) {
87 if (!strcmp (dp
->d_name
, ".") || !strcmp (dp
->d_name
, "..")) {
90 child
= concat (prefix
, dp
->d_name
, (void *)NULL
);
91 /* If we have no d_type or d_type is DT_LNK or DT_UNKNOWN, stat the
92 * child to see what it is. */
93 if (child_is_folder
== -1) {
94 child_is_folder
= (stat (child
, &st
) != -1 && S_ISDIR(st
.st_mode
));
96 if (child_is_folder
) {
97 /* add_folder saves child in the list, don't free it */
98 add_folder (child
, crawl
);
109 crawl_folders_body (struct crawl_context
*crawl
,
110 char *dir
, crawl_callback_t
*callback
, void *baton
)
113 int os
= crawl
->start
;
114 int of
= crawl
->foldp
;
116 crawl
->start
= crawl
->foldp
;
118 add_children (dir
, crawl
);
120 for (i
= crawl
->start
; i
< crawl
->foldp
; i
++) {
121 char *fold
= crawl
->folders
[i
];
122 int crawl_children
= 1;
124 if (callback
!= NULL
) {
125 crawl_children
= callback (fold
, baton
);
128 if (crawl_children
) {
129 crawl_folders_body (crawl
, fold
, callback
, baton
);
138 crawl_folders (char *dir
, crawl_callback_t
*callback
, void *baton
)
140 struct crawl_context
*crawl
= mh_xmalloc (sizeof(*crawl
));
141 crawl
->max
= CRAWL_NUMFOLDERS
;
142 crawl
->total
= crawl
->start
= crawl
->foldp
= 0;
143 crawl
->folders
= mh_xmalloc (crawl
->max
* sizeof(*crawl
->folders
));
145 crawl_folders_body (crawl
, dir
, callback
, baton
);
147 /* Note that we "leak" the folder names, on the assumption that the caller
149 free (crawl
->folders
);