Programming/C/C++
C/Linux/심볼릭 링크 정보 검색
현벨
2017. 11. 9. 15:35
반응형
심볼릭 링크 정보 검색
- 심볼릭 링크 정보 검색 : lstat(2)
- lstat : 심볼릭 링크 자체의 파일 정보 검색
- 심볼릭 링크를 stat 함수로 검색하면 원본 파일에 대한 정보가 검색된다.
- 심볼릭 링크의 내용 읽기 : readlink(2)
- 심볼릭 링크의 데이터 블록에 저장된 내용 읽기
- 원본 파일의 경로 읽기 : realpath(3)
- 심볼릭 링크가 가리키는 원본 파일의 실제 경로명 출력
lstatFunc 함수 사용 하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> int main(void) { struct stat buf; printf("1. stat : unix.txt ---\n"); stat("unix.txt", &buf); printf("unix.txt : Link Count = %d\n", (int)buf.st_nlink); printf("unix.txt : Inode = %d\n", (int)buf.st_ino); printf("2. stat : unix.sym ---\n"); stat("unix.sym",&buf); printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); printf("unix.sym : Inode = %d\n", (int)buf.st_ino); printf("3. lstat : unix.sym ---\n"); lstat("unix.sym", &buf); printf("unix.sym : Link Count = %d\n", (int)buf.st_nlink); printf("unix.sym : Inode = %d\n", (int)buf.st_ino); return 0; } |
실행 결과
readlink 함수 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> int main(void) { char buf[BUFSIZ]; int n; n = readlink("unix.sym", buf, BUFSIZ); if (n == -1) { perror("readlink"); exit(1); } buf[n] = '\0'; printf("unix.sym : READLINK = %s\n", buf); return 0; } |
실행 결과
realpath 함수 사용
1 2 3 4 5 6 7 8 9 10 11 12 | #include <sys/stat.h> #include <stdlib.h> #include <stdio.h> int main(void) { char buf[BUFSIZ]; realpath("unix.sym", buf); printf("unix.sym : REALPATH = %s\n", buf); return 0; } |
실행 결과
반응형