利用系统函数的调用来获取文件的属性,显示文件的类型和权限

#include
#include
#include
#include
#include
char filemode(mode_t m, char str)
{
if(S_IREG(m))
str[0]=’-’;
else if(S_ISDIR(m))
str[0] = ‘d’;
else if(S_ISCHR(m))
str[0] = ‘c’;
else if(S_ISBLK(m))
str[0] = ‘b’;
else if(S_ISFIFO(m))
str[0] = ‘q’;
else if(S_ISLNK(m))
str[0] = ‘l’;
else if(S_ISSOCK(m))
str[0] = ‘s’;
else
str[0] = ‘?’;
str[1] =’\0’;
strcat(str,S_IRUSR&m?“r”:"-");
strcat(str,S_IWUSR&m?“w”:"-");
strcat(str,S_IXUSR&m?“x”:"-");

strcat(str,S_IRGRP&m?“r”:"-");
strcat(str,S_IWGRP&m?“w”:"-");
strcat(str,S_IXGRP&m?“x”:"-");

strcat(str,S_IROTH&m?“r”:"-");
strcat(str,S_IWOTH&m?“w”:"-");
strcat(str,S_IXOTH&m?“x”:"-");
return str;
}
char filetime(time_t,char* str)
{
struct tm* it =localtime(&t);
sprintf(str,"%4d-%02d-%02d %02d: %02d: %02d",it->tmyear+1900,it- >tmmom+1,it->it_mday,it->tm_hour,it->tm_hour,it->tm_min,it->tm_sex);
return str;
}
int main()

{

struct stat buf ={};

if(stat(“link.txt”,&buf))

{

perror(“state”);

return -1;

}

char str[50] = {};

puts(filemode(buf,st_mode,str));

puts(filetime(buf,stmtime,str));

}

你可能感兴趣的:(利用系统函数的调用来获取文件的属性,显示文件的类型和权限)