linux中如何统计目录中的文件,[Linux目录文件]在Linux中统计目录内文件
//調用opendir和readdir函數對指定目錄進行遍歷操作
//然后打印輸出指定目錄中各種類型的文件數目
#include?
#include?
#include?
#include?
#include?
#include?
#include?
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)?
{
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)?
{
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);
}
總結
以上是生活随笔為你收集整理的linux中如何统计目录中的文件,[Linux目录文件]在Linux中统计目录内文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 精通ASP.NET MVC ——视图
- 下一篇: mysql innodb4大特征_MYS