@@ -73,6 +73,8 @@ OF SUCH DAMAGE.
7373#include "utils.h"
7474
7575struct PoolArgs {
76+ int lstat_first_file ;
77+
7678 sll_t * queue ;
7779 void (* enqueue )(sll_t * sll , void * data );
7880
@@ -93,34 +95,38 @@ int processdir(struct PoolArgs *pa, char *path) {
9395
9496 const size_t path_len = strlen (path );
9597
96- /* int count = 0; */
98+ int count = 0 ;
9799
98100 struct dirent * entry = NULL ;
99101 while ((entry = readdir (dir ))) {
100- if (entry -> d_type == DT_DIR ) {
101- const size_t entry_len = strlen (entry -> d_name );
102+ const size_t entry_len = strlen (entry -> d_name );
102103
103- if (((entry_len == 1 ) && !strncmp (entry -> d_name , "." , 1 )) ||
104- ((entry_len == 2 ) && !strncmp (entry -> d_name , ".." , 2 ))) {
105- continue ;
106- }
107-
108- const size_t subdir_len = path_len + 1 + entry_len ;
109- char * cp = malloc (subdir_len + 1 );
110- SNFORMAT_S (cp , subdir_len + 1 , 3 ,
111- path , path_len ,
112- "/" , (size_t ) 1 ,
113- entry -> d_name , entry_len );
104+ if (((entry_len == 1 ) && !strncmp (entry -> d_name , "." , 1 )) ||
105+ ((entry_len == 2 ) && !strncmp (entry -> d_name , ".." , 2 ))) {
106+ continue ;
107+ }
114108
115- /* if (count == 0) { */
116- /* struct stat st; */
117- /* lstat(cp, &st); */
118- /* count++; */
119- /* } */
109+ const size_t subdir_len = path_len + 1 + entry_len ;
110+ char * cp = malloc (subdir_len + 1 );
111+ SNFORMAT_S (cp , subdir_len + 1 , 3 ,
112+ path , path_len ,
113+ "/" , (size_t ) 1 ,
114+ entry -> d_name , entry_len );
120115
116+ if (entry -> d_type == DT_DIR ) {
121117 pa -> enqueue (pa -> queue , cp );
122118 }
123119 else {
120+ if (pa -> lstat_first_file ) {
121+ if (count == 0 ) {
122+ struct stat st ;
123+ lstat (cp , & st );
124+ count = 1 ;
125+ }
126+ }
127+
128+ free (cp );
129+
124130 pa -> nondirs ++ ;
125131 }
126132 }
@@ -132,7 +138,8 @@ int processdir(struct PoolArgs *pa, char *path) {
132138}
133139
134140static void sub_help (void ) {
135- printf ("<b|d> breadth/depth first traversal" );
141+ printf ("<b|d> breadth/depth first traversal\n" );
142+ printf ("<0|1> whether or not to lstat the first file encountered by readdir\n" );
136143 printf ("dir... walk one or more trees\n" );
137144 printf ("\n" );
138145}
@@ -145,7 +152,7 @@ int main(int argc, char *argv[]) {
145152 };
146153
147154 struct input in ;
148- process_args_and_maybe_exit (options , 2 , "<b|d> dir..." , & in );
155+ process_args_and_maybe_exit (options , 2 , "<b|d> <lstat-first-file> dir..." , & in );
149156
150157 const char * pos0 = argv [idx ++ ];
151158 if (!strlen (pos0 )) {
@@ -154,10 +161,13 @@ int main(int argc, char *argv[]) {
154161 return EXIT_FAILURE ;
155162 }
156163
164+ const char * pos1 = argv [idx ++ ];
165+
157166 sll_t queue ;
158167 sll_init (& queue );
159168
160169 struct PoolArgs pa = {
170+ .lstat_first_file = !!(pos1 [0 ] - '0' ),
161171 .queue = & queue ,
162172 .enqueue = NULL ,
163173 .dirs = 0 ,
0 commit comments