include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<grp.h>
#include<pwd.h>
#include<time.h>
//文件的权限
char *get_filepermission(mode_t mode,char p[10])
{
int a=0400;
char b[]="xrw";
for(int i=1;i<=9;i++)
{
if((mode&a)!=0)
{
// putchar(b[i%3]);
p[i-1]=b[i%3];
}
else
{
// putchar('-');
p[i-1]='-';
}
a=a>>1;
}
// puts("");
return p;
}
//文件的类型
char get_fileType(mode_t mode)
{
if(S_ISREG(mode))
{
return '-';
}
else if(S_ISDIR(mode))
{
return 'd';
}
else if(S_ISCHR(mode))
{
return 'c';
}
else if(S_ISBLK(mode))
{
return 'b';
}
else if(S_ISFIFO(mode))
{
return 'p';
}
else if(S_ISLNK(mode))
{
return 'l';
}
else if(S_ISSOCK(mode))
{
return 's';
}
}
//文件的所属用户
void getp(mode_t mode)
{
struct passwd *pass=getpwuid(mode);
if(NULL==pass)
{
perror("pass");
return ;
}
printf(" %s",pass->pw_name);
}
//文件的所属组用户
void getg(mode_t mode)
{
struct group *pass=getgrgid(mode);
if(NULL==pass)
{
perror("pass");
return ;
}
printf(" %s",pass->gr_name);
}
//文件的时间
void loca(time_t mode)
{
struct tm *p=localtime(&mode);
printf(" %d %d %d:%d",p->tm_mon+1,p->tm_mday,\
p->tm_hour,p->tm_min);
}
功能函数文件^
#ifndef __STAFUN_H__
#define __STAFUN_H__
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<grp.h>
#include<pwd.h>
#include<time.h>
char *get_filepermission(mode_t mode,char p[10]);
char get_fileType(mode_t mode);
void getp(mode_t mode);
void getg(mode_t mode);
void loca(time_t mode);
#endif
声明文件
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<grp.h>
#include<pwd.h>
#include<time.h>
#include"stafun.h"
#include<dirent.h>
#include<errno.h>
/*char *get_filepermission(mode_t mode,char p[10])
{
int a=0400;
char b[]="xrw";
for(int i=1;i<=9;i++)
{
if((mode&a)!=0)
{
// putchar(b[i%3]);
p[i-1]=b[i%3];
}
else
{
// putchar('-');
p[i-1]='-';
}
a=a>>1;
}
// puts("");
return p;
}
char get_fileType(mode_t mode)
{
if(S_ISREG(mode))
{
return '-';
}
else if(S_ISDIR(mode))
{
return 'd';
}
else if(S_ISCHR(mode))
{
return 'c';
}
else if(S_ISBLK(mode))
{
return 'b';
}
else if(S_ISFIFO(mode))
{
return 'p';
}
else if(S_ISLNK(mode))
{
return 'l';
}
else if(S_ISSOCK(mode))
{
return 's';
}
}
void getp(mode_t mode)
{
struct passwd *pass=getpwuid(mode);
if(NULL==pass)
{
perror("pass");
return ;
}
printf(" %s",pass->pw_name);
}
void getg(mode_t mode)
{
struct group *pass=getgrgid(mode);
if(NULL==pass)
{
perror("pass");
return ;
}
printf(" %s",pass->gr_name);
}
void loca(time_t mode)
{
struct tm *p=localtime(&mode);
printf(" %d %d %d:%d",p->tm_mon+1,p->tm_mday,\
p->tm_hour,p->tm_min);
}
*/
int main(int argc, const char *argv[])
{
char *p[100]={NULL};
int i=0;
int len=0;
DIR *dp=opendir(argv[1]);
if(NULL==dp)
{
perror("opendir");
return -1;
}
struct dirent *pr=NULL;
// int n=0;
while(1)
{
pr=readdir(dp);
if(NULL==pr)
{
if(errno!=0)
{
perror("reddir");
return -1;
}
else
{
printf("读取完毕\n");
break;
}
}
// n=strcmp(pr->d_name,"..");
if(pr->d_name[0]=='.')
{
continue;
}
printf("%s\n",pr->d_name);
p[i]=pr->d_name;
i++;
len++;
}
// int i=0;
// int len =3;
struct stat buf;
for(i=0;i<len;i++)
{
if(stat(p[i],&buf)<0)
{
perror("seat");
return -1;
}
//文件的类型及权限
// printf("mode:%o\n",buf.st_mode);
char n=get_fileType(buf.st_mode);
putchar(n);
char arr[10]="";
char *p1= get_filepermission(buf.st_mode,arr);
// puts(p);
printf("%s",p1);
//文件的硬链接数
printf(" %ld",buf.st_nlink);
//文件的所属用户
// printf("uid:%d\n",buf.st_uid);
getp(buf.st_uid);
//文件的所属组用户
// printf("gid:%d\n",buf.st_gid);
getg(buf.st_gid);
//文件的大小
printf(" %ld",buf.st_size);
//文件的时间
// printf("time:%ld\n",buf.st_ctime);
loca(buf.st_ctime);
//文件名
printf(" %s\n",p[i]);
}
return 0;
}
man函数^