1 // RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s 2 3 // UNSUPPORTED: solaris 4 5 #include <stdio.h> 6 7 void print_something() { 8 for (size_t i = 0; i < 10 * BUFSIZ; i++) 9 printf("Hello world %zu\n", i); 10 } 11 12 void print_one_byte(char *buf) { 13 printf("First byte is %c\n", buf[0]); 14 } 15 16 void test_setbuf() { 17 char buf[BUFSIZ]; 18 19 setbuf(stdout, NULL); 20 21 print_something(); 22 23 setbuf(stdout, buf); 24 25 print_something(); 26 27 print_one_byte(buf); 28 29 setbuf(stdout, NULL); 30 } 31 32 void test_setbuffer() { 33 char buf[BUFSIZ]; 34 35 setbuffer(stdout, NULL, 0); 36 37 print_something(); 38 39 // Ensure that interceptor reads correct size 40 // (not BUFSIZ as by default, hence BUFSIZ/2). 41 setbuffer(stdout, buf, BUFSIZ / 2); 42 43 print_something(); 44 45 print_one_byte(buf); 46 47 setbuffer(stdout, NULL, 0); 48 } 49 50 void test_setlinebuf() { 51 setlinebuf(stdout); 52 53 print_something(); 54 } 55 56 void test_setvbuf() { 57 char buf[BUFSIZ]; 58 59 setvbuf(stdout, NULL, _IONBF, 0); 60 61 print_something(); 62 63 setvbuf(stdout, buf, _IOLBF, BUFSIZ); 64 65 print_something(); 66 67 print_one_byte(buf); 68 69 setvbuf(stdout, buf, _IOFBF, BUFSIZ); 70 71 print_something(); 72 73 print_one_byte(buf); 74 75 setvbuf(stdout, NULL, _IONBF, 0); 76 } 77 78 int main(void) { 79 printf("setvbuf\n"); 80 81 test_setbuf(); 82 test_setbuffer(); 83 test_setlinebuf(); 84 test_setvbuf(); 85 86 // CHECK: setvbuf 87 88 return 0; 89 } 90