FreeBASIC  0.91.0
sys_getexename.c
Go to the documentation of this file.
1 /* get the executable's name */
2 
3 #include "../fb.h"
4 #include <sys/stat.h>
5 
6 char *fb_hGetExeName( 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 != NULL)
19  ++p;
20  else
21  p = dst;
22  } else {
23  p = NULL;
24  }
25 
26  return p;
27 }