以下是本人的学习笔记,代码并非原创,均摘自官方源码,贴出来仅供学习记录用

scandir 的使用要注意内存泄漏的问题

scandir函数实现:

vi ./uClibc-0.9.33.2/libc/misc/dirent/scandir.c

/*
* Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
*
* Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
*/ #include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include "dirstream.h" int scandir(const char *dir, struct dirent ***namelist,
int (*selector) (const struct dirent *),
int (*compar) (const struct dirent **, const struct dirent **))
{
DIR *dp = opendir (dir);
struct dirent *current;
struct dirent **names = NULL;
size_t names_size = , pos;
int save; if (dp == NULL)
return -; save = errno;
__set_errno (); pos = ;
while ((current = readdir (dp)) != NULL) {
int use_it = selector == NULL; if (! use_it)
{
use_it = (*selector) (current);
/* The selector function might have changed errno.
* It was zero before and it need to be again to make
* the latter tests work. */
if (! use_it)
__set_errno ();
}
if (use_it)
{
struct dirent *vnew;
size_t dsize; /* Ignore errors from selector or readdir */
__set_errno (); if (unlikely(pos == names_size))
{
struct dirent **new;
if (names_size == )
names_size = ;
else
names_size *= ;
new = (struct dirent **) realloc (names,
names_size * sizeof (struct dirent *));
if (new == NULL)
break;
names = new;
} dsize = &current->d_name[_D_ALLOC_NAMLEN(current)] - (char*)current;
vnew = (struct dirent *) malloc (dsize);
if (vnew == NULL)
break; names[pos++] = (struct dirent *) memcpy (vnew, current, dsize);
}
} if (unlikely(errno != ))
{
save = errno;
closedir (dp);
while (pos > )
free (names[--pos]);
free (names);
__set_errno (save);
return -;
} closedir (dp);
__set_errno (save); /* Sort the list if we have a comparison function to sort with. */
if (compar != NULL)
qsort (names, pos, sizeof (struct dirent *), (comparison_fn_t) compar);
*namelist = names;
return pos;
}

例子参考1:

vi ./uClibc-0.9.33.2/test/stdlib/qsort.c

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h> static int select_files(const struct dirent *dirbuf)
{
if (dirbuf->d_name[] == '.')
return ;
else
return ;
} int main(void)
{
struct dirent **array;
struct dirent *dirbuf; int i, numdir; chdir("/");
numdir = scandir(".", &array, select_files, NULL);
printf("\nGot %d entries from scandir().\n", numdir);
for (i = ; i < numdir; ++i) {
dirbuf = array[i];
printf("[%d] %s\n", i, dirbuf->d_name);
free(array[i]);
}
free(array);
numdir = scandir(".", &array, select_files, alphasort);
printf("\nGot %d entries from scandir() using alphasort().\n", numdir);
for (i = ; i < numdir; ++i) {
dirbuf = array[i];
printf("[%d] %s\n", i, dirbuf->d_name);
}
printf("\nCalling qsort()\n");
/* Even though some manpages say that alphasort should be
* int alphasort(const void *a, const void *b),
* in reality glibc and uclibc have const struct dirent**
* instead of const void*.
* Therefore we get a warning here unless we use a cast,
* which makes people think that alphasort prototype
* needs to be fixed in uclibc headers.
*/
qsort(array, numdir, sizeof(struct dirent *), (void*) alphasort);
for (i = ; i < numdir; ++i) {
dirbuf = array[i];
printf("[%d] %s\n", i, dirbuf->d_name);
free(array[i]);
}
free(array);
return ();
}

例子参考2:

man scandir

EXAMPLE
#define _SVID_SOURCE
/* print files in current directory in reverse order */
#include <dirent.h> int
main(void)
{
struct dirent **namelist;
int n; n = scandir(".", &namelist, NULL, alphasort);
if (n < )
perror("scandir");
else {
while (n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}

最新文章

  1. ubuntu12.04下一个简单的conky安装
  2. 邻接矩阵有向图(三)之 Java详解
  3. uva 725 division(水题)——yhx
  4. hiho 第116周,最大流最小割定理,求最小割集S,T
  5. BootStrap2学习日记14----导航
  6. C语言迭代求解
  7. C++ 虚函数表决心
  8. html5脚本编程
  9. [2012-06-29]sed根据行号范围执行替换
  10. 编程从入门到提高,然后放弃再跑路(Java)
  11. 「Vue」起步 - vue-router路由与页面间导航
  12. 》》jqurey mobile 初
  13. Sql中根据旧表创建新表的SQL语句
  14. __x__(26)0907第四天__文档流_网页最底层
  15. mybatis13--2级缓存
  16. Centos7部署kubernetes测试k8s应用(九)
  17. JPA中自动使用@Table(name = &quot;userTab&quot;)后自动将表名、列名添加了下划线的问题
  18. 实验---反汇编一个简单的C程序(杨光)
  19. BZOJ3504 CQOI2014危桥(最大流)
  20. c#后台访问接口

热门文章

  1. Spring boot整合shiro框架
  2. DataTable Excel 导出:
  3. PHP通过SMTP实现发送邮件_包括附件
  4. Luogu4897 【模板】最小割树
  5. C++解析(8):C++中的新成员
  6. 【CF438E】The Child and Binary Tree(多项式运算,生成函数)
  7. 【BZOJ5251】【八省联考2018】劈配(网络流,二分答案)
  8. 【CF484E】Sign on Fence(主席树)
  9. Netsh命令-网络禁用开启
  10. 使图片相对于上层DIV始终水平、垂直都居中