ATF_TC_BODY(readline__some, tc) { const char *l1 = "First line with % formatting % characters %"; const char *l2 = "Second line; much longer than the first one"; const char *l3 = "Last line, without terminator"; atf_utils_create_file("test.txt", "%s\n%s\n%s", l1, l2, l3); const int fd = open("test.txt", O_RDONLY); ATF_REQUIRE(fd != -1); char *line; line = atf_utils_readline(fd); ATF_REQUIRE_STREQ(l1, line); free(line); line = atf_utils_readline(fd); ATF_REQUIRE_STREQ(l2, line); free(line); line = atf_utils_readline(fd); ATF_REQUIRE_STREQ(l3, line); free(line); close(fd); }
/** 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; }
ATF_TC_BODY(readline__none, tc) { atf_utils_create_file("empty.txt", "%s", ""); const int fd = open("empty.txt", O_RDONLY); ATF_REQUIRE(fd != -1); ATF_REQUIRE(atf_utils_readline(fd) == NULL); close(fd); }
static void check_line(int fd, const char *exp) { char *line = atf_utils_readline(fd); ATF_CHECK(line != NULL); ATF_CHECK_STREQ_MSG(exp, line, "read: '%s', expected: '%s'", line, exp); free(line); }