int GrubcfgFindInitrdPaths()

in lsvmutils/grubcfg.c [64:158]


int GrubcfgFindInitrdPaths(
    const void* data,
    const UINTN size,
    StrArr* paths)
{
    int rc = -1;
    const char* text = data;
    const char* textEnd = data + size;
    const char* line;

    /* Check for bad parameters */
    if (!data || !size || !paths)
        goto done;

    /* Clear the array */
    StrArrRelease(paths);

    /* Process line by line */
    while ((line = _GetLine(&text, textEnd)))
    {
        const char* p = line;
        const char* end = text;
        BOOLEAN found = FALSE;

        /* Strip horizontal whitespace */
        p = _SkipWhitespace(p, end);

        /* Skip blank lines and comment lines */
        if (p == end || *p == '#')
            continue;

        /* Remove trailing whitespace */
        while (end != p && Isspace(end[-1]))
            end--;

        /* Skip blank lines */
        if (p == end)
            continue;

        /* Check for "initrd" and "initrdefi" */
        if (Strncmp(p, "initrd", 6) == 0 && Isspace(p[6]))
        {
            p += 7;
            found = TRUE;
        }
        else if (Strncmp(p, "initrdefi", 9) == 0 && Isspace(p[9]))
        {
            p += 10;
            found = TRUE;
        }

        if (!found)
            continue;

        /* Skip over whitespace leading up to initrd name */
        {
            p = _SkipWhitespace(p, end);

            if (*p == '\0')
                continue;
        }

        if (end - p >= PATH_MAX)
        {
            /* If too long */
            goto done;
        }

        /* Append this path to the array */
        {
            char path[PATH_MAX];
            Memcpy(path, p, end - p);
            path[end - p] = '\0';

            /* Insert if not a duplicate */
            if (StrArrFind(paths, path) == (UINTN)-1)
            {
                if (StrArrAppend(paths, path, TRUE) != 0)
                    goto done;
            }
        }
    }

    if (paths->size == 0)
        goto done;

    rc = 0;

done:

    if (rc != 0)
        StrArrRelease(paths);

    return rc;
}