Coder's Guild Mailing List

Re: executing programs from C/C++ code

Posted by Frank Hale on 1999-08-27

-- Morgan Terry <morgan@xxxxxxxxxxx.xxx> wrote:
> use $HOME instead of ~.  For example, intead of:
> 	 system("~/bin/app");
> do this:
> 	system("$HOME/bin/app);
> 

I didn't make myself very clear in the question. What
I meant to say was that I am parsing a config file
which contains paths to important files. These paths
can contain ~ to specify the home directory of where
the file is located. What I wanted to find out was if
there was a C or C++ function that automatically
converted the ~ (tilde) into the home directory of the
user using the program. 

I've found a solution:

char *buffer = new char[MaxBufferSize];
if (exec[0] == '~') 
{
	sprintf(buffer, "%s%s", getenv("HOME"), exec + 1);
	strcpy(exec, buffer);
}
delete [] buffer;
	
item->exec = new char[strlen(exec)];
strcpy(item->exec, exec);

This converts the path which contains ~ and substitues
in the actual home directory of the user executing the
program.

Frank
===
From: Frank Hale
ICQ: 7205161
Webpage: http://jade.netpedia.net/sapphire
Webpage: http://www.linuxstart.com/~frankhale

"Sapphire - a new window manager for X11"
__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com