FreeBASIC  0.91.0
sys_getexepath.c
Go to the documentation of this file.
1 /* get the executable path */
2 
3 #include "../fb.h"
4 #include <sys/stat.h>
5 
6 char *fb_hGetExePath( char *dst, ssize_t maxlen )
7 {
8  char *p;
9  char linkname[1024];
10  struct stat finfo;
11  ssize_t len;
12 
13  sprintf(linkname, "/proc/%d/exe", getpid());
14  if ((stat(linkname, &finfo) == 0) && ((len = readlink(linkname, dst, maxlen - 1)) > -1)) {
15  /* Linux-like proc fs is available */
16  dst[len] = '\0';
17  p = strrchr(dst, '/');
18  if (p == dst) /* keep the "/" rather than returning "" */
19  *(p + 1) = '\0';
20  else if (p)
21  *p = '\0';
22  else
23  dst[0] = '\0';
24  } else {
25  p = NULL;
26  }
27 
28  return p;
29 }