executing a program within a program

Read the title: how do i do it?

$ man exec
or execv etc

You also might want to look at "man system" and "man popen".

char **arguments;
int result,status;
....

if (!fork()) {
execvp(arguments[0],arguments);
exit(-1);
} else {
result=wait(&status);
...
}

I'm having a couple of problems with my CGI program.

First, I don't know how to get [the text our getfacl program would create] back so I can return it to a web browser. I was hoping that, since getfacl program writes to standard output, that it would just be written back to the browser (or to standard output) when called from within the hello.cgi program.

Secondly, the fork command doesn't seem to work the same for a CGI program.

When I call the program from the prompt (it's called hello.cgi), everything comes out as expected...

bash-2.05$ ./hello.cgi
Content-type: text/html

waiting<br>
calling getfacl<br>called getfacl<br>done waiting<br>

but when I hit it with the web browser, what comes back is:

Content-type: text/html

calling getfacl<br>called getfacl<br>Content-type: text/html

waiting<br>
done waiting<br>

which almost seems as though it's returning the output from one process at a time... first the child process, then the parent process.

So, my two questions are:

  • How do I get the text [the program I'm calling with execv] would normally write to stdout
  • How come it's right when I execute hello.cgi from the command line, but all jumbled up when I run it through cgi

By the way, I guess I should mention that it's running through apache 1.3.20 on red had linux. Wish I knew how to get the Linux version, but...

Although I understand the concepts fairly well, I've got almost no experience with fork or the exec family (and only a little UNIX/Linux experience),

The hello.cgi code is below. Any help would be greatly appreciated.

#include <stdio.h>

int main (int argc, char *argv[])
{
  int result,stat;
  char *args[]={"getfacl", ".", 0};
  
  printf("Content-type: text/html\n\n"); 
  
  if (fork()) {
    printf("waiting<br>\n");
    result=wait(&stat);
    printf("done waiting<br>\n");
  } else {
    printf ("calling getfacl<br>");
    execv ("getfacl", args);
    printf ("called getfacl<br>");
    exit(-1);
  }
  
  printf ("\n");
  
  return 0; 
}