4 #include <string.h> // strcmp()
5 #include <dirent.h> // opendir() readdir() closedir()
6 #include <sys/stat.h> // IS_DIR
11 extern int out_of_memory
;
13 // dir-is-album: all files in the dir ARE the same album, use the first name found.
14 // dir-is-album-name: if no tag found, use the dir's instead of "<no album tag>"
16 // files in different dirs are ALWAYS different albums
18 static char* strip_path
= NULL
;
19 static char* add_path
= NULL
;
21 static int iterate_dir(char* dir
);
22 /* Iterates over each item in the given directory
23 * calls add_file() on each file
24 * calls iterate_directory() on each directory (recursively)
27 static int iterate_dir(char* dir
) {
35 if(!( d
= opendir(dir
) )) {
36 DEBUGF("iterate_dir: could not open directory \"%s\"\n", dir
);
40 while(( e
= readdir(d
) )) {
43 if( strcmp(e
->d_name
, ".") == 0 || strcmp(e
->d_name
, "..") == 0 )
44 continue; // we don't want to descend or loop around...
46 path
= malloc(strlen(dir
) + 1 + strlen(e
->d_name
) + 1); // "dir/d_name\0"
48 DEBUGF("iterate_dir: could not malloc() directory-entry-name\n");
53 strcat(path
, e
->d_name
);
55 if( stat(path
, &s
) ) {
56 DEBUGF("iterate_dir: could not stat(\"%s\")\n", path
);
60 if( S_ISDIR(s
.st_mode
) ) {
61 #elif defined OS_ROCKBOX
62 #error "Rockbox: not yet implemented: don't know how to list directory"
64 #elif defined OS_WINDOWS
66 #error "Windows: not yet implemented: don't know how to list directory"
69 #error "No OS specified: don't know how to list directory"
71 if(( rc
= iterate_dir(path
) )) {
76 if(( rc
= db_add(path
, strip_path
, add_path
) )) {
85 DEBUGF("iterate_dir: could not close directory \"%s\", ignoring...\n", dir
);
91 int main(int argc
, char* argv
[]) {
95 printf("usage: ./songdb dir\n");
99 strip_path
= "/home/niels/";
104 iterate_dir(argv
[1]);
106 fd
= fopen("xxx.db", "w");