目录树的遍历实验报告(unix操作系统系)

实验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携带的函数指针参数决定。这样程序的结构清晰,可扩充性好。

三.实验源代码

#include #include <dirent.h>#include <limits.h>#include<malloc.h>#include<fcntl.h>#include<unistd.h>typedef stat *, int);static Myfunc myfunc;static Myfunc myfunc1;static Myfunc myfunc2;static intmyftw(char *, Myfunc *);static intdopath(Myfunc *);char *argv0[100];static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;int num=0,i=0;int temp,now;int f,f_le;char *fcontd;int fo,fo_le;char *focontd;static long nreg0=0;/*char *path_alloc(int *size)char *p=NULL;if(!size) return NULL;p=malloc(256);if(p)*size=256;else*size=0;return p;}*/intmain(int argc, char *argv[]){intret;if(argc==2)//两个参数{ret = myftw(argv[ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;if (ntot == 0)ntot = printf(, nreg,nreg*100.0/ntot);printf(,nreg0,nreg0*100.0/ntot);printf(, ndir,ndir*100.0/ntot);printf(, nblk,nblk*100.0/ntot);printf(, nchr,nchr*100.0/ntot);printf(, nfifo,nfifo*100.0/ntot);printf(, nslink,nslink*100.0/ntot);printf(, nsock,nsock*100.0/ntot);}))//四个参数{f=open(argv[3],O_RDWR);f_le=lseek(f,0,SEEK_END);temp=lseek(f,0,SEEK_SET);fcontd=(char *)malloc(sizeof(char)*f_le);now=read(f,fcontd,f_le);ret =myftw(argv[1],myfunc1);}))//大于四个参数{i=3;for(i=3;i<argc;i++){argv0[i-3]=argv[i];}i++;num=argc-3;ret =myftw(argv[1],myfunc2);}exit(ret);}/* * Descend through the hierarchy, starting at “pathname”. * The caller’s func() is called for every file. */#define FTW_F 1/* file other than directory */#define FTW_D 2/* directory */#define FTW_DNR 3/* directory that can’t be read */#define FTW_NS 4/* file that we can’t stat */myftw(char *pathname, Myfunc *func){int len;fullpath = path_alloc(&len); strncpy(fullpath, pathname, len); fullpath[len-(dopath(func));}/* * Descend through the hierarchy, starting at “fullpath”. * If “fullpath” is anything other than a directory, we lstat() it, * call func(), and return. For a directory, we call ourself * recursively for each name in the directory. dopath(Myfunc* func){struct statstatbuf;struct dirent *dirp;DIR*dp;intret;char*ptr;(func(fullpath, &statbuf, FTW_NS));(func(fullpath, &statbuf, FTW_F));/** It’s a directory. First call func() for the directory,* then process each filename in the directory.*/if ((ret = func(fullpath, &statbuf, FTW_D)) != 0)return(ret);ptr = fullpath + strlen(fullpath); *ptr++ = ;*ptr = 0;(func(fullpath, &statbuf, FTW_DNR));while ((dirp = readdir(dp)) != NULL) {) == 0 ||strcmp(dirp->d_name, ) == 0)strcpy(ptr, dirp->d_name); ((ret = dopath(func)) != ; }ptr[-(closedir(dp) < 0)err_ret(, fullpath);return(ret);}static intmyfunc(stat *statptr, int type)//两个参数{switch (type) {case FTW_F:switch (statptr->st_mode & S_IFMT) {S_IFREG: {nreg++;if(statptr->st_size<4096) nreg0++; }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(, pathname);}break;case FTW_D:ndir++;break;case FTW_DNR:err_ret(, pathname);break;case FTW_NS:err_ret(, pathname);break;default:err_dump(, type, pathname);}return(0);}static intmyfunc1(stat *statptr, int type)//四个参数{i=0;char *p;switch (type) {case FTW_F:{ //if(statptr->st_size==f_le){fo=open(pathname,O_RDWR);fo_le=lseek(fo,0,SEEK_END);now=lseek(fo,0,SEEK_SET);if(fo_le==f_le){focontd=(char *)malloc(sizeof(char)*fo_le);now=read(fo,focontd,fo_le);p=fcontd;for(i=0;i<fo_le;i++){p++;focontd++;if(*p!=*focontd)break;}if(i>=fo_le)printf(,pathname);}}case FTW_D:ndir++;break;case FTW_DNR:err_ret(, pathname);break;case FTW_NS:err_ret(, pathname);break;default:err_dump(, type, pathname);}return(0);}static intmyfunc2(stat *statptr, int type)//大于四个参数{*q=NULL;q=pathname;int leng=0;switch (type) {case FTW_F:switch (statptr->st_mode & S_IFMT) { S_IFREG: {(i=0;i<num;i++){leng=strlen(pathname);q=q+leng;)q–;q++;if(0==strcmp(argv0[i],q))printf(,pathname);}}break;}case FTW_D:ndir++;break;case FTW_DNR:err_ret(, pathname);break;case FTW_NS:err_ret(, pathname);break;default:err_dump(, type, pathname);}return(0);}

四.实验结果

说明:

Ftw4.c 为c源程序

Pathalloc.c pathalloc函数源程序

Abc.txt 为搜索条件(文件名)

home/cs09/cs094180/expmt//3/ 搜索路径

Ftw4 编译后的程序

运行结果详见上面的截图

,香港服务器租用有多少和我一样,坐在不足平米的空间里,

目录树的遍历实验报告(unix操作系统系)

相关文章:

你感兴趣的文章:

标签云: