I\'m trying to write a shell, and part of its construction is executing code from a user-inputted string (buffer). However, when I attempt to execvp the string with additional i
The argument buffer
is wrong. The second argument of execvp
is an array of pointers. With this cast, you are hidding a compiler warning, but it does not work.
I can see a couple of potential problems here.
First, you're allocating space with malloc
(meaning the contents aren't initialized), but immediately using strcat
to write to it. Unless (by whatever change) the first character is a '\0'
, that's going leave you with a string starting with garbage, followed by data you're trying to put there. It would also (very easily) lead to writing past the end of the buffer, giving undefined behavior.
If it were up to me, I think I'd use sprintf
instead of strcat
. At least what you've shown would work out to: sprintf(str, "./%43s.out", cmd);
Two problems: