URGENT: It works on linux but not on freeBSD, what could be wrong?

I'm posting this here as a last hope, I have a deadline which will expire in a few hours and I've tried everything and can't seem to figure it out.

When I call system("cc d.c -o d"), I get


Code:
unable to execute command: Executable "" doesn't exist!

But there's some things to note here,

When program A is executed, it creates/writes d.c. After the write is complete, then program A calls
Code:
system("cc d.c -o d")
Any ideas?
 
Are you sure it's the system() call that's producing the error? Have you tried to execute something else?
 
Are you sure it's the system() call that's producing the error? Have you tried to execute something else?

Yes, here's what I've tried:
system("ls"); // works
system("ls -la"); //doesn't work
system("ls -la\n"); //works
system("cc"); //works, asks for missing files
system("cc d.c"); //error
system("cc d.c\n"); //error

This is all in fileA, another thing to note:.

I also have fileB and the system calls work fine on there.

I'm really not sure what the relevant difference is between them.
 
Looking at your examples, it appears the common issue is when you are passing arguments along with the command. A quick google seems to indicate (for whatever reason) that you should put the command in a string and then pass the string in the system call, like:
Code:
char buf[128];
snprintf(buf, sizeof(buf), "echo %d",a);                                  
system(buf);
Worth a try at least.
 
Unfortunately I cannot reproduce your error, which means it is related to your execution environment.

The following works just fine for me:

Code:
/*
 * system.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main (int argc, char **argv)
{
    if ( system("cc -o exec.system system.c") < 0 )
    {
        fprintf(stderr, "error: system call: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    return EXIT_SUCCESS;
}
 
I have encountered this issue once before but got busy with other things and never quite got time to figure out what was going on.

Perhaps avoid the problem entirely and use popen instead?

Perhaps try changing your shell before you run the program? Just changing the variable might be enough.

SHELL=/bin/sh ./myprogram

Would be quite satisfying to finally put this mystery to rest :)
 
First, per documentation system is supposed to use sh. Second, "unable to execute command" is an error message from the compiler itself. Third, OP is obviously withholding information.
 
Thanks everyone. I found the issue:

I had started program A with execve but did not correctly pass arguments to the env parameters.

After I fixed that, I was able to call system from program A without issue.
 
Back
Top