I have been given an assignment where I have to create a small shell that the user will be able to enter in a path and 2 parameters. The shell must be able to process this input and run the program at the given path using execvp, and must also parse the parameters. I must use Fork() and Wait() along with that.

I've been able to get the users input, and have it print out what that input was. The problem lies in when I try to do the execvp() method, the program says that it was unable to run my program, or it will just wait for another command but not do anything. The only time I was able to get my shell to start a new program was when I used execl() method instead and I entered the path to the program staticly (instead of getting user input, just set the path manually).

Here is the code I'm using at the moment:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#define BUFFER_MAX (50)
#define MAX_ARGS (4) //Maximum parameters

main() {

    struct command_struct {
        char *name; //Name of the path
        char *argv[MAX_ARGS]; //The parameters
        char input[BUFFER_MAX]; //Users input holder
    } *myCmd;
    char *out = "<Command>";

    //Intro:
    printf("\t\tWelcome to the shell command line interpreter!\n"
            "\t\t    Please enter your command to execute:\n%s", out);

    //Get user input:
    while (fgets(myCmd->input, sizeof (myCmd->input), stdin) != NULL) {
        //Get name of the command:
        strncpy(myCmd->name, strtok(myCmd->input, " "), sizeof (myCmd->input) - 1);
        //Construct the parameters:
        myCmd->argv[0] = strtok(NULL, " ");
        myCmd->argv[1] = strtok(NULL, " ");

        pid_t pid = fork();

        //FORK:
        if (pid < 0) {
            printf("%sError running %s", out, myCmd->name);
        } else if (pid == 0) { //Child
            //Execute the command with params:
            execvp(myCmd->name, myCmd->argv);

            perror("execvp failed to run");
            exit(-1);
        } else { //Parent
            wait((int *) 0);
        }
        printf("%s", out);
    }
}
//EOF

I would be very grateful with any information I can receive to better understand how to create this shell! Thank you

>>I must use Fork() and Wait()
Why? Sounds like over complication of the problem.

>>myComd is an unallocated pointer which can not be used until you allocate memory to it. IMHO don't make it a pointer; remove the star and replace pointer notation "->" with dot notation "."

>> line 7: #define BUFFER_MAX (50)
Paths can be up to 255 characters.

>> line 26: strncpy(myCmd->name,
Again, you're program is scribbling all over memory because name is an unallocated pointer.

>> myCmd->argv[0] = strtok(NULL, " ");
Wrong. argv[0] must be the name of the program you want to execute -- it can also contain the path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.