1570af302Sopenharmony_ci// commit 3af2edee150484940916eba1984f78c3b965dd05 2014-02-07
2570af302Sopenharmony_ci// fix ftello result for append streams with unflushed output
3570af302Sopenharmony_ci#include <errno.h>
4570af302Sopenharmony_ci#include <fcntl.h>
5570af302Sopenharmony_ci#include <stdio.h>
6570af302Sopenharmony_ci#include <stdlib.h>
7570af302Sopenharmony_ci#include <string.h>
8570af302Sopenharmony_ci#include <unistd.h>
9570af302Sopenharmony_ci#include "test.h"
10570af302Sopenharmony_ci
11570af302Sopenharmony_ci#define ASSERT(c) do { \
12570af302Sopenharmony_ci	errno = 0; \
13570af302Sopenharmony_ci	if (!(c)) \
14570af302Sopenharmony_ci		t_error("%s failed (errno: %s)\n", #c, strerror(errno)); \
15570af302Sopenharmony_ci} while(0)
16570af302Sopenharmony_ci
17570af302Sopenharmony_ciint main(void)
18570af302Sopenharmony_ci{
19570af302Sopenharmony_ci	char tmp[] = "/data/local/tmp/testsuite-XXXXXX";
20570af302Sopenharmony_ci	int fd;
21570af302Sopenharmony_ci	FILE *f;
22570af302Sopenharmony_ci	off_t off;
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci	ASSERT((fd = mkstemp(tmp)) > 2);
25570af302Sopenharmony_ci	ASSERT(write(fd, "abcd", 4) == 4);
26570af302Sopenharmony_ci	ASSERT(close(fd) == 0);
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci	ASSERT((fd = open(tmp, O_WRONLY)) > 2);
29570af302Sopenharmony_ci	ASSERT(f = fdopen(fd, "a"));
30570af302Sopenharmony_ci	if (f) {
31570af302Sopenharmony_ci		ASSERT(fwrite("efg", 1, 3, f) == 3);
32570af302Sopenharmony_ci		ASSERT((off = ftello(f)) != -1);
33570af302Sopenharmony_ci		if (off != 7)
34570af302Sopenharmony_ci			t_error("ftello is broken before flush: got %lld, want 7\n", (long long)off);
35570af302Sopenharmony_ci		ASSERT(fflush(f) == 0);
36570af302Sopenharmony_ci		ASSERT((off = ftello(f)) != -1);
37570af302Sopenharmony_ci		if (off != 7)
38570af302Sopenharmony_ci			t_error("ftello is broken after flush: got %lld, want 7\n", (long long)off);
39570af302Sopenharmony_ci		ASSERT(fclose(f) == 0);
40570af302Sopenharmony_ci	}
41570af302Sopenharmony_ci	if (fd > 2)
42570af302Sopenharmony_ci		ASSERT(unlink(tmp) == 0);
43570af302Sopenharmony_ci	return t_status;
44570af302Sopenharmony_ci}
45