Click to See Complete Forum and Search --> : Problem with system() function.


ARIX
February 25th, 2003, 01:21 AM
Hi guys,
I'm coding under Solaris and using

int system(const char *string)

function in my program. In man pages I've read that this function
returns -1 when it fails but when it fails in my program it doesn't return -1. When I assigned the return value of function to variable of type int and printed it, it was 256 (strange???). Below I've placed the code:

#define STR_SIZE 4096
. . . .
. . . .

char cmd[STR_SIZE] = {0};
sprintf(cmd, "cd %s; %s", path, dc_str);
if(system(cmd) == -1)
{
printf("Error executing %s", cmd);
}
else
{
. . . . .
. . . . .
}

The failure is taking place because I'm passing to "path" variable such value which doesn't exist, and during attemption to change directory system() fails.In this case by documentation it must return -1 value and my programm must print the Error message.

PaulWendt
February 25th, 2003, 06:24 AM
system() doesn't return -1 in the event that the program you
called fails; it returns -1 if the actual CALL to your program fails.

I'm not sure if system() will return the program's actual return
value; I don't think that it does on Windows, but it might. On your
OS, you're obviously constrained by what it supports.

--Paul