操作系统第4次实验报告:文件系统

姓名:张一鸣

班级:计算1812

学号:201821121050

1. 编写程序

在服务器上用Vim编写一个程序:实现Linux系统命令ls -lai的功能,给出源代码:

 源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<time.h>
#include<pwd.h>
#include<grp.h>
 
void error_printf(const char* );
 
void list_dir(const char* );
void list_message(const char* , const struct stat*);
 
 
void file_type(const struct stat* );
void file_power(const struct stat* );
void file_id(const struct stat* );
 
void file_mtime(const struct stat* );
 
void link_printf(const char* );
void error_printf(const char* funname)
{
    perror(funname);
    exit(EXIT_FAILURE);
}
  
void list_dir(const char* pathname)
{
    DIR* ret_opendir = opendir(pathname); 
    if(ret_opendir == NULL)
        error_printf("opendir");
 
    int ret_chdir = chdir(pathname); 
    if(ret_chdir == -1)
        error_printf("chdir");
 
    struct dirent* ret_readdir = NULL; 
    while(ret_readdir = readdir(ret_opendir)) 
    {
        char* filename = ret_readdir->d_name;
        struct stat file_message = {}; 
        int ret_stat = lstat(filename, &file_message); 
        if(ret_stat == -1) 
            printf("%s error!", filename);
        else if(strcmp(filename,".") && strcmp(filename,"..")) 
            list_message(filename, &file_message);
    }
}
 
void list_message(const char* filename, const struct stat* file_message)
{  
 
    file_type(file_message); 
 
 printf("%d ",(int)file_message->st_ino);
    file_power(file_message); 
    printf("%d ", file_message->st_nlink);  
    file_id(file_message); 
    printf("%5ld ", file_message->st_size); 
    file_mtime(file_message); 
    printf("%s ", filename); 
    if(S_ISLNK(file_message->st_mode)) 
        link_printf(filename);
    puts("");
}
void file_type(const struct stat* file_message)
{
    mode_t mode = file_message->st_mode;
 
    if     (S_ISREG(mode))  printf("-"); 
    else if(S_ISDIR(mode))  printf("d"); 
    else if(S_ISCHR(mode))  printf("c"); 
    else if(S_ISBLK(mode))  printf("b"); 
    else if(S_ISFIFO(mode)) printf("p"); 
    else if(S_ISLNK(mode))  printf("l"); 
    else                    printf("s"); 
}
 
void file_power(const struct stat* file_message)
{
    mode_t mode = file_message->st_mode;
 
 
    printf("%c", mode&S_IRUSR?'r':'-');
    printf("%c", mode&S_IWUSR?'w':'-');
    printf("%c", mode&S_IXUSR?'x':'-');
 
 
    printf("%c", mode&S_IRGRP?'r':'-');
    printf("%c", mode&S_IWGRP?'w':'-');
    printf("%c", mode&S_IXGRP?'x':'-');
 
    printf("%c", mode&S_IROTH?'r':'-');
    printf("%c", mode&S_IWOTH?'w':'-');
    printf("%c ", mode&S_IXOTH?'x':'-');
}
 
void file_id(const struct stat* file_message)
{
 
    struct passwd* pwd;
    pwd = getpwuid(file_message->st_uid);
    printf("%s ",pwd->pw_name);
 
    struct group* grp;
    grp = getgrgid(file_message->st_gid);
    printf("%s ",grp->gr_name);
    
}
 
void file_mtime(const struct stat* file_message)
{
    struct tm* t = localtime(&file_message->st_mtime);
    printf("%2d月 %2d %02d:%02d ", t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min);
}
 
void link_printf(const char* filename)
{
    char buf[1024] = "123";
    if(0 == readlink(filename, buf, sizeof(buf)))
        error_printf("readlink");
    printf("-> %s ",buf);
}
 int main()
 { char path[1024] = {};
strcpy(path,"./");
 struct stat file_message = {};
 
int ret_stat = lstat(path, &file_message);
if(ret_stat == -1)
error_printf("stat");
 
if(S_ISDIR(file_message.st_mode)) 
 list_dir(path);
else
list_message(path, &file_message);
 return 0;
 
 }

2. 分析运行结果

给出运行结果截图,对于每一列是如何获取的,结合源代码做解释

第一列为文件索引号.

 

 第二列首字母为文件类型,“-”开头的是普通文件,代表无对应条件。"p"开头的是管道文件。"rwx"分别代表具有读写执行的权限等等。

 

 

第三列为硬连接个数。

 

第四列为文件拥有者。

 

第五列为文件对应用户组的信息。

 

第六列为以字节为单位的文件大小。

 

第七列为最后修改文件的时间。

 

第八列为文件路径。

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

 

posted on 2020-04-30 19:05  噬世  阅读(161)  评论(0编辑  收藏  举报

导航