简体   繁体   中英

execvp() system call not executing

char *args[41];
char str[41], teststr[41];
const char delimiter[2]=" ";
memcpy(teststr, str, sizeof(str));
args[i]=strtok(teststr, delimiter);
while(args[i]!=NULL)
{
    printf("args[%d]=%s\n", i, args[i]);
    i++;
    args[i]=strtok(NULL, delimiter);
}

This is a code I used to intialize args[].

Below code is to execute execvp() system call.

pid=fork();
if(pid==0)
{
    execvp(args[0], args);
}

When I run the code, execvp runs few commands. Such as when I try to execute 'ls' command, it will work but when I try to run 'date' command or 'cd' command, it does not work. And when I try to execute 'cat' command, prompt does not show anything and at the same time it does not come out of it.

Looks like you have issues with how you're getting your arguments, although since you don't show your input, it's hard to tell. strtok() modifies the string you pass to it, so storing pointers to that string you're modifying looks like trouble.

This will do what you want:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_BUFFER_LEN 1024
#define MAX_ARGS 100

int main(void) {
    char command[MAX_BUFFER_LEN];

    printf("myshell> ");
    fflush(stdout);
    fgets(command, MAX_BUFFER_LEN, stdin); 
    command[strlen(command) - 1] = '\0';

    char * args[MAX_ARGS];
    char * temparg;
    int i = 0;

    temparg = strtok(command, " ");
    while ( temparg ) {
        args[i] = strdup(temparg);
        ++i;
        temparg = strtok(NULL, " ");
    }

    i = 0;
    while ( args[i] != NULL ) {
        printf("Argument %d: %s\n", i + 1, args[i]);
        ++i;
    }

    pid_t my_pid = fork();
    if ( my_pid == 0 ) {
        execvp(args[0], args);
    }

    wait(NULL);

    return EXIT_SUCCESS;
}

which outputs:

paul@MacBook:~/Documents/src/scratch$ ./exectest
myshell> cat exectest.c
Argument 1: cat
Argument 2: exectest.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_BUFFER_LEN 1024
#define MAX_ARGS 100

int main(void) {
    char command[MAX_BUFFER_LEN];

    printf("myshell> ");
    fflush(stdout);
    fgets(command, MAX_BUFFER_LEN, stdin); 
    command[strlen(command) - 1] = '\0';

    char * args[MAX_ARGS];
    char * temparg;
    int i = 0;

    temparg = strtok(command, " ");
    while ( temparg ) {
        args[i] = strdup(temparg);
        ++i;
        temparg = strtok(NULL, " ");
    }

    i = 0;
    while ( args[i] != NULL ) {
        printf("Argument %d: %s\n", i + 1, args[i]);
        ++i;
    }

    pid_t my_pid = fork();
    if ( my_pid == 0 ) {
        execvp(args[0], args);
    }

    wait(NULL);

    return EXIT_SUCCESS;
}

paul@MacBook:~/Documents/src/scratch$ 

Some error-checking omitted for brevity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM