Index: uspace/lib/posix/stdio.c
===================================================================
--- uspace/lib/posix/stdio.c	(revision fb872c13e0bc74c1e24370db653a898ac5407185)
+++ uspace/lib/posix/stdio.c	(revision 1978a5fa6a143ff2f26ae41fe890b8cb99bc4066)
@@ -38,4 +38,5 @@
 #include <assert.h>
 #include <errno.h>
+#include <bool.h>
 
 #include "internal/common.h"
@@ -44,6 +45,7 @@
 #include "string.h"
 #include "libc/str.h"
-
-/* not the best of solutions, but freopen will eventually
+#include "sys/types.h"
+
+/* not the best of solutions, but freopen and ungetc will eventually
  * need to be implemented in libc anyway
  */
@@ -52,5 +54,5 @@
 /** Clears the stream's error and end-of-file indicators.
  *
- * @param stream
+ * @param stream Stream whose indicators shall be cleared.
  */
 void posix_clearerr(FILE *stream)
@@ -61,7 +63,8 @@
 
 /**
- *
- * @param s
- * @return
+ * Generate a pathname for the controlling terminal.
+ *
+ * @param s Allocated buffer to which the pathname shall be put.
+ * @return Either s or static location filled with the requested pathname.
  */
 char *posix_ctermid(char *s)
@@ -81,10 +84,53 @@
 
 /**
- * 
- * @param c
+ * Push byte back into input stream.
+ * 
+ * @param c Byte to be pushed back.
+ * @param stream Stream to where the byte shall be pushed.
+ * @return Provided byte on succes or EOF if not possible.
+ */
+int posix_ungetc(int c, FILE *stream)
+{
+	uint8_t b = (uint8_t) c;
+
+	bool can_unget =
+		/* Provided character is legal. */
+	    c != EOF &&
+		/* Stream is consistent. */
+	    !stream->error &&
+		/* Stream is buffered. */
+	    stream->btype != _IONBF &&
+		/* Last operation on the stream was a read operation. */
+	    stream->buf_state == _bs_read &&
+		/* Stream buffer is already allocated (i.e. there was already carried
+		 * out either write or read operation on the stream). This is probably
+		 * redundant check but let's be safe. */
+	    stream->buf != NULL &&
+		/* There is still space in the stream to retreat. POSIX demands the
+		 * possibility to unget at least 1 character. It should be always
+		 * possible, assuming the last operation on the stream read at least 1
+		 * character, because the buffer is refilled in the lazily manner. */
+	    stream->buf_tail > stream->buf;
+
+	if (can_unget) {
+		--stream->buf_tail;
+		stream->buf_tail[0] = b;
+		stream->eof = false;
+		return (int) b;
+	} else {
+		return EOF;
+	}
+}
+
+/**
+ *
+ * @param lineptr
+ * @param n
+ * @param delimiter
  * @param stream
- * @return 
- */
-int posix_ungetc(int c, FILE *stream)
+ * @return
+ */
+ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
+    int delimiter, FILE *restrict stream)
 {
 	// TODO
@@ -92,6 +138,13 @@
 }
 
-ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
-    int delimiter, FILE *restrict stream)
+/**
+ * 
+ * @param lineptr
+ * @param n
+ * @param stream
+ * @return
+ */
+ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
+    FILE *restrict stream)
 {
 	// TODO
@@ -99,17 +152,14 @@
 }
 
-ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
-    FILE *restrict stream)
-{
-	// TODO
-	not_implemented();
-}
-
-/**
- * 
- * @param filename
- * @param mode
- * @param stream
- * @return
+/**
+ * Reopen a file stream.
+ * 
+ * @param filename Pathname of a file to be reopened or NULL for changing
+ *     the mode of the stream.
+ * @param mode Mode to be used for reopening the file or changing current
+ *     mode of the stream.
+ * @param stream Current stream associated with the opened file.
+ * @return On success, either a stream of the reopened file or the provided
+ *     stream with a changed mode. NULL otherwise.
  */
 FILE *posix_freopen(
@@ -159,4 +209,11 @@
 }
 
+/**
+ *
+ * @param buf
+ * @param size
+ * @param mode
+ * @return
+ */
 FILE *posix_fmemopen(void *restrict buf, size_t size,
     const char *restrict mode)
@@ -166,4 +223,10 @@
 }
 
+/**
+ *
+ * @param bufp
+ * @param sizep
+ * @return
+ */
 FILE *posix_open_memstream(char **bufp, size_t *sizep)
 {
@@ -173,6 +236,7 @@
 
 /**
- *
- * @param s
+ * Write error messages to standard error.
+ *
+ * @param s Error message.
  */
 void posix_perror(const char *s)
@@ -217,9 +281,10 @@
 
 /**
- * 
- * @param stream
- * @param offset
- * @param whence
- * @return
+ * Reposition a file-position indicator in a stream.
+ * 
+ * @param stream Stream to seek in.
+ * @param offset Direction and amount of bytes to seek.
+ * @param whence From where to seek.
+ * @return Zero on success, -1 otherwise.
  */
 int posix_fseek(FILE *stream, long offset, int whence)
@@ -229,9 +294,10 @@
 
 /**
- * 
- * @param stream
- * @param offset
- * @param whence
- * @return
+ * Reposition a file-position indicator in a stream.
+ * 
+ * @param stream Stream to seek in.
+ * @param offset Direction and amount of bytes to seek.
+ * @param whence From where to seek.
+ * @return Zero on success, -1 otherwise.
  */
 int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
@@ -241,7 +307,8 @@
 
 /**
- * 
- * @param stream
- * @return
+ * Discover current file offset in a stream.
+ * 
+ * @param stream Stream for which the offset shall be retrieved.
+ * @return Current offset or -1 if not possible.
  */
 long posix_ftell(FILE *stream)
@@ -251,7 +318,8 @@
 
 /**
- * 
- * @param stream
- * @return
+ * Discover current file offset in a stream.
+ * 
+ * @param stream Stream for which the offset shall be retrieved.
+ * @return Current offset or -1 if not possible.
  */
 posix_off_t posix_ftello(FILE *stream)
@@ -260,4 +328,11 @@
 }
 
+/**
+ * Print formatted output to the opened file.
+ *
+ * @param fildes File descriptor of the opened file.
+ * @param format Format description.
+ * @return Either the number of printed characters or negative value on error.
+ */
 int posix_dprintf(int fildes, const char *restrict format, ...)
 {
@@ -269,4 +344,12 @@
 }
 
+/**
+ * Write ordinary string to the opened file.
+ *
+ * @param str String to be written.
+ * @param size Size of the string (in bytes)..
+ * @param fd File descriptor of the opened file.
+ * @return The number of written characters.
+ */
 static int _dprintf_str_write(const char *str, size_t size, void *fd)
 {
@@ -275,4 +358,12 @@
 }
 
+/**
+ * Write wide string to the opened file.
+ * 
+ * @param str String to be written.
+ * @param size Size of the string (in bytes).
+ * @param fd File descriptor of the opened file.
+ * @return The number of written characters.
+ */
 static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
 {
@@ -299,4 +390,12 @@
 }
 
+/**
+ * Print formatted output to the opened file.
+ * 
+ * @param fildes File descriptor of the opened file.
+ * @param format Format description.
+ * @param ap Print arguments.
+ * @return Either the number of printed characters or negative value on error.
+ */
 int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
 {
@@ -311,9 +410,10 @@
 
 /**
- * 
- * @param s
- * @param format
- * @param ...
- * @return
+ * Print formatted output to the string.
+ * 
+ * @param s Output string.
+ * @param format Format description.
+ * @return Either the number of printed characters (excluding null byte) or
+ *     negative value on error.
  */
 int posix_sprintf(char *s, const char *format, ...)
@@ -327,9 +427,11 @@
 
 /**
- * 
- * @param s
- * @param format
- * @param ap
- * @return
+ * Print formatted output to the string.
+ * 
+ * @param s Output string.
+ * @param format Format description.
+ * @param ap Print arguments.
+ * @return Either the number of printed characters (excluding null byte) or
+ *     negative value on error.
  */
 int posix_vsprintf(char *s, const char *format, va_list ap)
@@ -339,9 +441,9 @@
 
 /**
- * 
- * @param stream
- * @param format
- * @param ...
- * @return
+ * Convert formatted input from the stream.
+ * 
+ * @param stream Input stream.
+ * @param format Format description.
+ * @return The number of converted output items or EOF on failure.
  */
 int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
@@ -354,4 +456,12 @@
 }
 
+/**
+ * Convert formatted input from the stream.
+ * 
+ * @param stream Input stream.
+ * @param format Format description.
+ * @param arg Output items.
+ * @return The number of converted output items or EOF on failure.
+ */
 int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg)
 {
@@ -361,8 +471,8 @@
 
 /**
- * 
- * @param format
- * @param ...
- * @return
+ * Convert formatted input from the standard input.
+ * 
+ * @param format Format description.
+ * @return The number of converted output items or EOF on failure.
  */
 int posix_scanf(const char *restrict format, ...)
@@ -376,8 +486,9 @@
 
 /**
- * 
- * @param format
- * @param arg
- * @return
+ * Convert formatted input from the standard input.
+ * 
+ * @param format Format description.
+ * @param arg Output items.
+ * @return The number of converted output items or EOF on failure.
  */
 int posix_vscanf(const char *restrict format, va_list arg)
@@ -387,9 +498,9 @@
 
 /**
- * 
- * @param s
- * @param format
- * @param ...
- * @return
+ * Convert formatted input from the string.
+ * 
+ * @param s Input string.
+ * @param format Format description.
+ * @return The number of converted output items or EOF on failure.
  */
 int posix_sscanf(const char *s, const char *format, ...)
@@ -403,9 +514,10 @@
 
 /**
- * 
- * @param s
- * @param format
- * @param arg
- * @return
+ * Convert formatted input from the string.
+ * 
+ * @param s Input string.
+ * @param format Format description.
+ * @param arg Output items.
+ * @return The number of converted output items or EOF on failure.
  */
 int posix_vsscanf(
@@ -416,4 +528,9 @@
 }
 
+/**
+ * Acquire file stream for the thread.
+ *
+ * @param file File stream to lock.
+ */
 void posix_flockfile(FILE *file)
 {
@@ -421,4 +538,10 @@
 }
 
+/**
+ * Acquire file stream for the thread (non-blocking).
+ *
+ * @param file File stream to lock.
+ * @return Zero for success and non-zero if the lock cannot be acquired.
+ */
 int posix_ftrylockfile(FILE *file)
 {
@@ -427,4 +550,9 @@
 }
 
+/**
+ * Relinquish the ownership of the locked file stream.
+ *
+ * @param file File stream to unlock.
+ */
 void posix_funlockfile(FILE *file)
 {
@@ -432,4 +560,10 @@
 }
 
+/**
+ * Get a byte from a stream (thread-unsafe).
+ *
+ * @param stream Input file stream.
+ * @return Either read byte or EOF.
+ */
 int posix_getc_unlocked(FILE *stream)
 {
@@ -437,4 +571,9 @@
 }
 
+/**
+ * Get a byte from the standard input stream (thread-unsafe).
+ *
+ * @return Either read byte or EOF.
+ */
 int posix_getchar_unlocked(void)
 {
@@ -442,4 +581,11 @@
 }
 
+/**
+ * Put a byte on a stream (thread-unsafe).
+ *
+ * @param c Byte to output.
+ * @param stream Output file stream.
+ * @return Either written byte or EOF.
+ */
 int posix_putc_unlocked(int c, FILE *stream)
 {
@@ -447,4 +593,10 @@
 }
 
+/**
+ * Put a byte on the standard output stream (thread-unsafe).
+ * 
+ * @param c Byte to output.
+ * @return Either written byte or EOF.
+ */
 int posix_putchar_unlocked(int c)
 {
@@ -453,7 +605,8 @@
 
 /**
- *
- * @param path
- * @return
+ * Remove a file.
+ *
+ * @param path Pathname of the file that shall be removed.
+ * @return Zero on success, -1 otherwise.
  */
 int posix_remove(const char *path)
