This line:
// makes a two element array
char *commands[] = {*command_tok, NULL};
is wrong.
It's creating a new array consisting of just two elements, the first of which is command_tok[0]. Instead what you want is
command_tok[i] = NULL
execvp(command_tok[0], command_tok);
Also
command[strlen(command)-1] = '\0'
is nonsensical: strlen finds the length by searching for the null byte, which you then set to null again.
Here's my version:
#include <string.h>
#include <unistd.h>
#include <cstdio>
static const int BUFFSIZE=200;
int main(int argc, char* argv[]) {
char command[BUFFSIZE] = { 0 };
fgets(command, BUFFSIZE, stdin);
// remove trailing new line if present
commmand[strcspn(command, "\n")] = 0;
char *p = strtok(command, " ");
char *command_tok[BUFFSIZE];
int i = 0;
while (p) {
command_tok[i++] = p;
p = strtok(NULL, " ");
}
command_tok[i] = NULL;
execvp(command_tok[0], command_tok);
}
The one-liner for removing trailing new line came from Removing trailing newline character from fgets() input