시스템 프로그래밍
심볼릭 ( 아침 시작 )
// 하드링크 (hard link), 소프트 링크 (soft link)
[1] >>> man -s 3 link
******************************************************************
# include <stdio.h>
# include <unistd.h>
int main(void) {
if (link("./s", "./hardlink_file") == -1) {
printf("error ... !!!\n");
}
printf("success ... \n");
return 0;
}
# ls -il
[2] >>> man -s 3 symlink
******************************************************************
# include <stdio.h>
# include <unistd.h>
int main(void) {
if (symlink("s", "symlink_file") == -1) {
printf("error ... !!!\n");
}
else {
printf("success ... !!!\n");
}
return 0;
}
[++] >> 파일 정보 검색 : stat
******************************************************************
# include <sys/stat.h>
# include <stdio.h> // standard input output
int main(void) {
struct stat buf;
stat("file1", &buf);
printf("buf.st_nlink => %4d\n", buf.st_nlink);
printf("buf.st_uid => %4d\n", buf.st_uid); // User Identification Number
printf("buf.st_gid => %4d\n", buf.st_gid); // Group Identification Number
return 0;
}
[3] >>> 심볼릭 링크의 정보 검색 : lstat
******************************************************************
# include <sys/stat.h>
# include <stdio.h>
int main(void) {
struct stat buf;
lstat("syml", &buf);
printf("Link count => %d\n", (int)buf.st_nlink);
printf("Inode => %d\n", (int)buf.st_ino);
return 0;
}
[4] >>> 심볼릭 링크의 내용 읽기 : readlink
******************************************************************
man -s3 readlink
# include <sys/stat.h>
# include <unistd.h>
# include <stdio.h>
# include <stdlib.h> // exit
# include <string.h> // strlen
# define BUFSIZE 100
int main(void) {
char buf[BUFSIZE] = {0,};
int result = 0;
result = readlink("./syml", buf, BUFSIZE);
if (result == -1) {
printf("error ... \n");
exit(1);
}
printf("result > %d\n", result);
buf[strlen(buf)] = '\0';
printf("file.sym : %s\n", buf);
return 0;
}
[5] >>> 원본 파일의 경로 읽기 : realpath
******************************************************************
man -s3 realpath
[-] 필요한 헤더 정보
# include <stdio.h>
# include <limits.h>
# include <stdlib.h>
# define BUFSIZE 100
int main(void) {
char buf[BUFSIZE] = {0, };
realpath("syml", buf);
printf("syml : REALPATH => %s\n", buf);
return 0;
}
'System programming' 카테고리의 다른 글
시스템 프로그래밍 파일종류 검색 (0) | 2018.03.09 |
---|---|
시스템 프로그래밍_01 (0) | 2018.03.09 |
strcpy (0) | 2017.11.08 |
시스템 프로그래밍_1 (0) | 2017.10.09 |