I think that you should rely less on environment variables and external programs, and more on what you can programmatically find and do with the C++ libraries and POSIX APIs. In this case, you don't need much, really:
getuid
to find the current user;getpwuid
to find the current user's home directory;getcwd
to get the current working directory;- pretty standard file output stream stuff.
This six-line program appends export PATH="$PATH:current_working_path"
to ~/.bash_profile
.
#include <fstream>
#include <string>
#include <pwd.h>
#include <unistd.h>
#include <sys/param.h>
using namespace std;
int main()
{
char path[MAXPATHLEN];
struct passwd* user_info = getpwuid(getuid());
string user_directory = user_info->pw_dir;
string current_directory = getcwd(path, sizeof path);
ofstream bash_profile(user_directory + "/.bash_profile", ios_base::app);
bash_profile << "export PATH=\"$PATH:" << current_directory << '"' << endl;
}
Note that this will not affect the bash process from which you call the executable. Environment variables are strictly inherited: modifying them from a child process will not affect the parent process. There is no way to make that happen from a child process, as far as I know. (The commands that let you manipulate the environment of the bash process are built into the shell and do not run as a separate process, precisely for this reason.)
However, you could make it print the same string to the standard output and run it with backticks, or run source ~/.bash_profile
after you run this program.