From 33767fc71ba9b77953e55b78a9672f6101008599 Mon Sep 17 00:00:00 2001 From: Ben Cheng Date: Thu, 23 Oct 2008 16:00:08 -0700 Subject: [PATCH] Implement scandir() for OProfile. --- libc/include/dirent.h | 6 ++++- libc/unistd/opendir.c | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/libc/include/dirent.h b/libc/include/dirent.h index edfb63953..6e7b20456 100644 --- a/libc/include/dirent.h +++ b/libc/include/dirent.h @@ -68,7 +68,11 @@ extern int readdir_r(DIR* dirp, struct dirent *entry, struct dire extern int closedir(DIR* dirp); extern void rewinddir(DIR *dirp); extern int dirfd(DIR* dirp); - +extern int alphasort(const void *a, const void *b); +extern int scandir(const char *dir, struct dirent ***namelist, + int(*filter)(const struct dirent *), + int(*compar)(const struct dirent **, + const struct dirent **)); __END_DECLS #endif /* _DIRENT_H_ */ diff --git a/libc/unistd/opendir.c b/libc/unistd/opendir.c index 1b0605788..afa3ea0ee 100644 --- a/libc/unistd/opendir.c +++ b/libc/unistd/opendir.c @@ -203,3 +203,65 @@ void rewinddir(DIR *dir) pthread_mutex_unlock( &dir->_DIR_lock ); } + +int alphasort(const void *a, const void *b) +{ + struct dirent **d1, **d2; + + d1 = (struct dirent **) a; + d2 = (struct dirent **) b; + return strcmp((*d1)->d_name, (*d2)->d_name); +} + + +int scandir(const char *dir, struct dirent ***namelist, + int(*filter)(const struct dirent *), + int(*compar)(const struct dirent **, const struct dirent **)) +{ + DIR *d; + int n_elem = 0; + struct dirent *this_de, *de; + struct dirent **de_list = NULL; + int de_list_size = 0; + + d = opendir(dir); + if (d == NULL) { + return -1; + } + + while ((this_de = readdir(d)) != NULL) { + if (filter && (*filter)(this_de) == 0) { + continue; + } + if (n_elem == 0) { + de_list_size = 4; + de_list = (struct dirent **) + malloc(sizeof(struct dirent *)*de_list_size); + if (de_list == NULL) { + return -1; + } + } + else if (n_elem == de_list_size) { + struct dirent **de_list_new; + + de_list_size += 10; + de_list_new = (struct dirent **) + realloc(de_list, sizeof(struct dirent *)*de_list_size); + if (de_list_new == NULL) { + free(de_list); + return -1; + } + de_list = de_list_new; + } + de = (struct dirent *) malloc(sizeof(struct dirent)); + *de = *this_de; + de_list[n_elem++] = de; + } + closedir(d); + if (n_elem && compar) { + qsort(de_list, n_elem, sizeof(struct dirent *), + (int (*)(const void *, const void *)) compar); + } + *namelist = de_list; + return n_elem; +}