Excerpt from: http://blog.csdn.net/djzhao/article/details/8178375
Linux about Readlink function get running path of small program
Related functions: Stat, Lstat, symlink
Table header files: #include <unistd.h>
Define functions: Int readlink (const char *path, char *buf, size_t bufsiz);
Function Description: Readlink () will connect the symbolic path of the parameter to the memory space referred to in parameter buf, the returned content is not NULL as the end of the string, but will return the character number of the string. If the parameter bufsiz is less than the content length of the symbolic connection, the content that is too long is truncated
Return value: Successful execution the file path string referred to by the symbolic connection, failure returns-1, error code stored in errno
Error code:
Eaccess rejected when fetching files, insufficient permissions
EINVAL parameter bufsiz is negative
EIO o Access Error
Eloop the file you want to open has too many symbolic connection issues
Enametoolong parameter path name of path is too long
ENOENT parameter The file specified by path does not exist
Enomem Core memory is low
Enotdir parameter in path path directory exists but is not a real directory
Example two: (Personal feeling this procedure more reasonable, recommended)
#include <stdio.h>
#include <unistd.h>
char * Get_exe_path (char * buf, int count)
{
int i;
int rslt = Readlink ("/proc/self/exe", buf, count-1);
if (Rslt < 0 | | (Rslt >= count-1))
{
return NULL;
}
BUF[RSLT] = ' + ';
for (i = rslt; I >= 0; i--)
{
printf ("buf[%d]%c\n", I, buf[i]);
if (buf[i] = = '/')
{
Buf[i + 1] = ' + ';
Break
}
}
return buf;
}
int main (int argc, char * * argv)
{
Char path[1024];
printf ("%s\n", Get_exe_path (path, 1024));
return 0;
}
Linux about Readlink function get running path of small program