#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
int aflag = 0;
int lflag = 0;
typedef char * datatype;
typedef struct _node_
{
char data[256];
struct _node_ *next;
}linknode,*linklist;
void Display_file(char *path,char *name); //打印文件的信息
void Mode(struct stat buf); //查看文件的各种属性
void SeleFi(struct stat buf); //打印文件的类型
void Max(struct stat buf); //
void Time(struct stat buf); //打印修改文件的时间
void GetUID(struct stat buf); //获得UID并打印
void GetGID(struct stat buf); //获得GID并打印
void Display_Dir(linklist list,char *name); //打印目录
void ifDir(linklist list,char *path); //判断是否是目录
void getflag(int argc,char **argv,int *aflag,int *lflag); //获得-a或-l的参数
void GetName(linklist list,char *name); //获得目录中所有文件名
linklist CreateEmptyLinklist()
{
linklist h=(linklist)malloc(sizeof(linknode));
h->next=NULL;
return h;
}
int EmptyLinklist(linklist h)
{
return (NULL==h->next);
}
int LengthLinklist(linklist h)
{
int i=-1;
while(h->next!=NULL) {
i++;
h=h->next;
}
return i;
}
void VisitLinklist(linklist h)
{
h=h->next;
while(h!=NULL) {
// printf("%d\n",h->data);
Display_file(h->data,h->data);
h=h->next;
}
return ;
}
void InsertLinklist_2(linklist h,datatype x)
{
while((h->next!=NULL)&&(strcmp(h->next->data,x) < 0)) {
h=h->next;
}
linklist new=(linklist)malloc(sizeof(linknode));
strcpy(new->data,x);
new->next=h->next;
h->next=new;
return ;
}
void DeleteLinklist_2(linklist h,datatype x)
{
linklist p;
p=h->next;
while(p!=NULL) {
if(p->data==x) {
h->next=p->next;
free(p);
}
h=h->next;
p=p->next;
}
}
int main(int argc,char **argv)
{
struct stat buf;
int ch;
int i;
linklist list = CreateEmptyLinklist();
getflag(argc,argv,&aflag,&lflag);
for ( i = optind ; i < argc ; i++ ) {
ifDir(list,argv[i]);
}
if (optind == argc) {
Display_Dir(list,".");
}
VisitLinklist(list);
printf("\n");
return 0;
}
void SeleFi(struct stat buf)
{
if ( S_ISREG(buf.st_mode) ) {
printf("-");
}
if ( S_ISDIR(buf.st_mode) ) {
printf("d");
}
if ( S_ISCHR(buf.st_mode) ) {
printf("c");
}
if ( S_ISBLK(buf.st_mode) ) {
printf("b");
}
if ( S_ISFIFO(buf.st_mode) ) {
printf("p");
}
if ( S_ISLNK(buf.st_mode) ) {
printf("l");
}
if ( S_ISSOCK(buf.st_mode) ) {
printf("s");
}
}
void Mode(struct stat buf)
{
if ( (buf.st_mode)&S_IRUSR ) {
printf("r");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IWUSR ) {
printf("w");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IXUSR ) {
printf("x");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IRGRP ) {
printf("r");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IWGRP ) {
printf("w");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IXGRP ) {
printf("x");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IROTH ) {
printf("r");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IWOTH ) {
printf("w");
}else {
printf("-");
}
if ( (buf.st_mode)&S_IXOTH ) {
printf("x");
}else {
printf("-");
}
printf(" ");
}
void Max(struct stat buf)
{
printf("%8ld",buf.st_size);
printf(" ");
}
void Time(struct stat buf)
{
struct tm *tp;
tp = localtime(&(buf.st_ctime));
printf("%2d-%2d-%2d %2d:%2d",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,tp->tm_hour,tp->tm_min);
printf(" ");
}
void GetUID(struct stat buf)
{
struct passwd *p;
p = getpwuid(buf.st_uid);
printf("%3s ",p->pw_name);
}
void GetGID(struct stat buf)
{
struct group *g;
g = getgrgid(buf.st_gid);
printf("%3s ",g->gr_name);
}
void Display_file(char *path,char *name)
{
struct stat buf;
if ( stat(path,&buf) == -1 ) {
perror("ls:");
return ;
}
if ( lflag == 1 ) {
SeleFi(buf);
Mode(buf);
printf("%3d ",buf.st_nlink);
GetUID(buf);
GetGID(buf);
Max(buf);
Time(buf);
printf("%s\n",name);
return ;
}
printf("%s ",name);
}
void Display_Dir(linklist list,char * dirname)
{
DIR *mydir;
struct dirent *dir;
char path[256];
if ( (mydir = opendir(dirname)) == NULL ) {
perror("DIR:");
return ;
}
printf("%s :\n",dirname);
while ( (dir = readdir(mydir)) != NULL ) {
if ( dir->d_name[0]== '.' ) {
if ( aflag == 0 ) {
continue;
}
}
sprintf(path,"%s/%s",dirname,dir->d_name);
// Display_file(path,dir->d_name);
GetName(list,dir->d_name);
}
closedir(mydir);
}
void ifDir(linklist list,char *path)
{
struct stat buf;
if ( stat(path,&buf) < 0 ) {
perror("path:");
}
if ( S_ISDIR(buf.st_mode) ) {
Display_Dir(list,path);
}else {
// Display_file(path,path);
GetName(list,path);
}
}
void getflag(int argc,char **argv,int *aflag,int *lflag)
{
int ch;
while ( (ch = getopt(argc,argv,"la")) != EOF ) {
switch (ch) {
case 'a':
*aflag = 1;
break;
case 'l':
*lflag = 1;
break;
default:
printf("Wrong option !\n");
break;
}
}
}
void GetName(linklist list,char *name)
{
InsertLinklist_2(list,name);
}