1570af302Sopenharmony_ci#include <errno.h> 2570af302Sopenharmony_ci#include <stdio.h> 3570af302Sopenharmony_ci#include <stdlib.h> 4570af302Sopenharmony_ci#include <string.h> 5570af302Sopenharmony_ci#include <unistd.h> 6570af302Sopenharmony_ci#include "test.h" 7570af302Sopenharmony_ci 8570af302Sopenharmony_ci#define TEST(c) do { \ 9570af302Sopenharmony_ci errno = 0; \ 10570af302Sopenharmony_ci if (!(c)) \ 11570af302Sopenharmony_ci t_error("%s failed (errno = %d)\n", #c, errno); \ 12570af302Sopenharmony_ci} while(0) 13570af302Sopenharmony_ci 14570af302Sopenharmony_civoid fdopen_1() 15570af302Sopenharmony_ci{ 16570af302Sopenharmony_ci char tmp[] = "/data/local/tmp/testsuite-XXXXXX"; 17570af302Sopenharmony_ci char foo[6]; 18570af302Sopenharmony_ci int fd; 19570af302Sopenharmony_ci FILE *f; 20570af302Sopenharmony_ci 21570af302Sopenharmony_ci TEST((fd = mkstemp(tmp)) > 2); 22570af302Sopenharmony_ci TEST(write(fd, "hello", 6)==6); 23570af302Sopenharmony_ci TEST(f = fdopen(fd, "rb")); 24570af302Sopenharmony_ci if (f) { 25570af302Sopenharmony_ci TEST(ftello(f)==6); 26570af302Sopenharmony_ci TEST(fseeko(f, 0, SEEK_SET)==0); 27570af302Sopenharmony_ci TEST(fgets(foo, sizeof foo, f)); 28570af302Sopenharmony_ci if (strcmp(foo,"hello") != 0) 29570af302Sopenharmony_ci t_error("fgets read back: \"%s\"; wanted: \"hello\"\n", foo); 30570af302Sopenharmony_ci fclose(f); 31570af302Sopenharmony_ci } 32570af302Sopenharmony_ci if (fd > 2) 33570af302Sopenharmony_ci TEST(unlink(tmp) != -1); 34570af302Sopenharmony_ci} 35570af302Sopenharmony_ci 36570af302Sopenharmony_civoid fdopen_2() 37570af302Sopenharmony_ci{ 38570af302Sopenharmony_ci FILE *f = fdopen(-1, "w"); 39570af302Sopenharmony_ci TEST(f == NULL); 40570af302Sopenharmony_ci printf("f == %p\n", f); 41570af302Sopenharmony_ci} 42570af302Sopenharmony_ci 43570af302Sopenharmony_ciint main(void) 44570af302Sopenharmony_ci{ 45570af302Sopenharmony_ci fdopen_1(); 46570af302Sopenharmony_ci fdopen_2(); 47570af302Sopenharmony_ci return t_status; 48570af302Sopenharmony_ci} 49