Skip to content

Commit 4a39942

Browse files
committed
Implement fflush for stdout and stderr. #2770.
1 parent b7d0003 commit 4a39942

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/library.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,10 @@ LibraryManager.library = {
24202420
fflush: function(stream) {
24212421
// int fflush(FILE *stream);
24222422
// http://pubs.opengroup.org/onlinepubs/000095399/functions/fflush.html
2423-
// we don't currently perform any user-space buffering of data
2423+
stream = FS.getStreamFromPtr(stream);
2424+
if (stream.stream_ops.flush) {
2425+
stream.stream_ops.flush(stream);
2426+
}
24242427
},
24252428
fgetc__deps: ['$FS', 'fread'],
24262429
fgetc__postset: '_fgetc.ret = allocate([0], "i8", ALLOC_STATIC);',

src/library_tty.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ mergeInto(LibraryManager.library, {
4141
},
4242
close: function(stream) {
4343
// flush any pending line data
44-
if (stream.tty.output.length) {
45-
stream.tty.ops.put_char(stream.tty, {{{ charCode('\n') }}});
46-
}
44+
stream.tty.ops.flush(stream.tty);
45+
},
46+
flush: function(stream) {
47+
stream.tty.ops.flush(stream.tty);
4748
},
4849
read: function(stream, buffer, offset, length, pos /* ignored */) {
4950
if (!stream.tty || !stream.tty.ops.get_char) {
@@ -123,6 +124,12 @@ mergeInto(LibraryManager.library, {
123124
}
124125
return tty.input.shift();
125126
},
127+
flush: function(tty) {
128+
if (tty.output && tty.output.length > 0) {
129+
Module['print'](tty.output.join(''));
130+
tty.output = [];
131+
}
132+
},
126133
put_char: function(tty, val) {
127134
if (val === null || val === {{{ charCode('\n') }}}) {
128135
Module['print'](tty.output.join(''));
@@ -140,7 +147,13 @@ mergeInto(LibraryManager.library, {
140147
} else {
141148
tty.output.push(TTY.utf8.processCChar(val));
142149
}
143-
}
150+
},
151+
flush: function(tty) {
152+
if (tty.output && tty.output.length > 0) {
153+
Module['printErr'](tty.output.join(''));
154+
tty.output = [];
155+
}
156+
}
144157
}
145158
}
146159
});

0 commit comments

Comments
 (0)