40,12 → 40,6 |
#include <string.h> |
#include <fcntl.h> |
|
#ifndef HELENOS |
#include <linux/limits.h> |
#include <sys/errno.h> |
#include <sys/wait.h> |
#endif |
|
#include "config.h" |
#include "util.h" |
#include "exec.h" |
81,23 → 75,10 |
found = (char *)malloc(PATH_MAX); |
|
/* The user has specified a full or relative path, just give it back. */ |
#ifdef HELENOS |
if (-1 != try_access(cmd)) |
#else |
if (-1 != access(cmd, F_OK)) |
#endif |
{ |
if (-1 != try_access(cmd)) { |
return (char *) cmd; |
} |
|
#ifdef HELENOS |
path_orig = PATH; |
#else |
path_orig = getenv("PATH"); |
if (NULL == path_orig) |
path_orig = PATH; |
#endif |
|
path_tok = cli_strdup(path_orig); |
|
/* Extract the PATH env to a path[] array */ |
116,12 → 97,7 |
for (i=0; path[i]; i++) { |
memset(found, 0, sizeof(found)); |
snprintf(found, PATH_MAX, "%s/%s", path[i], cmd); |
#ifdef HELENOS |
if (-1 != try_access(found)) |
#else |
if (-1 != access(found, F_OK)) |
#endif |
{ |
if (-1 != try_access(found)) { |
free(path_tok); |
return (char *) found; |
} |
133,7 → 109,6 |
return (char *) cmd; |
} |
|
#ifdef HELENOS |
task_id_t try_exec(char *cmd, char **argv) |
{ |
task_id_t tid; |
155,74 → 130,3 |
|
return tid; |
} |
|
#else |
int try_exec(char *cmd, char **argv) |
{ |
char *tmp; |
pid_t pid; |
int status; |
|
/* Copy the result of allocated 'found' */ |
tmp = cli_strdup(find_command(cmd)); |
/* free the pointer, no longer needed */ |
free(found); |
|
if (-1 == (access(tmp, F_OK))) { |
cli_error(CL_EFAIL, |
"%s : No such external, modular or builtin command", tmp); |
free(tmp); |
return CL_EFAIL; |
} |
|
/* Create a child to run the program */ |
pid = fork(); |
if (pid == 0) { |
execv(tmp, argv); |
exit(errno); |
} else if (pid > 0) { |
/* Block until we get a PID (and result) */ |
wait(&status); |
status = status / 256; |
} else { |
/* Could not fork, ulimit or out of memory */ |
cli_error(CL_ENOMEM, "Could not fork"); |
free(tmp); |
return CL_ENOMEM; |
} |
|
free(tmp); |
|
/* Decode any errors from execv() (these explain themselves) */ |
switch (status) { |
case ENOTDIR: |
cli_error(CL_EFAIL, "%s : No such file or directory", tmp); |
status = CL_EFAIL; |
break; |
case EISDIR: |
cli_error(CL_EFAIL, "%s : Command is a directory", tmp); |
status = CL_EFAIL; |
break; |
case EACCES: |
cli_error(CL_EPERM, "%s :", tmp); |
status = CL_EFAIL; |
break; |
case ENOMEM: |
cli_error(CL_ENOMEM, "%s :", tmp); |
status = CL_EFAIL; |
break; |
case ENAMETOOLONG: |
cli_error(CL_EFAIL, "Argument list too long"); |
status = CL_EFAIL; |
break; |
case EFAULT: |
cli_error(CL_EFAIL, "Invalid argument pointer\n(please report " |
"this to %s)", PACKAGE_BUGREPORT); |
status = CL_EFAIL; |
break; |
} |
|
return status; |
} |
|
#endif /* HELENOS */ |