实验3 目录树的遍历

时间:2020-12-28 19:06:13

实验3 目录树的遍历

一、实验目的

掌握与文件和目录树有关的系统调用和库函数。

二、实验要求

1、编写程序myfind

命令语法:

        myfind  <pathname>  [-comp <filename> | -name <str>…]

命令语义:

(1)myfind  <pathname> 的功能:

除了具有与程序4-7相同的功能外,还要输出在<pathname>目录子树之下,文件长度不大于4096字节的常规文件,在所有允许访问的普通文件中所占的百分比。程序不允许打印出任何路径名。

(2)myfind  <pathname>  -comp <filename>的功能:

<filename>是常规文件的路径名(非目录名,但是其路径可以包含目录)。命令仅仅输出在<pathname>目录子树之下,所有与<filename>文件内容一致的文件的绝对路径名。不允许输出任何其它的路径名,包括不可访问的路径名。

(3)myfind  <pathname>  -name <str>…的功能:

<str>…是一个以空格分隔的文件名序列(不带路径)。命令输出在<pathname>目录子树之下,所有与<str>…序列中文件名相同的文件的绝对路径名。不允许输出不可访问的或无关的路径名。

       <pathname>和<filename>均既可以是绝对路径名,也可以是相对路径名。<pathname>既可以是目录,也可以是文件,此时,目录为当前工作目录。

2、注意尽可能地提高程序的效率。注意避免因打开太多文件而产生的错误。

3、遍历目录树时,访问结点(目录项)的具体操作应当由遍历函数dopath携带的函数指针参数决定。这样程序的结构清晰,可扩充性好。

三、实验过程

(1)除了要输出目录中各种类型的文件数外,还要输出文件长度不大于4096字节的文件,因此在myfunc1函数中用if语句判断文件大小并输出这种文件个数。

(2)定义myfunc2函数,在当前目录中先判断出与<filename>文件名相同的文件,然后用strcmp比较该文件的内容是否与<filename>的内容一致,如果都一致就用getcwd来获取当前工作目录,当pathname是绝对路径名时直接输出,如果是相对路径名时用strcat获取完整的绝对路径。

(3)定义myfunc3函数,主要判断当前目录中是否存在与<filename>文件名相同的文件,如果相同且<pathname>为绝对路径则直接输出,如果是相对路径则用(2)的步骤来输出绝对路径。又该命令中是一个文件序列,故在main函数中用for循环来依次赋予<filename>不同的文件名,然后用myftw函数遍历目录。

四、实验程序

#include "apue.h"
#include <dirent.h>
#include <limits.h>
#include <sys/stat.h>
#include <string.h>
#include <malloc.h>
#include <fcntl.h>
typedef int Myfunc(const char*,const struct stat *,int);
static Myfunc myfunc1,myfunc2,myfunc3;
static int myftw(char *,Myfunc *);
static int dopath(Myfunc *);
static longnreg,ndir,nblk,nchr,nfifo,nslink,nsock,ntot,size,dsize,count;
static char *dbuf,*filename;
int
main(int argc,char *argv[])
{
int ret,fd,i;
struct stat buf;
if(argc==2)
{
ret=myftw(argv[1],myfunc1);
ntot=nreg+ndir+nblk+nchr+nfifo+nslink+nsock;
if(ntot==0)ntot=1;
printf("regular files =%7ld, %5.2f%%\n",nreg,nreg*100.0/ntot);
printf("directories =%7ld, %5.2f%%\n",ndir,ndir*100.0/ntot);
printf("block special =%7ld, %5.2f%%\n",nblk,nblk*100.0/ntot);
printf("char special =%7ld, %5.2f%%\n",nchr,nchr*100.0/ntot);
printf("FIFOs =%7ld, %5.2f%%\n",nfifo,nfifo*100.0/ntot);
printf("symboliclinks =%7ld, %5.2f%%\n",nslink,nslink*100.0/ntot);
printf("sockets =%7ld, %5.2f%%\n",nsock,nsock*100.0/ntot);
printf("filesize<4096 =%7ld, %5.2f %%\n",size,size*100.0/nreg);
}
else if(argc>=4)
{
if(argc==4&&strcmp(argv[2],"-comp")==0)
{
if((fd=open(argv[3],O_RDONLY))==-1)
err_ret("can't open thefile %s\n",argv[3]);
lstat(argv[3],&buf);
dsize=buf.st_size;
dbuf=(char*)malloc(sizeof(char)*dsize);
read(fd,dbuf,dsize);
close(fd);
count=0;
ret=myftw(argv[1],myfunc2);
if(count==0)printf("can't find the file.\n");
}
if(strcmp(argv[2],"-name")==0)
{
for(i=4;i<=argc;i++)
{
count=0;
printf("the paths ofthis file '%s':\n",argv[i-1]);
filename=argv[i-1];
ret=myftw(argv[1],myfunc3);
if(count==0)printf("Nosuch path.\n");
}
}
}
exit(ret);
}
#define FTW_F 1
#define FTW_D 2
#define FTW_DNR 3
#define FTW_NS 4
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 stat statbuf;
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;
strcpy(ptr,dirp->d_name);
if((ret=dopath(func))!=0)
break;
}
ptr[-1]=0;
if(closedir(dp)<0)
err_ret("cant closedirectory %s",fullpath);
return(ret);
}
static int
myfunc1(const char *pathname,const struct stat *statptr,int type)
{
switch(type)
{
case FTW_F:

switch(statptr->st_mode&S_IFMT){
case S_IFREG: nreg++;
if(statptr->st_size<4096) size++;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:
err_dump("for S_IFDIR for%s",pathname);
}
break;
case FTW_D:
ndir++;
break;
case FTW_DNR: break;
case FTW_NS: break;
}
return(0);
}
static int
myfunc2(const char *pathname,const struct stat *statptr,int type)
{
int fd,len;
char *buf,*fullpathname;
buf=(char *)malloc(sizeof(char)*dsize);
fullpathname=path_alloc(&len);
if(type==FTW_F)
{
if(dsize==statptr->st_size)
{
if((fd=open(pathname,O_RDONLY))==-1)
err_ret("cant't openfile\n");
read(fd,buf,dsize);
if(strcmp(dbuf,buf)==0)
{
getcwd(fullpathname,len);
count++;
if(*pathname=='/')printf("%s\n",pathname);
else if(*pathname=='.')
{
strcat(fullpathname,"/");
strcat(fullpathname,pathname+2);
printf("%s\n",fullpathname);
}
else
{
strcat(fullpathname,"/");
strcat(fullpathname,pathname);
printf("%s\n",fullpathname);
}
}
close(fd);
}
}
return(0);
}
static int
myfunc3(const char *pathname,const struct stat *statptr,int type)
{
int len;
char *fullpathname;
const char *q;
fullpathname=path_alloc(&len);
if(type==FTW_F)
{
getcwd(fullpathname,len);
q=pathname+(strlen(pathname)-1);
while(*q!='/') q--;
if(strcmp(filename,++q)==0)
{
count++;
if(*pathname=='/')printf("%s\n",pathname);
else if(*pathname=='.')
{
strcat(fullpathname,"/");
strcat(fullpathname,pathname+2);
printf("%s\n",fullpathname);
}
else
{
strcat(fullpathname,"/");
strcat(fullpathname,pathname);
printf("%s\n",fullpathname);
}
}
}
return(0);
}