LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2005, 12:11 PM   #1
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Rep: Reputation: 15
Storing the result of execvp to a char array in C


I would like to know how to store the result of execvp to a char array. Below is my code:

Code:
int main () {
    char response[1000];
    char *buffer[100];
     int pid, status;
     printf("Please enter the shell command:  ");
     scanf("%s",&response);
     pid = fork();
     if (pid < 0) {
                    printf("Unable to create child process, exiting.\n");
                    exit(0);
                    }
     if (pid == 0) {
                printf("I'm the child.\n");
                *buffer = response;
                execvp(*buffer,buffer);
                printf("execvp failed\n");
                     } 
                     
     else{
          wait(&status);
          exit(0); 
          }
}
If a client types "ls" then execvp will execute ls command and instead of displaying it to the screen I would like to save it as a string in a char array. Thanks in advance.

J
 
Old 12-14-2005, 01:37 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I understand what you're trying to achieve, but don't see an easy option for you. The program executed by execvp will place it's output on file descriptor #1 (aka, stdout). You could redirect this to a file or similar using freopen in the fork. MMap can back a file using memory mapping, I wonder if there is something in the reverse... (named pipe, perhaps?)

Perhaps something like:
Code:
char *fname="pipeXXXXXX";
mktemp(fname);
int fd=mkfifo(fname,"r");
if(!fork()){
    freopen(fname,"w",stdout);
    execvp(...);
} else {
    //read fd into your buffer
}
This is not nice, so there's probably something much better. I just have no idea right now. Looking at the bash source code for the ` ` operator might give you some clue what I am missing.
 
Old 12-14-2005, 02:53 PM   #3
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
Thanks... I'll look more into it... Can I use popen?
 
Old 12-14-2005, 05:45 PM   #4
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
I modified my code using popen and fopen...

Code:
int main () {
    FILE *pipe_fp, *infile;

    char response[1000];
    char result[1000];
    char *buffer[100];
     int pid, status,file[2];
     printf("Please enter the shell command:  ");
     scanf("%s",&response);
     pid = fork();
     if (pipe(file)<0) { printf("pipe error\n"); exit(1); };
     if (pid < 0) {
                    printf("Unable to create child process, exiting.\n");
                    exit(0);
                    }
     if (pid == 0) {
                printf("I'm the child.\n");
                *buffer = response;
                infile = fopen(*buffer, "rt");
                pipe_fp = popen(result, "w");
                do { 
                fgets(*buffer, 100, infile);
                if(feof(infile)) break;
                fputs(result, pipe_fp);
                } while(!feof(infile));
                fclose(infile); 
                pclose(pipe_fp);
                printf("Display response:\n,%s\n",response);
                
                     } 
     else{
          wait(&status);
          exit(0); 
          }
}
When I type in "ls" for the command I get
"I'm the child.
[pegasus SHELLed ~]$ sh: 4: command not found"

How do I fix this? THanks in advance.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
problem in copying array to char * monil Programming 15 03-13-2005 04:22 PM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
search in char array xxfunkxx Programming 2 12-12-2004 11:23 PM
Char array to int without losing value ? Dimitris Programming 3 01-14-2004 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration