regex memory corruption regression test
[libc-test] / src / regression / ftello-unflushed-append.c
1 // commit 3af2edee150484940916eba1984f78c3b965dd05 2014-02-07
2 // fix ftello result for append streams with unflushed output
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include "test.h"
10
11 #define ASSERT(c) do { \
12         errno = 0; \
13         if (!(c)) \
14                 t_error("%s failed (errno: %s)\n", #c, strerror(errno)); \
15 } while(0)
16
17 int main(void)
18 {
19         char tmp[] = "/tmp/testsuite-XXXXXX";
20         int fd;
21         FILE *f;
22         off_t off;
23
24         ASSERT((fd = mkstemp(tmp)) > 2);
25         ASSERT(write(fd, "abcd", 4) == 4);
26         ASSERT(close(fd) == 0);
27
28         ASSERT((fd = open(tmp, O_WRONLY)) > 2);
29         ASSERT(f = fdopen(fd, "a"));
30         if (f) {
31                 ASSERT(fwrite("efg", 1, 3, f) == 3);
32                 ASSERT((off = ftello(f)) != -1);
33                 if (off != 7)
34                         t_error("ftello is broken before flush: got %lld, want 7\n", (long long)off);
35                 ASSERT(fflush(f) == 0);
36                 ASSERT((off = ftello(f)) != -1);
37                 if (off != 7)
38                         t_error("ftello is broken after flush: got %lld, want 7\n", (long long)off);
39                 ASSERT(fclose(f) == 0);
40         }
41         if (fd > 2)
42                 ASSERT(unlink(tmp) == 0);
43         return t_status;
44 }