Changes in uspace/app/bdsh/exec.c [587867a:31872f7] in mainline


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/exec.c

    r587867a r31872f7  
    4747#include "errors.h"
    4848
    49 static errno_t find_command(char *, char **);
     49/* FIXME: Just have find_command() return an allocated string */
     50static char *found;
     51
     52static char *find_command(char *);
    5053static int try_access(const char *);
    5154
     
    6568}
    6669
    67 /** Returns EOK if no internal failure or else ENOMEM
     70/** Returns the full path of "cmd" if cmd is found
    6871 *
    69  * When the parameter `cmd` can be found then the absolute path will be set in `found`.
    70  * Or else `found` will be NULL.
    71  * `found` will be newly allocated and must be freed by the caller
     72 * else just hand back cmd as it was presented
    7273 */
    73 static errno_t find_command(char *cmd, char **found)
     74static char *find_command(char *cmd)
    7475{
     76        size_t i;
     77
     78        found = (char *)malloc(PATH_MAX);
     79
    7580        /* The user has specified a full or relative path, just give it back. */
    76         if (is_path(cmd)) {
    77                 if (-1 != try_access(cmd)) {
    78                         *found = str_dup(cmd);
    79                         return EOK;
    80                 }
    81 
    82                 *found = NULL;
    83                 return EOK;
    84         }
    85 
    86         *found = (char *)malloc(PATH_MAX);
    87         if (*found == NULL) {
    88                 return ENOMEM;
     81        if (-1 != try_access(cmd)) {
     82                return (char *) cmd;
    8983        }
    9084
    9185        /* We now have n places to look for the command */
    92         size_t i;
    93         size_t cmd_length = str_length(cmd);
    9486        for (i = 0; search_dir[i] != NULL; i++) {
    95                 if (str_length(search_dir[i]) + cmd_length + 2 > PATH_MAX) {
    96                         free(*found);
    97                         return ENOMEM;
    98                 }
    99 
    100                 memset(*found, 0, PATH_MAX);
    101                 snprintf(*found, PATH_MAX, "%s/%s", search_dir[i], cmd);
    102                 if (-1 != try_access(*found)) {
    103                         return EOK;
     87                memset(found, 0, PATH_MAX);
     88                snprintf(found, PATH_MAX, "%s/%s", search_dir[i], cmd);
     89                if (-1 != try_access(found)) {
     90                        return (char *) found;
    10491                }
    10592        }
    106         free(*found);
    107         *found = NULL;
    10893
    109         /* We didn't find it, return NULL */
    110         return EOK;
     94        /* We didn't find it, just give it back as-is. */
     95        return (char *) cmd;
    11196}
    11297
     
    122107        FILE *files[3];
    123108
    124         rc = find_command(cmd, &tmp);
    125         if (rc != EOK) {
    126                 cli_error(CL_ENOMEM, "%s: failure executing find_command()", progname);
    127                 return 1;
    128         }
    129 
    130         if (tmp == NULL) {
    131                 cli_error(CL_EEXEC, "%s: Command not found '%s'", progname, cmd);
    132                 return 1;
    133         }
     109        tmp = str_dup(find_command(cmd));
     110        free(found);
    134111
    135112        files[0] = io->stdin;
Note: See TracChangeset for help on using the changeset viewer.