[Linux]getcwd, chdir, stat, unlink
getcwd(3)
#include <unistd.h>
char *getcwd(char *buf, size_t size);
현재 작업 디렉토리의 절대경로를 문자열로 return.
ex
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void)
{
char path[1024];
if (getcwd(path, 1024) == NULL)
{
fprintf(stderr, "Error: %s\n", strerror(errno));
return -1;
}
printf("dir = %s\n", path);
}
위 코드를 실행시키면 다음과 같은 결과를 얻을 수 있다.
dir = /Users/taehykim/workspace
size를 작게 전달하면 다음과 같은 결과값이 나온다.
Error: Result too large
chdir (Change Directory)
#include <unistd.h>
int chdir(const char *dirname);
작업 디렉토리를 변경한다.
ex
#include <stdio.h>
#include <unistd.h>
int main( )
{
int result = chdir(/Users/taehykim/workspace);
if (result == 0 )
printf("Directory changed\n");
else
perror("Directory change failed\n");
}
stat (2)
man 2 stat
파일의 정보를 담고 있는 stat 구조체를 리턴하는 시스템 함수.
stat(const char *restrict path, struct stat *restruct buf);
fstat(int fildes, struct stat *buf);
lstat(cont char *restrict path, struct stat *restrict buf);
buf : stat 구조체
struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is NOT defined */
dev_t st_dev; /* device inode resides on 디바이스 번호*/
ino_t st_ino; /* inode's number */
mode_t st_mode; /* inode protection mode 파일 종류 & 접근권한*/
nlink_t st_nlink; /* number of hard links to the file 하드링크 수*/
uid_t st_uid; /* user-id of owner 소유자 아이디*/
gid_t st_gid; /* group-id of owner 소유자의 그룹아이디*/
dev_t st_rdev; /* device type, for special file inode 디바이스 아이디*/
struct timespec st_atimespec; /* time of last access */
struct timespec st_mtimespec; /* time of last data modification */
struct timespec st_ctimespec; /* time of last file status change */
off_t st_size; /* file size, in bytes */
quad_t st_blocks; /* blocks allocated for file */
u_long st_blksize;/* optimal file sys I/O ops blocksize */
u_long st_flags; /* user defined flags for file */
u_long st_gen; /* file generation number */
};
st_mode : 파일 접근 권한 확인 POSIX Macro
stat(2) : path의 파일상태를 buf에 저장
fstat : 심볼릭링크라면 원본의 상태를 buf에 저장
lstat : path대신 fd값으로 대신한다.
ex
#include <sys/stat.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int result;
char *file_name;
struct stat file_info;
struct passwd *my_passwd;
struct group *my_group;
mode_t file_mode;
if (argc != 2 )
{
printf("Usage : ./file_info [file name]\n");
exit(0);
}
file_name = argv[1];
if ((result = stat(file_name, &file_info)) == -1)
{
perror("Error : ");
exit(0);
}
file_mode = file_info.st_mode;
printf("파일이름 : %s\n", file_name);
printf("=======================================\n");
printf("파일 타입 : ");
if (S_ISREG(file_mode))
printf("정규파일\n");
else if (S_ISLNK(file_mode))
printf("심볼릭 링크\n");
else if (S_ISDIR(file_mode))
printf("디렉토리\n");
else if (S_ISCHR(file_mode))
printf("문자 디바이스\n");
else if (S_ISBLK(file_mode))
printf("블럭 디바이스\n");
else if (S_ISFIFO(file_mode))
printf("FIFO\n");
else if (S_ISSOCK(file_mode))
printf("소켓\n");
my_passwd = getpwuid(file_info.st_uid);
my_group = getgrgid(file_info.st_gid);
printf("OWNER : %s\n", my_passwd->pw_name);
printf("GROUP : %s\n", my_group->gr_name);
printf("FILE SIZE IS : %lld\n", file_info.st_size);
printf("마지막 읽은 시간 : %ld\n", file_info.st_atime);
printf("마지막 수정 시간 : %ld\n", file_info.st_mtime);
printf("하드링크된 파일수 : %d\n", file_info.st_nlink);
}
unlink
#include <unistd.h>
int unlink(const char *path);
path : full path || relative path
파일을 삭제하는 system call
hard link의 이름을 삭제.
hark link가 참조하는 count가 0이 되면 파일의 disk space를 free.
파일이 열려진 상태에서는 count가 0이 되어도 disk space가 free되지 않음.
댓글남기기