NAME
scandir, alphasort, versionsort - scan a directory for matching entries
SYNOPSIS
#include
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
int alphasort(const void *a, const void *b);
int versionsort(const void *a, const void *b);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
scandir(), alphasort(): _BSD_SOURCE || _SVID_SOURCE
versionsort(): _GNU_SOURCE
DESCRIPTION
The scandir() function scans the directory dirp, calling filter() on each directory entry. Entries for which filter() returns non-zero are stored in strings allocated via mal‐
loc(3), sorted using qsort(3) with the comparison function compar(), and collected in array namelist which is allocated via malloc(3). If filter is NULL, all entries are
selected.
The alphasort() and versionsort() functions can be used as the comparison function compar(). The former sorts directory entries using strcoll(3), the latter using strver‐
scmp(3) on the strings (*a)->d_name and (*b)->d_name.
#define _SVID_SOURCE
/* print files in current directory in reverse order */
#include
int
main(void)
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
沒有留言:
張貼留言
使用 google 的 reCAPTCHA 驗證碼, 總算可以輕鬆留言了。
我實在受不了 spam 了, 又不想讓大家的眼睛花掉, 只好放棄匿名留言。這是沒辦法中的辦法了。留言的朋友需要有 google 帳號。