手动实现简易版 ls -l
添加如下的四个C语言函数代码,实现四个功能,使得显示内容与系统自带的ls更接近。
(1)把数字形式的模式字段转换为字符
mode:100644转化为“-rw-r-r-”
void mode_to_char(int mode,char str[])
void mode_to_char(int mode,char str[])
{
strcpy(str,"----------");
// mode_t newmode = mode & 07777;//get the last four digits
// int i=0;
//judge user's
if(mode & S_IRUSR)str[1]='r';
if(mode & S_IWUSR)str[2]='w';
if(mode & S_IXUSR)str[3]='x';
//judge group's
if(mode & S_IRGRP)str[4]='r';
if(mode & S_IWGRP)str[5]='w';
if(mode & S_IXGRP)str[6]='x';
//judge other's
if((mode & S_IROTH)==S_IROTH)str[7]='r';
if(mode & S_IWOTH)str[8]='w';
if(mode & S_IXOTH)str[9]='x';
}
(2)将用户ID转换成字符串
char* uid_to_name(int uid)
#include<pwd.h>
char* uid_to_name(uid_t uid)
{
struct passwd * pw_ptr;//Gets the username based on the user id
pw_ptr = getpwuid(uid);
return pw_ptr->pw_name;
}
(3)将组ID转换成字符串
char* gid_to_name(gid_t gid)
#include<grp.h>
char* gid_to_name(gid_t gid)
{
struct group * grp_ptr;//Gets the group name based on the group id
grp_ptr = getgrgid(gid);
return grp_ptr->gr_name;
}
(4)利用ctime函数将日期格式进行转换;
C 库函数 char *ctime(const time_t *timer)
返回一个表示当地时间的字符串,当地时间是基于参数 timer。
返回的字符串格式如下: Www Mmm dd hh:mm:ss yyyy 其中,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份。
“4+”是因为要跳过前四位表示星期的数,
“%.12s”表示打印十二位宽的右对齐字符
printf("%.12s ",4+ctime(&buf->st_mtime));
完整代码
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
void show_stat_info(char *,struct stat * );
void mode_to_char(int ,char str[]);
char* uid_to_name(uid_t );
char* gid_to_name(gid_t );
//char* gid_to_name(int gid)
int main(int ac,char *av[])
{
struct stat info;
if(ac>1)
if(stat(av[1], &info)!= -1)
{
show_stat_info(av[1],&info);
// mode_to_char(buf->st_mode,a);
return 0;
}
else
perror(av[1]);
return 1;
}
show_stat_info(char *fname,struct stat * buf)
{
char * uid_to_name(), * gid_to_name(),*ctime();
void mode_to_char();
char modestr[11];
mode_to_char(buf->st_mode,modestr);
printf("%s ",modestr);
printf("%d ",(int)buf->st_nlink);
printf("%s ",uid_to_name(buf->st_uid));
printf("%s ",gid_to_name(buf->st_gid));
printf("%ld ",(long)buf->st_size);
printf("%.12s ",4+ctime(&buf->st_mtime));
printf("%s\n",fname);
//+4: Removes the display of the day of the week
//%.12s: Print 12 aligned characters
}
#include<pwd.h>
char* uid_to_name(uid_t uid)
{
struct passwd * pw_ptr;//Gets the username based on the user id
pw_ptr = getpwuid(uid);
return pw_ptr->pw_name;
}
#include<grp.h>
char* gid_to_name(gid_t gid)
{
struct group * grp_ptr;//Gets the group name based on the group id
grp_ptr = getgrgid(gid);
return grp_ptr->gr_name;
}
void mode_to_char(int mode,char str[])
{
strcpy(str,"----------");
// mode_t newmode = mode & 07777;//get the last four digits
// int i=0;
//judge user's
if(mode & S_IRUSR)str[1]='r';
if(mode & S_IWUSR)str[2]='w';
if(mode & S_IXUSR)str[3]='x';
//judge group's
if(mode & S_IRGRP)str[4]='r';
if(mode & S_IWGRP)str[5]='w';
if(mode & S_IXGRP)str[6]='x';
//judge other's
if((mode & S_IROTH)==S_IROTH)str[7]='r';
if(mode & S_IWOTH)str[8]='w';
if(mode & S_IXOTH)str[9]='x';
}