Luna/apps/buffer-test.cpp

28 lines
690 B
C++
Raw Normal View History

2023-07-21 22:02:12 +00:00
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
2023-07-22 09:17:51 +00:00
fprintf(stderr, "Writing incomplete line to stdout (_IOLBF=%d)...\n", stdout->_buf.mode);
2023-07-21 22:02:12 +00:00
fputs("hi!", stdout);
sleep(3);
putchar('\n');
fprintf(stderr, "Incomplete line should have been written.\n");
FILE* f = fopen("/dev/console", "w+");
assert(f);
assert(setvbuf(f, NULL, _IOFBF, 0) == 0);
2023-07-22 09:17:51 +00:00
fprintf(stderr, "Writing long text to file (_IOFBF=%d)...\n", f->_buf.mode);
2023-07-21 22:02:12 +00:00
fputs("Hello world!\nHow are you doing!\nThis is a test for many lines of buffering.\n", f);
sleep(3);
fflush(f);
fprintf(stderr, "Long text should have been written.\n");
fclose(f);
}