tags:

views:

1034

answers:

3

I'm writing my own unix terminal and I'm running into a problem executing commands:

First I take the user input and store it into a buffer, then I separate the words and store them into my argv[] array. i.e command is "firefox" to launch firefox which is stored in argv[0]

How do I launch the command? This is what I'm trying to do, but I'm getting errors:

void launchProcess(char *command[], char *file){
        pid_t pid;
        pid = fork();
        if (pid == -1) {
                perror("Error when forking");
                exit(EXIT_FAILURE);
        }
        if (pid == 0){            
          fprintf(stderr, "Child pid = %d\n", getpid());
          execv(file, command);
         }

When I call:

launchProcess(commandArgv, "STANDARD");

I get the following errors: error: conflicting types for launchProcess

+8  A: 

If you have a conflicting type error, you should make sure that the function you listed has the same type as its declaration.

Also, you probably already know, but execv requires a fully qualified path to the executable, so a value like "STANDARD" isn't going to work; use execvp if you want it to use the PATH variable to determine the location of the binary. You should also make sure the last value in the argv array is NULL. Finally, make sure to check the return value of execv; there is a definite possibility it can fail, e.g., if the user tries to execute a program that doesn't exist.

Jay Conrod
A: 

These notes on exec and wait might help some.

thepocketwade
+2  A: 

You need to prototype the function: add "void launchProcess(char *command[], char *file);" above your main function.

Your data types look correct, but based on the parameter names: "file" and "command", It looks like you might be using the function in the wrong way.

Here's an example of executing the ls function with no arguments.

char *args[] = { NULL }; execv("/bin/ls", args);

idea