0 Star 0 Fork 3

EnErGy/dirsize

forked from 焦乐乐/dirsize 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dirsize.c 2.56 KB
一键复制 编辑 原始数据 按行查看 历史
EnErGy 提交于 2018-06-19 19:19 . 更新 dirsize.c
<<<<<<< HEAD
#define _GNU_SOURCE
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
size_t total = 0;
//递归计算目录中各个文件的大小
void walk(char * path)
{
DIR* dp;
struct dirent* entry;
struct stat statbuf;
dp = opendir(path);
if (!dp)
{
perror("opendir");
return;
}
while((entry = readdir(dp)))
{
char * subpath;
if (!strcmp(entry->d_name, ".") ||
!strcmp(entry->d_name, ".."))
continue;
//printf("%s ", entry->d_name);
asprintf(&subpath, "%s/%s", path, entry->d_name);
if (lstat(subpath, &statbuf))
{
perror("lstat");
continue;
}
//printf("size: %lu\n", statbuf.st_size);
total += statbuf.st_size;
if (entry->d_type == DT_DIR)
{
printf("%s\n", subpath);
walk(subpath);
}
}
closedir(dp);
}
int main(int argc, char** argv)
{
char* path;
//./a.out dir
if (argc < 2)
{
path = ".";
}
else
{
path = argv[1];
}
walk(path);
printf("total: %lu\n", total);
return 0;
}
=======
#define _GNU_SOURCE
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
//目录大小
size_t total = 0;
//进入目录计算大小
void walk(char * path)
{
DIR* dp;
struct dirent* entry;
struct stat statbuf;
dp = opendir(path);
if (!dp)
{
perror("opendir");
return;
}
while((entry = readdir(dp)))
{
char * subpath;
if (!strcmp(entry->d_name, ".") ||
!strcmp(entry->d_name, ".."))
continue;
//printf("%s ", entry->d_name);
asprintf(&subpath, "%s/%s", path, entry->d_name);
if (lstat(subpath, &statbuf))
{
perror("lstat");
continue;
}
//printf("size: %lu\n", statbuf.st_size);
total += statbuf.st_size;
if (entry->d_type == DT_DIR)
{
printf("%s\n", subpath);
walk(subpath);
}
}
closedir(dp);
}
int main(int argc, char** argv)
{
char* path;
//./a.out dir
if (argc < 2)
{
path = ".";
}
else
{
path = argv[1];
}
walk(path);
printf("total: %lu\n", total);
return 0;
}
>>>>>>> 0bf51f640a62b580898431f1d9df84a91659e7b9
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/kouzhiwu/dirsize.git
git@gitee.com:kouzhiwu/dirsize.git
kouzhiwu
dirsize
dirsize
master

搜索帮助