C program for unix shell, forking()

Posted on 2004-11-10
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

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 54
    Excel object stays open 19 56
    Fibonacci challenge 11 61
    Remove Settings button from start menu (Windows 10) 1 29
    A short article about a problem I had getting the GPS LocationListener working.
    This is about my first experience with programming Arduino.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    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 …

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now