Subversion Repositories HelenOS

Rev

Rev 3265 | Rev 3298 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3265 Rev 3287
Line 59... Line 59...
59
unsigned int try_access(const char *f)
59
unsigned int try_access(const char *f)
60
{
60
{
61
    int fd;
61
    int fd;
62
 
62
 
63
    fd = open(f, O_RDONLY);
63
    fd = open(f, O_RDONLY);
64
    if (fd > 0) {
64
    if (fd > -1) {
65
        close(fd);
65
        close(fd);
66
        return 1;
-
 
67
    } else
-
 
68
        return 0;
66
        return 0;
-
 
67
    } else
-
 
68
        return -1;
69
}
69
}
70
 
70
 
71
/* Returns the full path of "cmd" if cmd is found, else just hand back
71
/* Returns the full path of "cmd" if cmd is found, else just hand back
72
 * cmd as it was presented */
72
 * cmd as it was presented */
73
char *find_command(char *cmd)
73
char *find_command(char *cmd)
74
{
74
{
75
    char *path_orig, *path_tok;
75
    char *path_orig, *path_tok;
76
    char *path[PATH_MAX];
76
    char *path[PATH_MAX];
77
    int rc, n = 0, i = 0;
77
    int n = 0, i = 0;
78
    size_t x = strlen(cmd) + 2;
78
    size_t x = strlen(cmd) + 2;
79
 
79
 
80
    found = (char *)malloc(PATH_MAX);
80
    found = (char *)malloc(PATH_MAX);
81
 
81
 
82
    /* The user has specified the full path, just give it back. Don't test it,
82
    /* The user has specified a full or relative path, just give it back. */
-
 
83
#ifdef HELENOS
83
     * they seem to know what they're doing. */
84
    if (-1 != try_access(cmd))
-
 
85
#else
84
    if (cmd[0] == '/' || cmd [0] == '.')
86
    if (-1 != access(cmd, F_OK))
-
 
87
#endif
-
 
88
    {
85
        return (char *)cmd;
89
        return (char *) cmd;
-
 
90
    }
86
 
91
 
87
#ifdef HELENOS
92
#ifdef HELENOS
88
    path_orig = PATH;
93
    path_orig = PATH;
89
#else
94
#else
90
    path_orig = getenv("PATH");
95
    path_orig = getenv("PATH");
Line 109... Line 114...
109
    /* We now have n places to look for the command */
114
    /* We now have n places to look for the command */
110
    for (i=0; path[i]; i++) {
115
    for (i=0; path[i]; i++) {
111
        memset(found, 0, sizeof(found));
116
        memset(found, 0, sizeof(found));
112
        snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
117
        snprintf(found, PATH_MAX, "%s/%s", path[i], cmd);
113
#ifdef HELENOS
118
#ifdef HELENOS
114
        rc = try_access(found);
119
        if (-1 != try_access(found))
115
#else
120
#else
116
        rc = access(found, F_OK);
121
        if (-1 != access(found, F_OK))
117
#endif
122
#endif
118
        if (rc >= 0) {
123
        {
119
            /* We found it */
-
 
120
            free(path_tok);
124
            free(path_tok);
121
            return (char *) found;
125
            return (char *) found;
122
        }
126
        }
123
    }
127
    }
124
 
128
 
Line 183... Line 187...
183
        return CL_ENOMEM;
187
        return CL_ENOMEM;
184
    }
188
    }
185
 
189
 
186
    free(tmp);
190
    free(tmp);
187
 
191
 
188
    /* No need to go any further if status == 0 */
-
 
189
    /*
-
 
190
    if (status == 0)
-
 
191
        return status;
-
 
192
    */
-
 
193
    /* Decode any errors from execv() (these explain themselves) */
192
    /* Decode any errors from execv() (these explain themselves) */
194
    switch (status) {
193
    switch (status) {
195
    case ENOTDIR:
194
    case ENOTDIR:
196
        cli_error(CL_EFAIL, "%s : No such file or directory", tmp);
195
        cli_error(CL_EFAIL, "%s : No such file or directory", tmp);
197
        status = CL_EFAIL;
196
        status = CL_EFAIL;