1 #include<stdio.h> 2 #include<unistd.h> 3 #include<sys/stat.h> 4 #include<sys/types.h> 5 #include<time.h> 6 #include<pwd.h> 7 #include<grp.h> 8 #include<glob.h> 9 #include<string.h> 10 #include<errno.h> 11 #include<dirent.h> 12 #define BUFSIZE 1024 13 int _myls_l(char *p); 14 int is_d(char *p) 15 { 16 char buf[BUFSIZE] = {}; 17 struct stat mystat; 18 int ch; 19 char *chr = NULL; 20 int sum = 0; 21 glob_t glb; 22 struct stat block; 23 ch = lstat(p,&mystat); 24 if(ch == -1) 25 { 26 perror("lstat()"); 27 return -1; 28 } 29 if(S_ISDIR(mystat.st_mode)) 30 { 31 strcpy(buf,p); 32 strcat(buf,"/*"); 33 glob(buf,0,NULL,&glb); 34 if(glob(buf,0,NULL,&glb) != 0) 35 { 36 fprintf(stderr,"glob() is failed"); 37 return -1; 38 } 39 for(int i = 0;i < glb.gl_pathc;i++) 40 { 41 _myls_l((glb.gl_pathv)[i]); 42 chr = strrchr((glb.gl_pathv)[i],'/'); 43 printf(" %s\n",chr+1); 44 lstat(glb.gl_pathv[i],&block); 45 if(lstat(glb.gl_pathv[i],&block) == -1) 46 { 47 perror("lstat()"); 48 return -1; 49 } 50 sum += block.st_blocks; 51 } 52 53 globfree(&glb); 54 printf("总用量 %d \n",sum/2); 55 } 56 else 57 { 58 _myls_l(p); 59 printf("\n"); 60 } 61 return 1; 62 } 63 64 int _myls_l(char *p) 65 { 66 struct group *grp = NULL; 67 struct tm *tim = NULL; 68 char buf[BUFSIZE] = {}; 69 struct passwd *pwd = NULL; 70 struct stat tp; 71 int ch; 72 ch = lstat(p,&tp); 73 if(ch == -1) 74 { 75 perror("lstat()"); 76 return -1; 77 } 78 //文件类型 79 switch(tp.st_mode & S_IFMT) 80 { 81 case S_IFSOCK: 82 printf("s"); 83 break; 84 case S_IFLNK: 85 printf("l"); 86 break; 87 case S_IFREG: 88 printf("-"); 89 break; 90 case S_IFBLK: 91 printf("b"); 92 break; 93 case S_IFDIR: 94 printf("d"); 95 break; 96 default: 97 break; 98 } 99 //权限 100 if(tp.st_mode & S_IRUSR) 101 printf("r"); 102 else 103 printf("-"); 104 if(tp.st_mode & S_IWUSR) 105 printf("w"); 106 else 107 printf("-"); 108 if(tp.st_mode & S_IXUSR) 109 { 110 if(tp.st_mode & S_ISUID) 111 printf("s"); 112 else 113 printf("x"); 114 } 115 else 116 printf("-"); 117 if(tp.st_mode & S_IRGRP) 118 printf("r"); 119 else 120 printf("-"); 121 if(tp.st_mode & S_IWGRP) 122 printf("r"); 123 else 124 printf("-"); 125 if(tp.st_mode & S_IXGRP) 126 { 127 if(tp.st_mode & S_ISGID) 128 printf("s"); 129 else 130 printf("x"); 131 } 132 else 133 printf("-"); 134 if(tp.st_mode & S_IROTH) 135 printf("r"); 136 else 137 printf("-"); 138 if(tp.st_mode & S_IWOTH) 139 printf("w"); 140 else 141 printf("-"); 142 if(tp.st_mode & S_IXOTH) 143 { 144 if(tp.st_mode & S_ISVTX) 145 printf("t"); 146 else 147 printf("x"); 148 } 149 else 150 printf("-"); 151 //硬链接 152 printf(" %ld",tp.st_nlink); 153 154 //拥有者 155 pwd = getpwuid(tp.st_uid); 156 printf(" %s",pwd->pw_name); 157 158 //所属组 159 grp = getgrgid(tp.st_gid); 160 printf(" %s",grp->gr_name); 161 printf(" %ld",tp.st_size); 162 163 //时间 164 tim = localtime(&tp.st_mtim.tv_sec); 165 strftime(buf,BUFSIZE,"%m月 %d %H:%M",tim); 166 printf(" %s",buf); 167 168 169 } 170 171 int _inode(char *p) 172 { 173 struct stat id; 174 lstat(p,&id); 175 if(lstat(p,&id) == -1) 176 { 177 perror("lstat()"); 178 return -1; 179 } 180 printf(" %ld",id.st_ino); 181 return 1; 182 } 183 int _isd_ino(char *p) 184 { 185 char buf[BUFSIZE] = {}; 186 struct stat mystat; 187 int ch; 188 char *chr; 189 glob_t glb; 190 ch = lstat(p,&mystat); 191 if(ch == -1) 192 { 193 perror("lstat()"); 194 return -1; 195 } 196 if(S_ISDIR(mystat.st_mode)) 197 { 198 strcpy(buf,p); 199 strcat(buf,"/*"); 200 glob(buf,0,NULL,&glb); 201 if(glob(buf,0,NULL,&glb) != 0) 202 { 203 fprintf(stderr,"glob() is failed"); 204 return -1; 205 } 206 for(int i = 0;i < glb.gl_pathc; i++) 207 { 208 _inode((glb.gl_pathv)[i]); 209 chr = strrchr((glb.gl_pathv)[i],'/'); 210 printf(" %s",chr+1); 211 printf("\n"); 212 } 213 globfree(&glb); 214 } 215 else 216 { 217 _inode(p); 218 printf(" %s\n",p); 219 } 220 return 1; 221 } 222 223 int myls_h(char *p) 224 { 225 struct stat mystat; 226 int ch; 227 char buf[BUFSIZE] = {}; 228 glob_t glb; 229 char *chr = NULL; 230 ch = lstat(p,&mystat); 231 if(ch == -1) 232 { 233 perror("lstat()"); 234 return -1; 235 } 236 if(S_ISDIR(mystat.st_mode)) 237 { 238 strcpy(buf,p); 239 strcat(buf,"/*"); 240 glob(buf,0,NULL,&glb); 241 if(glob(buf,0,NULL,&glb) != 0 ) 242 { 243 fprintf(stderr,"glob() is failed"); 244 return -1; 245 } 246 for(int i = 0;i < glb.gl_pathc;i++) 247 { 248 chr = strrchr((glb.gl_pathv)[i],'/'); 249 printf("%s\n",chr+1); 250 } 251 globfree(&glb); 252 } 253 return 1; 254 } 255 256 int myls_a(char *p) 257 { 258 DIR *dp = NULL; 259 struct dirent *entry = NULL; 260 dp = opendir(p); 261 if(dp == NULL) 262 { 263 perror("opendir()"); 264 return -1; 265 } 266 while(1) 267 { 268 entry = readdir(dp); 269 if(entry == NULL) 270 { 271 if(errno) 272 { 273 closedir(dp); 274 return -1; 275 } 276 break; 277 } 278 printf("%s",entry->d_name); 279 printf("\n"); 280 } 281 closedir(dp); 282 return 1; 283 } 284 int main(int argc,char *argv[]) 285 { 286 int ch; 287 int n; 288 char *optstring = "li:h:a:"; 289 if(argc < 3) 290 return 1; 291 n = argc; 292 //printf("%d\n",n); 293 while(1) 294 { 295 ch = getopt(argc,argv,optstring); 296 if(ch == -1) 297 break; 298 switch(ch) 299 { 300 case 'a': 301 myls_a(argv[n-1]); 302 break; 303 case 'h': 304 myls_h(argv[n-1]); 305 break; 306 case 'l': 307 is_d(argv[n-1]); 308 // _myls_l(is_d(argv[2])); 309 break; 310 case 'i': 311 _isd_ino(argv[n-1]); 312 break; 313 //case 'l': 314 // is_d(argv[2]); 315 // _isd_ino(argv[2]); 316 // break; 317 case '?': 318 printf("i do not know"); 319 break; 320 case 1: 321 printf("%s\n",argv[optind -1]); 322 break; 323 default: 324 break; 325 } 326 } 327 return 0; 328 }
王志颖_myls功能实现
猜你喜欢
转载自www.cnblogs.com/wzyhlj/p/10542636.html
今日推荐
周排行