build system updates, port libc-testsuit changes
[libc-test] / src / stdio / fdopen.c
1 #define _XOPEN_SOURCE 700
2 #include "test.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8
9 #define TEST(c) do { \
10         errno = 0; \
11         if (!(c)) \
12                 error("%s failed (errno = %d)\n", #c, errno); \
13 } while(0)
14
15 void test_fdopen(void)
16 {
17         char tmp[] = "/tmp/testsuite-XXXXXX";
18         char foo[6];
19         int fd;
20         FILE *f;
21
22         TEST((fd = mkstemp(tmp)) > 2);
23         TEST(write(fd, "hello", 6)==6);
24         TEST(f = fdopen(fd, "rb"));
25         if (f) {
26                 TEST(ftello(f)==6);
27                 TEST(fseeko(f, 0, SEEK_SET)==0);
28                 TEST(fgets(foo, sizeof foo, f));
29                 if (strcmp(foo,"hello") != 0)
30                         error("fgets read back wrong message: \"%s\" wanted: \"hello\"\n", foo);
31                 fclose(f);
32         }
33         if (fd > 2)
34                 TEST(unlink(tmp) != -1);
35 }