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

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 = 0, pos;int save;if (dp == NULL)return -1;save = errno;__set_errno (0);pos = 0;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 (0);}if (use_it){struct dirent *vnew;size_t dsize;/* Ignore errors from selector or readdir */__set_errno (0);if (unlikely(pos == names_size)){struct dirent **new;if (names_size == 0)names_size = 10;elsenames_size *= 2;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 != 0)){save = errno;closedir (dp);while (pos > 0)free (names[--pos]);free (names);__set_errno (save);return -1;}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[0] == '.')return 0;elsereturn 1;
}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 = 0; 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 = 0; 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 = 0; i < numdir; ++i) {dirbuf = array[i];printf("[%d] %s\n", i, dirbuf->d_name);free(array[i]);}free(array);return (0);
}

例子参考2:

man scandir

EXAMPLE#define _SVID_SOURCE/* print files in current directory in reverse order */#include <dirent.h>intmain(void){struct dirent **namelist;int n;n = scandir(".", &namelist, NULL, alphasort);if (n < 0)perror("scandir");else {while (n--) {printf("%s\n", namelist[n]->d_name);free(namelist[n]);}free(namelist);}}
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sky-heaven/p/8183958.html,如需转载请自行联系原作者

scandir函数的研究【笔记】相关推荐

  1. c语言 自动测试,C语言测试。自己实现scandir 函数

    在C语言课程的后端,讲完指针和标准文件IO处理,我会做出一个难度较大练习,题目就是,利用标准的目录处理函数 opendir/readdir/closedir实现类似于 scandir的功能.其中接口要 ...

  2. ES6基础(var let const 箭头函数)-学习笔记

    文章目录 ES6基础(var let const 箭头函数)- 学习笔记 定义:var let const 箭头函数 数据结构 set map ES6基础(var let const 箭头函数)- 学 ...

  3. php scandir遍历,php使用scandir()函数扫描指定目录下所有文件示例

    本文实例讲述了php使用scandir()函数扫描指定目录下所有文件.分享给大家供大家参考,具体如下: //遍历子文件夹和文件夹的内容 并且计算出文件的多少 //一个demo 引号替换下 functi ...

  4. php scandir 目录树,使用php scandir函数遍历文件夹目录和所有文件

    使用php scandir函数遍历文件夹目录和所有文件 $dir = "."; //当前目录 list_file($dir); function list_file($dir){ ...

  5. VS2017 常用函数与数据类型-笔记

    一.MFC入门常用函数和数据类型笔记 CString:MFC控件从面板上得到数据一般是CString GetDlgItem(控件ID)->GetwindowText(CString字符串):获取 ...

  6. scandir函数详解

    scandir函数详解 2009-10-30 10:51 scandir函数:读取特定的目录数据 表头文件:#include <dirent.h> 定义函数:int scandir(con ...

  7. oracle中常用关键字,oracle常用函数及关键字笔记

    --函数及关键字-- 1.trim,ltrim,rtrim 去除字符,无指定默认去除空格 SELECT TRIM('a' FROM 'aafhfhaaaaaaaa'), LTRIM('aafhfhaa ...

  8. 中国移动飞信的研究 笔记一

    中国移动飞信的研究 笔记一 早就 听说 飞信是微软为移动做的.而且 程序还没有 加混淆 所以 可以反编译. 用 Reflector 就可以看见源代码. 网上一搜 发现到目前为止 已经有不少公司或个人对 ...

  9. C语言测试。自己实现scandir 函数

    在C语言课程的后端,讲完指针和标准文件IO处理,我会做出一个难度较大练习,题目就是,利用标准的目录处理函数 opendir/readdir/closedir实现类似于 scandir的功能.其中接口要 ...

  10. linux scandir函数,Linux的scandir函数

    学习目的: 熟悉linux下scandir函数的使用 1.函数的功能 遍历指定目录下满足某种过滤模式的文件,返回结果可通过指定函数进行排序,并将返回的子目录信息(不递归遍历子目录的目录)存放到函数内部 ...

最新文章

  1. pandas中dataframe索引排序实战:pandas中dataframe索引降序排序、pandas中dataframe索引升序排序
  2. 计算机中位运算的一些性质与技巧
  3. 一网打尽2013最常用的NoSQL数据库
  4. 从一个疯狂下载者变成一个学习者
  5. windows重绘机制原理
  6. 百度贴吧排名计算方式
  7. HTML 禁止数字因被自动识别为手机号,而被添加拨号链接样式
  8. Modbus协议栈开发笔记之六:Modbus RTU Master开发
  9. 前端路由的两种实现原理
  10. opencv6-调整图像亮度和对比度
  11. 经典面试题(40):以下代码将输出的结果是什么?
  12. for、while、do while 3种循环异同点
  13. STM32工作笔记0049---JLINK在线调试__软件调试方法与技巧
  14. c大小写转换函数_字符处理——大小写转换编程思路扩展
  15. linux图片处理工具GraphicsMagick安装使用
  16. 在ASP.NET Atlas中调用Web Service——介绍及简单应用
  17. Python项目部署(宝塔面板)
  18. Java NIO?看这一篇就够了!
  19. c#控件弹幕效果_求C#弹幕游戏弹幕的代码
  20. 一个屌丝程序猿的人生(十五)

热门文章

  1. 机器学习指南_管理机器学习实验的快速指南
  2. +0.5(加0.5)配合int()实现四舍五入
  3. Word插入插图清单目录、附表清单目录
  4. 五分钟看懂快速幂算法
  5. php砸金蛋程序,简单的几句PHP生成美团3周年砸金蛋抽奖代码
  6. java 释放对象_java基础:对象的销毁
  7. 流计算技术实战 - CEP
  8. linux下免密认证登录失败原因总结
  9. CentOS 6.x通过yum安装php7.1及相关环境
  10. 理解JavaScript内联命名函数---var fun = function f() {}