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 14void fdopen_1() 15{ 16 char tmp[] = "/data/local/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} 35 36void fdopen_2() 37{ 38 FILE *f = fdopen(-1, "w"); 39 TEST(f == NULL); 40 printf("f == %p\n", f); 41} 42 43int main(void) 44{ 45 fdopen_1(); 46 fdopen_2(); 47 return t_status; 48} 49