_紫萱

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

  • 姓名:黄财泽
  • 学号:201821121014
  • 班级:计算1811

一、实验目的

通过编程进一步了解文件系统。

二、实验内容

1. 编写程序

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

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
char* get_mode(mode_t m,char* str)
{
   if(S_ISREG(m))
       strcpy(str,"-");
   else if(S_ISDIR(m))
       strcpy(str,"d");
   else if(S_ISCHR(m))
       strcpy(str,"c");
   else if(S_ISBLK(m))
       strcpy(str,"b");
   else if(S_ISFIFO(m))
       strcpy(str,"f");
   else if(S_ISLNK(m))
       strcpy(str,"l");
   else if(S_ISSOCK(m))
       strcpy(str,"n");

   strcat(str,m&S_IRUSR?"r":"-");
   strcat(str,m&S_IWUSR?"w":"-");
   strcat(str,m&S_IXUSR?"x":"-");

   strcat(str,m&S_IRGRP?"r":"-");
   strcat(str,m&S_IWGRP?"w":"-");
   strcat(str,m&S_IXGRP?"x":"-");

   strcat(str,m&S_IROTH?"r":"-");
   strcat(str,m&S_IWOTH?"w":"-");
   strcat(str,m&S_IXOTH?"x":"-");

   return str;
}
int _time(int year)
{
   if(year%4==0 && year%100 !=0 || year%400 == 0)
       return 29;
   return 28;
}
void time_ch(time_t num)
{
   int year=1970;
   int month =1;
   int day =1;
   num = num + 8*3600;
   while(num >= 86400)
{
       num-=86400;
       day++;
       if(month==1 && day == 32)
       {
           month++; 
           day =1;
       }
       else if(month == 2 && day ==_time(year)+1)
       {
           month++;
           day =1;
       }
       else if(month == 3 && day == 32)
       {
           month++;
           day =1;
       }
       else if(month == 4 && day == 31)
       {
           month++;
           day=1;
       }
       else if(month == 5 && day == 32)
       {
           month++;
           day=1;
       }
       else if(month == 6 && day == 31)
       {
           month++;
           day=1;
       }
       else if(month == 7 && day == 32)
       {
           month++;
           day=1;
       }
       else if(month == 8 && day == 32)
       {
           month++;
           day=1;
       }
       else if(month == 9 && day == 31)
       {
           month++;
           day=1;
       }
       else if(month == 10 && day == 32)
       {
           month++;
           day=1;
       }
       else if(month == 11 && day == 31)
       {
           month++;
           day=1;
       }
       else if(month == 12 && day == 32)
       {
           month=1;
           day=1;
           year++;
       }
       
   }
   int hour = num/3600;
   int minute =num/60 -hour*60;
   printf("%2d月 %2d %2d:%2d ",month,day,hour,minute);
}

int main(int argc,char** argv,char** environ)
{
   char* dir_name=NULL;
   if(argc == 1)
   {
       dir_name=".";
   } 
   else if(argc == 2)
   {
       dir_name = argv[1];
   }
   else
   {
       puts("user:ls dir");
       return -1;
   }
   DIR* dp=opendir(dir_name);
   if(NULL == dp)
   {
       perror("opendir");
       return -1;
   }
   struct dirent* de=readdir(dp);
   for(;de;de=readdir(dp))
   {
       if('.'==de->d_name[0]) 
       continue;
       struct stat s;
       int ret = lstat(de->d_name,&s);
       if(0 > ret)
       {
           perror("stat");
           return -1;
       }
       char str[11] = {};
       printf("%s ",get_mode(s.st_mode,str));
       struct passwd *passwd;
       passwd = getpwuid(s.st_uid);
       printf ("%s ", passwd->pw_name);
       struct group *group;
       group = getgrgid(passwd->pw_gid);
       printf ("%s ", group->gr_name); 
       printf("%5lu ",s.st_size);
       time_ch(s.st_mtime);
       printf("%s\t",de->d_name);
       printf("\n");
   }
   closedir(dp);
}

 

2. 分析运行结果

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

第一列表示 文件读写权限

  char* get_mode(mode_t m,char* str)

第二列表示 文件的深度(本代码未实现)
第三列和第四列分别表示文件的属主和属组

  printf ("%s ", passwd->pw_name);

  printf ("%s ", group->gr_name);

第五列是文件的大小,以字节作为单位

  printf("%5lu ",s.st_size);//大小

第六,七,八列是文件最后一次修改的时间

  void time_ch(time_t num)这个函数用来计算最后一次修改的时间

最后一列是文件名

  printf("%s\t",de->d_name);//文件名

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

  疑问:本代码中没有将ls -l的中表示文件深度的函数实现

posted on 2020-04-30 16:05  _紫萱  阅读(171)  评论(0编辑  收藏  举报

导航