/* 使用getcwd()获取的是当前工作路径,而不一定是程序的路径 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAXBUFSIZE 1024
int main ( int argc, char * argv[] )
{
char buf[ MAXBUFSIZE ];
getcwd(buf, MAXBUFSIZE);
printf(buf);
return 0;
}
下面这个是一个x86/Linux Kernel 2.4.7-10系统中利用proc获取绝对路径的例子
--------------------------------------------------------------------------
/*
* gcc -Wall -pipe -g -static -o myprog_2 myprog_2.c
*/
#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 );
} /* end of main */
--------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char * argv[] )
{
return( EXIT_SUCCESS );
} /* end of main */