资源简介
未实现功能:没有将结果按文件名排序
假设参数名是目录名
不能显示指定目录的信息
不支持选项-a、-l等
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void do_ls(char []);
void do_stat(char *);
void show_file_info(char *struct stat *);
void mode_to_letters(int char []);
char * uid_to_name(uid_t);
char * gid_to_name(fid_t);
main(int acchar * av[])
{
if(ac == 1)
{
do_ls(“.“);
}
else
{
while(--ac)
{
printf(“%s:\n“*++av);
do_ls(*av);
}
}
}
void do_ls(char dirname[])
/*
*list files in directory called dirname
*/
{
DIR *dir_ptr;
struct dirent *direntp;
if((dir_ptr = opendir(dirname)) == NULL)
{
fprintf(stderr“ls:cannot open %s\n“dirname);
}
else
{
while((direntp = readdir(dir_ptr)) != NULL)
{
do_stat(direntp->d_name);
}
closedir(dir_ptr);
}
}
void do_stat(char *filename)
{
struct stat info;
if(stat(filename&info) == -1)
{
perror(filename);
}
else
{
show_file_info(filename&info);
}
}
void show_file_info(char *filenamestruct stat *info_p)
/*
*display the info about filename.Theinfo is stored in struct at *info_p
*/
{
char *uid_to_name()*ctime()*gid_to_name()*filemode();
void mode_to_letters();
char modestr[11];
mode_to_letters(info_p->st_modemodestr);
printf(“%s“modestr);
printf(“%4d “(int) info_p->st_nlink);
printf(“%-8s “uid_to_name(info_p->st_uid));
printf(“%-8s“gid_to_name(info_p->st_gid));
printf(“%8ld“(long)info_p->st_size);
printf(“%.12s“4+ctime(&info_p->st_mtime));
printf(“%s\n“filename);
}
- 上一篇:uif65d.zip
- 下一篇:采用TVAL_3算法
评论
共有 条评论