1. 程式人生 > >linux ls 源碼 (自寫)

linux ls 源碼 (自寫)

lose mod size 信息 oca 大小 alt end 文件

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <dirent.h>
  6 #include <errno.h>
  7 #include <string.h>
  8 #include <time.h>
  9 #include <pwd.h>
 10 #include <grp.h>
 11
#include <glob.h> 12 #define BUFSIZE 1024 13 static int is_dot_dir(const char *path) 14 { 15 char *p = NULL; 16 17 p = strrchr(path, .); 18 if (p != NULL) { 19 if (!strcmp(p, ".") || !strcmp(p, "..") || !strcmp(p,"./")) 20 return 1; 21 }
22 23 return 0; 24 } 25 26 static int myino(const char *path) 27 { 28 DIR *dp = NULL; 29 struct dirent *entry = NULL; 30 char buf[BUFSIZE]={}; 31 dp = opendir(path); 32 int i; 33 if (dp == NULL) 34 { 35 perror("opendir()"); 36 return
1; 37 } 38 while (1) 39 { 40 entry = readdir(dp); 41 if (entry == NULL) 42 { 43 if (errno) 44 { 45 perror("readdir()"); 46 goto ERROR; 47 } 48 break; 49 } 50 51 if(!is_dot_dir(entry->d_name)) 52 { 53 strcpy(buf,entry->d_name); 54 printf(" %ld %s",entry->d_ino,buf); 55 } 56 57 } 58 printf("\n"); 59 60 61 ERROR: 62 closedir(dp); 63 return 1; 64 65 } 66 67 68 static int mypath(const char *path) 69 { 70 struct stat mystat; 71 if (lstat(path, &mystat) == -1) 72 { 73 perror("stat()"); 74 return 1; 75 } 76 77 78 switch(mystat.st_mode & S_IFMT)// 類型 79 { 80 case S_IFREG: 81 printf("-"); 82 break; 83 case S_IFDIR: 84 putchar(d); 85 break; 86 default: 87 break; 88 } 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 { 101 if (mystat.st_mode & S_ISUID) 102 { 103 putchar(s); 104 } else 105 putchar(x); 106 } else 107 putchar(-); 108 109 110 printf(" %ld ", mystat.st_nlink);// 硬鏈接 111 112 // 文件擁有者名 113 struct passwd *pwd = NULL; 114 pwd = getpwuid(mystat.st_uid); 115 printf("%s ", pwd->pw_name); 116 117 // 文件所屬組 118 struct group *grp = NULL; 119 grp = getgrgid(mystat.st_gid); 120 printf("%s ", grp->gr_name); 121 122 // 總字節個數 磁盤空間大小st_blocks/2 k 123 printf("%ld ", mystat.st_size); 124 125 // 獲取文件時間 atime mtime ctime 126 struct tm *tmp = NULL; 127 tmp = localtime(&mystat.st_mtim.tv_sec);//將時間錯轉換成已分解的結構體 128 // if error 129 char buf[BUFSIZE] = {}; 130 strftime(buf, BUFSIZE, "%m月 %d %H:%M", tmp); 131 printf("%s ", buf); 132 133 // 文件名 134 printf("%s ", path); 135 136 putchar(\n); 137 return 0; 138 } 139 140 static int mydir(const char *path) 141 { 142 143 DIR *dp = NULL; 144 struct dirent *entry = NULL; 145 char buf[BUFSIZE]={}; 146 dp = opendir(path); 147 if (dp == NULL) 148 { 149 perror("opendir()"); 150 return 1; 151 } 152 while (1) 153 { 154 entry = readdir(dp); 155 if (entry == NULL) 156 { 157 if (errno) 158 { 159 perror("readdir()"); 160 goto ERROR; 161 } 162 break; 163 } 164 165 166 printf("%s ", entry->d_name); 167 168 } 169 printf("\n"); 170 171 closedir(dp); 172 return 0; 173 174 ERROR: 175 closedir(dp); 176 return 1; 177 178 } 179 180 181 182 static int myndir(const char *path) 183 { 184 185 DIR *dp = NULL; 186 struct dirent *entry = NULL; 187 char buf[BUFSIZE]={}; 188 dp = opendir(path); 189 if (dp == NULL) 190 { 191 perror("opendir()"); 192 return 1; 193 } 194 while (1) 195 { 196 entry = readdir(dp); 197 if (entry == NULL) 198 { 199 if (errno) 200 { 201 perror("readdir()"); 202 goto ERROR; 203 } 204 break; 205 } 206 if(!is_dot_dir(entry->d_name)) 207 { 208 strcpy(buf,entry->d_name); 209 printf("%s ",buf); 210 } 211 212 213 214 } 215 printf("\n"); 216 217 closedir(dp); 218 return 0; 219 220 ERROR: 221 closedir(dp); 222 return 1; 223 224 } 225 226 227 int main(int argc, char **argv) 228 { 229 if(argc < 2) 230 return 1; 231 char *optstring = "-l:a:i:h:"; 232 int c; 233 234 while (1) { 235 c = getopt(argc, argv, optstring); 236 if (c == -1) 237 break; 238 switch (c) { 239 case l: 240 mypath(argv[2]);//顯示文件的詳細信息 241 242 break; 243 case a: 244 mydir(argv[2]);//顯示目錄文件名(帶隱藏文件) 245 246 break; 247 case i: 248 myino(argv[2]);//顯示文件的inod號 249 break; 250 case h: 251 myndir(argv[2]);//顯示目錄名(不帶隱藏文件) 252 break; 253 case ?: 254 printf("不認識此選項%s\n", argv[optind-1]); 255 break; 256 case 1: 257 printf("非選項參數%s\n", argv[optind-1]); 258 break; 259 default: 260 break; 261 } 262 } 263 264 return 0; 265 266 }

linux ls 源碼 (自寫)