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