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 #define SEARCHPATH_PRE \
27 char *head = searchpath; \
28 char chunk [ FILENAME_MAX ]; \
30 /*fprintf ( stderr, "sp %s\n", searchpath );*/ \
33 colon = strchr ( head, ':' ); \
34 end = strchr ( head, '\0' ); \
36 if ( colon && colon < end ) { \
37 memset ( chunk, '\0', FILENAME_MAX ); \
38 strncpy ( chunk, head, colon - head ); \
40 strncpy ( chunk, head, FILENAME_MAX - 1 ); \
43 /*fprintf ( stderr, "-> %s\n", chunk ); */ \
45 struct stat statbuf; \
49 char buffer [ FILENAME_MAX ]; \
51 if ( wordexp ( chunk, &_p, 0 ) != 0 ) { \
52 /* ignore this chunk I guess.. */ \
56 for ( _i=0; _i < _p.we_wordc; _i++ ) { \
57 strcpy ( buffer, _w [ _i ] ); \
58 /*fprintf ( stderr, "glob %s\n", buffer );*/ \
59 if ( ( stat ( buffer, &statbuf ) == 0 ) \
60 && ( S_ISDIR(statbuf.st_mode) ) ) \
63 #define SEARCHPATH_POST \
65 } /* for each glob result */ \
67 } /* if wordexp succeeds */ \
68 /* next search path */ \
69 if ( colon && colon < end ) { \
77 #endif // globbing is done
80 #if 0 // deprecated simple (no globbing/expansion)
82 #define SEARCHPATH_PRE \
84 char *head = searchpath; \
85 char buffer [ FILENAME_MAX ]; \
88 colon = strchr ( head, ':' ); \
89 end = strchr ( head, '\0' ); \
91 if ( colon && colon < end ) { \
92 memset ( buffer, '\0', FILENAME_MAX ); \
93 strncpy ( buffer, head, colon - head ); \
95 strncpy ( buffer, head, FILENAME_MAX - 1 ); \
98 //printf ( "Path to search: '%s'\n", buffer );
100 #define SEARCHPATH_POST \
101 /* next search path */ \
102 if ( colon && colon < end ) { \
110 #endif // deprecated simple