Example #1
0
/** Searches for a regexp in a file.
 *
 * \param regex The regexp to look for.
 * \param file The file in which to look for the expression.
 * \param ... Positional parameters to the regex.
 *
 * \return True if there is a match; false otherwise. */
bool
atf_utils_grep_file(const char *regex, const char *file, ...)
{
    int fd;
    va_list ap;
    atf_dynstr_t formatted;
    atf_error_t error;

    va_start(ap, file);
    error = atf_dynstr_init_ap(&formatted, regex, ap);
    va_end(ap);
    ATF_REQUIRE(!atf_is_error(error));

    ATF_REQUIRE((fd = open(file, O_RDONLY)) != -1);
    bool found = false;
    char *line = NULL;
    while (!found && (line = atf_utils_readline(fd)) != NULL) {
        found = grep_string(atf_dynstr_cstring(&formatted), line);
        free(line);
    }
    close(fd);

    atf_dynstr_fini(&formatted);

    return found;
}
Example #2
0
bool
grep_file(const char *file, const char *regex, ...)
{
    bool done, found;
    int fd;
    va_list ap;
    atf_dynstr_t formatted;

    va_start(ap, regex);
    RE(atf_dynstr_init_ap(&formatted, regex, ap));
    va_end(ap);

    done = false;
    found = false;
    ATF_REQUIRE((fd = open(file, O_RDONLY)) != -1);
    do {
        atf_dynstr_t line;

        RE(atf_dynstr_init(&line));

        done = read_line(fd, &line);
        if (!done)
            found = grep_string(&line, atf_dynstr_cstring(&formatted));

        atf_dynstr_fini(&line);
    } while (!found && !done);
    close(fd);

    atf_dynstr_fini(&formatted);

    return found;
}
Example #3
0
static
void
init_fmt(atf_dynstr_t *str, const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    RE(atf_dynstr_init_ap(str, fmt, ap));
    va_end(ap);
}
Example #4
0
atf_error_t
atf_text_format_ap(char **dest, const char *fmt, va_list ap)
{
    atf_error_t err;
    atf_dynstr_t tmp;
    va_list ap2;

    va_copy(ap2, ap);
    err = atf_dynstr_init_ap(&tmp, fmt, ap2);
    va_end(ap2);
    if (!atf_is_error(err))
        *dest = atf_dynstr_fini_disown(&tmp);

    return err;
}
Example #5
0
/** Searches for a regexp in a string.
 *
 * \param regex The regexp to look for.
 * \param str The string in which to look for the expression.
 * \param ... Positional parameters to the regex.
 *
 * \return True if there is a match; false otherwise. */
bool
atf_utils_grep_string(const char *regex, const char *str, ...)
{
    bool res;
    va_list ap;
    atf_dynstr_t formatted;
    atf_error_t error;

    va_start(ap, str);
    error = atf_dynstr_init_ap(&formatted, regex, ap);
    va_end(ap);
    ATF_REQUIRE(!atf_is_error(error));

    res = grep_string(atf_dynstr_cstring(&formatted), str);

    atf_dynstr_fini(&formatted);

    return res;
}
Example #6
0
/** Creates a file.
 *
 * \param name Name of the file to create.
 * \param contents Text to write into the created file.
 * \param ... Positional parameters to the contents. */
void
atf_utils_create_file(const char *name, const char *contents, ...)
{
    va_list ap;
    atf_dynstr_t formatted;
    atf_error_t error;

    va_start(ap, contents);
    error = atf_dynstr_init_ap(&formatted, contents, ap);
    va_end(ap);
    ATF_REQUIRE(!atf_is_error(error));

    const int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    ATF_REQUIRE_MSG(fd != -1, "Cannot create file %s", name);
    ATF_REQUIRE(write(fd, atf_dynstr_cstring(&formatted),
                      atf_dynstr_length(&formatted)) != -1);
    close(fd);

    atf_dynstr_fini(&formatted);
}