2019.3.16-李博

main.c
1 #include<stdio.h> 2 #include"stat.h" 3 #include<unistd.h> 4 5 int main(int argc,char *argv[]) 6 { 7 char *optstring = "-l:a:i:h:";//定义一个char *类型的选项字符串。 8 int c; 9 10 while(1) 11 { 12 c = getopt(argc,argv,optstring); 13 if(c==-1) 14 break; 15 switch(c) 16 { 17 case'l': 18 l(optarg); //optarg保存选项参数的。 19 break; 20 case'a': 21 a(optarg); 22 break; 23 case'i': 24 i(optarg); 25 break; 26 case'h': 27 h(optarg); 28 break; 29 case'?': 30 printf("不认识此选项%s\n",argv[optind-1]);//optind用来记录下一个检索位置。 31 break; 32 case'1': 33 printf("非选项参数%s\n",argv[optind-1]); 34 break; 35 default: 36 break; 37 } 38 } 39 40 41 return 0;

stat.c
1
#include"stat.h" 2 3 #define BUFSIZE 1024 4 void h(char *path) 5 { 6 struct stat cur_stat; 7 glob_t globres; 8 char buf[BUFSIZE] = {}; 9 10 if(lstat(path,&cur_stat)<0) 11 { 12 perror("lstat()"); 13 return ; 14 } 15 if(!S_ISDIR(cur_stat.st_mode)) 16 { 17 printf("%s",path); 18 } 19 strcpy(buf,path); 20 strcat(buf,"/*"); 21 glob(buf,0,NULL,&globres); 22 for(int i = 0;i<globres.gl_pathc;i++) 23 { 24 printf("%s\n",((globres.gl_pathv)[i])); 25 } 26 globfree(&globres); 27 28 } 29 void l(char *path) 30 { 31 struct stat cur_stat; 32 glob_t globres; 33 char buf[BUFSIZE] = {}; 34 35 if(lstat(path,&cur_stat)<0) 36 { 37 perror("lstat()"); 38 return ; 39 } 40 if(!S_ISDIR(cur_stat.st_mode))//判断传递过来的路径是目录文件还是普通文件 41 { 42 mystat(path); 43 return ; 44 } 45 46 strcpy(buf,path);//把path路径复制到buf里 47 strcat(buf,"/*");//将/*与buf里的路径相连 48 glob(buf,0,NULL,&globres); 49 for(int i = 0;i < globres.gl_pathc;i++) //gl_pathc判断到目前位置匹配的路径数 50 { 51 mystat((globres.gl_pathv)[i]);//gl_pathv 匹配路径名的列表 char **类型 52 } 53 globfree(&globres); 54 } 55 void a(char *path) 56 { 57 struct stat cur_stat; 58 glob_t globres; 59 char buf[BUFSIZE]={}; 60 61 if(lstat(path,&cur_stat)<0) 62 { 63 perror("lstat()"); 64 return ; 65 } 66 if(!S_ISDIR(cur_stat.st_mode)) 67 { 68 printf("%s",path); 69 } 70 strcpy(buf,path); 71 strcat(buf,"/*"); 72 glob(buf,0,NULL,&globres); 73 74 memset(buf,'\0',BUFSIZE);//buf里面都是空的字符 75 76 strcpy(buf,path); 77 strcat(buf,"/.*");//带点是显示隐藏文件。 78 glob(buf,GLOB_APPEND,NULL,&globres); 79 80 for(int i = 0;i<globres.gl_pathc;i++) 81 { 82 printf("%s\n",((globres.gl_pathv)[i])); 83 } 84 globfree(&globres); 85 } 86 void i(char *path) 87 { 88 struct stat cur_stat; 89 glob_t globres; 90 char buf[BUFSIZE] = {}; 91 92 if(lstat(path,&cur_stat)<0) 93 { 94 perror("lstat()"); 95 return ; 96 } 97 if(!S_ISDIR(cur_stat.st_mode)) 98 { 99 istat(path); 100 return ; 101 } 102 103 strcpy(buf,path); 104 strcat(buf,"/*"); 105 glob(buf,0,NULL,&globres); 106 107 for(int i = 0;i<globres.gl_pathc;i++) 108 { 109 istat(((globres.gl_pathv)[i])); 110 111 } 112 globfree(&globres); 113 114 } 115 void mystat(char *path) 116 { 117 struct stat mystat; 118 struct passwd *pwd = NULL; 119 struct group *grp = NULL; 120 struct tm *tmp = NULL; 121 char buf[BUFSIZE]={}; 122 if(stat(path,&mystat)==-1) 123 { 124 perror("stat()"); 125 return ; 126 } 127 128 switch(mystat.st_mode & S_IFMT)//判断文件类型 &全1才是1 有0就是0 |有1就是1,全0才是0. ^ 相同为0 不同为1。 129 { 130 case S_IFREG://普通文件 131 printf("-"); 132 break; 133 case S_IFDIR://目录文件 134 printf("d"); 135 break; 136 case S_IFBLK: 137 printf("b");//块设备文件 138 break; 139 default: 140 break; 141 // c 字符设备文件 S_IFCHR 142 // p 管道文件 S_IFIFO 143 // s 套接子文件 S_IFSOCK 144 // l 符号链接文件 S_IFLNK 145 } 146 //判断文件权限 拥有者的权限 147 if(mystat.st_mode & S_IRUSR) //读权限 148 putchar('r'); 149 else 150 putchar('-'); 151 if(mystat.st_mode & S_IWUSR) //写权限 152 putchar('w'); 153 else 154 putchar('-'); 155 if(mystat.st_mode & S_IXUSR) //执行权限 156 { 157 if(mystat.st_mode & S_ISUID) 158 { 159 putchar('s'); 160 } 161 else 162 putchar('x'); 163 } 164 else 165 putchar('-'); 166 // S_IRWXU 可读,可写,可执行。 167 168 //所属组的权限 169 if(mystat.st_mode & S_IRGRP) //读权限 170 putchar('r'); 171 else 172 putchar('-'); 173 if(mystat.st_mode & S_IWGRP) //写权限 174 putchar('w'); 175 else 176 putchar('-'); 177 if(mystat.st_mode & S_IXGRP) //执行权限 178 { 179 if(mystat.st_mode & S_ISGID) 180 { 181 putchar('s'); 182 } 183 else 184 putchar('x'); 185 } 186 else 187 putchar('-'); 188 189 //其他用户权限 190 if(mystat.st_mode & S_IROTH) //读权限 191 putchar('r'); 192 else 193 putchar('-'); 194 if(mystat.st_mode & S_IWOTH) //写权限 195 putchar('w'); 196 else 197 putchar('-'); 198 if(mystat.st_mode & S_IXOTH) //执行权限 199 { 200 if(mystat.st_mode & S_ISVTX) 201 { 202 putchar('o'); 203 } 204 else 205 putchar('x'); 206 } 207 else 208 putchar('-'); 209 210 211 //硬链接 212 printf(" %ld ",mystat.st_nlink); 213 214 //文件拥有者名 215 pwd = getpwuid(mystat.st_uid); 216 printf("%s ",pwd->pw_name); 217 218 //文件所属组 219 grp = getgrgid(mystat.st_gid); 220 printf("%s ",grp->gr_name); 221 222 //文件总字节个数 磁盘空间大小 st_blocks/2 blocks 512个字节 223 printf("%ld ",mystat.st_size); 224 //获取文件时间 atime(访问文件时间) mtime(修改文件内容时间) ctime(改变文件状态时间) 225 tmp = localtime(&mystat.st_mtime); 226 if(tmp==NULL) 227 return ; 228 strftime(buf,BUFSIZE,"%m月 %d %H:%M",tmp); 229 printf("%s ",buf); 230 231 //文件名 232 char *p=strrchr(path,'/'); 233 printf("%s ",++p); 234 235 putchar('\n'); 236 return ; 237 } 238 void istat(char *path) 239 { 240 struct stat istat; 241 if(stat(path,&istat)==-1) 242 { 243 perror("stat()"); 244 return ; 245 } 246 printf("%ld %s\n",istat.st_ino,path); 247 return ; 248 }

 stat.h
1
#ifndef __STAT_H 2 #define __STAT_H 3 #include<stdio.h> 4 #include<sys/stat.h> 5 #include<sys/types.h> 6 #include<unistd.h> 7 #include<string.h> 8 #include<glob.h> 9 #include<pwd.h> 10 #include<grp.h> 11 #include<time.h> 12 13 void l(char *path); 14 void i(char *path); 15 void a(char *path); 16 void h(char *path); 17 void mystat(char *path); 18 void istat(char *path); 19 20 #endif
makefile
1
main : main.o stat.o 2 gcc main.o stat.o -o main 3 clean : 4 rm -rf *.o main
 
    
   
 

猜你喜欢

转载自www.cnblogs.com/boxiansheng/p/10543242.html