MOOC《Linux操作系统编程》学习笔记-实验三

------------恢复内容开始------------

文件目录与操作实验

https://www.icourse163.org/learn/UESTC-1003040002?tid=1455108444#/learn/content?type=detail&id=1228729536&sm=1

 

程序流程

 

一种实现方法:

  1 #include "stdio.h"
  2 #include "stdint.h"
  3 #include "sys/types.h"
  4 #include "dirent.h"
  5 #include "unistd.h"
  6 #include "string.h"
  7 #include "errno.h"
  8 #include "fcntl.h"
  9 #include "sys/stat.h"
 10 #include "stdlib.h"
 11 #include "time.h"
 12 #include "pwd.h"
 13 #include "grp.h"
 14 
 15 static void print_type(mode_t st_mode)
 16 {
 17     char *ptr = NULL;
 18 
 19     if(S_ISREG(st_mode))  ptr = "-";
 20     else if(S_ISDIR(st_mode))  ptr = "d";
 21     else if(S_ISCHR(st_mode))  ptr = "c";
 22     else if(S_ISBLK(st_mode))  ptr = "b";
 23     else if(S_ISFIFO(st_mode))  ptr = "p";
 24     else if(S_ISLNK(st_mode))  ptr = "l";
 25     else if(S_ISSOCK(st_mode))  ptr = "s";
 26     else ptr = "*";
 27 
 28     printf("%s",ptr);
 29 }
 30 
 31 static void print_perm(mode_t st_mode)
 32 {
 33     const char permList[] = {'x','w','r','x','w','r','x','w','r'};
 34     uint8_t i = 9;
 35     
 36     while(i--)
 37     {
 38         uint8_t maskValue = (1<<i);
 39         if((maskValue & st_mode) == maskValue)
 40             printf("%c",permList[i]);
 41         else printf("%c",'-');
 42     }
 43 
 44     printf(" ");
 45 }
 46 
 47 static void print_link(nlink_t st_nlink)
 48 {
 49     printf("%ld ",st_nlink);
 50 }
 51 
 52 static void print_usrname(uid_t st_uid)
 53 {
 54     struct passwd *ptr = getpwuid(st_uid);
 55 
 56     printf("%s ",ptr->pw_name);
 57 }
 58 
 59 static void print_grname(gid_t st_gid)
 60 {
 61     struct group *ptr = getgrgid(st_gid);
 62 
 63     printf("%s ",ptr->gr_name);    
 64 }
 65 
 66 static void print_time(time_t mtime)
 67 {
 68     char buff[100] = {0},Len = 0;
 69     memcpy(buff,ctime(&mtime),sizeof(buff));
 70     Len = strlen(buff);
 71     buff[Len - 1] = 0x00;
 72     printf("%s ",buff);
 73 }
 74 
 75 static void print_filename(struct dirent * currentdp)
 76 {
 77     printf("%s \n",currentdp->d_name);    
 78 }
 79 
 80 
 81 int main(int argc, char *argv[])
 82 {
 83     char *buffer = NULL;
 84     buffer = (char *)malloc(100 * sizeof(char));
 85 
 86     if(NULL == getcwd(buffer,100))
 87     {
 88         perror("get work path error");
 89     }
 90     else
 91     {
 92         printf("%s \n",buffer);
 93 
 94         DIR *currentdir;
 95         if((currentdir = opendir(buffer)) == NULL)
 96         {
 97             printf("open directory fail \n");
 98             return 0;
 99         }
100         else
101         {
102             struct dirent * currentdp;
103             struct stat currentStat;
104             printf("file in directory include: \n");
105             while((currentdp = readdir(currentdir))!= NULL)
106             {
107                 if(currentdp->d_name[0] != '.')
108                 {
109                     if(lstat(currentdp->d_name,&currentStat) < 0)
110                     {
111                         perror("lstat error");
112                         continue;
113                     }
114 
115                     print_type(currentStat.st_mode);
116                     print_perm(currentStat.st_mode);
117                     print_link(currentStat.st_nlink);
118                     print_usrname(currentStat.st_uid);
119                     print_grname(currentStat.st_gid);
120                     print_time(currentStat.st_mtime);
121                     print_filename(currentdp);
122 
123                 }
124             }
125         }
126 
127         if(-1 == closedir(currentdir))
128         {
129             perror("close directory fail \n"); 
130         }
131     }
132 
133     free(buffer);
134     buffer = NULL;
135     return 0;
136 }

 

posted @ 2021-06-04 21:04  天悦桐汐  阅读(71)  评论(0)    收藏  举报