void output_info(struct fnode *head)
{
struct fnode *temp=head;
while(temp!=NULL)
{
struct stat mystat;
if(-1==stat(temp->name,&mystat))
{
perror("stat");exit(EXIT_FAILURE);
}
output_type_perm(mystat.st_mode);
printf(" %4d",mystat.st_nlink);
output_user_group(mystat.st_uid,mystat.st_gid);
printf(" %8ld",mystat.st_size);
output_mtime(mystat.st_mtime);
printf(" %s\n",temp->name);
temp=temp->next;
}
}
void free_list(struct fnode *ptr)
{
struct fnode *temp=ptr;
struct fnode *link=ptr;
while(ptr)
{
temp=ptr;
ptr=ptr->next;
free(temp);
}
}
// main()函数源码
int main(int argc,char *argv[])
{
if(argc < 2)
{
printf("usage :%s dir_name\n",argv[0]);exit(EXIT_FAILURE);
}
int i;
for(i=1;i<argc;i++)
{
struct fnode *linklist=NULL;
struct stat stat_info;
if(stat(argv[i],&stat_info)==-1)
{
perror("stat");exit(EXIT_FAILURE);
}
if (S_ISREG(stat_info.st_mode)) //regular file
{
struct fnode *temp=(struct fnode *)malloc(sizeof(struct fnode));
if(NULL==temp)
{
perror("malloc");exit(EXIT_FAILURE);
}
temp->next=NULL;
memset(temp->name,'\0',NAME_SIZE);
memcpy(temp->name,argv[i],strlen(argv[i]));
linklist=insert_list(temp,linklist);
output_info(linklist);//output information of the file
}
else if(S_ISDIR(stat_info.st_mode))
{
char buf[NAME_SIZE];
getcwd(buf,128);
DIR *dirp=NULL;
dirp=opendir(argv[i]);
if(NULL==dirp)
{
perror("opendir");exit(EXIT_FAILURE);
}
struct dirent *entp=NULL;
while(entp=readdir(dirp))
{
struct fnode *temp=(struct fnode *)malloc(sizeof(struct fnode));
if(NULL==temp)
{
perror("malloc");exit(EXIT_FAILURE);
}
temp->next=NULL;
memset(temp->name,'\0',NAME_SIZE);
memcpy(temp->name,entp->d_name,strlen(entp->d_name));
linklist=insert_list(temp,linklist);
}
chdir(argv[i]);//change the current dirctory
close(dirp);
output_info(linklist);
chdir(buf);
}
free_list(linklist);
}
return 1;
}