在看APUE的时候看到程序清单4-7能够统计一个目录下的各种文件的数量!了解了这个程序之后,我在此基础上也写了一个程序,查看某个目录下的所有文件,并输出出来,类似与ls -R 命令!代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<dirent.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<limits.h>
char *fullpath;
int readfile(char *k)
{
DIR * dp;
struct dirent * dirrp;
struct stat buf;
//创建一个p指针指向fullpath的最末尾
char *p;
p = fullpath + strlen(fullpath);
if(-1 == lstat(fullpath,&buf))
{
printf("%s[lstat]%s\n",fullpath,strerror(errno));
return -1;
}
//***************如果不是目录文件则返回********************************//
if(S_ISDIR(buf.st_mode)==0)
{
printf("%s ",k);
return 1;
}
//***************如果是目录文件则继续********************************//
//如果是目录且在尾部没