Thread: I need some help using execvp

  1. #1
    Registered User
    Join Date
    May 2023
    Posts
    3

    I need some help using execvp

    I am relativly new to C and I am trying to create a basic shell, I am trying to use the execvp function, passing it user input and trying to execute it, the problem I am having is that it does not execute anything, I followed a pdf from here http://www.csc.villanova.edu/~mprobs...5/3.1-Exec.pdf which explained it for me.

    here is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    static void vsh_cmd_exec(char *cmd[])
    {
        int vsh_fork = fork();
        printf("%s\n", cmd[0]); 
        if(vsh_fork == 0)
        {
            execvp(cmd[0], cmd);
            exit(1);
        }else
            wait(NULL);
    }
    
    static void vsh_loop(void)
    {
        int loop = 1;
        char *cmd = malloc(sizeof(char));
    
        while(loop == 1)
        {
            printf("[vsh]$ ");
            if(fgets(cmd, 1024, stdin) != NULL)
                vsh_cmd_exec(&cmd);
    
            fflush(stdin);
        }
        free(cmd);
    }
    
    int main(int argc, char *argv[])
    {
        if(argc < 2)
            vsh_loop();
    
        return 0;
    }
    right now it just prints the user input twice and jumps back to the prompt, instead of executing the command, I have tried running vim first, then ls but neither seem to do anything. Any help would be much great, thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char *cmd = malloc(sizeof(char));
    You only allocate 1 char, then later on pretend there are 1024 chars available.

    > vsh_cmd_exec(&cmd);
    You only have an array of chars, but you then go on to pretend it's an array of pointers.

    In your document, look at the picture associated with "Build the array argv"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    fflush(stdin) is undefined behavior (i.e., the C standard does not define what it does).
    It is defined in Windows to empty stdin, but in *nix it does nothing.
    Emptying stdin doesn't seem to be needed in your code anyway.

    Your cmd is printed twice since you print it right after the fork so it's printed by both copies of the program.

    A simplistic version (breaks "words" on whitespace only, so no knowledge of escapes (quotes, etc.)) :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <unistd.h>
    #include <sys/wait.h>
     
    void vsh_cmd_exec(char **cmd) {
        int ret = fork();
        if (ret == -1)
            perror("fork");
        else if (ret == 0) {
            execvp(*cmd, cmd);
            perror("exec");
            exit(EXIT_FAILURE);
        }
        else
            wait(NULL);
    }
     
    void vsh_loop(void) {
        while (1) {
            char cmd[1024], *cmdp[64];
            printf("[vsh]$ ");
            if (fgets(cmd, sizeof cmd, stdin) == NULL)
                break;
            unsigned i = 0;
            for (char *c = cmd; i < sizeof cmdp / sizeof *cmdp - 1; ++i) {
                while (isspace(*c)) ++c;
                if (!*c) break;
                cmdp[i] = c;
                while (*c && !isspace(*c)) ++c;
                if (!*c) break;
                *c++ = 0;
            }
            cmdp[i] = NULL;
            if (*cmdp)
                vsh_cmd_exec(cmdp);
        }
    }
     
    int main() {
        vsh_loop();
        putchar('\n');
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    May 2023
    Posts
    3
    Thank you, I think I'll need to brush up on pointers and strings again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execvp help
    By hambergler in forum C Programming
    Replies: 16
    Last Post: 06-02-2010, 09:06 PM
  2. Using execvp
    By kotoko in forum C Programming
    Replies: 21
    Last Post: 01-06-2009, 02:05 PM
  3. some help regarding execvp
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-27-2008, 09:49 PM
  4. execvp not working right
    By Nessarose in forum C Programming
    Replies: 5
    Last Post: 07-18-2005, 05:51 AM
  5. execvp and ampersand?
    By slevytam in forum C Programming
    Replies: 16
    Last Post: 02-02-2005, 08:36 PM

Tags for this Thread