You pass one character as the command name, and the name string as the beginning of the argument list - as if the prototype for execv() was int execv(char cmd, char *args) .
Actual prototype: int execv(char *cmd, char **args) , so you need:
char *args[2] = { fileName, 0 }; execv(args[0], args);
I assume that you set fileName to a meaningful value somewhere - this is not shown. For example, it could be "./local_program" . It will be considered as the path to the executable file.
If you want to read the name, you can use fgets() or getline() , but you need to remove the new line:
if (fgets(fileName, sizeof(fileName), stdin) != 0) { fileName[strcspn(fileName, "\n")] = '\0'; …as before… }
or
char *fileName = 0; size_t length = 0; if (getline(&fileName, &length, stdin) != -1) { fileName[strcspn(fileName, "\n")] = '\0'; …as before… } free(fileName);
Using strcspn() avoids the special case of overly long command lines, so there is no new line in fileName . Note that this does not try to break the input string into the command name and arguments in spaces or something like that. This is the next level of shell implementation:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(void) { char fileName[256]; if (fgets(fileName, sizeof(fileName), stdin) != 0) { fileName[strcspn(fileName, "\n")] = '\0'; char *args[129]; char **argv = args; char *cmd = fileName; const char *whisp = " \t\f\r\b\n"; /* Ugh — strtok()? OK while only handling white space separators */ char *token; while ((token = strtok(cmd, whisp)) != 0) { *argv++ = token; cmd = 0; } *argv = 0; execv(args[0], args); fprintf(stderr, "Oops!\n"); } return 1; }
I do not need to check the args array overflow, since 256 input characters, minus the terminating zero, cannot be divided to create more than 128 single-character arguments, each of which is separated from the neighboring ones by a white space. Using strtok() is temporary tape help. As soon as you need to deal with the real shell syntax (pipes, I / O redirects, quoted strings with spaces, etc.), strtok() is a bad tool. (It is strtok() is also an incorrect function to use in any library function. Use POSIX strtok_r() on Unix or Microsoft strtok_s() on Windows if you must use strtok() -like in library code.)