C program for unix shell, forking()

hi,

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");
          fflush(stdout);
          fflush(stderr);
          exit(0);
          }

     else if (pid1==0) {
          fflush(stdout);
          /* duplicate pipes to stdin/stdout */
          dup2(to_child[0], 0);
         
               
          execl("./tmp/nutsh", "nutsh", NULL);
          //sprintf(str, "%s%d", "Execl() Failed: ", errno);
          //perror(str);
         
          fflush(stdout);
          fflush(stderr);
          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);
          }
       
          close(to_child[1]);
          close(to_child[0]);
          fflush(stdout);
          fflush(stderr);
                   
          while (read(to_child[0], &c, 1) == 1) {
               putchar(c);
               strcat(result, &c);
          }

     
     return (result);
}
david_2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kledbetterCommented:
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)
  {
     putchar(c);
     *tmp++ = c;                // stuff it in "result"
  }
  *tmp = '\0';                    // terminate the C string result

  return (result);

-Keith
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
david_2005Author Commented:
when exec. the while loop

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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.