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>
17 // this really should be replaced by a function pair .. one to
18 // start a new search and one to go to next, bailing when done. Maybe
19 // a state struct. Like we have time. OR perhaps just a single
20 // func with a callback. Whatever.
23 #if 1 // globbing is performed
25 // only iterates actual existing paths; if you want to return
26 // non-matching paths, see below
28 #define SEARCHPATH_PRE \
30 char *head = searchpath; \
31 char chunk [ FILENAME_MAX ]; \
33 /*fprintf ( stderr, "sp %s\n", searchpath );*/ \
36 colon = strchr ( head, ':' ); \
37 end = strchr ( head, '\0' ); \
39 if ( colon && colon < end ) { \
40 memset ( chunk, '\0', FILENAME_MAX ); \
41 strncpy ( chunk, head, colon - head ); \
43 strncpy ( chunk, head, FILENAME_MAX - 1 ); \
46 /*fprintf ( stderr, "-> %s\n", chunk ); */ \
48 struct stat statbuf; \
52 char buffer [ FILENAME_MAX ]; \
54 if ( wordexp ( chunk, &_p, 0 ) != 0 ) { \
55 /* ignore this chunk I guess.. */ \
59 for ( _i=0; _i < _p.we_wordc; _i++ ) { \
60 strcpy ( buffer, _w [ _i ] ); \
61 /*fprintf ( stderr, "glob %s\n", buffer );*/ \
62 if ( ( stat ( buffer, &statbuf ) == 0 ) \
63 && ( S_ISDIR(statbuf.st_mode) ) ) \
66 #define SEARCHPATH_POST \
68 } /* for each glob result */ \
70 } /* if wordexp succeeds */ \
71 /* next search path */ \
72 if ( colon && colon < end ) { \
81 // the following will return even non-matching chunks, but is not doing wordexp() expansion on it
83 #define SEARCHCHUNK_PRE \
85 char *head = searchpath; \
86 char chunk [ FILENAME_MAX ]; \
88 /*fprintf ( stderr, "sp %s\n", searchpath );*/ \
91 colon = strchr ( head, ':' ); \
92 end = strchr ( head, '\0' ); \
94 if ( colon && colon < end ) { \
95 memset ( chunk, '\0', FILENAME_MAX ); \
96 strncpy ( chunk, head, colon - head ); \
98 strncpy ( chunk, head, FILENAME_MAX - 1 ); \
101 /*fprintf ( stderr, "-> %s\n", chunk ); */ \
103 char buffer [ FILENAME_MAX ]; \
105 strcpy ( buffer, chunk ); \
106 /*fprintf ( stderr, "glob %s\n", buffer );*/ \
109 #define SEARCHCHUNK_POST \
111 /* next search path */ \
112 if ( colon && colon < end ) { \
121 #endif // globbing is done
124 #if 0 // deprecated simple (no globbing/expansion)
126 #define SEARCHPATH_PRE \
128 char *head = searchpath; \
129 char buffer [ FILENAME_MAX ]; \
132 colon = strchr ( head, ':' ); \
133 end = strchr ( head, '\0' ); \
135 if ( colon && colon < end ) { \
136 memset ( buffer, '\0', FILENAME_MAX ); \
137 strncpy ( buffer, head, colon - head ); \
139 strncpy ( buffer, head, FILENAME_MAX - 1 ); \
142 //printf ( "Path to search: '%s'\n", buffer );
144 #define SEARCHPATH_POST \
145 /* next search path */ \
146 if ( colon && colon < end ) { \
154 #endif // deprecated simple