an exercise: how to get the path of the current executable

Note: this is a non-portable Linux solution
This commit is contained in:
Fischlurch 2011-01-31 05:35:43 +01:00
parent d9f90c2c04
commit 29e2233e6a
2 changed files with 32 additions and 1 deletions

View file

@ -17,7 +17,7 @@ rsvg = envSvg.Program('rsvg-convert','rsvg-convert.c')
# build additional test and administrative tools....
artifacts['tools'] = [ env.Program('hello-world','hello.c', install=True) #### hello world (checks C build)
+ env.Program('try', 'try.cpp') #### to try out some feature...
+ env.Program('try', 'try.cpp', LIBS=support_lib) #### to try out some feature...
# + luidgen
+ rsvg
]

View file

@ -15,15 +15,45 @@
// 6/09 - investigating how to build a mixin template providing an operator bool()
// 12/9 - tracking down a strange "warning: type qualifiers ignored on function return type"
// 1/10 - can we determine at compile time the presence of a certain function (for duck-typing)?
// 1/11 - how to fetch the path of the own executable -- at least under Linux?
#include "include/nobugcfg.h"
#include <iostream>
#include <string>
extern "C" {
#include <unistd.h>
}
//#include "lib/error.hpp"
//#include "lib/symbol.hpp"
using std::string;
using std::cout;
//using lib::Literal;
//using lib::STRING_MAX_RELEVANT;
const size_t STRING_MAX_RELEVANT = 1000;
//namespace error = lumiera::error;
//Literal GET_PATH_TO_EXECUTABLE ("/proc/self/exe");
const char * const GET_PATH_TO_EXECUTABLE ("/proc/self/exe");
string
catchMyself ()
{
string buff(STRING_MAX_RELEVANT+1, '\0' );
ssize_t chars_read = readlink (GET_PATH_TO_EXECUTABLE, &buff[0], STRING_MAX_RELEVANT);
if (0 > chars_read || chars_read == ssize_t(STRING_MAX_RELEVANT))
// throw error::Fatal ("unable to discover path of running executable")
throw string("unable to discover path of running executable");
buff.resize(chars_read);
return buff;
}
int
@ -32,6 +62,7 @@ main (int, char**) //(int argc, char* argv[])
NOBUG_INIT;
cout << "\n\nich bin :" << catchMyself();
cout << "\n.gulp.\n";
return 0;