设为首页 收藏本站
查看: 966|回复: 0

[经验分享] [Linux目录文件]在Linux中统计目录内文件

[复制链接]

尚未签到

发表于 2018-5-16 13:04:12 | 显示全部楼层 |阅读模式
//调用opendir和readdir函数对指定目录进行遍历操作
//然后打印输出指定目录中各种类型的文件数目
#include <stdio.h>
#include <fcntl.h>
#include <dirent.h>
#include <limits.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
typedefint Myfunc(const char *, const struct stat *, int);   //定义一个函数
static Myfunc myfunc;
static int myftw(char *, Myfunc *);
static int dopath(Myfunc *);
static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
//各种类型的文件数目对应的变量
char *path_alloc(int* size);
int main(int argc, char *argv[])
{
  int ret;
  if (argc != 2)
  {
     printf("请输入正确的参数!\n");   //参数错误
     return 1;
  }
  ret = myftw(argv[1], myfunc);/* does it all */
  ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
  //计算文件总量
  if (ntot == 0)     //如果目录中没有文件则将ntot设置为1以避免除数为0
  {
    ntot = 1;
  }
  //以下一次打印各种类型文件的数据
  printf("普通文件 = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
  printf("目录文件 = %7ld, %5.2f %%\n", ndir,ndir*100.0/ntot);
  printf("块设备文件 = %7ld, %5.2f %%\n", nblk,nblk*100.0/ntot);
  printf("字设备文件 = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);
  printf("FIFOs = %7ld, %5.2f %%\n", nfifo,nfifo*100.0/ntot);
  printf("符号链接文件 = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);
  printf("套接字文件 = %7ld, %5.2f %%\n", nsock,nsock*100.0/ntot);
  return ret;
}
//路径缓冲区分配函数
char *path_alloc(int* size)
{
  char *p = NULL;
  if(!size)
  {
    return NULL;
  }
  p = malloc(256);
  if(p)
  {
    *size = 256;
  }
  else
  {
    *size = 0;
  }
  return p;
}
#defineFTW_F1//
#defineFTW_D2//目录
#defineFTW_DNR3//不能读的目录
#defineFTW_NS4//不能获得状态的文件
static char*fullpath;//存放每个文件完整路径
static int myftw(char *pathname, Myfunc *func)
{
  int len;
  fullpath = path_alloc(&len);//给路径缓冲区分配一个长度
  strncpy(fullpath, pathname, len);//复制文件名称
  fullpath[len-1] = 0;
  return(dopath(func));
}
//获得文件的状态
static int dopath(Myfunc* func)
{
  struct statstatbuf;
  struct dirent*dirp;
  DIR *dp;
  int ret;
  char *ptr;
  if (lstat(fullpath, &statbuf) < 0)//获得文件状态失败
  {
    return(func(fullpath, &statbuf, FTW_NS));
  }
  if (S_ISDIR(statbuf.st_mode) == 0)//如果不是目录
  {
    return(func(fullpath, &statbuf, FTW_F));
  }
  if ((ret = func(fullpath, &statbuf, FTW_D)) != 0)
  {
    return(ret);
  }
  ptr = fullpath + strlen(fullpath);//指向路径缓冲区结尾
  *ptr++ = '/';
  *ptr = 0;
  if ((dp = opendir(fullpath)) == NULL)//如果不能读目录
  {
    return(func(fullpath, &statbuf, FTW_DNR));
  }
  while ((dirp = readdir(dp)) != NULL) {
if (strcmp(dirp->d_name, ".") == 0  ||
    strcmp(dirp->d_name, "..") == 0)
continue;/* ignore dot and dot-dot */
strcpy(ptr, dirp->d_name);/* append name after slash */
if ((ret = dopath(func)) != 0)/* recursive */
break;/* time to leave */
}
ptr[-1] = 0;/* erase everything from slash onwards */
if (closedir(dp) < 0)
{
printf("can't close directory %s\n", fullpath);
    }
return(ret);
}
static int myfunc(const char *pathname, const struct stat *statptr, int type)
{
switch (type) {
case FTW_F:
switch (statptr->st_mode & S_IFMT) {
case S_IFREG:nreg++;break;
case S_IFBLK:nblk++;break;
case S_IFCHR:nchr++;break;
case S_IFIFO:nfifo++;break;
case S_IFLNK:nslink++;break;
case S_IFSOCK:nsock++;break;
case S_IFDIR:
printf("for S_IFDIR for %s\n", pathname);
}
break;
case FTW_D:
ndir++;
break;
case FTW_DNR:
printf("can't read directory %s\n", pathname);
break;
case FTW_NS:
printf("stat error for %s\n", pathname);
break;
default:
printf("unknown type %d for pathname %s\n", type, pathname);
}
return(0);
}  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-461000-1-1.html 上篇帖子: linux基础精简 下篇帖子: [Linux线程]线程分离
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表