scan_Dir

本文介绍了一个使用C语言实现的目录遍历程序,该程序能够统计指定目录下文件和子目录的数量。通过调用opendir、readdir等函数,程序可以读取目录内容,并根据d_type字段区分文件和目录。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>

int display_dir(const char *name)
{
	DIR *pdir;
	struct dirent *pdirent;
	int file_count = 0,dir_count = 0;
	
	if((pdir = opendir(name)) == NULL)
	{
		fprintf(stderr,"Fail to opendir %s : %s.\n",name,strerror(errno));
		return -1;
	}

	while( pdirent = readdir(pdir) )
	{
		if(pdirent->d_name[0] == '.')
			continue;
	
		printf("%s\n",pdirent->d_name);

		switch(pdirent->d_type)
		{
		case DT_REG:
			file_count ++;
			break;

		case DT_DIR:
			dir_count ++;
			break;
		}
	}

	printf("dir : %d file:%d.\n",dir_count,file_count);

	return 0;
}

//./a.out  . 
int main(int argc, const char *argv[])
{	
	if(argc < 2)
	{
		fprintf(stderr,"Usage : %s argv[1].\n",argv[0]);
		return -1;
	}

	display_dir(argv[1]);
	
	return 0;
}

 

char **generate_current_dir_paths(const char *base_dir, const char *prefix, int add_empty) { const char *scan_dir = base_dir; if (!scan_dir || !*scan_dir) { scan_dir = "."; } printf("[generate] Scanning directory: '%s' with prefix: '%s'\n", scan_dir, prefix ? prefix : "(none)"); DIR *dir = opendir(scan_dir); if (!dir) { if (add_empty) { char **matches = (char **)XCALLOC(MTYPE_TMP, sizeof(char *)); matches[0] = XSTRDUP(MTYPE_TMP, ""); return matches; } return NULL; } int capacity = 32; int count = 0; char **matches = (char **)XCALLOC(MTYPE_TMP, capacity * sizeof(char *)); struct dirent *entry; int has_f_prefix = prefix && prefix[0] == 'f'; while ((entry = readdir(dir)) != NULL) { const char *name = entry->d_name; if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue; // 跳过隐藏文件 if (name[0] == '.') continue; // 跳过软链接(直接判断 d_type) if (entry->d_type == DT_LNK) { printf("跳过软链接: %s\n", name); continue; } if (prefix && *prefix && strncmp(name, prefix, strlen(prefix)) != 0) continue; int is_dir = (entry->d_type == DT_DIR); if (entry->d_type == DT_UNKNOWN) { char full_path[PATH_MAX]; snprintf(full_path, sizeof(full_path), "%s/%s", scan_dir, name); struct stat st; if (stat(full_path, &st) == 0 && S_ISDIR(st.st_mode)) { is_dir = 1; } } char *new_name; if (is_dir) { new_name = (char *)XCALLOC(MTYPE_TMP, strlen(name) + 2); snprintf(new_name, strlen(name) + 2, "%s/", name); } else { new_name = XSTRDUP(MTYPE_TMP, name); } if (count >= capacity) { capacity *= 2; matches = (char **)XREALLOC(MTYPE_TMP, matches, capacity * sizeof(char *)); } matches[count++] = new_name; } closedir(dir); // 添加虚拟flash:/路径(如果前缀以'f'开头且未选择flash) if (has_f_prefix && !comp_state.flash_selected) { int flash_exists = 0; for (int i = 0; i < count; i++) { if (strcmp(matches[i], "flash:/") == 0) { flash_exists = 1; break; } } if (!flash_exists) { if (count >= capacity) { capacity += 1; matches = (char **)XREALLOC(MTYPE_TMP, matches, capacity * sizeof(char *)); } matches[count++] = XSTRDUP(MTYPE_TMP, "flash:/"); } } if (count > 0) { qsort(matches, count, sizeof(char *), compare_paths); } matches = (char **)XREALLOC(MTYPE_TMP, matches, (count + 2) * sizeof(char *)); matches[count] = NULL; if (!add_empty) { matches[count] = XSTRDUP(MTYPE_TMP, ""); matches[count + 1] = NULL; } printf("\n[generate] Found %d matches.\n", count); if (matches && matches[0]) { printf("\ngenerate_current_dir_paths: matches = "); for (int i = 0; matches[i]; i++) { printf("%s, ", matches[i]); } printf("\n"); } return matches; } 这个函数我是这样写的,有什么问题吗?
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静思心远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值