//printf
#include <stdio.h>
//stat
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//getpwuid
#include <pwd.h>
//getgrpid
#include <grp.h>
//ctime
#include <time.h>
int main(int argc,char *argv[])
{
//判断输入是否正确
if(argc!=2)
{
printf("%s filename\n",argv[0]);
return -1;
}
//获得文件的状态
struct stat st;
int ret=stat(argv[1],&st);
if(ret==-1)
{
perror("stat");
return -1;
}
char state[11]={0};
//判断文件的类型
switch(st.st_mode & S_IFMT)
{
case S_IFSOCK:
state[0]='s';
break;
case S_IFLNK:
state[0]='l';
break;
case S_IFREG:
state[0]='-';
break;
case S_IFBLK:
state[0]='b';
break;
case S_IFDIR:
state[0]='d';
break;
case S_IFCHR:
state[0]='c';
break;
case S_IFIFO:
state[0]='p';
break;
default:
state[0]='?';
break;
}
//判断当前用户的权限
state[1]=st.st_mode&S_IRUSR?'r':'-';
state[2]=st.st_mode&S_IWUSR?'w':'-';
state[3]=st.st_mode&S_IXUSR?'x':'-';
//判断当前用户所在组的权限
state[4]=st.st_mode&S_IRGRP?'r':'-';
state[5]=st.st_mode&S_IWGRP?'w':'-';
state[6]=st.st_mode&S_IXGRP?'x':'-';
//判断其他用户的权限
state[7]=st.st_mode&S_IROTH?'r':'-';
state[8]=st.st_mode&S_IWOTH?'w':'-';
state[9]=st.st_mode&S_IXOTH?'x':'-';
//该文件的硬链接数目
int links=st.st_nlink ;
char *fileuser=getpwuid(st.st_uid)->pw_name;
char *filegrp=getgrgid(st.st_gid)->gr_name;
long sz=st.st_size;
//该文件的最后一次修改时间
char* mtime=ctime(&st.st_mtime);
char buf[1024];
sprintf(buf,"%s %d %s %s %ld %s %s",state,links,fileuser,filegrp,sz,mtime,argv[1]);
printf("%s\n",buf);
return 0;
}
输出结果:
发现输出的内容不在同一行,是因为ctime函数得到的结果后面有一换行符,下面进行修改:
添加下面两行,并添加头文件string.h:
修改之后的代码为:
//printf
#include <stdio.h>
//stat
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//getpwuid
#include <pwd.h>
//getgrpid
#include <grp.h>
//ctime
#include <time.h>
//strncpy
#include <string.h>
int main(int argc,char *argv[])
{
//判断输入是否正确
if(argc!=2)
{
printf("%s filename\n",argv[0]);
return -1;
}
//获得文件的状态
struct stat st;
int ret=stat(argv[1],&st);
if(ret==-1)
{
perror("stat");
return -1;
}
char state[11]={0};
//判断文件的类型
switch(st.st_mode & S_IFMT)
{
case S_IFSOCK:
state[0]='s';
break;
case S_IFLNK:
state[0]='l';
break;
case S_IFREG:
state[0]='-';
break;
case S_IFBLK:
state[0]='b';
break;
case S_IFDIR:
state[0]='d';
break;
case S_IFCHR:
state[0]='c';
break;
case S_IFIFO:
state[0]='p';
break;
default:
state[0]='?';
break;
}
//判断当前用户的权限
state[1]=st.st_mode&S_IRUSR?'r':'-';
state[2]=st.st_mode&S_IWUSR?'w':'-';
state[3]=st.st_mode&S_IXUSR?'x':'-';
//判断当前用户所在组的权限
state[4]=st.st_mode&S_IRGRP?'r':'-';
state[5]=st.st_mode&S_IWGRP?'w':'-';
state[6]=st.st_mode&S_IXGRP?'x':'-';
//判断其他用户的权限
state[7]=st.st_mode&S_IROTH?'r':'-';
state[8]=st.st_mode&S_IWOTH?'w':'-';
state[9]=st.st_mode&S_IXOTH?'x':'-';
//该文件的硬链接数目
int links=st.st_nlink ;
char *fileuser=getpwuid(st.st_uid)->pw_name;
char *filegrp=getgrgid(st.st_gid)->gr_name;
long sz=st.st_size;
//该文件的最后一次修改时间
char* mtime=ctime(&st.st_mtime);
//将mtime除了最后换行符之外的内容拷贝到mmtime
char mmtime[512]={0};
strncpy(mmtime,mtime,strlen(mtime)-1);
char buf[1024];
sprintf(buf,"%s %d %s %s %ld %s %s",state,links,fileuser,filegrp,sz,mmtime,argv[1]);
printf("%s\n",buf);
return 0;
}
运行结果:
参考:牛客网 C++高薪求职项目《Linux高并发服务器开发》1.25模拟实现ls -l命令
专属优惠链接:
https://www.nowcoder.com/courses/cover/live/504?coupon=AvTPnSG