0

I have a simple program where I take in a command and call execve() to execute the command. I run into this problem where

sortedCmd[100][100];
status = execve(sortedCmd[0],sortedCmd, envp);

will not work (strerror(errno) = No such file or directory), however, the following code work

status = execve(sortedCmd[0],(char *[]){sortedCmd[0], NULL}, envp);

I made sure there is a NULL character there after the last parameter. I guess the problem is char *[] and char[][]. Is there a way to work around this? Since the program can take multiple arguments, I cannot just hardcode it. Please help me, thanks!

UPDATE: The thing is my path is right, one syntax twist would make it work.
  • Possible duplicate of [execve - No such file or directory?](https://stackoverflow.com/questions/10158782/execve-no-such-file-or-directory) – Quentin Laillé Sep 19 '17 at 07:51
  • 2
    Well an array of pointers and a double array are two completely different things. They are not interchangeable like this. Although I do not see how the error with `argv` leads to `No such file`... – Hans Petter Taugbøl Kragset Sep 19 '17 at 08:13
  • From the manpage: _**ENOENT** The file filename or a script or ELF interpreter does not exist, or a shared library needed for file or interpreter cannot be found._ So either your first argument changes too, or the path to the binary is relative and you start it from another directory, or something in your filesystem changes. I cannot see how it should be related to the _second_ argument of execve. – Ctx Sep 19 '17 at 08:56
  • 1
    @ctx using a 2d array as second parameter invokes Undefined Behaviour so we just might have some nasal demons here – Ingo Leonhardt Sep 19 '17 at 09:29
  • @IngoLeonhardt This is not the reason for the observed behaviour, as you probably well know... EFAULT is the return code if there is a problem with memory access. – Ctx Sep 19 '17 at 10:08

0 Answers0