#include <my_head.h>
void get_filePermission(mode_t m)
{
char per[4]="rwx";
for(int i=0;i<9;i++)
{
if((m&(0400>>i))==0){
putchar('-');
continue;
}
putchar(per[i%3]);
}
putchar(' ');
}void get_fileType(mode_t m)
{
switch(m&S_IFMT)
{
case S_IFSOCK: putchar('s'); break;
case S_IFLNK: putchar('l'); break;
case S_IFREG: putchar('-'); break;
case S_IFBLK: putchar('b'); break;
case S_IFDIR: putchar('d'); break;
case S_IFCHR: putchar('c'); break;
case S_IFIFO: putchar('p'); break;
default:printf("错误\n");
}
putchar(' ');
return ;
}
int main(int argc, const char *argv[])
{DIR* dp=opendir("./");
if(NULL==dp)
{
perror("opendir");
return -1;
}struct dirent *rp=NULL;
int count=0;
while(1)
{
rp=readdir(dp);
if(NULL==rp)
{
break;
}struct stat buf;
if(stat(rp->d_name,&buf)<0)
{
perror("stat");
return -1;
}get_fileType(buf.st_mode);
get_filePermission(buf.st_mode);printf("%ld ",buf.st_nlink);
struct passwd*pwd=getpwuid(buf.st_uid);
if(NULL==pwd)
{
perror("getpwuid");
return -1;
}
printf("%s ",pwd->pw_name);
struct group* grp=getgrgid(buf. st_gid);
if(NULL==grp)
{
perror("getgrgid");
return -1;
}
printf("%s ",grp->gr_name);printf("%6ld ",buf.st_size);
struct tm* info=localtime(&buf.st_ctime);
printf("%02d %02d %02d:%02d ",info->tm_mon+1,info->tm_mday,\
info->tm_hour,info->tm_sec);printf("%s",rp->d_name);
putchar(10);
}closedir(dp);
return 0;
}
IO Day3
于 2023-09-07 19:41:18 首次发布