linux ls 命令实现
 1. 前置知识
1 2 3 4 5 6 7 8 9 10 11 12 13
   | struct dirent   { #ifndef __USE_FILE_OFFSET64     __ino_t d_ino;     __off_t d_off; #else     __ino64_t d_ino;     __off64_t d_off; #endif     unsigned short int d_reclen;      unsigned char d_type;      char d_name[256];		   };
   | 
 
d_name是文件的名称
 2. 实现思路
使用opendir函数处理读入的文件路径,随后遍历结构体。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | void unix_error(const char *msg) {     int errnum = errno;     fprintf(stderr, "%s (%d: %s)\n", msg, errnum, strerror(errnum));     exit(EXIT_FAILURE); }
  int main(int argc, char** argv) {     DIR *streamp;     struct dirent *dep;
      streamp = opendir(argv[1]);
      errno = 0;     while ((dep = readdir(streamp)) != NULL){         printf("Found file: %s %d\n", dep->d_name, dep->d_type);     }     if (errno != 0) unix_error("readdir error");     closedir(streamp);     exit(0); }
   |