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

x
?
Solved

redirect output C++, using execl()

Posted on 2004-11-05
21
Medium Priority
?
1,041 Views
Last Modified: 2012-06-27
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);
}
0
Comment
Question by:david_2005
  • 12
  • 9
21 Comments
 
LVL 5

Expert Comment

by:dennis_george
ID: 12510609
Do the nutsh file have execute permission ??? or Do you have the permission to execute that file....

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

0
 

Author Comment

by:david_2005
ID: 12510648
yes, do  chmod to 777 on nutsh
0
 

Author Comment

by:david_2005
ID: 12510659
I have permission to exec the "nutsh"
I did try execl("nutsh", "ls", NULL)
and sitll not get output
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:dennis_george
ID: 12511578
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
 

Author Comment

by:david_2005
ID: 12511586
how do i pos the nutsh file
0
 

Author Comment

by:david_2005
ID: 12511591
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
 
LVL 5

Expert Comment

by:dennis_george
ID: 12511595
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
 

Author Comment

by:david_2005
ID: 12511606
It still not work can you compile on your machine and see if it work

thank
 
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 12511610
I will and let you know
0
 

Author Comment

by:david_2005
ID: 12511613
thank you very much
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 12511768
meanwhile you check executing the nutsh file directly

nutsh ls

Is it running ?

0
 
LVL 5

Expert Comment

by:dennis_george
ID: 12511773
can you mail me the executable file at dennis.george@gmail.com
0
 

Author Comment

by:david_2005
ID: 12511776
you do type "nutsh" first so it will bring a prompt from "nutsh", then type "ls" it will work just fine.
0
 

Author Comment

by:david_2005
ID: 12511783
I think It might have something to do with the pipe issue
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 12511791
I think your program (nutsh) does not handle command line arguements...... Because if its direclty asking user (prompt for input).

0
 
LVL 5

Accepted Solution

by:
dennis_george earned 2000 total points
ID: 12511802
try this

system("echo ls | nutsh") ;

0
 

Author Comment

by:david_2005
ID: 12511803
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
 

Author Comment

by:david_2005
ID: 12511820
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
 
LVL 5

Expert Comment

by:dennis_george
ID: 12512251
A nice weekend to you also

Dennis
0
 

Author Comment

by:david_2005
ID: 12513254
Dennis,

I look like the "nutsh" not getting execute when I do  system("echo nutsh | ls")
0
 

Author Comment

by:david_2005
ID: 12516188
Thank you for your help.
I got this problem solve by myself already.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

873 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