• 朱笃信
• 201821121021
• 计算1811

# 1. 编写程序

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<dirent.h>
#include<grp.h>
#include<pwd.h>
#include <time.h>
#define NAME_SIZE 20

struct fnode
{
struct fnode *next;
char name[NAME_SIZE];       //文件名
};

struct fnode* insert_list(struct fnode *temp , struct fnode *linklist)    //向文件中添加路径
{
{
}
else
{
struct fnode *node ;
while(node->next != NULL)
{
node = node->next ;
}
node->next = temp ;
}
}

{
struct passwd *pw;
struct group *group;
char type[7] = {'p' , 'c' , 'd' , 'b' , '-' , 'l' , 's'};      //文件类型
char *perm[8] = {"---" , "--x" , "-w-" , "-wx" , "r--" , "r-x" , "rw-" , "rwx"};       //权限类型
while(temp != NULL)
{
struct stat mystat;
if(stat(temp->name,&mystat) == -1)
{
perror("stat");
exit(EXIT_FAILURE);
}
printf("%4d ",mystat.st_mode);
int index=((mystat.st_mode >> 12) & 0xF) / 2;    //查询文件类型
printf("%c",type[index]);
printf("%s",perm[mystat.st_mode >> 6 & 07]);       //本人权限
printf("%s",perm[mystat.st_mode >> 3 & 07]);       //组员权限
printf("%s ",perm[mystat.st_mode >> 0 & 07]);      //他人权限
pw=getpwuid(mystat.st_uid);             //通过uid获取passwd数据
printf("%10s ",pw->pw_name);
group=getgrgid(mystat.st_gid);           //通过gid获取组数据
printf("%10s ",group->gr_name);
printf("%8ld ",mystat.st_size);        //文件大小
char buf[30];
memset(buf,'\0',30) ;
ctime_r(&mystat.st_mtime,buf);        //上一次被修改的时间
buf[16] ='\0';
printf("%s ", buf);
printf(" %s\n",temp->name);         //文件相对路径
temp = temp->next ;
}
}

int main()
{
char path[]={"./"};
struct fnode *linklist = NULL;     //初始化链表
struct stat stat_info;
DIR *dirp = NULL;
struct dirent *entp = NULL;

if(stat(path, &stat_info) == -1)    //获取文件属性
{
perror("stat");
exit(EXIT_FAILURE);
}

dirp = opendir(path); //打开当前路径
if(dirp == NULL)
{
perror("open dir");
exit(EXIT_FAILURE);
}

{
struct fnode *temp = (struct fnode *)malloc(sizeof(struct fnode));
if(temp == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
temp->next = NULL;
memset(temp->name,'\0',NAME_SIZE);          //清除节点数据
memcpy(temp->name,entp->d_name,strlen(entp->d_name));   //赋予新数据
}
closedir(dirp) ;

//free
struct fnode *temp;
while(temp != NULL)
{
}
return 0;
}


# 2. 分析运行结果

ls -lai执行结果：

本代码执行结果：

本次实验思路：

# 3. 通过该实验产生新的疑问及解答

void ListSort(node **head)
{
Node *p = NULL;
Node *q = NULL;
Node *t = NULL;

{
return 0;
}

for(p = *head; p != NULL; p = p->next)
{
for(q = *head; q->next != NULL; q = q->next)
{
if(q->name > q->next->name)
{
swap(q->name, q->next->name);
}
}
}
}


posted @ 2020-04-29 21:55  duxinZhu  阅读(281)  评论(0编辑  收藏  举报