Index: uspace/lib/libc/generic/io/io.c
===================================================================
--- uspace/lib/libc/generic/io/io.c	(revision ac47b7c2b6e75a9b9aa60b443fca9a151cf89e5c)
+++ uspace/lib/libc/generic/io/io.c	(revision c063d96e697d575d95acdcf12a82fa94719306b0)
@@ -181,9 +181,26 @@
 }
 
+static void _setvbuf(FILE *stream)
+{
+	/* FIXME: Use more complex rules for setting buffering options. */
+	
+	switch (stream->fd) {
+	case 1:
+		setvbuf(stream, NULL, _IOLBF, BUFSIZ);
+		break;
+	case 0:
+	case 2:
+		setvbuf(stream, NULL, _IONBF, 0);
+		break;
+	default:
+		setvbuf(stream, NULL, _IOFBF, BUFSIZ);
+	}
+}
+
 /** Allocate stream buffer. */
 static int _fallocbuf(FILE *stream)
 {
 	assert(stream->buf == NULL);
-
+	
 	stream->buf = malloc(stream->buf_size);
 	if (stream->buf == NULL) {
@@ -191,5 +208,5 @@
 		return -1;
 	}
-
+	
 	stream->buf_head = stream->buf;
 	return 0;
@@ -226,7 +243,5 @@
 	stream->klog = false;
 	stream->phone = -1;
-
-	/* FIXME: Should select buffering type based on what was opened. */
-	setvbuf(stream, NULL, _IOFBF, BUFSIZ);
+	_setvbuf(stream);
 	
 	list_append(&stream->link, &files);
@@ -249,7 +264,5 @@
 	stream->klog = false;
 	stream->phone = -1;
-
-	/* FIXME: Should select buffering type based on what was opened. */
-	setvbuf(stream, NULL, _IOLBF, BUFSIZ);
+	_setvbuf(stream);
 	
 	list_append(&stream->link, &files);
@@ -282,7 +295,5 @@
 	stream->klog = false;
 	stream->phone = -1;
-
-	/* FIXME: Should select buffering type based on what was opened. */
-	setvbuf(stream, NULL, _IOLBF, BUFSIZ);
+	_setvbuf(stream);
 	
 	list_append(&stream->link, &files);
@@ -332,8 +343,8 @@
 	size_t left = size * nmemb;
 	size_t done = 0;
-
+	
 	/* Make sure no data is pending write. */
 	_fflushbuf(stream);
-
+	
 	while ((left > 0) && (!stream->error) && (!stream->eof)) {
 		ssize_t rd = read(stream->fd, buf + done, left);
@@ -380,12 +391,12 @@
 {
 	size_t bytes_used;
-
-	if (!stream->buf || stream->btype == _IONBF || stream->error)
+	
+	if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
 		return;
-
+	
 	bytes_used = stream->buf_head - stream->buf;
 	if (bytes_used == 0)
 		return;
-
+	
 	(void) _fwrite(stream->buf, 1, bytes_used, stream);
 	stream->buf_head = stream->buf;
@@ -410,9 +421,12 @@
 	uint8_t b;
 	bool need_flush;
-
+	
 	/* If not buffered stream, write out directly. */
-	if (stream->btype == _IONBF)
-		return _fwrite(buf, size, nmemb, stream);
-
+	if (stream->btype == _IONBF) {
+		now = _fwrite(buf, size, nmemb, stream);
+		fflush(stream);
+		return now;
+	}
+	
 	/* Perform lazy allocation of stream buffer. */
 	if (stream->buf == NULL) {
@@ -420,12 +434,11 @@
 			return 0; /* Errno set by _fallocbuf(). */
 	}
-
+	
 	data = (uint8_t *) buf;
 	bytes_left = size * nmemb;
 	total_written = 0;
 	need_flush = false;
-
-	while (!stream->error && bytes_left > 0) {
-
+	
+	while ((!stream->error) && (bytes_left > 0)) {
 		buf_free = stream->buf_size - (stream->buf_head - stream->buf);
 		if (bytes_left > buf_free)
@@ -433,13 +446,13 @@
 		else
 			now = bytes_left;
-
+		
 		for (i = 0; i < now; i++) {
 			b = data[i];
 			stream->buf_head[i] = b;
-
-			if (b == '\n' && stream->btype == _IOLBF)
+			
+			if ((b == '\n') && (stream->btype == _IOLBF))
 				need_flush = true;
 		}
-
+		
 		buf += now;
 		stream->buf_head += now;
@@ -447,5 +460,5 @@
 		bytes_left -= now;
 		total_written += now;
-
+		
 		if (buf_free == 0) {
 			/* Only need to drain buffer. */
@@ -454,8 +467,8 @@
 		}
 	}
-
+	
 	if (need_flush)
 		fflush(stream);
-
+	
 	return (total_written / size);
 }
@@ -496,5 +509,5 @@
 {
 	char c;
-
+	
 	/* This could be made faster by only flushing when needed. */
 	if (stdout)
@@ -502,5 +515,5 @@
 	if (stderr)
 		fflush(stderr);
-
+	
 	if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
 		return EOF;
@@ -535,5 +548,5 @@
 {
 	_fflushbuf(stream);
-
+	
 	if (stream->klog) {
 		klog_update();
