#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h> int llstat(char *p);
int astat(char *p);
int istat(char *p);
int hstat(char *p);
static int nums; int main(int argc, char **argv)
{
int c;
char *optstring = "-laih";
if(argc < )
return ;
while (){
c = getopt(argc, argv, optstring);
if(c == -)
break;
switch(c){
case 'l': llstat(argv[]);break;
case 'a': astat(argv[]);break;
case 'i': istat(argv[]);break;
case 'h': hstat(argv[]);break;
// case '1': printf("非选项参数\n");break;
case '?': printf("?\n");break;
default : break;
}
}
return ; } //-l
//文件名->路径
static char *buf_cat(const char *p, const char *name)
{
char *bufcat = malloc();
memset(bufcat,'\0',);
strcpy(bufcat,p);
strcat(bufcat,"/");
strcat(bufcat,name);
return bufcat;
}
// 判断是否为隐藏文件
static int is_i(const char *p)
{
if(*p == '.')
return ;
else
return ;
} int l_llstat(const char *p, const char *name)
{
struct stat mystat;
struct passwd *pwd = NULL;
struct group *grp = NULL;
struct tm *tmp = NULL;
char *buf=NULL; buf = buf_cat(p,name);
if(lstat(buf,&mystat) == -){
perror("stat()");
return ;
}
if(is_i(name) == ){
nums+=mystat.st_blocks/; //类型
switch(mystat.st_mode & 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: 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){
if(mystat.st_mode & S_ISUID)
putchar('s');
else
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){
if(mystat.st_mode & S_ISGID)
putchar('s');
else
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){
if(mystat.st_mode & S_ISVTX)
putchar('t');
else
putchar('x');
}
else
putchar('-');
//硬链接
printf(" %ld ", mystat.st_nlink);
//文件拥有者名
pwd = getpwuid(mystat.st_uid);
printf("%s ", pwd->pw_name);
//文件所有组
grp = getgrgid(mystat.st_gid);
printf("%s ",grp->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", name);
}
return ;
} int llstat(char *p)
{
DIR *dp = NULL;
struct dirent *entry = NULL;
char buf[] = {};
struct stat mystat1;
if(lstat(p,&mystat1) == -){
perror("stat()");
return ;
}
if(S_ISREG(mystat1.st_mode)){
l_llstat(".", p);
}else{
dp = opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
l_llstat(p, entry->d_name);
}
printf("总用量:%d\n", nums);
closedir(dp);
} return ; } // a int a_llstat(const char *p, const char *name)
{
struct stat mystat;
char *buf=NULL; buf = buf_cat(p,name);
if(lstat(buf,&mystat) == -){
perror("stat()");
return ;
}
//文件名
printf("%s ", name);
return ;
} int astat(char *p)
{
struct stat mystat2;
struct dirent *entry = NULL;
DIR *dp = NULL;
if(lstat(p,&mystat2) == -){
perror("stat()");
return ;
} if(S_ISREG(mystat2.st_mode)){
printf("%s ", p);
}
dp= opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
a_llstat(p, entry->d_name);
} closedir(dp);
printf("\n");
return ; } // i int i_llstat(const char *p, const char *name)
{
struct stat mystat;
char *buf=NULL; buf = buf_cat(p,name);
if(lstat(buf,&mystat) == -){
perror("stat()");
return ;
}
printf("%ld ",mystat.st_ino);
//文件名
printf("%s ", name);
return ;
} int istat(char *p)
{
struct stat mystat2;
struct dirent *entry = NULL;
DIR *dp = NULL;
if(lstat(p,&mystat2) == -){
perror("stat()");
return ;
} if(S_ISREG(mystat2.st_mode)){
if(is_i(p))
return ;
printf("%ld ",mystat2.st_ino);
printf("%s ", p);
}
else{
dp= opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
if(is_i(entry->d_name))
continue;
i_llstat(p, entry->d_name);
}
}
closedir(dp);
printf("\n");
return ;
} //h
int hstat(char *p)
{
struct stat mystat2;
struct dirent *entry = NULL;
DIR *dp = NULL;
if(lstat(p,&mystat2) == -){
perror("stat()");
return ;
} if(!((mystat2.st_mode & S_IFMT) == S_IFDIR)){
if(!is_i(p)){
printf("%s\n", p);
return ;
}
} else{ dp= opendir(p);
if(dp == NULL){
perror("opendir()");
return ;
} while(){
entry = readdir(dp);
if(NULL == entry){
if(errno){
perror("readdir()");
closedir(dp);
return ;
}
break;
}
if(is_i(entry->d_name) == )
printf("%s ", entry->d_name);
}
closedir(dp);
}
printf("\n");
return ;
}