#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
#include<pwd.h>
#include<grp.h>
#include<dirent.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
int l_stat(char * p1,char * p);
void a_stat(char * p);
void i_stat(char * p);
void e_dir(char * p);
static long numbers;
//判断是否隐藏文件
int is_spot(const char * p)
{
if(*p == '.')
return ;
else
return ;
}
void e_dir(char * p)
{
struct stat mystat;
DIR * dir;
struct dirent * pd;
int cent = lstat(p,&mystat);
if((mystat.st_mode & S_IFMT) == S_IFDIR)
{
dir = opendir(p);
if(dir == NULL)
{
if(errno)
{
perror("opendir()");
return;
}
}
while()
{
pd = readdir(dir);
if(pd == NULL)
{
if(errno)
{
perror("readdir()");
return ;
}
break;
}
l_stat(p,pd->d_name);
}
closedir(dir);
}
else
l_stat(".",p);
printf("%ldk\n",numbers/);
return ;
}
void h_stat(char * p)
{
struct stat mystat;
DIR * dir;
struct dirent * pd;
int cent = stat(p,&mystat);
if((mystat.st_mode & S_IFMT) == S_IFDIR)
{
dir = opendir(p);
if(dir == NULL)
{
if(errno)
{
perror("opendir()");
return;
}
}
while()
{
pd = readdir(dir);
if(pd == NULL)
{
if(errno)
{
perror("readdir()");
return ;
}
break;
}
if(is_spot(pd->d_name) == )
printf("%s \n",pd->d_name);
}
closedir(dir);
}
else
printf("%s \n",p);
return;
}
int main(int argc,char ** argv)
{
if(argc < )
return ;
char * op = "-laih";
int s;
while()
{
s = getopt(argc,argv,op);
if(s == -)
break;
switch(s)
{
case 'l':e_dir(argv[]); break;
case 'a':a_stat(argv[]);break;
case 'i':i_stat(argv[]);break;
case 'h':h_stat(argv[]);break;
case '?':printf("无该选项\n");break;
// case 1 : printf("%s\n",argv[2]);break;
default:break;
}
}
return ;
}
void i_stat(char * p)
{
struct stat mystat;
int nodenumber;
DIR * dir;
struct dirent * pd;
int cent = stat(p,&mystat);
if((mystat.st_mode & S_IFMT) == S_IFDIR)
{
dir = opendir(p);
if(dir == NULL)
{
if(errno)
{
perror("opendir()");
return;
}
}
while()
{
pd = readdir(dir);
if(pd == NULL)
{
if(errno)
{
perror("readdir()");
return ;
}
break;
}
if(is_spot(pd->d_name) == )
{
printf("%ld ",pd->d_ino);
printf("%s\n",pd->d_name);
}
}
closedir(dir);
}
else
{
printf("%ld ",mystat.st_ino);
printf("%s\n",p);
}
}
void a_stat(char * p)
{
struct stat mystat;
DIR * dir;
struct dirent * pd;
int cent = stat(p,&mystat);
if((mystat.st_mode & S_IFMT) == S_IFDIR)
{
dir = opendir(p);
if(dir == NULL)
{
if(errno)
{
perror("opendir()");
return;
}
}
while()
{
pd = readdir(dir);
if(pd == NULL)
{
if(errno)
{
perror("readdir()");
return ;
}
break;
}
printf("%s ",pd->d_name);
}
}
else
printf("%s ",p);
}
// 追加/获得路径
char * dir_cat(const char * p,const char * buf)
{
char * buf_cat = malloc();
memset(buf_cat,'\0',);
strcpy(buf_cat,p);
buf_cat[strlen(buf_cat)] = '/';
strcat(buf_cat,buf);
return buf_cat;
}
//获取文件属性
int l_stat(char * p,char * p_name)
{
struct tm * tmp = NULL;
struct passwd * pwd = NULL;
struct group * wd = NULL;
struct stat mystat;
char buf[];
char * p1;
int cnt;
p1 = dir_cat(p,p_name);
cnt = stat(p1,&mystat);
if(cnt == -)
{
perror("stat()");
return -;
}
//判断是不是隐藏文件
if(is_spot(p_name) == )
{
numbers += mystat.st_blocks;
switch(mystat.st_mode & S_IFMT)
{
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:break;
}
//拥有者
if(mystat.st_mode & S_IRUSR)
putchar('r');
else
putchar('-');
if(mystat.st_mode & S_IWUSR)
putchar('w');
else
putchar('-');
if(mystat.st_mode & S_IXUSR)
putchar('x');
else
putchar('-');
//所属组
if(mystat.st_mode & S_IRGRP)
putchar('r');
else
putchar('-');
if(mystat.st_mode & S_IWGRP)
putchar('w');
else
putchar('-');
if(mystat.st_mode & S_IXGRP)
putchar('x');
else
putchar('-');
//其他
if(mystat.st_mode & S_IROTH)
putchar('r');
else
putchar('-');
if(mystat.st_mode & S_IWOTH)
putchar('w');
else
putchar('-');
if(mystat.st_mode & S_IXOTH)
putchar('x');
else
putchar('-');
//硬链接个数
printf("%2ld ",mystat.st_nlink);
//文件拥有者名字
pwd = getpwuid(mystat.st_uid);
printf("%s ",pwd->pw_name);
//文件所属组
wd = getgrgid(mystat.st_gid);
printf("%s ",wd->gr_name);
//总字节个数
printf("%5ld ",mystat.st_size);
//获取时间
tmp = localtime(&mystat.st_mtim.tv_sec);
strftime(buf,,"%m 月 %d %H:%M",tmp);
printf("%s ",buf);
//文件名
printf("%s\n",p_name);
}
return ;
}
05-11 17:28