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

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

scandir函数的研究【笔记】的更多相关文章

  1. Hive自定义函数的学习笔记(1)

    前言: hive本身提供了丰富的函数集, 有普通函数(求平方sqrt), 聚合函数(求和sum), 以及表生成函数(explode, json_tuple)等等. 但不是所有的业务需求都能涉及和覆盖到 ...

  2. linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体[转]

    linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体 1.引用头文件#include<dirent.h> struct ...

  3. scandir函数详解

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

  4. R语言函数化学习笔记6

    R语言函数化学习笔记 1.apply函数 可以让list或者vector的元素依次执行一遍调用的函数,输出的结果是list格式 2.sapply函数 原理和list一样,但是输出的结果是一个向量的形式 ...

  5. R语言函数化学习笔记3

    R语言函数化学习笔记3 R语言常用的一些命令函数 1.getwd()查看当前R的工作目录 2.setwd()修改当前工作目录 3.str()可以输出指定对象的结构(类型,位置等),同理还有class( ...

  6. R语言函数化编程笔记2

    R语言函数化编程笔记2 我学过很多的编程语言,可以我写的代码很啰嗦,一定是我太懒了.或许是基础不牢地动山摇 1.为什么要学函数 函数可以简化编程语言,减少重复代码或者说面向对象的作用 2.函数 2.1 ...

  7. R语言函数化编程笔记1

    R语言函数化编程笔记1 notes:有一个不错的网站叫做stack overflow,有问题可以从上面找或者搜索答案,会有大佬相助. 在github上面可以找到很多R的扩展包,如果自己额修改被接受,那 ...

  8. PHP scandir() 函数

    ------------恢复内容开始------------ 实例 列出 images 目录中的文件和目录: <?php$dir = "/images/"; // Sort ...

  9. JavaScript权威设计--JavaScript函数(简要学习笔记十一)

    1.函数调用的四种方式 第三种:构造函数调用 如果构造函数调用在圆括号内包含一组实参列表,先计算这些实参表达式,然后传入函数内.这和函数调用和方法调用是一致的.但如果构造函数没有形参,JavaScri ...

随机推荐

  1. 第156天:canvas(三)

    一.变形 1.1 translate translate(x, y) ​ 用来移动 canvas 的原点到指定的位置 ​ translate方法接受两个参数.x 是左右偏移量,y 是上下偏移量,如右图 ...

  2. 迁移数据到历史表SQL(转)

    有时工作需要需要把当前表的数据,移到历史表中,而历史表基本是以时间(年)为后缀来命名历史表的,如 A_2011,A_2012,在移数据时,要按数据的时间,移到不同的表中,且由于如果数据有同步.一次处理 ...

  3. C++解析(7):函数重载分析

    0.目录 1.重载的概念 2.C++中的函数重载 3.函数默认参数遇上函数重载 4.编译器调用重载函数的准则 5.重载与指针 6.C++和C相互调用 7.小结 1.重载的概念 自然语言中的上下文--你 ...

  4. 深入理解JVM一类加载器原理

    我们知道我们编写的java代码,会经过编译器编译成字节码文件(class文件),再把字节码文件装载到JVM中,映射到各个内存区域中,我们的程序就可以在内存中运行了.那么字节码文件是怎样装载到JVM中的 ...

  5. Python敏感地址扫描和爬取工具

    0×01 说明: 为了方便信息安全测评工作,及时收集敏感地址(初衷是爬取api地址),所以写了这么个小工具.两个简单的功能(目录扫描和url地址爬取). 0×02 使用参数: python spide ...

  6. jsp中文乱码终极解决方法

    转载http://blog.csdn.net/csh624366188/article/details/6657350 一 找出问题的根源    乱码可能出现的地方:1 jsp页面中          ...

  7. 「Django」学习之路,持续更改

    一.setting设置 1.设置 局域网可以部署连接 ALLOWED_HOSTS = ['*.besttome.com','192.168.1.100'] 2.static配置 STATIC_URL ...

  8. [DeeplearningAI笔记]序列模型1.7-1.9RNN对新序列采样/GRU门控循环神经网络

    5.1循环序列模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.7对新序列采样 基于词汇进行采样模型 在训练完一个模型之后你想要知道模型学到了什么,一种非正式的方法就是进行一次新序列采 ...

  9. CCD与CMOS的区别

    我们在购买相机或是摄像机时,都会看到使用CMOS镜头或是CCD镜头,那么CCD与CMOS是什么意思呢,CCD与CMOS的区别是什么?首先,让我们了解CCD与CMOS的意思. CCDCCD使用一种高感光 ...

  10. JS笔记-强化版1

    1.函数:可以理解为-命令,做一些事~~       function abc(){ // 肯定不会主动执行的!       ……       }       直接调用:abc();       事件 ...