手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux C 实现ls庖丁解牛

[日期:2014-07-26] 来源:Linux社区  作者:forlinux [字体:大 中 小]

四:ctime时间处理

    stat中的st_mtime是一个时间戳,而ls -l显示出来的是一个格式化后的字符串,所以需要对st_mtime字段进行格式化处理。

ctime接受一个time_t类型的值,可将其转换为Fri Jul 18 22:12:43 2014这样的格式,其中最前面的星期几不是我们要的,只要其后的值。代码如下

printf("%.12s",4+ctime(&info_p->st_mtime));

info_p是一个文件的stat结构体。

五:属组和属主处理

    通过stat结构体的注释信息可以看出,stat只能获取文件的uid和gid并不是uid gid对应的用户或组的名称。

uid找到对应的用户名是可以通过getpwuid这个系统调用获取,这个系统调用接受一个uid返回一个passwd的结构体,这个结构体成员如下:

              char    *pw_name  User’s login name.

              uid_t    pw_uid    Numerical user ID.

              gid_t    pw_gid    Numerical group ID.

              char    *pw_dir    Initial working directory.

              char    *pw_shell  Program to use as shell.


pw_name字段就是我们要的。实现代码如下:

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;

} 

      gid对应的组名同样可以找到一个系统调用来获取,通过man我发现可以通过getgrgid()系统调用传入一个gid

返回这个gid对应的一个group结构体,这个结构体成员如下:

              char  *gr_name The name of the group.

              gid_t  gr_gid  Numerical group ID.

              char  **gr_mem  Pointer to a null-terminated array of character

                              pointers to member names.


实现gid到组名的转换的代码如下:

char *gid_to_name(gid_t gid)

{

        struct group *getgrgid(),*grp_ptr;

        static char numstr[10];

        if((grp_ptr = getgrgid(gid)) == NULL){

                sprintf(numstr,"%d",gid);

                return numstr;

        }

        else

                return grp_ptr->gr_name;

}


到此为止,ls -l基本就完成了。后续再进行完善。

六:完整代码示例

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<dirent.h>

#include<stdio.h>

#include<stdlib.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(uid_t);

 

int main(int argc,char *argv[])

{

        if(argc == 1)

                do_ls(".");

        else

/*

 *依次遍历每一个参数,打印目录下的所有文件

 */

                while(--argc){

                        printf("%s:\n",*++argv);

                        do_ls(*argv);

                }

}

 

void do_ls(char dirname[])

{

/*

 *定义一个目录流,和目录流结构体保存读到的结果。

 */

        DIR *dir_ptr;

        struct dirent *direntp;

        if((dir_ptr = opendir(dirname)) == NULL)

                fprintf(stderr,"ls1:cannot open %s\n",dirname);

        else

        {

                while((direntp = readdir(dir_ptr)) != NULL)

        //打印结果

                        dostat(direntp->d_name);

 

//printf("%s\n",direntp->d_name);

        //关闭目录流

                closedir(dir_ptr);

        }

}

 

//获取文件信息stat结构体

void dostat(char *filename)

{

        struct stat info;

        if(stat(filename,&info) == -1)

                perror(filename);

        else

 

//分析stat结构体

                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("%-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);

}

 

//分析mode权限

void mode_to_letters(int mode,char str[])

{

//S_IS***测试宏

        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';

}

// uid gid to name group

#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 *getgrgid(),*grp_ptr;

        static char numstr[10];

        if((grp_ptr = getgrgid(gid)) == NULL){

                sprintf(numstr,"%d",gid);

                return numstr;

        }

        else

                return grp_ptr->gr_name;

}

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-07/104739.htm

linux
相关资讯       Linux c 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款