/proc/self/exe
http://hi.baidu.com/xlt1888/blog/item/14126e84b870afb90df4d26b.html
提供一个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 );
}
1 http://www.smth.edu.cn/bbsanc.php?path=%2Fgroups%2Fos.faq%2FUnix%2F13%2Fhellguard_unix_faq%2FM.1031923592.U0
2 http://blog.linux.org.tw/~jserv/archives/002041.html