redirect output C++, using execl()

can some can help me with the execl() output redirect on my program. The execl("/bin/ls", "ls", 0) will output correctly in my code; however, when I execute an external binary executable file it just hang and does not give me an output to the screen.

In this code it will work fine with  
                         if ( 0 > execl("/bin/ls", "ls", NULL))
            cerr <<errno<<endl;

but will not give me any outout when execute this line:
                           if ( 0 > execl("./nutsh", "ls", NULL))
            cerr <<errno<<endl;
"nutsh" is a binary executable file.

Thank you in advance!


#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <errno.h>
#include <signal.h>

using namespace std;
char* redirectOutput(char*);
static const int MAX_LINE = 80;

int main(int argc,char* argv[])
{      
      char buff[MAX_LINE];
      char* cmdLine;
      char* result;
      
      cout << ">> ";
      memset(buff, '\0', MAX_LINE);
      gets(buff);
      cmdLine = strtok(buff, "\n");
            
      result = redirectOutput(cmdLine);
      
      cout << "RESULT\n\n" << result << "\nDONE!" << endl;
      return 0;
}
char* redirectOutput(char* cmdLine)
{
  /* for return string */
  char* result;
  long lSize;
  /* pipes */
  int to_child[2];
  int from_child[2];
  /* pid's for forking */
  pid_t pid1;
  pid_t pid2;
  /* tmp */
  char c;
  int ch;

  /* allocate memory for result */
  result = (char*) malloc (lSize);  
 
  /* create pipes */
  if (pipe(to_child))    
        perror("\n Pipe Error. \n");
  if (pipe(from_child))  
        perror("\n Pipe Error. \n");
 
  pid1 = fork();

  if (pid1==0)
  {
    /* duplicate pipes to stdin/stdout */
    dup2(to_child[0], STDIN_FILENO);
    dup2(from_child[1], STDOUT_FILENO);
    /* close unused file descriptors */
    //close(to_child[0]);
    close(to_child[1]);
    //close(from_child[0]);
    close(from_child[1]);
    /* start execl */
      
      if ( 0 > execl("./nutsh", "ls", NULL))
            cerr <<errno<<endl;
      
      /*
    if ( 0 > execl("/bin/ls", "ls", NULL))
            cerr <<errno<<endl;
      */
            
      exit (0);
        perror("\n Execl Error. \n");
  }else if (pid1 > 0)
  {
      write(to_child[0],cmdLine,strlen(cmdLine));
      write(to_child[0],"\nexit\n",6);
  }
 
  close(to_child[0]);
  close(from_child[1]);
 
  pid2 = fork();
  if (pid2==0)
  {
    close(from_child[0]);
      close(from_child[1]);
      while ((ch=getchar())!=EOF)
      {
      c = ch;
      write(to_child[0], &c, 1);
    }
      exit(0);
      perror("\n Copy stdin to pipe Error. \n");
  }
 
  //close(to_child[1]);
  /* copy .exe output in out-pipe to stdout */
  while (read(from_child[0], &c, 1)==1)
  {
       //putchar(c);  /* stdout to screen */
       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.

dennis_georgeCommented:
Do the nutsh file have execute permission ??? or Do you have the permission to execute that file....

Also try
  execl("nutsh", "ls", NULL) ;

0
david_2005Author Commented:
yes, do  chmod to 777 on nutsh
0
david_2005Author Commented:
I have permission to exec the "nutsh"
I did try execl("nutsh", "ls", NULL)
and sitll not get output
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

dennis_georgeCommented:
I hope your execl is not returning any error (cout << errno).... So I think there is some problem in your nutsh file.... can you post the code of nutsh.....

0
david_2005Author Commented:
how do i pos the nutsh file
0
david_2005Author Commented:
here is the link for my nutsh and C++ file:

http://www.cox-internet.com/nguyen/nutsh

http://www.cox-internet.com/nguyen/work.cpp

I really need your help to get this work
thank you in advance
0
dennis_georgeCommented:
Hi,

I think I got your error.....

in execl the arguement list is arg0, arg1, ....

so as in normal program arg0 is always the program name and arg1 is the start of arguement list....

so change your call to execl to
 execl("nutsh", "nutsh", "ls", NULL)

and check

Dennis
0
david_2005Author Commented:
It still not work can you compile on your machine and see if it work

thank
 
0
dennis_georgeCommented:
I will and let you know
0
david_2005Author Commented:
thank you very much
0
dennis_georgeCommented:
meanwhile you check executing the nutsh file directly

nutsh ls

Is it running ?

0
dennis_georgeCommented:
can you mail me the executable file at dennis.george@gmail.com
0
david_2005Author Commented:
you do type "nutsh" first so it will bring a prompt from "nutsh", then type "ls" it will work just fine.
0
david_2005Author Commented:
I think It might have something to do with the pipe issue
0
dennis_georgeCommented:
I think your program (nutsh) does not handle command line arguements...... Because if its direclty asking user (prompt for input).

0
dennis_georgeCommented:
try this

system("echo ls | nutsh") ;

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:
you right, it does not take an argument directly. And now i try to make the system do like automatic type a command into it. That why I have "cmdLine".  b/c later on I will replace "ls" with a cmdLine

David
0
david_2005Author Commented:
Actually I have to do system("echo nutsh | ls");

It works now,

Thanks again for helping to solve the problem.
Have a nice weekend.

David
0
dennis_georgeCommented:
A nice weekend to you also

Dennis
0
david_2005Author Commented:
Dennis,

I look like the "nutsh" not getting execute when I do  system("echo nutsh | ls")
0
david_2005Author Commented:
Thank you for your help.
I got this problem solve by myself already.
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
C++

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.