myls
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <unistd.h> 5 #include <pwd.h> 6 #include <grp.h> 7 #include <time.h> 8 #include <errno.h> 9 #include <dirent.h> 10 #include <string.h> 11 #include <stdlib.h> 12 13 int llstat(char *p); 14 int astat(char *p); 15 int istat(char *p); 16 int hstat(char *p); 17 static int nums; 18 19 int main(int argc, char **argv) 20 { 21 int c; 22 char *optstring = "-laih"; 23 if(argc < 3) 24 return 1; 25 while (1){ 26 c = getopt(argc, argv, optstring); 27 if(c == -1) 28 break; 29 switch(c){ 30 case 'l': llstat(argv[2]);break; 31 case 'a': astat(argv[2]);break; 32 case 'i': istat(argv[2]);break; 33 case 'h': hstat(argv[2]);break; 34 // case '1': printf("非选项参数\n");break; 35 case '?': printf("?\n");break; 36 default : break; 37 } 38 } 39 return 0; 40 41 } 42 43 //-l 44 //文件名->路径 45 static char *buf_cat(const char *p, const char *name) 46 { 47 char *bufcat = malloc(1024); 48 memset(bufcat,'\0',1024); 49 strcpy(bufcat,p); 50 strcat(bufcat,"/"); 51 strcat(bufcat,name); 52 return bufcat; 53 } 54 // 判断是否为隐藏文件 55 static int is_i(const char *p) 56 { 57 if(*p == '.') 58 return 1; 59 else 60 return 0; 61 } 62 63 int l_llstat(const char *p, const char *name) 64 { 65 struct stat mystat; 66 struct passwd *pwd = NULL; 67 struct group *grp = NULL; 68 struct tm *tmp = NULL; 69 char *buf=NULL; 70 71 buf = buf_cat(p,name); 72 if(lstat(buf,&mystat) == -1){ 73 perror("stat()"); 74 return 1; 75 } 76 if(is_i(name) == 0){ 77 nums+=mystat.st_blocks/2; 78 79 //类型 80 switch(mystat.st_mode & S_IFMT){ 81 case S_IFSOCK : putchar('s');break; 82 case S_IFLNK : putchar('l');break; 83 case S_IFREG : putchar('-');break; 84 case S_IFBLK : putchar('b');break; 85 case S_IFDIR : putchar('d');break; 86 case S_IFCHR : putchar('c');break; 87 case S_IFIFO : putchar('p');break; 88 default: break; 89 } 90 //权限 91 if(mystat.st_mode & S_IRUSR) 92 putchar('r'); 93 else 94 putchar('-'); 95 if(mystat.st_mode & S_IWUSR) 96 putchar('w'); 97 else 98 putchar('-'); 99 if(mystat.st_mode & S_IXUSR){ 100 if(mystat.st_mode & S_ISUID) 101 putchar('s'); 102 else 103 putchar('x'); 104 } 105 else 106 putchar('-'); 107 108 if(mystat.st_mode & S_IRGRP) 109 putchar('r'); 110 else 111 putchar('-'); 112 if(mystat.st_mode & S_IWGRP) 113 putchar('w'); 114 else 115 putchar('-'); 116 if(mystat.st_mode & S_IXGRP){ 117 if(mystat.st_mode & S_ISGID) 118 putchar('s'); 119 else 120 putchar('x'); 121 } 122 else 123 putchar('-'); 124 125 if(mystat.st_mode & S_IROTH) 126 putchar('r'); 127 else 128 putchar('-'); 129 if(mystat.st_mode & S_IWOTH) 130 putchar('w'); 131 else 132 putchar('-'); 133 if(mystat.st_mode & S_IXOTH){ 134 if(mystat.st_mode & S_ISVTX) 135 putchar('t'); 136 else 137 putchar('x'); 138 } 139 else 140 putchar('-'); 141 //硬链接 142 printf(" %ld ", mystat.st_nlink); 143 //文件拥有者名 144 pwd = getpwuid(mystat.st_uid); 145 printf("%s ", pwd->pw_name); 146 //文件所有组 147 grp = getgrgid(mystat.st_gid); 148 printf("%s ",grp->gr_name); 149 //总字节个数 150 printf("%5ld ", mystat.st_size); 151 //获取文件时间 152 tmp = localtime(&mystat.st_mtim.tv_sec); 153 strftime(buf, 1024, "%m月 %d %H:%M", tmp); 154 printf("%s ", buf); 155 //文件名 156 printf("%s\n", name); 157 } 158 return 0; 159 } 160 161 int llstat(char *p) 162 { 163 DIR *dp = NULL; 164 struct dirent *entry = NULL; 165 char buf[1024] = {}; 166 struct stat mystat1; 167 if(lstat(p,&mystat1) == -1){ 168 perror("stat()"); 169 return 1; 170 } 171 if(S_ISREG(mystat1.st_mode)){ 172 l_llstat(".", p); 173 }else{ 174 dp = opendir(p); 175 if(dp == NULL){ 176 perror("opendir()"); 177 return 1; 178 } 179 180 while(1){ 181 entry = readdir(dp); 182 if(NULL == entry){ 183 if(errno){ 184 perror("readdir()"); 185 closedir(dp); 186 return 1; 187 } 188 break; 189 } 190 l_llstat(p, entry->d_name); 191 } 192 printf("总用量:%d\n", nums); 193 closedir(dp); 194 } return 0; 195 196 } 197 198 199 200 // a 201 202 int a_llstat(const char *p, const char *name) 203 { 204 struct stat mystat; 205 char *buf=NULL; 206 207 buf = buf_cat(p,name); 208 if(lstat(buf,&mystat) == -1){ 209 perror("stat()"); 210 return 1; 211 } 212 //文件名 213 printf("%s ", name); 214 return 0; 215 } 216 217 int astat(char *p) 218 { 219 struct stat mystat2; 220 struct dirent *entry = NULL; 221 DIR *dp = NULL; 222 if(lstat(p,&mystat2) == -1){ 223 perror("stat()"); 224 return 1; 225 } 226 227 if(S_ISREG(mystat2.st_mode)){ 228 printf("%s ", p); 229 } 230 dp= opendir(p); 231 if(dp == NULL){ 232 perror("opendir()"); 233 return 1; 234 } 235 236 while(1){ 237 entry = readdir(dp); 238 if(NULL == entry){ 239 if(errno){ 240 perror("readdir()"); 241 closedir(dp); 242 return 1; 243 } 244 break; 245 } 246 a_llstat(p, entry->d_name); 247 } 248 249 closedir(dp); 250 printf("\n"); 251 return 0; 252 253 } 254 255 // i 256 257 int i_llstat(const char *p, const char *name) 258 { 259 struct stat mystat; 260 char *buf=NULL; 261 262 buf = buf_cat(p,name); 263 if(lstat(buf,&mystat) == -1){ 264 perror("stat()"); 265 return 1; 266 } 267 printf("%ld ",mystat.st_ino); 268 //文件名 269 printf("%s ", name); 270 return 0; 271 } 272 273 int istat(char *p) 274 { 275 struct stat mystat2; 276 struct dirent *entry = NULL; 277 DIR *dp = NULL; 278 if(lstat(p,&mystat2) == -1){ 279 perror("stat()"); 280 return 1; 281 } 282 283 if(S_ISREG(mystat2.st_mode)){ 284 if(is_i(p)) 285 return 1; 286 printf("%ld ",mystat2.st_ino); 287 printf("%s ", p); 288 } 289 else{ 290 dp= opendir(p); 291 if(dp == NULL){ 292 perror("opendir()"); 293 return 1; 294 } 295 296 while(1){ 297 entry = readdir(dp); 298 if(NULL == entry){ 299 if(errno){ 300 perror("readdir()"); 301 closedir(dp); 302 return 1; 303 } 304 break; 305 } 306 if(is_i(entry->d_name)) 307 continue; 308 i_llstat(p, entry->d_name); 309 } 310 } 311 closedir(dp); 312 printf("\n"); 313 return 0; 314 } 315 316 //h 317 int hstat(char *p) 318 { 319 struct stat mystat2; 320 struct dirent *entry = NULL; 321 DIR *dp = NULL; 322 if(lstat(p,&mystat2) == -1){ 323 perror("stat()"); 324 return 1; 325 } 326 327 if(!((mystat2.st_mode & S_IFMT) == S_IFDIR)){ 328 if(!is_i(p)){ 329 printf("%s\n", p); 330 return 1; 331 } 332 } else{ 333 334 dp= opendir(p); 335 if(dp == NULL){ 336 perror("opendir()"); 337 return 1; 338 } 339 340 while(1){ 341 entry = readdir(dp); 342 if(NULL == entry){ 343 if(errno){ 344 perror("readdir()"); 345 closedir(dp); 346 return 1; 347 } 348 break; 349 } 350 if(is_i(entry->d_name) == 0) 351 printf("%s ", entry->d_name); 352 } 353 closedir(dp); 354 } 355 printf("\n"); 356 return 0; 357 }