Linux about Readlink function get running path of small program

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.