/*
* Author: dearvoid at gmail.com
*
* $Date: 2006-06-14 09:01:52 +0800 (Wed, 14 Jun 2006) $
* $HeadURL: svn://svnserver/clark/void/trunk/c/ls-R.c $
* $Revision: 554 $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
bool
isDir(const char * path, const struct dirent * ent)
{
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
/*
* On Mac OS X 10.3.x (PPC) the struct dirent does not
* has the d_type field.
*/
return ent->d_type == DT_DIR;
#else
char buf[1024]; // FIXME
struct stat fstat;
sprintf(buf, "%s/%s", path, ent->d_name);
if (stat(buf, &fstat) < 0) {
perror("stat()");
exit(1);
} else {
return S_ISDIR(fstat.st_mode);
}
#endif
}
int
ls_R(char * path)
{
int i;
int nEntries;
char buf[1024]; // FIXME
struct dirent ** list;
nEntries = scandir(path, &list, NULL, NULL);
if (nEntries < 0) {
printf("scandir('%s'): %s\n", path, strerror(errno));
return 1;
}
printf("%s:\n", path);
for (i = 0; i < nEntries; ++i) {
if (strcmp(list[i]->d_name, "..") && strcmp(list[i]->d_name, "."))
printf("\t%s\n", list[i]->d_name);
if (!isDir(path, list[i]) || !strcmp(list[i]->d_name, ".")
|| !strcmp(list[i]->d_name, "..")) {
free(list[i]);
list[i] = NULL;
}
}
printf("\n");
for (i = 0; i < nEntries; ++i) {
if (list[i]) {
sprintf(buf, "%s/%s", path, list[i]->d_name);
ls_R(buf);
}
}
for (i = 0; i < nEntries; ++i) {
free(list[i]);
}
free(list);
return 0;
}
int
main(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage:\n");
printf(" %s /your/dir\n", argv[0]);
exit(1);
}
argv[1][strlen(argv[1])-1]=(argv[1][strlen(argv[1])-1]=='/')?'\0':'/';
puts(argv[1]);
ls_R(argv[1]);
return 0;
}
lsR.c.tar.gz_遍历目录
版权申诉
53 浏览量
2022-09-22
18:12:53
上传
评论
收藏 988B GZ 举报
APei
- 粉丝: 64
- 资源: 1万+
最新资源
- 【全网最新最全】大气公司年度月度总结汇报PPT
- C语言实现低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等).zip
- C语言实现基于STM32 的联合调试侦听设备解决方案(原理图、PCB源文件、调试工具、视频).zip
- 【全网最全最酷】部门年度年终工作总结汇报PPT模板
- C语言实现基于STM32F103RC的电子相册(原理图、PCB源文件、程序源码及制作).zip
- C语言实现基于stm32和mpu9250的usb hid键盘、鼠标、游戏控制器.zip
- SAP GUI for Windows 7.70 Patch16
- C语言实现基于物联网的户外环境检测装置(STM32、APP、WIFI).zip
- Models for ICM/MCM美赛常用模型.zip
- cef源码CEF全称Chromium Embedded Framework
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈