# executing a program within a program

**URL:** <https://community.unix.com/t/executing-a-program-within-a-program/141205>\
**Category:** Programming\
**Created:** [February 5, 2002, 1:56am UTC](https://community.unix.com/t/executing-a-program-within-a-program/141205 "2002-02-05T01:56:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gekko](https://community.unix.com/letter_avatar/gekko/32/6_449c533f586939b6e5f246632a46421f.png) [@Gekko](https://community.unix.com/u/Gekko)\
**Post date:** [February 5, 2002, 1:56am UTC](https://community.unix.com/t/executing-a-program-within-a-program/141205/1 "2002-02-05T01:56:22Z")

</div>

Read the title: how do i do it?

---

<div class="post-metadata">

**Author:** ![thalex](https://community.unix.com/letter_avatar/thalex/32/6_449c533f586939b6e5f246632a46421f.png) [@thalex](https://community.unix.com/u/thalex)\
**Post date:** [February 5, 2002, 4:53am UTC](https://community.unix.com/t/executing-a-program-within-a-program/141205/2 "2002-02-05T04:53:28Z")

</div>

$ man exec  
or execv etc

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [February 5, 2002, 8:15am UTC](https://community.unix.com/t/executing-a-program-within-a-program/141205/3 "2002-02-05T08:15:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bangieff](https://community.unix.com/letter_avatar/bangieff/32/6_449c533f586939b6e5f246632a46421f.png) [@bangieff](https://community.unix.com/u/bangieff)\
**Post date:** [February 6, 2002, 12:38pm UTC](https://community.unix.com/t/executing-a-program-within-a-program/141205/4 "2002-02-06T12:38:21Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![hirni](https://community.unix.com/letter_avatar/hirni/32/6_449c533f586939b6e5f246632a46421f.png) [@hirni](https://community.unix.com/u/hirni)\
**Post date:** [June 27, 2002, 3:36pm UTC](https://community.unix.com/t/executing-a-program-within-a-program/141205/5 "2002-06-27T15:36:25Z")

</div>

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...

```nohighlight
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:

```nohighlight
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.

```nohighlight
#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; 
} 

```
