Operating System - HP-UX
1828586 Members
2491 Online
109982 Solutions
New Discussion

Help with UNIX 'system' calls in a CPP program...

 
Amber_1
Occasional Contributor

Help with UNIX 'system' calls in a CPP program...

Do any of you know the best way for me to perform a "system" call in a CPP file and place the output into a char pointer (or string) instead of writing it to the screen?

I need to print the UNIX calls to a XmTextField, rather than to the screen....
1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor

Re: Help with UNIX 'system' calls in a CPP program...

Hi Amber,

You could easily do it as an extern "C" something like this:

char *one_liner_system(char *s_cmd, int *result)
{
static char buff[512];
char *s = NULL,cmd2[1024];
FILE *p = NULL;

buff[0] = '\0';
*result = -1;
(void) sprintf(cmd2,"%s 2>/dev/null",s_cmd);
p = popen(cmd2,"p");
if (p != NULL)
{
s = fgets(buff,(sizeof) buff,p);
*result = pclose(p);
}
return(s)
}

The above function will return one line of the system call and set *result to the status of the system call and redirect stderr to /dev/null.

Regards, Clay
If it ain't broke, I can fix that.