내가 ls
을 mimicks 다음 코드가 : 어떤 이유디렉토리의 경우 d_type == DT_UNKNOWN 인 dirent를 반환하는 readdir입니다. 및
#include <dirent.h>
#include <stdio.h>
char* dirent_type_to_str(unsigned char dirent_type) {
switch (dirent_type) {
case DT_DIR:
return "Dir ";
case DT_REG:
return "File";
}
printf("DEBUG: Unknown type %x\n", dirent_type);
return "Unk ";
}
int main(int argc, char** argv) {
char* dir_path = argc > 1 ? argv[1] : ".";
DIR* dir_stream = opendir(dir_path);
struct dirent* dirent_ptr;
while (dirent_ptr = readdir(dir_stream)) {
printf("%s %s\n", dirent_type_to_str(dirent_ptr->d_type), dirent_ptr->d_name);
}
closedir(dir_stream);
return 0;
}
을 내가 프로그램을 실행할 때, .
및 ..
과 관련된 d_type
를 알 수 없음을 말하고있다 :
[email protected]:~/Desktop/systems-11-02$ ./main
DEBUG: Unknown type 0
Unk .
DEBUG: Unknown type 0
Unk ..
File .gitignore
File main.o
File Makefile~
File Makefile
File main.c
Dir .git
File main
File main.c~
조사 결과, 0
은 this answer에 따르면 DT_UNKNOWN
과 같습니다. 왜 이런 현상이 발생합니까 (DT_DIR
을 산출하는 대신)?
FWIW 당신은 그 지식을 단순히 하드 코딩 할 수 있습니다. 및 .. 디렉토리입니다. 그것들은 POSIX에 의해 위임되었습니다. –