Hello,
Svante Signell, on ven. 22 sept. 2017 16:24:11 +0200, wrote:
> Final version?
Almost there :)
> -/* Replace the current process, executing FILE_NAME with arguments ARGV and
> - environment ENVP. ARGV and ENVP are terminated by NULL pointers. */
> +/* Replace the current process, executing ABS_NAME, a canonicalized
> + absolute path name of FILE_NAME, with arguments ARGV and
> + environment ENVP. ARGV and ENVP are terminated by NULL
> + pointers. */
> int
> __execve (const char *file_name, char *const argv[], char *const envp[])
> {
> error_t err;
> - file_t file = __file_name_lookup (file_name, O_EXEC, 0);
> + char *cwd = NULL, *concat_name = NULL;
> + const char *abs_name;
>
> + file_t file = __file_name_lookup (file_name, O_EXEC, 0);
This creates a file_t, so return paths have to deallocate it (just like
it is done at the end of the function).
> if (file == MACH_PORT_NULL)
> return -1;
>
> + /* Absolute path */
> + if (file_name[0] == '/')
> + {
> + abs_name = file_name;
> + }
> + /* Relative path */
> + else
> + {
> + cwd = getcwd (NULL, 0);
> + if (cwd == NULL)
Here
> + return -1;
> + int res = asprintf (&concat_name, "%s/%s", cwd, file_name);
> + if (res == -1)
> + {
and there
> + free (cwd);
> + return -1;
> + }
> + abs_name = concat_name;
> + free (cwd);
> + }
Along the way, you could move the declaration f the char *cwd variable
on the cwd = getcwd() line, it'll be just a little bit better :)
Samuel