Untitled

 avatar
unknown
plain_text
2 years ago
1.6 kB
5
Indexable
char *search_file(char *str)
{
        char *value, *full_path, *cp, *dir;
        size_t len, i, j;
        struct stat st;

        value = _getenv("PATH");
        cp = _strdup(value);
        if (cp == NULL)
            perror("strdup failed");
        dir = strtok(cp, ":");
        while (dir != NULL)
        {
                len = _strlen(dir) + _strlen(str) + 1;
                full_path = malloc(len);
                if (full_path == NULL)
                {
                    perror("malloc failed");
                    free(cp);
                    free(str);
                    cp = NULL;
                    str = NULL;
                    return (NULL);
                }
                for (i = 0; i < strlen(dir); i++)
                        *(full_path + i) = dir[i];
                j = 0;
                while (i < len - 1)
                {
                        *(full_path + i) = str[j];
                        j++;
                        i++;
                }
                *(full_path + i) = '\0';
                if (stat(full_path, &st) == 0)
                {
                        free(str);
                        free(cp);
                        str = NULL;
                        cp = NULL;
                        return (full_path);
                }
                else
                {
                        dir = strtok(NULL, ":");
                        free(full_path);
                        full_path = NULL;
                }
        }
        free(cp);
        free(str);
        return (NULL);
Editor is loading...
Leave a Comment