思路:先打开目录 -----> 再读取目录 ----->从读到的目录中获取文件名 ----->stat获取文件信息------>显示信息
注意:对于指定的目录,则需要获取它的绝对路径来实现。
/*************************************************************************
> File Name: ls_v2.c
> Author: casess
> Mail: posixssW@gmail.com
> Created Time: 2013年12月20日 星期五 14时59分34秒
************************************************************************/
//显示目录下的文件信息
//如何显示指定的目录信息:给出那个目录的绝对路径即可
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<pwd.h>
#include<grp.h>
void do_ls(const char * dirname);
void do_stat(const char * fullpath,const char * filename);
void show_file_info(const char * filename, const struct stat * file_inf_ptr);
char * uid_to_name(uid_t uid);
void mode_to_letters(int mode , char * mode_str);
char * gid_to_name(gid_t uid);
int main(int argc,char * argv[])
{
if(1 == argc)
do_ls("."); //ls current directory
else
{
while(--argc)
{
printf("dir : %s \n",* ++argv);
do_ls( * argv );
}
}
return 0;
}
void do_ls(const char * dirname)
{
DIR * dir_ptr = NULL;
struct dirent * dir_entry_ptr = NULL;
char * fullpath = NULL;
//分配绝对路径名的最大空间
if( NULL == ( fullpath = (char *)malloc(strlen(*dirname)+1 + NAME_MAX+1) ) )
{
puts("malloc in fullpath failed");
exit(1);
}
if(NULL == (dir_ptr = opendir(dirname))) //打开目录
{
fprintf(stderr,"cannot open dir :%s\n",dirname);
perror("open dir failed :");
exit(1);
}
else
{
while(NULL != (dir_entry_ptr = readdir( dir_ptr )) ) //读取目录,成功:返回dirent结构体指针
{
sprintf(fullpath,"%s/%s",dirname,dir_entry_ptr->d_name);
// printf("%s",fullpath); //也代表根目录
do_stat( fullpath , dir_entry_ptr->d_name );
}
closedir(dir_ptr);
}
}
void do_stat(const char * fullpath ,const char * filename)
{
struct stat file_info;
if(-1 == stat(fullpath,&file_info))
{
perror("get file-info failed ");
return ;
}
else
{
show_file_info(filename,&file_info);
}
}
void show_file_info(const char * filename, const struct stat * file_inf_ptr)
{
char mode[11]; //file mode
mode_to_letters(file_inf_ptr->st_mode,mode);
printf( "%s" , mode );
printf( " %4d" , (int)file_inf_ptr->st_nlink );
printf( " %-8s" , uid_to_name(file_inf_ptr->st_uid) );
printf( " %-8s" , gid_to_name(file_inf_ptr->st_gid) );
printf( " %8ld" , (long)file_inf_ptr->st_size );
printf( " %.12s" , 4+ctime(&(file_inf_ptr->st_mtime)) );
printf( " %s\n" , filename );
}
void mode_to_letters(int mode , char * mode_str)
{
strncpy(mode_str,"-----------",10); //此处容易出错,注意mode_str必须要以 ‘\0'结尾且放入的字符串长度不能超过10
if(S_ISDIR(mode)) mode_str[0] = 'd'; //is directory ?
if(S_ISCHR(mode)) mode_str[0] = 'c'; //is char devices ?
if(S_ISBLK(mode)) mode_str[0] = 'b'; //is block devices ?
if(mode & S_IRUSR) mode_str[1] = 'r';
if(mode & S_IWUSR) mode_str[2] = 'w';
if(mode & S_IXUSR) mode_str[3] = 'x';
if(mode & S_IRGRP) mode_str[4] = 'r';
if(mode & S_IWGRP) mode_str[5] = 'w';
if(mode & S_IXGRP) mode_str[6] = 'x';
if(mode & S_IROTH) mode_str[7] = 'r';
if(mode & S_IWOTH) mode_str[8] = 'w';
if(mode & S_IWOTH) mode_str[9] = 'x';
}
char * uid_to_name(uid_t uid)
{//更具有可移植性,如果uid不是合法用户,那么就会返回 NULL
struct passwd * pw_ptr = NULL;
static char numstr[10]; //static不能省略,否则返回的是栈地址,会出现错误
if(NULL == (pw_ptr = getpwuid(uid)) )
{
sprintf(numstr,"%9d",uid);
return numstr;
}
else
return pw_ptr->pw_name;
}
char * gid_to_name(gid_t gid)
{//更具有可移植性,如果gid不是合法组,那么就会返回 NULL
struct group * gr_ptr = NULL;
static char numstr[10]; //static不能省略,否则返回的是栈地址,会出现错误
if(NULL == (gr_ptr = getgrgid(gid)) )
{
sprintf(numstr,"%9d",gid);
return numstr;
}
else
return gr_ptr->gr_name;
}