提供一个linux advanced programming 上的得到绝对路径目录的函数:
char* get_self_executable_directory ()
{
int rval;
char link_target[1024];//目标地址
char* last_slash;
size_t result_length;//结果的长度
char* result;
/* Read the target of the symbolic link /proc/self/exe. */
读取绝对路径
rval = readlink ("/proc/self/exe", link_target, sizeof (link_target) - 1);
if (rval == -1)
/* The call to readlink failed, so bail. */
abort ();
else
/* NUL-terminate the target. */
link_target[rval] = '\0';
/* We want to trim the name of the executable file, to obtain the
directory that contains it. Find the rightmost slash. */
找到最后一个/
last_slash = strrchr (link_target, '/');
如果是空或者是以/开头,则退出
if (last_slash == NULL || last_slash == link_target)
/* Something stange is going on. */
abort ();
last_slash保存的是最后的/的地址
/* Allocate a buffer to hold the resulting path. */
link_target开始的地址
result_length = last_slash - link_target;
result = (char*) xmalloc (result_length + 1);
/* Copy the result. */
strncpy (result, link_target, result_length);
result[result_length] = '\0';
return result;
}
同时可以用以下程序得到绝对路径
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXBUFSIZE 1024
int main ( int argc, char * argv[] )
{
char buf[ MAXBUFSIZE ];
int count;
count = readlink( "/proc/self/exe", buf, MAXBUFSIZE );
if ( count < 0 || count >= MAXBUFSIZE )
{
printf( "Failed\n" );
return( EXIT_FAILURE );
}
buf[ count ] = '\0';
printf( "/proc/self/exe -> [%s]\n", buf );
return( EXIT_SUCCESS );
}
char* get_self_executable_directory ()
{
int rval;
char link_target[1024];//目标地址
char* last_slash;
size_t result_length;//结果的长度
char* result;
/* Read the target of the symbolic link /proc/self/exe. */
读取绝对路径
rval = readlink ("/proc/self/exe", link_target, sizeof (link_target) - 1);
if (rval == -1)
/* The call to readlink failed, so bail. */
abort ();
else
/* NUL-terminate the target. */
link_target[rval] = '\0';
/* We want to trim the name of the executable file, to obtain the
directory that contains it. Find the rightmost slash. */
找到最后一个/
last_slash = strrchr (link_target, '/');
如果是空或者是以/开头,则退出
if (last_slash == NULL || last_slash == link_target)
/* Something stange is going on. */
abort ();
last_slash保存的是最后的/的地址
/* Allocate a buffer to hold the resulting path. */
link_target开始的地址
result_length = last_slash - link_target;
result = (char*) xmalloc (result_length + 1);
/* Copy the result. */
strncpy (result, link_target, result_length);
result[result_length] = '\0';
return result;
}
同时可以用以下程序得到绝对路径
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXBUFSIZE 1024
int main ( int argc, char * argv[] )
{
char buf[ MAXBUFSIZE ];
int count;
count = readlink( "/proc/self/exe", buf, MAXBUFSIZE );
if ( count < 0 || count >= MAXBUFSIZE )
{
printf( "Failed\n" );
return( EXIT_FAILURE );
}
buf[ count ] = '\0';
printf( "/proc/self/exe -> [%s]\n", buf );
return( EXIT_SUCCESS );
}