#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
int main()
{
char buff[256] = {0};
getcwd(buff,256);
//打开文件
DIR* ptr = opendir(buff);
if( ptr == NULL )
{
exit(0);
}
struct stat st;
//读文件
struct dirent *p = NULL;
while( ( p = readdir(ptr)) != NULL )
{
//不显示隐藏文件
if( strncmp(p->d_name,".",1) == 0 )
{
continue;
}
lstat(p->d_name, &st);
//颜色打印
if( S_ISDIR(st.st_mode))
{
printf("\033[1;34m%s \033[0m",p->d_name);
}
else
{
if( st.st_mode & ( S_IXUSR | S_IXGRP | S_IXOTH ) )
{
printf("\033[1;32m%s \033[0m",p->d_name);
}
else
{
printf("%s ",p->d_name);
}
}
}
printf("\n");
}
运行测试: