scandir

SCANDIR

章節:Linux 程序員手冊 (3)
更新:2009-02-10

易美翻譯 翻譯 本頁

名字

scandir, alphasort, versionsort - 爲尋找項目掃描目錄

概要

#include <dirent.h>

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);

glibc 需要特性測試宏(參看 feature_test_macros(7)):

scandir()、alphasort():_BSD_SOURCE || _SVID_SOURCE
versionsort():_GNU_SOURCE

描述

scandir() 函數掃描目錄 dirp,對每一個目錄項(文件名)調用filter()。把每一個filter() 返回非零項目保存在一個通過malloc(3) 分配的緩存區裏,再通過比較函數是compar() 的qsort(3) 函數排序,最後收集在namelist 的數組裏,這個數組也是通過malloc(3) 分配的。如果filter 是 NULL,所有項目都被選擇。

alpthasort() 和 versionsort() 可以用作比較函數 compar()。前者通過strcoll(3) 來排序項目,後者使用字符串字段(*a)->d_name(*b)->d_name 上應用 strverscmp(3)。

返回值

scandir() 函數返回被選擇的目錄條數,或者如果出錯返回 -1。

alphasort() 和 versionsort() 函數返回一個小於、等於或大於零的整數,當第一個參數被認爲是小於、等於或大於第二個參數時。

錯誤

ENOMEM
沒有足夠的內存來完成操作。

版本

versionsort() 在 glibc 2.1 中首次出現。

遵循於

alphasort() 和 scandir() 由 POSIX.1-2008 定義,並且廣泛存在。versionsort() 是 GNU 擴展。

函數 scandir() 和 alphasort() 來自 4.3BSD,並且在 Linux libc4 裏已經存在。libc4 和 libc5 使用更安全的原型:

    int alphasort(const struct dirent ** a, const struct dirent **b);

但 glibc 2.0 使用不太精確的 BSD 原型。

函數 versionsort() 是 GNU 擴展,從 glibc 2.1 開始有效。

從 glibc 2.1 開始,alphasort() 調用 strcoll(3);之前的調用strcmp(3)。

示例

#define _SVID_SOURCE
/* 以反序打印當前目錄裏的文件名 */
#include <dirent.h>

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);
    }
}
--------------------------------------------------2
scandir()函數
2011-08-24 14:20

#include <dirent.h>

  int scandir( const char *dir,

  struct dirent **namelist,

  int (*filter) (const void *b),

  int ( * compare )( const struct dirent **, const struct dirent ** ) );

  int alphasort(const void *a, const void *b);

  int versionsort(const void *a, const void *b);

  函數scandir掃描dir目錄下以及dir子目錄下滿足filter過濾模式的文件,返回的結果是compare函數經過排序的,並保存在namelist中。注意namelist是通過malloc動態分配內存的,所以在使用時要注意釋放內存。alphasort和versionsort是使用到的兩種排序的函數。

  當函數成功執行時返回找到匹配模式文件的個數,如果失敗將返回-1。

  eg:

  #include <dirent.h>

  int main()

  {

  struct dirent **namelist;

  int n;

  n = scandir(".", &namelist, 0, alphasort);

  if (n < 0)

  {

  perror("not found\n");

  }

  else

  {

  while(n--)

  {

  printf("%s\n", namelist[n]->d_name);

  free(namelist[n]);

  }

  free(namelist);

  }

  }

---------------------------------------------------------------------------------3

dirent  結構體 介紹:
  LINUX系統下的一個頭文件,在這個目錄下/usr/include
  爲了獲取某文件夾目錄內容,所使用的結構體。
  引用頭文件#include<dirent.h>

結構體說明

  struct dirent
 
  {
  long d_ino; /* inode number 索引節點號 */
  off_t d_off; /* offset to this dirent 在目錄文件中的偏移 */
  unsigned short d_reclen; /* length of this d_name 文長
  unsigned char d_type; /* the type of d_name 文件類型 */
  char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最長255字符 */
 }

相關函數

 opendir(),readdir(),closedir();
使用實例
  #include <stdio.h> #include <errno.h>
  #include <string.h> #include <sys/types.h>
  #include <dirent.h>
  #ifndef DT_DIR
  #error "DT_DIR not defined, maybe d_type not a mumber of struct dirent!"
  #endif
 int main(int argc, char*argv[])
 {
 
  staticchar dot[] =".", dotdot[] ="..";
  constchar*name;
  DIR *dirp;
  struct dirent *dp;
  if (argc ==2)
  name = argv[1];
  else
  name = dot;
  dirp = opendir(name);
  if (dirp == NULL) {
  (void)fprintf(stderr, "%s: opendir(): %s: %s\n",
  argv[0], name, strerror(errno));
  exit(errno);
  }
 
  while ((dp = readdir(dirp)) != NULL) {
  if (dp->d_type == DT_DIR)
  if ( strcmp(dp->d_name, dot)
  && strcmp(dp->d_name, dotdot) )
  (void)printf("%s/\n", dp->d_name);
  }
  (void)closedir(dirp);
  return (0);
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章