#() <-- 확실 하지 않음.
Created
June 16, 2015 10:48
-
-
Save kimhoki/0a6e0c90acb52b45797e to your computer and use it in GitHub Desktop.
[Linux_File_System] getdents 함수를 이용한 #(물리 파일) 시스템 접속
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#define _GNU_SOURCE | |
#include <dirent.h> /* Defines DT_* constants */ | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#include <sys/syscall.h> | |
#define handle_error(msg) \ | |
do { perror(msg); exit(EXIT_FAILURE); } while (0) | |
struct linux_dirent { | |
long d_ino; /* Inode number*/ | |
off_t d_off; /* Offset to next linux_dirent */ | |
unsigned short d_reclen; /* Length of this linux_dirent */ | |
char d_name[]; /* Filename (Null -terminated */ | |
}; | |
#define BUF_SIZE 1024 | |
int | |
main(int argc, char *argv[]) | |
{ | |
int fd, nread; | |
char buf[BUF_SIZE]; | |
struct linux_dirent *d; | |
int bpos; | |
char d_type; | |
fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY); | |
if (fd == -1) | |
{ | |
perror("open error"); | |
//handle_error("open"); | |
} | |
for ( ; ; ) { | |
nread = syscall(SYS_getdents, fd, buf, BUF_SIZE); | |
if (nread == -1) | |
perror("getdents error"); | |
// handle_error("getdents"); | |
if (nread == 0) | |
break; | |
printf("--------------- nread=%d ---------------\n", nread); | |
printf("i-node# file type d_reclen \t d_off d_name\n"); | |
for (bpos = 0; bpos < nread;) { | |
d = (struct linux_dirent *) (buf + bpos); | |
printf("%8ld ", d->d_ino); | |
d_type = *(buf + bpos + d->d_reclen - 1); | |
printf("%-10s ", (d_type == DT_REG) ? "regular" : | |
(d_type == DT_DIR) ? "directory" : | |
(d_type == DT_FIFO) ? "FIFO" : | |
(d_type == DT_SOCK) ? "socket" : | |
(d_type == DT_LNK) ? "symlink" : | |
(d_type == DT_BLK) ? "block dev" : | |
(d_type == DT_CHR) ? "char dev" : "???"); | |
printf("%4d\t %10lld %s\n", d->d_reclen, | |
(long long) d->d_off, d->d_name); | |
bpos += d->d_reclen; | |
} | |
} | |
return 0; | |
// exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment