#include <stdio.h>
#include <time.h>
#include <linux/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

char *wday[]={"Sun","Mon","Tue","Wen","Thu","Fri","Sat"};

void list(char *name, int suojin)
{
	DIR *dirname;
	struct dirent *content;
	struct stat sb;
	struct tm *ctime;
	int i;
	if((dirname=opendir(name))==NULL)
	{
		printf("No such directory!\n");
		return;
	}
	chdir(name);
	while((content=readdir(dirname))!=NULL)
	{
		for(i=0;i<suojin;i++)
			putchar('\t');
		if(content->d_type==4)
			printf("direc\t");
		else if(content->d_type==8)
			printf("file\t");
		else
			printf("others\t");
		stat(content->d_name,&sb);
		ctime=gmtime(&sb.st_mtime);
		printf("%2d/%2d/%4d  %s  %2d:%2d:%2d\t",ctime->tm_mday,ctime->tm_mon+1,ctime->tm_year+1900,wday[ctime->tm_wday],ctime->tm_hour,ctime->tm_min,ctime->tm_sec);
		printf("%d\t",sb.st_size);
		printf("%s\n",content->d_name);
		if(content->d_type==4&&strcmp(content->d_name,"..")&&strcmp(content->d_name,"."))
		{
			list(content->d_name,suojin+1);

		}
	}
	closedir(dirname);
	chdir("..");
}

int main(int argc, char * argv[])
{
	char name[256];
	printf("type\tlc_time\t\t\t\tsize\tname\n");
	printf("******************************************\n");
	if(argc==1)
	{
		printf("Enter directory name:");
		scanf("%s",name);
		list(name,0);
	}
	else
		list(argv[1],0);
}
