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

0. 个人信息

  • 姓名  雷坛春
  • 学号  201821121030
  • 班级  计算1811

1. 编写程序

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

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <string.h>
 
#define LS_NONE 0
#define LS_L 101
 
// 展示单个文件的详细信息
void show_file_info(char* filename, struct stat* info_p)
{
    char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
    void mode_to_letters();
    char modestr[11];
 
    mode_to_letters(info_p->st_mode, modestr);
 
    printf("%s", modestr);
    printf(" %4d", (int) info_p->st_nlink);
    printf(" %-8s", uid_to_name(info_p->st_uid));
    printf(" %-8s", gid_to_name(info_p->st_gid));
    printf(" %8ld", (long) info_p->st_size);
    printf(" %.12s", 4 + ctime(&info_p->st_mtime));
    printf(" %s\n", filename);
}
 
void mode_to_letters(int mode, char str[])
{
    strcpy(str, "----------");
 
    if (S_ISDIR(mode))
    {
        str[0] = 'd';
    }
 
    if (S_ISCHR(mode))
    {
        str[0] = 'c';
    }
 
    if (S_ISBLK(mode))
    {
        str[0] = 'b';
    }
 
    if ((mode & S_IRUSR))
    {
        str[1] = 'r';
    }
 
    if ((mode & S_IWUSR))
    {
        str[2] = 'w';
    }
 
    if ((mode & S_IXUSR))
    {
        str[3] = 'x';
    }
 
    if ((mode & S_IRGRP))
    {
        str[4] = 'r';
    }
 
    if ((mode & S_IWGRP))
    {
        str[5] = 'w';
    }
 
    if ((mode & S_IXGRP))
    {
        str[6] = 'x';
    }
 
    if ((mode & S_IROTH))
    {
        str[7] = 'r';
    }
 
    if ((mode & S_IWOTH))
    {
        str[8] = 'w';
    }
 
    if ((mode & S_IXOTH))
    {
        str[9] = 'x';
    }
}
 
char* uid_to_name(uid_t uid)
{
    struct passwd* getpwuid(),* pw_ptr;
    static char numstr[10];
 
    if((pw_ptr = getpwuid(uid)) == NULL)
    {
        sprintf(numstr,"%d",uid);
 
        return numstr;
    }
    else
    {
        return pw_ptr->pw_name;
    }
}
 
char* gid_to_name(gid_t gid)
{
    struct group* getgrgid(),* grp_ptr;
    static char numstr[10];
 
    if(( grp_ptr = getgrgid(gid)) == NULL)
    {
        sprintf(numstr,"%d",gid);
        return numstr;
    }
    else
    {
        return grp_ptr->gr_name;
    }
}
 
void do_ls(char dirname[])
{
    DIR* dir_ptr;
    struct dirent* direntp;
 
    if ((dir_ptr = opendir(dirname)) == NULL)
    {
        fprintf(stderr, "ls2: cannot open %s \n", dirname);
    }
    else
    {

        char dirs[20][100];
        int dir_count = 0;
            
        while ((direntp = readdir(dir_ptr)) != NULL)
        {
 
            if(direntp->d_name[0]=='.')
            {
                continue;
            }
 
            char complete_d_name[200];  // 文件的完整路径
            strcpy (complete_d_name,dirname);
            strcat (complete_d_name,"/");
            strcat (complete_d_name,direntp->d_name);
                
            struct stat info;
            if (stat(complete_d_name, &info) == -1)
            {
                perror(complete_d_name);
            }
            else
            {
                show_file_info(direntp->d_name, &info);
            }
   
        }
        closedir(dir_ptr);
    }
}
 

 
int main(int ac,char* av[])
{
 
    if(ac == 1)
    {
        do_ls(".");
    }
    else
    {
     
        while(ac>1)
        {
            ac--;
            av++;
            do
            {
                printf("%s:\n", *av);
                do_ls(*av);
                printf("\n");
 
                ac--;
                av++;
            }while(ac>=1);

        }
        
    }     

}

 

2. 运行结果

1.ls -lai输出结果

 

2.lai.c 运行结果

 

3.实验结果

1.获取单个文件的信息

void show_file_info(char* filename, struct stat* info_p)
{
    char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
    void mode_to_letters();
    char modestr[11];
 
    mode_to_letters(info_p->st_mode, modestr); 
     printf(" %d  ",(int) info_p->st_ino);         //索引号 
    printf("%s", modestr);                        //文件权限 
    printf(" %4d", (int) info_p->st_nlink);        //连接个数 
    printf(" %-8s", uid_to_name(info_p->st_uid));//用户信息 
    printf(" %-8s", gid_to_name(info_p->st_gid));//组信息 
    printf(" %8ld", (long) info_p->st_size);    //文件大小 
    printf(" %.12s", 4 + ctime(&info_p->st_mtime));//最近被改时间 
    printf(" %s\n", filename);                    //文件路径 
}

2.lai释义

(1)-a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为影藏,不会列出);

(2)-l:以长格式显示目录下的内容列表。输出的信息从左到右依次包括文件名,文件类型、权限模式、硬连接数、所有者、组、文件大小和文件的最后修改时间等;

(3)-i:显示文件索引节点号(inode)。一个索引节点代表一个文件;

4. 通过该实验产生问题及解答

posted @ 2020-04-30 20:23  殇墨痕  阅读(169)  评论(0编辑  收藏  举报