프로그래밍/리눅스 프로그래밍
[Linux] realpath 구하기
jinkwon.kim
2021. 8. 11. 03:33
728x90
반응형
개요
상대 경로 실행 된 프로그램의 realpath를 구합니다.
realpath 함수를 시용하여 구합니다.
헤더
#include <iostream> /* realpath */
#include <limits.h> /* PATH_MAX */
Sample Code
1. 실행 파일을 포함한 절대 경로 구하기
#include <iostream>
#include <limits.h> /* PATH_MAX */
int main(int argc, char** argv)
{
char buff[PATH_MAX];
char *res = realpath(argv[0], buff);
if (res) {
std::cout << buff << std::endl;
}
}
root@ubuntu:~/# ./realpath
/root/develop/exampleCode.com/realpath/realpath
2. 실행 파일을 제외한 절대 경로 구하기
#include <iostream>
#include <limits.h> /* PATH_MAX */
#include <libgen.h> /* dirname */
int main(int argc, char** argv)
{
char buff[PATH_MAX];
char *res = realpath(argv[0], buff);
if (res) {
std::cout << buff << std::endl;
}
std::cout << dirname(buff) << std::endl;
}
root@ubuntu:~/# ./realpath
/root/develop/exampleCode.com/realpath/realpath
/root/develop/exampleCode.com/realpath
728x90
반응형