So I am building my own shell and one of the commands I want is the "cat" function. To do this, I am using the execv function and passing in the correct bin path of "cat" as well as the file name that I want to concatenate and print to the terminal.
The code below works perfectly fine, but I am hard coding in the file name that I want to "cat"
void cat(char *fileName)
{
char *bin_path = "/bin/cat";
printf("%d\n", strncmp(fileName, "junk.txt", strlen("junk.txt"))); // returns 0, meaning equal values
char *args[] = {bin_path, "junk.txt", NULL};
execv(bin_path, args);
}
The output from above is shown below argument is hardcoded in
But I want to be able to make this function dynamic and have the file name be passed into the function, so that's why I have the variable fileName as a parameter. You can see that I am comparing the string literal "junk.txt" with fileName variable and it returns 0, meaning they are EQUAL! But when I pass in the variable fileName to the execv function, it doesn't work. Please see my broken code below
void cat(char *fileName)
{
char *bin_path = "/bin/cat";
printf("%d\n", strncmp(fileName, "junk.txt", strlen("junk.txt"))); // returns 0, meaning equal values
char *args[] = {bin_path, fileName, NULL};
execv(bin_path, args);
}
no file or directory that matches that name
Anyone have any idea why that is?