[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


C program for unix shell, forking()

Posted on 2004-11-10
Medium Priority
Last Modified: 2010-04-17

I try to write this code to execute an external binary file 'nutsh' taken a command 'cmd' and capture it output to a string 'result'. Somehow this code does not return a string output for me. Can someone tell me what have I done wrong!

char* redirectOutput(char* cmd)
     /* for return string */
     char* result;
     /* pipes */
     int to_child[2];
//     int from_child[2];
     /* pid's for forking */
     int pid1;
     int i = 0;
     /* tmp */
     char c;
     char str[20];
     /* allocate memory */
     result  = (char*) malloc (4096);
     pid1 = fork();
     if (pid1<0)     {
          perror("\n Forking Error. \n");

     else if (pid1==0) {
          /* duplicate pipes to stdin/stdout */
          dup2(to_child[0], 0);
          execl("./tmp/nutsh", "nutsh", NULL);
          //sprintf(str, "%s%d", "Execl() Failed: ", errno);
          exit (0);
     else if (pid1 > 0) {
          write(to_child[1], "\n", 1);
          write(to_child[1], cmd, strlen(cmd));
          write(to_child[1], "\n", 1);
          while (read(to_child[0], &c, 1) == 1) {
               strcat(result, &c);

     return (result);
Question by:david_2005

Accepted Solution

kledbetter earned 1000 total points
ID: 12554586
hi David --

  There are a few problems I see right off the top of my head (hey, it's early and I haven't  had coffee yet!).  But, you "write (to_child [1],...)" but I see nowhere where  you set a value for to_child [1].

  But, the biggie is this:

      strcat (result, &c);

  strcat() works with STRINGS, which in C is defined as "a byte array that ends with a binary 0".  Since you don't know what is physically behind the variable "c" in memory, this is at best undefined behavior.

   Here's one way to do it:

  char *tmpP = result;         // point to result string area

  while (read(to_child[0], &c, 1) == 1)
     *tmp++ = c;                // stuff it in "result"
  *tmp = '\0';                    // terminate the C string result

  return (result);


Author Comment

ID: 12554791
when exec. the while loop

      while (read(to_child[0], &c, 1) == 1) {
            strcat(result, &c);
I only see the command (cmd) I pass to execl(), and not the out put after execl() done.


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question