Beispiel #1
0
int main(void)
{
	struct rbuf in;
	char buf[4096], *p;
	int fd = open("test/run-term-eof.c", O_RDONLY), len;

	/* This is how many tests you plan to run */
	plan_tests(6);

	/* Grab ourselves for comparison. */
	len = read(fd, buf, sizeof(buf));
	buf[len] = '\0';
	lseek(fd, SEEK_SET, 0);

	/* We have exact-size buffer, which causes problems adding term. */
	rbuf_init(&in, fd, malloc(len), len);
	p = rbuf_read_str(&in, 64, NULL); /* At symbol does not appear. */
	ok1(errno == ENOMEM);
	ok1(!p);
	/* This should succeed... */
	p = rbuf_read_str(&in, 64, realloc);
	ok1(p);
	ok1(strcmp(p, buf) == 0);
	free(in.buf);

	/* Try again. */
	lseek(fd, SEEK_SET, 0);
	rbuf_init(&in, fd, malloc(len), len);
	p = rbuf_read_str(&in, 64, realloc);
	ok1(p);
	ok1(strcmp(p, buf) == 0);
	free(in.buf);

	return exit_status();
}
Beispiel #2
0
int main(void)
{
	struct rbuf in;
	char buf[4096], *p;
	int fd = open("test/run-term-eof.c", O_RDONLY), len;

	/* This is how many tests you plan to run */
	plan_tests(10);

	/* Grab ourselves for comparison. */
	len = read(fd, buf, sizeof(buf));
	buf[len] = '\0';
	lseek(fd, SEEK_SET, 0);

	/* We have exact-size buffer, which causes problems adding term. */
	rbuf_init(&in, fd, malloc(len), len, test_realloc);
	test_realloc_fail = true;
	p = rbuf_read_str(&in, 64); /* At symbol does not appear. */
	ok1(errno == ENOMEM);
	ok1(!p);
	/* This should succeed... */
	test_realloc_fail = false;
	p = rbuf_read_str(&in, 64);
	ok1(p);
	ok1(strcmp(p, buf) == 0);
	ok1(rbuf_start(&in) == p + strlen(p));
	free(rbuf_cleanup(&in));

	/* Try again. */
	lseek(fd, SEEK_SET, 0);
	rbuf_init(&in, fd, malloc(len), len, test_realloc);
	p = rbuf_read_str(&in, 64);
	ok1(p);
	ok1(strcmp(p, buf) == 0);
	ok1(rbuf_start(&in) == p + strlen(p));
	free(rbuf_cleanup(&in));

	/* Normal case, we get rbuf_start after nul */
	lseek(fd, SEEK_SET, 0);
	rbuf_init(&in, fd, NULL, 0, test_realloc);
	p = rbuf_read_str(&in, '^');
	ok1(p);
	ok1(rbuf_start(&in) == p + strlen(p) + 1);
	free(rbuf_cleanup(&in));

	return exit_status();
}
Beispiel #3
0
lines_from_cmd(const void *ctx, const char *format, ...)
{
	va_list ap;
	char *cmd;
	FILE *p;
	struct rbuf in;

	va_start(ap, format);
	cmd = tal_vfmt(ctx, format, ap);
	va_end(ap);

	p = popen(cmd, "r");
	if (!p)
		err(1, "Executing '%s'", cmd);

	/* FIXME: Use rbuf_read_str(&in, '\n') rather than strsplit! */
	rbuf_init(&in, fileno(p), tal_arr(ctx, char, 0), 0);
	if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
		err(1, "Reading from '%s'", cmd);
	pclose(p);

	return tal_strsplit(ctx, in.buf, "\n", STR_EMPTY_OK);
}