遍历函数
#include
#include
#include
#include
#include
#include
#include
void mylist(char *filename)
{
//0.切换目录到指定的目录下
chdir(filename);
char mymode[11]="----------";
//1。打开目录
DIR *mydir=opendir("./");
if(mydir==NULL)
{
printf("opendir error!\n");
return;
}
//2。遍历目录下的文件名
struct dirent *p=NULL;
printf("------------------------------------------\n");
while( (p=readdir(mydir))!=NULL )
{
//3.把每个文件的属性读取出来
//printf("%s\n",p->d_name);
struct stat buf;
stat(p->d_name,&buf);
//取文件的I节点信息演示
//printf("%s %d\n",p->d_name,buf.st_ino);
//如果是设备文件,在写文件大小的地方是设备的驱动号 主驱动号/次驱动号
switch(buf.st_mode&S_IFMT)
{
case S_IFREG: mymode[0]='-'; break;//一般文件
case S_IFDIR: mymode[0]='d'; break;//目录
case S_IFCHR: mymode[0]='c'; break;//字符设备
case S_IFLNK: mymode[0]='l'; break;//链接文件
case S_IFBLK: mymode[0]='b'; break;//块设备
case S_IFSOCK:mymode[0]='s'; break;//套接口文件
case S_IFIFO: mymode[0]='p'; break;//管道文件
default:break;
}
mymode[1]=buf.st_mode&S_IRUSR?'r':'-' ;
mymode[2]=buf.st_mode&S_IWUSR?'w':'-' ;
mymode[3]=buf.st_mode&S_IXUSR?'x':'-' ;
mymode[4]=buf.st_mode&S_IRGRP?'r':'-' ;
mymode[5]=buf.st_mode&S_IWGRP?'w':'-' ;
mymode[6]=buf.st_mode&S_IXGRP?'x':'-' ;
mymode[7]=buf.st_mode&S_IROTH?'r':'-' ;
mymode[8]=buf.st_mode&S_IWOTH?'w':'-' ;
mymode[9]=buf.st_mode&S_IXOTH?'x':'-' ;
printf(&
quot;%-25s %10s\n",p->d_name,mymode);
}
}
int main(int argc, char *argv[])
{
mylist(argv[1]);
return 0;
}