aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/dprintf.c
blob: 4746edb86bc838aabaf392ff8157bd1263e1f66e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>

#ifdef USE_HOST_LIBC
#define TEST_FILE "dprintf-host-libc.tmp"
#else
#define TEST_FILE "dprintf.tmp"
#endif

int main() {
	int fd = open(TEST_FILE, O_RDWR | O_CREAT, 0666);
	assert(fd > -1);

	int ret = dprintf(fd, "aaa");
	assert(ret == 3);

	// Check if we can read back the same things.
	// Also checks to see if the fd is still open,
	// which is issue #199.

	off_t seek = lseek(fd, 0, SEEK_SET);
	assert(seek == 0);

	char buf[3];
	ssize_t num_read = read(fd, buf, 3);

	assert(num_read == 3);
	assert(buf[0] == 'a'
		&& buf[1] == 'a'
		&& buf[2] == 'a');

	// All the tests pass, now we can unlink the file.
	ret = unlink(TEST_FILE);
	assert(ret == 0);
	return 0;
}