scandir函数的研究【笔记】
以下是本人的学习笔记,代码并非原创,均摘自官方源码,贴出来仅供学习记录用
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 = ¤t->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函数的研究【笔记】的更多相关文章
- Hive自定义函数的学习笔记(1)
前言: hive本身提供了丰富的函数集, 有普通函数(求平方sqrt), 聚合函数(求和sum), 以及表生成函数(explode, json_tuple)等等. 但不是所有的业务需求都能涉及和覆盖到 ...
- linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体[转]
linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体 1.引用头文件#include<dirent.h> struct ...
- scandir函数详解
scandir函数详解2009-10-30 10:51scandir函数:读取特定的目录数据表头文件:#include <dirent.h>定义函数:int scandir(const c ...
- R语言函数化学习笔记6
R语言函数化学习笔记 1.apply函数 可以让list或者vector的元素依次执行一遍调用的函数,输出的结果是list格式 2.sapply函数 原理和list一样,但是输出的结果是一个向量的形式 ...
- R语言函数化学习笔记3
R语言函数化学习笔记3 R语言常用的一些命令函数 1.getwd()查看当前R的工作目录 2.setwd()修改当前工作目录 3.str()可以输出指定对象的结构(类型,位置等),同理还有class( ...
- R语言函数化编程笔记2
R语言函数化编程笔记2 我学过很多的编程语言,可以我写的代码很啰嗦,一定是我太懒了.或许是基础不牢地动山摇 1.为什么要学函数 函数可以简化编程语言,减少重复代码或者说面向对象的作用 2.函数 2.1 ...
- R语言函数化编程笔记1
R语言函数化编程笔记1 notes:有一个不错的网站叫做stack overflow,有问题可以从上面找或者搜索答案,会有大佬相助. 在github上面可以找到很多R的扩展包,如果自己额修改被接受,那 ...
- PHP scandir() 函数
------------恢复内容开始------------ 实例 列出 images 目录中的文件和目录: <?php$dir = "/images/"; // Sort ...
- JavaScript权威设计--JavaScript函数(简要学习笔记十一)
1.函数调用的四种方式 第三种:构造函数调用 如果构造函数调用在圆括号内包含一组实参列表,先计算这些实参表达式,然后传入函数内.这和函数调用和方法调用是一致的.但如果构造函数没有形参,JavaScri ...
随机推荐
- [LeetCode] PathSum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Shell逐行读取文件的3种方法
方法1:while循环中执行效率最高,最常用的方法. while read linedoecho $linedone < filename 注释:这种方式在结束的时候需要执行文件,就好像是执行 ...
- ubuntu下搭建openGL环境
1. 建立基本编译环境 sudo apt-get install build-essential 2. 安装OpenGL Library sudo apt-get install ...
- bzoj4815[CQOI2017]小Q的格子
题意 不简述题意了,简述题意之后这道题就做出来了.放个原题面. 小Q是个程序员. 作为一个年轻的程序员,小Q总是被老C欺负,老C经常把一些麻烦的任务交给小Q来处理. 每当小Q不知道如何解决时,就只好向 ...
- 使用android资源
1.我们可以命名的资源种类有多少? 答: res/anim/ XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象 ...
- Eclipse如何将代码变成大写/小写
代码变小写:选中要换的代码,操作Ctrl+Shift+y即可将大写变小写 代码变大写:选中要换的代码,操作Ctrl+Shift+x即可将小写变大写
- HDU1402:A * B Problem Plus——题解
http://acm.hdu.edu.cn/showproblem.php?pid=1402 给出两个高精度正整数,求它们的积,最长的数长度不大于5e4. FFT裸题,将每个数位看做是多项式的系数即可 ...
- 洛谷 P3157 [CQOI2011]动态逆序对 解题报告
P3157 [CQOI2011]动态逆序对 题目描述 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n ...
- 【bzoj3672】购票
Portal -->bzoj3672 Solution 天知道我是怎么调完的qwq调到天昏地暗系列.. 不管这么多,先尝试列一个最简单的状态转移方程 用\(f[i]\)表示\(i\)点到\( ...
- 【SQL优化】MySQL官网中可优化的层次结构
正如上一篇中我翻译的那篇文章,关于MySQL数据库优化的宏观介绍,了解到了从大体上来讲,优化MySQL可以从3个角度来讲.那么这一篇文章,则从一个个优化点出发,统计出究竟有多少个地方我们可以来优化My ...