加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

my_ls-ailh

发布时间:2020-12-14 02:01:32 所属栏目:Linux 来源:网络整理
导读://*** myls--h.c文件 ***// 1 #includestdio.h 2 #include string .h 3 #include " h.h " 4 int is_or_hidenode( char * path) 5 { 6 char *str = NULL; 7 str=strrchr(path, ‘ / ‘ ); 8 if (str != NULL) 9 { 10 if (!strcmp(str, " /. " ) || !strcmp(st
//*** myls--h.c文件 ***//
1 #include<stdio.h> 2 #include<string.h> 3 #include "h.h" 4 int is_or_hidenode(char *path) 5 { 6 char *str = NULL; 7 str=strrchr(path,/); 8 if(str != NULL) 9 { 10 if(!strcmp(str,"/.") || !strcmp(str,"/..")) 11 return 1; 12 } 13 return 0; 14
 
//***myls--h.h文件***//
 1 #ifndef __H_H 2 #define __H_H 3 4 int is_or_hidenode(char *path); 5 6 #endif
 1 //*** myls--i.c文件***//
 2   #include<stdio.h>
 3   #include<sys/types.h>
 4   #include<sys/stat.h>
 5   #include "i.h"
 6   int my_inode(char *path)
 7   {
 8        char buf[BUFSIZE]={};
 9        struct stat ownper;
10        if(stat(path,&ownper)==-1)
11           return -1;
12       return ownper.st_ino;
13  }
1 //*** myls--i.h文件***//
2 #ifndef __I_H
3 #define __I_H
4 
5 #define BUFSIZE 1024
6 
7 int my_inode(char *path);
8 
9 #endif
  1 //*** myls--l.c文件***//
  2 #include<stdio.h>
  3 #include<sys/types.h>
  4 #include<sys/stat.h>
  5 #include<unistd.h>
  6 #include<pwd.h>
  7 #include<grp.h>
  8 #include<time.h>
  9 #include<errno.h>
 10 #include<string.h>
 11 #include<dirent.h>
 12 #include<stdlib.h>
 13 #include "h.h"
 14 #include "l.h"
 15 
 16 #define BUFSIZE 1024
 17 
 18 char file_type(struct stat ownper)
 19 {
 20     switch(ownper.st_mode & S_IFMT )
 21     {
 22         case S_IFSOCK:
 23             return s;
 24         case S_IFLNK:
 25             return l;
 26         case S_IFREG :
 27             return -;
 28         case S_IFBLK :
 29             return b;
 30         case S_IFDIR:
 31             return d;
 32         case S_IFCHR:
 33             return c;
 34         case S_IFIFO:
 35             return p;
 36     }
 37 }
 38 
 39 char * file_permission(struct stat ownper)
 40 {
 41     //user permission0
 42     if(ownper.st_mode & S_IRUSR)
 43     {
 44         printf("%c",r);
 45     }
 46     else
 47         printf("%c",-);
 48     if(ownper.st_mode & S_IWUSR)
 49     {
 50         printf("%c",w);
 51     }
 52     else
 53         printf("%c",-);
 54     if(ownper.st_mode & S_IXUSR)
 55     {
 56         if(ownper.st_mode & S_ISUID)
 57             printf("%c",s);
 58         else
 59             printf("%c",x);
 60     }
 61     else if(ownper.st_mode & S_ISUID)
 62     {
 63         printf("%c",S);
 64     }
 65     else
 66         printf("%c",-);
 67     
 68     /////////////group permission
 69     if(ownper.st_mode & S_IRGRP)
 70     {
 71         printf("%c",r);
 72     }
 73     else
 74         printf("%c",-);
 75     if(ownper.st_mode & S_IWGRP)
 76     {
 77         printf("%c",w);
 78     }
 79     else
 80         printf("%c",-);
 81     if(ownper.st_mode & S_IXGRP)
 82     {
 83         if(ownper.st_mode & S_ISGID)
 84             printf("%c",s);
 85         else
 86             printf("%c",x);
 87     }
 88     else if(ownper.st_mode & S_ISGID)
 89     {
 90         printf("%c",S);
 91     }
 92     else
 93         printf("%c",-);
 94 
 95     ////===============other permission
 96     if(ownper.st_mode & S_IROTH)
 97     {
 98         printf("%c",r);
 99     }
100     else
101         printf("%c",-);
102     if(ownper.st_mode & S_IWOTH)
103     {
104         printf("%c",w);
105     }
106     else
107         printf("%c",-);
108     if(ownper.st_mode & S_IXOTH)
109     {
110         if(ownper.st_mode & S_ISVTX)
111             printf("%c",t);
112         else
113             printf("%c",x);
114     }
115     else if(ownper.st_mode & S_ISVTX)
116     {
117         printf("%c",T);
118     }    
119     else
120         printf("%c",-);
121 }
122 
123 void my_ls_l(char *path)
124 {
125     struct tm *tmp=NULL;
126     struct stat ownper;
127     char file_name[BUFSIZE]={};
128     struct passwd *pwd=NULL;
129     struct group *grp=NULL;
130     
131     if(lstat(path,&ownper)==-1) //lstat() 里的成员st_size显示的是链接文件的真实大小
132     {
133         perror("stat()");
134         return;
135     }
136     printf("%c",file_type(ownper));//获取用户类型
137     
138     file_permission(ownper);
139     
140     //硬链接数
141     printf("%ld ",ownper.st_nlink);
142 
143     //用户名
144     pwd=getpwuid(ownper.st_uid);
145     if(pwd==NULL)
146         return;
147     printf("%s ",pwd->pw_name);
148     
149     //所属组名
150     grp=getgrgid(ownper.st_gid);
151     if(grp==NULL)
152         return;
153     printf("%s ",grp->gr_name);
154 
155     //文件大小
156     printf("%ld ",ownper.st_size);
157 
158     //修改时间
159     tmp=localtime(&ownper.st_mtim.tv_sec);
160     char m_time[1024]={};
161     strftime(m_time,1024,"%m月  %d %H:%M",tmp);
162     printf("%s ",m_time);
163 
164     //文件名
165     strcpy(file_name,strrchr(path,/));
166     file_name[0]= ;
167     printf("%s",file_name);
168     printf("n");
169 }
 1 //*** myls--l.h文件***//
 2 #ifndef __L_H
 3 #define __L_H
 4 
 5 char file_type(struct stat ownper);
 6 
 7 char * file_permission(struct stat ownper);
 8 
 9 void  my_ls_l(char *path);
10 
11 #endif
  1 //***myls--main.c***//
  2 #include<stdio.h>
  3 #include<sys/types.h>
  4 #include<sys/stat.h>
  5 #include<dirent.h>
  6 #include<unistd.h>
  7 #include<errno.h>
  8 #include<string.h>
  9 #include<malloc.h>
 10 #include "h.h"
 11 #include "i.h"
 12 #include "l.h"
 13 
 14 #define BUFSIZE 1024
 15 
 16 int main(int argc,char *argv[])
 17 {
 18 
 19     if(argc<3)
 20     {
 21         printf("errorn");
 22         return -1;
 23     }
 24     struct stat cur_stat;
 25     int is_dir=0;
 26     const char * optstring="-ailh";
 27     char buf[BUFSIZE]={};
 28     char *usr_per=NULL;
 29     DIR *dp = NULL;
 30     struct dirent *entry;
 31     int c=0;
 32     int ls_h=0,ls_i=0,ls_a=0,ls_l=0;
 33 
 34     if(stat(argv[2],&cur_stat)==-1)
 35         return -1;
 36     if(file_type(cur_stat)==d)
 37         is_dir=1;
 38 
 39     while(1)
 40     {
 41         c=getopt(argc,argv,optstring);
 42         if(c==-1)
 43         {
 44             break;
 45         }
 46         switch(c)
 47         {
 48             case h:
 49                 ls_h=1;
 50                 break;
 51             case i:
 52                 ls_i=1;
 53                 break;
 54             case a:
 55                 ls_a=1;
 56                 break;
 57             case l:
 58                 ls_l=1;
 59                 break;
 60             case ?:
 61                 printf("unknow optionn");
 62                 break;
 63             default:
 64                 break;
 65 
 66         }
 67     }
 68     if(!is_dir)//普通文件的option处理
 69     {
 70         if(ls_i==ls_l==1){
 71             printf("%ld ",cur_stat.st_ino);
 72             my_ls_l(argv[2]);
 73         }
 74         else if(ls_i==1){
 75             printf("%ld ",cur_stat.st_ino);
 76             printf("%sn",argv[2]);
 77         }
 78         else if(ls_l==1){
 79             my_ls_l(argv[2]);
 80         }
 81         else
 82             printf("%sn",argv[2]);
 83     }
 84     else//目录的option处理
 85     {
 86         dp=opendir(argv[2]);
 87         if(dp == NULL)
 88             return -1;
 89         while(1)
 90         {
 91             if((entry=readdir(dp)) == NULL)
 92             {
 93                 if(errno){
 94                     perror("readdir()");
 95                     closedir(dp);
 96                     return -1;
 97                 }
 98                 break;
 99             }
100             memset(buf,,BUFSIZE);
101             sprintf(buf,"%s/%s",argv[2],entry->d_name);    
102             //根据option统计目录下的各个文件的信息
103             if(ls_i==1 && ls_l==1 && ls_a==1){
104                 printf(" %d ",my_inode(buf));
105                 my_ls_l(buf);
106             }
107 
108             else if(ls_i==1 && ls_l==1){
109                 if(!is_or_hidenode(buf)){
110                     printf(" %d ",my_inode(buf));
111                     my_ls_l(buf);
112                 }
113             }
114 
115             else if(ls_i==1 && ls_a==1){
116                 printf(" %d  ",my_inode(buf));
117                 printf(" %s  ",entry->d_name);
118             }
119 
120             else if(ls_i==1){
121                 if(!is_or_hidenode(buf)){
122                     printf(" %d ",my_inode(buf));
123                     printf("%s  ",entry->d_name);
124                 }
125             }
126 
127             else if(ls_l==1 && ls_a==1){
128                 my_ls_l(buf);
129             }
130 
131             else if(ls_l==1){
132                 if(!is_or_hidenode(buf))
133                 {
134                     my_ls_l(buf);
135                 }
136 
137             }
138             else if(ls_a==1)
139                 printf(" %s  ",entry->d_name);
140             else
141             {
142                 if(!is_or_hidenode(buf))
143                     printf(" %s  ",entry->d_name);
144             }
145         }
146         closedir(dp);
147     }
148     return 0;
149 }
1 //***myls--makefile***//
2 main : main.o h.o i.o l.o
3     gcc main.o h.o i.o l.o -o main
4 clean :
5     rm -rf *.o main
1 ///*****readme*****///
2 1.命令行参数格式 
3     ./main -l /etc
4 2.命令行选项
5     可支持-laih的自由组合
6 -----------------------------
没有实现-l下的总用量(total)的计算

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读