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 
5 char *fb_hGetExePath( char *dst, ssize_t maxlen )
6 {
7  char *p;
8  ssize_t argv_len, len;
9 
10  argv_len = strlen( __fb_ctx.argv[0] );
11 
12  /* check for drive letter - if there, get full path from argv[0] */
13  if( isalpha(__fb_ctx.argv[0][0]) && __fb_ctx.argv[0][1] == ':' )
14  {
15  len = argv_len;
16  if( len >= maxlen)
17  return NULL;
18 
19  memcpy( dst, __fb_ctx.argv[0], len );
20  dst[len] = '\0';
21  }
22  /* check for \ at beginning - get drive letter from cwd */
23  else if( __fb_ctx.argv[0][0] == '\\' || __fb_ctx.argv[0][0] == '/' )
24  {
25  len = 1 + argv_len;
26  if( len >= maxlen )
27  return NULL;
28 
29  dst[0] = __fb_startup_cwd[0];
30  dst[1] = ':';
31  memcpy( dst + 2, __fb_ctx.argv[0], argv_len );
32  dst[len] = '\0';
33  }
34  /* no drive letter, no \, so relative path - get cur dir from startup */
35  else
36  {
37  ssize_t cwd_len;
38  cwd_len = strlen(__fb_startup_cwd);
39 
40  len = cwd_len + 1 + argv_len;
41  if( len >= maxlen )
42  return NULL;
43 
44  memcpy( dst, __fb_startup_cwd, cwd_len );
45  dst[cwd_len] = '\\';
46  memcpy( dst + cwd_len + 1, __fb_ctx.argv[0], argv_len );
47  dst[len] = '\0';
48  }
49 
50  fb_hConvertPath( dst );
51 
52  p = strrchr( dst, '\\' );
53  if( p != NULL )
54  *p = '\0';
55 
56  /* upcase drive letter to be consistent with win32 port */
57  dst[0] = toupper( dst[0] );
58 
59  /* just a drive letter? make sure \ follows to prevent using relative path */
60  if( maxlen > 3 && dst[2] == '\0' && dst[1] == ':' && isalpha(dst[0]) )
61  {
62  dst[2] = '\\';
63  dst[3] = '\0';
64  }
65 
66  return p;
67 }