Added globbing support for all searchpaths! (can now search /media/*/pandora/apps...
[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 #define SEARCHPATH_PRE                            \
26   char *end, *colon;                              \
27   char *head = searchpath;                        \
28   char chunk [ FILENAME_MAX ];                    \
29                                                   \
30   /*fprintf ( stderr, "sp %s\n", searchpath );*/  \
31                                                   \
32   while ( 1 ) {                                   \
33     colon = strchr ( head, ':' );                 \
34     end = strchr ( head, '\0' );                  \
35                                                   \
36     if ( colon && colon < end ) {                 \
37       memset ( chunk, '\0', FILENAME_MAX );       \
38       strncpy ( chunk, head, colon - head );      \
39     } else {                                      \
40       strncpy ( chunk, head, FILENAME_MAX - 1 );  \
41     }                                             \
42                                                   \
43     /*fprintf ( stderr, "-> %s\n", chunk ); */    \
44                                                   \
45     struct stat statbuf;                          \
46     wordexp_t _p;                                 \
47     char **_w;                                    \
48     int _i;                                       \
49     char buffer [ FILENAME_MAX ];                 \
50                                                   \
51     if ( wordexp ( chunk, &_p, 0 ) != 0 ) {       \
52       /* ignore this chunk I guess.. */           \
53     } else {                                      \
54       _w = _p.we_wordv;                           \
55                                                   \
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) ) )       \
61         { /* user code */
62
63 #define SEARCHPATH_POST                           \
64         } /* user code */                         \
65       } /* for each glob result */                \
66       wordfree ( &_p );                           \
67     } /* if wordexp succeeds */                   \
68     /* next search path */                        \
69     if ( colon && colon < end ) {                 \
70       head = colon + 1;                           \
71     } else {                                      \
72       break; /* done! */                          \
73     }                                             \
74                                                   \
75   } // while
76
77 #endif // globbing is done
78
79
80 #if 0 // deprecated simple (no globbing/expansion)
81
82 #define SEARCHPATH_PRE                            \
83   char *end, *colon;                              \
84   char *head = searchpath;                        \
85   char buffer [ FILENAME_MAX ];                   \
86                                                   \
87   while ( 1 ) {                                   \
88     colon = strchr ( head, ':' );                 \
89     end = strchr ( head, '\0' );                  \
90                                                   \
91     if ( colon && colon < end ) {                 \
92       memset ( buffer, '\0', FILENAME_MAX );      \
93       strncpy ( buffer, head, colon - head );     \
94     } else {                                      \
95       strncpy ( buffer, head, FILENAME_MAX - 1 ); \
96     }                                             \
97                                                   \
98     //printf ( "Path to search: '%s'\n", buffer );
99
100 #define SEARCHPATH_POST                           \
101     /* next search path */                        \
102     if ( colon && colon < end ) {                 \
103       head = colon + 1;                           \
104     } else {                                      \
105       break; /* done! */                          \
106     }                                             \
107                                                   \
108   } // while
109
110 #endif // deprecated simple
111
112
113 #endif // #ifndef