1 // RUN: %clangxx -g %s -o %t && %run %t | FileCheck %s
3 // CHECK: READ CALLED; len={{[0-9]*}}
4 // CHECK-NEXT: READ: test
5 // CHECK-NEXT: WRITE CALLED: test
6 // CHECK-NEXT: READ CALLED; len={{[0-9]*}}
7 // CHECK-NEXT: READ: test
8 // CHECK-NEXT: WRITE CALLED: test
9 // CHECK-NEXT: CLOSE CALLED
10 // CHECK-NEXT: SEEK CALLED; off=100, whence=0
11 // CHECK-NEXT: READ CALLED; len={{[0-9]*}}
12 // CHECK-NEXT: READ: test
14 // UNSUPPORTED: darwin, target={{.*(linux|solaris).*}}
23 int f_read(void *cookie
, char *buf
, int len
) {
24 assert(cookie
== &cookie_var
);
26 printf("READ CALLED; len=%d\n", len
);
27 return strlcpy(buf
, "test\n", len
);
30 int f_write(void *cookie
, const char *buf
, int len
) {
31 assert(cookie
== &cookie_var
);
32 char *data
= strndup(buf
, len
);
34 printf("WRITE CALLED: %s\n", data
);
39 off_t
f_seek(void *cookie
, off_t off
, int whence
) {
40 assert(cookie
== &cookie_var
);
41 assert(whence
== SEEK_SET
);
42 printf("SEEK CALLED; off=%d, whence=%d\n", (int)off
, whence
);
46 int f_close(void *cookie
) {
47 assert(cookie
== &cookie_var
);
48 printf("CLOSE CALLED\n");
56 // 1. read-only variant
57 fp
= fropen(&cookie_var
, f_read
);
59 // verify that fileno() does not crash or report nonsense
60 assert(fileno(fp
) == -1);
61 assert(fgets(buf
, sizeof(buf
), fp
));
62 printf("READ: %s", buf
);
65 // 2. write-only variant
66 fp
= fwopen(&cookie_var
, f_write
);
68 assert(fileno(fp
) == -1);
69 assert(fputs("test", fp
) >= 0);
72 // 3. read+write+close
73 fp
= funopen(&cookie_var
, f_read
, f_write
, NULL
, f_close
);
75 assert(fileno(fp
) == -1);
76 assert(fgets(buf
, sizeof(buf
), fp
));
77 printf("READ: %s", buf
);
78 assert(fputs("test", fp
) >= 0);
82 fp
= funopen(&cookie_var
, f_read
, NULL
, f_seek
, NULL
);
84 assert(fileno(fp
) == -1);
85 assert(fseek(fp
, 100, SEEK_SET
) == 0);
86 assert(fgets(buf
, sizeof(buf
), fp
));
87 printf("READ: %s", buf
);