[程式碼例項][Linux系統程式設計]相對路徑轉絕對路徑
阿新 • • 發佈:2019-01-07
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
char * rpath_to_apath(const char * rpath, char * buf, int buf_size);
int main(int argc, char * argv[])
{
char * rpath = NULL;
char * apath = NULL;
if((argc == 2 && strcmp(argv[1], "--help") == 0)
|| argc > 2)
{
printf("Usage: %s <relative_path>\n", argv[0]);
return EXIT_SUCCESS;
}
if(argc == 1)
rpath = "./";
else
rpath = argv[1];
if(rpath[0] == '/')
{
fprintf (stderr, "path must be relative path!\n");
return EXIT_FAILURE;
}
printf("relative path: %s\n", rpath);
if((apath = malloc(PATH_MAX)) == NULL)
{
perror("malloc");
return EXIT_FAILURE;
}
memset(apath, 0, PATH_MAX);
printf("absolute path: %s\n", rpath_to_apath(rpath, apath, PATH_MAX));
free (apath);
return EXIT_SUCCESS;
}
char * rpath_to_apath(const char * rpath, char * buf, int buf_size)
{
char cur_work_dir[PATH_MAX] = {0};
int index = 0;
const char * p = rpath;
while(*p == '.')
p++;
if(*p == '/')
p++;
if(getcwd(cur_work_dir, PATH_MAX) == NULL)
{
perror("getcwd");
return NULL;
}
index = strlen(strncpy(buf, cur_work_dir, buf_size - 1));
if(*p)
{
buf[index] = '/';
strncpy(buf + index + 1, p,
MIN(strlen(p), (buf_size - 1) - (index + 1)));
}
return buf;
}
這段程式碼僅僅是實現相對路徑和絕對路徑之間的轉換,但其中存在一個BUG:程式碼嚴重依賴於當前工作目錄。
正確的做法是呼叫系統呼叫realpath()
:
#include <stdlib.h>
char * realpath(const char * pathname, char * resolved_path);