2 #ifndef h_pnd_pathiter_h
3 #define h_pnd_pathiter_h
5 // man don'y you wish it was python, perl or c++ right about now?
6 // perl: foreach i ( split ( ':', path ) ) would be sauce right now
8 // for wordexp(); nice thign to have bundled into libc!
13 #include <sys/types.h>
16 #include <ctype.h> // for isdigit()
18 // this really should be replaced by a function pair .. one to
19 // start a new search and one to go to next, bailing when done. Maybe
20 // a state struct. Like we have time. OR perhaps just a single
21 // func with a callback. Whatever.
24 // will be set to 0xFFFF for no limit currently set, or a smaller number
25 // if that is the depth limit
26 unsigned int pathiter_depthlimit;
29 #if 1 // globbing is performed
31 // only iterates actual existing paths; if you want to return
32 // non-matching paths, see below
34 #define SEARCHPATH_PRE \
36 char *head = searchpath; \
37 char chunk [ FILENAME_MAX ]; \
40 /*fprintf ( stderr, "sp %s\n", searchpath );*/ \
43 colon = strchr ( head, ':' ); \
44 end = strchr ( head, '\0' ); \
46 if ( colon && colon < end ) { \
47 memset ( chunk, '\0', FILENAME_MAX ); \
48 strncpy ( chunk, head, colon - head ); \
50 strncpy ( chunk, head, FILENAME_MAX - 1 ); \
53 pathiter_depthlimit = 0xFFFF; \
55 if ( ( lt = strchr ( chunk, '<' ) ) ) { \
56 if ( isdigit ( *(lt+1) ) ) { \
57 pathiter_depthlimit = atoi ( lt + 1 ); \
62 /*fprintf ( stderr, "-> chunk %s limit %d\n", chunk, pathiter_depthlimit );*/ \
64 struct stat statbuf; \
68 char buffer [ FILENAME_MAX ]; \
70 if ( wordexp ( chunk, &_p, 0 ) != 0 ) { \
71 /* ignore this chunk I guess.. */ \
75 for ( _i=0; _i < _p.we_wordc; _i++ ) { \
76 strcpy ( buffer, _w [ _i ] ); \
77 /*fprintf ( stderr, "glob %s\n", buffer );*/ \
78 if ( ( stat ( buffer, &statbuf ) == 0 ) \
79 && ( S_ISDIR(statbuf.st_mode) ) ) \
82 #define SEARCHPATH_POST \
84 } /* for each glob result */ \
86 } /* if wordexp succeeds */ \
87 /* next search path */ \
88 if ( colon && colon < end ) { \
97 // the following will return even non-matching chunks, but is not doing wordexp() expansion on it
99 #define SEARCHCHUNK_PRE \
101 char *head = searchpath; \
102 char chunk [ FILENAME_MAX ]; \
104 /*fprintf ( stderr, "sp %s\n", searchpath );*/ \
107 colon = strchr ( head, ':' ); \
108 end = strchr ( head, '\0' ); \
110 if ( colon && colon < end ) { \
111 memset ( chunk, '\0', FILENAME_MAX ); \
112 strncpy ( chunk, head, colon - head ); \
114 strncpy ( chunk, head, FILENAME_MAX - 1 ); \
117 /*fprintf ( stderr, "-> %s\n", chunk ); */ \
119 char buffer [ FILENAME_MAX ]; \
121 strcpy ( buffer, chunk ); \
122 /*fprintf ( stderr, "glob %s\n", buffer );*/ \
125 #define SEARCHCHUNK_POST \
127 /* next search path */ \
128 if ( colon && colon < end ) { \
137 #endif // globbing is done
140 #if 0 // deprecated simple (no globbing/expansion)
142 #define SEARCHPATH_PRE \
144 char *head = searchpath; \
145 char buffer [ FILENAME_MAX ]; \
148 colon = strchr ( head, ':' ); \
149 end = strchr ( head, '\0' ); \
151 if ( colon && colon < end ) { \
152 memset ( buffer, '\0', FILENAME_MAX ); \
153 strncpy ( buffer, head, colon - head ); \
155 strncpy ( buffer, head, FILENAME_MAX - 1 ); \
158 //printf ( "Path to search: '%s'\n", buffer );
160 #define SEARCHPATH_POST \
161 /* next search path */ \
162 if ( colon && colon < end ) { \
170 #endif // deprecated simple