LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Storing the result of execvp to a char array in C (https://www.linuxquestions.org/questions/programming-9/storing-the-result-of-execvp-to-a-char-array-in-c-392807/)

kponenation 12-14-2005 12:11 PM

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

Matir 12-14-2005 01:37 PM

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.

kponenation 12-14-2005 02:53 PM

Thanks... I'll look more into it... Can I use popen?

kponenation 12-14-2005 05:45 PM

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.


All times are GMT -5. The time now is 04:15 PM.