5e13aa853abb9e3c750ba0769839df0c0afe34a0
[pandora-libraries.git] / lib / pnd_pathiter.h
1
2 #ifndef h_pnd_pathiter_h
3 #define h_pnd_pathiter_h
4
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
7
8 // for wordexp(); nice thign to have bundled into libc!
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <wordexp.h>
12 // for stat()
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
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.
21
22
23 #if 1 // globbing is performed
24
25 // only iterates actual existing paths; if you want to return
26 // non-matching paths, see below
27
28 #define SEARCHPATH_PRE                            \
29   char *end, *colon;                              \
30   char *head = searchpath;                        \
31   char chunk [ FILENAME_MAX ];                    \
32                                                   \
33   /*fprintf ( stderr, "sp %s\n", searchpath );*/  \
34                                                   \
35   while ( 1 ) {                                   \
36     colon = strchr ( head, ':' );                 \
37     end = strchr ( head, '\0' );                  \
38                                                   \
39     if ( colon && colon < end ) {                 \
40       memset ( chunk, '\0', FILENAME_MAX );       \
41       strncpy ( chunk, head, colon - head );      \
42     } else {                                      \
43       strncpy ( chunk, head, FILENAME_MAX - 1 );  \
44     }                                             \
45                                                   \
46     /*fprintf ( stderr, "-> %s\n", chunk ); */    \
47                                                   \
48     struct stat statbuf;                          \
49     wordexp_t _p;                                 \
50     char **_w;                                    \
51     int _i;                                       \
52     char buffer [ FILENAME_MAX ];                 \
53                                                   \
54     if ( wordexp ( chunk, &_p, 0 ) != 0 ) {       \
55       /* ignore this chunk I guess.. */           \
56     } else {                                      \
57       _w = _p.we_wordv;                           \
58                                                   \
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) ) )       \
64         { /* user code */
65
66 #define SEARCHPATH_POST                           \
67         } /* user code */                         \
68       } /* for each glob result */                \
69       wordfree ( &_p );                           \
70     } /* if wordexp succeeds */                   \
71     /* next search path */                        \
72     if ( colon && colon < end ) {                 \
73       head = colon + 1;                           \
74     } else {                                      \
75       break; /* done! */                          \
76     }                                             \
77                                                   \
78   } // while
79
80
81 // the following will return even non-matching chunks, but is not doing wordexp() expansion on it
82
83 #define SEARCHCHUNK_PRE                           \
84   char *end, *colon;                              \
85   char *head = searchpath;                        \
86   char chunk [ FILENAME_MAX ];                    \
87                                                   \
88   /*fprintf ( stderr, "sp %s\n", searchpath );*/  \
89                                                   \
90   while ( 1 ) {                                   \
91     colon = strchr ( head, ':' );                 \
92     end = strchr ( head, '\0' );                  \
93                                                   \
94     if ( colon && colon < end ) {                 \
95       memset ( chunk, '\0', FILENAME_MAX );       \
96       strncpy ( chunk, head, colon - head );      \
97     } else {                                      \
98       strncpy ( chunk, head, FILENAME_MAX - 1 );  \
99     }                                             \
100                                                   \
101     /*fprintf ( stderr, "-> %s\n", chunk ); */    \
102                                                   \
103     char buffer [ FILENAME_MAX ];                 \
104                                                   \
105     strcpy ( buffer, chunk );                     \
106     /*fprintf ( stderr, "glob %s\n", buffer );*/  \
107     { /* user code */
108
109 #define SEARCHCHUNK_POST                          \
110     } /* user code */                             \
111     /* next search path */                        \
112     if ( colon && colon < end ) {                 \
113       head = colon + 1;                           \
114     } else {                                      \
115       break; /* done! */                          \
116     }                                             \
117                                                   \
118   } // while
119
120
121 #endif // globbing is done
122
123
124 #if 0 // deprecated simple (no globbing/expansion)
125
126 #define SEARCHPATH_PRE                            \
127   char *end, *colon;                              \
128   char *head = searchpath;                        \
129   char buffer [ FILENAME_MAX ];                   \
130                                                   \
131   while ( 1 ) {                                   \
132     colon = strchr ( head, ':' );                 \
133     end = strchr ( head, '\0' );                  \
134                                                   \
135     if ( colon && colon < end ) {                 \
136       memset ( buffer, '\0', FILENAME_MAX );      \
137       strncpy ( buffer, head, colon - head );     \
138     } else {                                      \
139       strncpy ( buffer, head, FILENAME_MAX - 1 ); \
140     }                                             \
141                                                   \
142     //printf ( "Path to search: '%s'\n", buffer );
143
144 #define SEARCHPATH_POST                           \
145     /* next search path */                        \
146     if ( colon && colon < end ) {                 \
147       head = colon + 1;                           \
148     } else {                                      \
149       break; /* done! */                          \
150     }                                             \
151                                                   \
152   } // while
153
154 #endif // deprecated simple
155
156
157 #endif // #ifndef