Index: library.c =================================================================== --- library.c (revision 3266) +++ library.c (revision 3267) @@ -32,143 +32,7 @@ #include #include #include -#include "message.h" #include "library.h" -void csch_lib_find_alloc(csch_lib_it_t *it, rnd_hidlib_t *hidlib, vts0_t *libs, const char *cwd) -{ - int n; - memset(it, 0, sizeof(csch_lib_it_t)); - it->hidlib = hidlib; - it->dir = NULL; - gds_init(&it->path); - vts0_init(&it->names); - vts0_init(&it->open); - for(n = libs->used-1; n >= 0; n--) { - char *p = libs->array[n]; - if ((p == NULL) || (*p =='\0')) - continue; - if ((p[0] != '~') && (p[0] != '$') && (!rnd_is_path_abs(p))) { - if (p[0] == '.') p++; - if (p[0] == '/') p++; - vts0_append(&it->open, rnd_concat(cwd, "/", p, NULL)); - } - else - vts0_append(&it->open, rnd_strdup(p)); - } -} - -void csch_lib_find_add_name(csch_lib_it_t *it, const char *name) -{ - vts0_append(&it->names, (char *)name); -} - - -void csch_lib_find_free(csch_lib_it_t *it) -{ - long n; - for(n = 0; n < it->open.used; n++) - free(it->open.array[n]); - vts0_uninit(&it->open); - if (it->dir != NULL) { - rnd_closedir(it->dir); - it->dir = NULL; - } - vts0_uninit(&it->names); - gds_uninit(&it->path); -} - -char *csch_lib_find_next(csch_lib_it_t *it) -{ - char fn[RND_PATH_MAX], *fne; - long maxlen; - - if (it->names.used == 0) - return NULL; - - if (it->dir == NULL) { - char *dirn; - - next_lib:; - if (it->open.used == 0) - return NULL; - - dirn = it->open.array[it->open.used-1]; - it->open.array[it->open.used-1] = NULL; - it->open.used--; - - gds_truncate(&it->path, 0); - gds_append_str(&it->path, dirn); - gds_append(&it->path, '/'); - it->dirlen = it->path.used; - free(dirn); - it->dir = rnd_opendir(it->hidlib, it->path.array); - if (it->dir == NULL) - goto next_lib; - } - - memcpy(fn, it->path.array, it->path.used); - fne = fn + it->path.used; - *fne = RND_DIR_SEPARATOR_C; - fne++; - maxlen = sizeof(fn) - it->path.used - 4; - - for(;;) { - struct stat st; - struct dirent *de; - int cmp, n; - long len; - - de = rnd_readdir(it->dir); - if (de == NULL) { - rnd_closedir(it->dir); - it->dir = NULL; - goto next_lib; - } - - len = strlen(de->d_name); - if (len > maxlen) { - rnd_message(RND_MSG_ERROR, "Path too long; dir '%s' fn '%s', max length %ld\n", it->path.array, de->d_name, sizeof(fn)); - continue; - } - - memcpy(fne, de->d_name, len+1); - if (stat(fn, &st) != 0) - continue; - - if (S_ISDIR(st.st_mode)) { - if (de->d_name[0] == '.') { - if (de->d_name[1] == '\0') continue; /* . is the current dir */ - if ((de->d_name[1] == '.') && (de->d_name[2] == '\0')) continue; /* .. is the parent dir */ - } - } - else if (S_ISREG(st.st_mode)) { - /* proceed to file name matching below */ - } - else if (RND_WRAP_S_ISLNK(st.st_mode)) { - TODO("symlink"); - csch_message(CSCH_MSG_WARNING, "symlinks in library not yet supported\n"); - continue; - } - else - continue; /* anything else (e.g. block devices, sockets, pipes) -> ignore */ - - for(n = 0; n < it->names.used; n++) { - const char *name = it->names.array[n]; - - gds_truncate(&it->path, it->dirlen); - - if (it->case_insensitive) - cmp = rnd_strcasecmp(de->d_name, name); - else - cmp = strcmp(de->d_name, name); - if (cmp == 0) { - gds_append_str(&it->path, name); - gds_append(&it->path, '\0'); - return it->path.array; /* found a matching file name! */ - } - } - } -}