您的位置:首页 > 运维架构 > Linux

《unix/linux编程实践教程》学习笔记:第三章 编写ls

2017-03-03 22:24 239 查看
概念准备

目录是一种特殊的文件,它的内容是文件和目录的名字,每一个目录都包括两个特殊的项——“.”(当前目录文件列表)和“..”(上一级目录文件列表)。可以通过命令显示隐藏的文件

ls -a




显示出来,也可以通过
vim .
打开目录文件。



编写ls的思路就是读取
.文件
,通过printf打印。

如何读取目录的内容?

学习三个系统调用:opendir,readdir,closedir

编写ls

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

void do_ls(char []);

int main(int ac,char *av[])
{
if(ac == 1)
do_ls(".");
else
while(-- ac){
printf("%s:\n",*++av);
do_ls(*av);
}
return 0;
}

void do_ls(char dirname[]){
DIR * dir_ptr;
struct dirent * direntp;

if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr,"lsl:cannot open %s\n",dirname);
else
{
while((direntp = readdir(dir_ptr)) != NULL)
printf("%s\n",direntp -> d_name);
closedir(dir_ptr);
}

}


编写ls -l

(1)ls - l 可以查看文件的属性和信息,要实现它的功能,我们需要学习系统调用stat

stat使用例子

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int ac, char *av[])
{
struct stat info;
if(ac > 1)
if( stat(av[1],&info) != -1){
show_stat_info(av[1],&info);
return 0;
}else{
perror(av[1]);
}
return 1;
}

show_stat_info(char *fname, struct stat *buf)
{
printf("    mode: %o\n",buf->st_mode);
printf("   links: %d\n",buf->st_nlink);
printf("    user: %d\n",buf->st_uid);
printf("   group: %d\n",buf->st_gid);
printf("    size: %d\n",buf->st_size);
printf(" modtime: %d\n",buf->st_mtime);
printf("    name: %s\n",fname);
}


(2)模式字段转换为字符

使用掩码解码,例如:

if(mode & S_IRUSR) str[1] = ‘r’;.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>

void do_ls(char []);
void dostat(char *);
void show_file_info(char *,struct stat *);
void mode_to_letters(int,char []);
char *uid_to_name(uid_t);
char *gid_to_name(gid_t);

int main(int ac,char *av[])
{
if(ac == 1)
do_ls(".");
else
while(-- ac){
printf("%s:\n",*++av);
do_ls(*av);
}
return 0;
}

void do_ls(char dirname[]){
DIR * dir_ptr;
struct dirent * direntp;

if((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr,"lsl:cannot open %s\n",dirname);
else
{
while((direntp = readdir(dir_ptr)) != NULL)
dostat(direntp->d_name);
closedir(dir_ptr);
}

}

void dostat(char *filename)
{
struct stat info;
if(stat(filename,&info) == -1)
perror(filename);
else
show_file_info(filename,&info);
}

void show_file_info(char * filename,struct stat * info_p)
{
char *uid_to_name(),*ctime(),*gid_to_name(),*filemode();
void mode_to_letters();
char modestr[11];

mode_to_letters(info_p->st_mode,modestr);

printf("%s",modestr);
printf("%4d",(int)info_p->st_nlink);
printf("%-5s",uid_to_name(info_p->st_uid));
printf("%-5s",gid_to_name(info_p->st_gid));
printf("%5ld ",(long)info_p->st_size);
printf("%.10s ",4+ctime(&info_p->st_mtime));
printf("%s\n",filename);
}

void mode_to_letters(int mode,char str[])
{
strcpy(str,"------------");

if(S_ISDIR(mode)) str[0] = 'd';
if(S_ISCHR(mode)) str[0] = 'c';
if(S_ISBLK(mode)) str[0] = 'b';

if(mode & S_IRUSR) str[1] = 'r';
if(mode & S_IWUSR) str[2] = 'w';
if(mode & S_IXUSR) str[3] = 'x';

if(mode & S_IRGRP) str[4] = 'r';
if(mode & S_IWGRP) str[5] = 'w';
if(mode & S_IXGRP) str[6] = 'x';

if(mode & S_IROTH) str[7] = 'r';
if(mode & S_IWOTH) str[8] = 'w';
if(mode & S_IXOTH) str[9] = 'x';

}

#include <pwd.h>
char *uid_to_name( uid_t uid)
{
struct passwd * getpwuid(), *pw_ptr;
static char numstr[10];

if((pw_ptr = getpwuid(uid)) == NULL){
sprintf(numstr,"%d",uid);
return numstr;
}
else
return pw_ptr->pw_name;
}

#include <grp.h>
char *gid_to_name(gid_t gid)
{
struct group *getgrpid(),*grp_ptr;
static char numstr[10];

if((grp_ptr = getgrgid(gid)) == NULL){
sprintf(numstr,"%d",gid);
return numstr;
}
else
return grp_ptr->gr_name;
}


设置和修改文件属性

chmod:修改文件的许可权限和特殊属性

chown:修改文件所有者和组

utime:修改文件的最后访问时间和最后修改时间

rename:修改文件名或移动文件的位置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: