嵌入式 计算Linux下文件夹的大小

root@u12d32:/opt/qy_test/size_folder# cat DIR_SIZE.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <unistd.h>
#include <signal.h>
#include <net/if_arp.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/vfs.h>
#include <stdarg.h>
#include <dirent.h>
#include <sys/stat.h>
#include <assert.h>

typedef unsigned long long offf_t;

offf_t file_size(const char *filename)
{
 struct stat statbuf;
 if(lstat(filename, &statbuf) !=0 )
 {
  perror("lstat");
  return -1;
 }
 return (statbuf.st_blocks);
}

offf_t  dir_size(const char *filename)
{
 DIR *dir;
 struct dirent *entry;
 char newfile[512];
 offf_t total;
 total =0;
 
 struct stat statbuf;
 if(lstat(filename, &statbuf) !=0 )
 {
  perror("lstat");
  return -1;
 }
 total+=statbuf.st_blocks;
 printf("%s, %d===%lld\n",__FILE__, __LINE__, total);
 dir=opendir(filename);
 if(!dir)
 {
  perror("opendir");
  return -1;
 }
 while((entry = readdir(dir)) != NULL)
 {
  if(strcmp(entry->d_name,".")==0 || strcmp(entry->d_name,"..")==0)    ///current dir OR parrent dir
    continue;
  
  printf("%s\n", entry->d_name);
  memset(newfile,'\0',sizeof(newfile));
  strcpy(newfile, filename);
  strcat(newfile, "/");
  strcat(newfile, entry->d_name);
  printf("%s\n", newfile);
  
  if(entry->d_type == DT_DIR)
  {
   total+=dir_size(newfile);
   printf("%s, %d===%lld\n",__FILE__, __LINE__, total);
  }
  else
  {
   total+=file_size(newfile);
   printf("%s, %d===%lld\n",__FILE__, __LINE__, total);
  }
 }
 return total;
}

int main(int argc , char **argv)
{
 offf_t sum;
 if(argc < 2)
 {
  return -1;
 }
 assert(argv[1]);
 sum=dir_size(argv[1]);
 printf("%lu\n",(sum/2));
 return 0;
}
root@u12d32:/opt/qy_test/size_folder#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值