diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c
index 30aeb03..02466dc 100644
--- a/lib/libc/stdio/fgetln.c
+++ b/lib/libc/stdio/fgetln.c
@@ -30,15 +30,16 @@
  * SUCH DAMAGE.
  *
  * @(#)fgetln.c	8.2 (Berkeley) 1/2/94
- * $FreeBSD: src/lib/libc/stdio/fgetln.c,v 1.11 2007/01/09 00:28:06 imp Exp $
+ * $FreeBSD: head/lib/libc/stdio/fgetln.c 304819 2016-08-25 21:14:26Z ache $
  */
 
 #include "namespace.h"
+#include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "un-namespace.h"
-
 #include "libc_private.h"
 #include "local.h"
 
@@ -59,6 +60,10 @@ __slbexpand(FILE *fp, size_t newsize)
 #endif
 	if (fp->_lb._size >= newsize)
 		return (0);
+	if (newsize > INT_MAX) {
+		errno = ENOMEM;
+		return (-1);
+	}
 	if ((p = realloc(fp->_lb._base, newsize)) == NULL)
 		return (-1);
 	fp->_lb._base = p;
@@ -113,7 +118,7 @@ fgetln(FILE *fp, size_t *lenp)
 	 * As a bonus, though, we can leave off the __SMOD.
 	 *
 	 * OPTIMISTIC is length that we (optimistically) expect will
-	 * accomodate the `rest' of the string, on each trip through the
+	 * accommodate the `rest' of the string, on each trip through the
 	 * loop below.
 	 */
 #define OPTIMISTIC 80
@@ -131,8 +136,11 @@ fgetln(FILE *fp, size_t *lenp)
 		memcpy((void *)(fp->_lb._base + off), (void *)fp->pub._p,
 		    len - off);
 		off = len;
-		if (__srefill(fp))
-			break;	/* EOF or error: return partial line */
+		if (__srefill(fp)) {
+			if (__sfeof(fp))
+				break;
+			goto error;
+		}
 		if ((p = memchr((void *)fp->pub._p, '\n', (size_t)fp->pub._r))
 		    == NULL)
 			continue;
@@ -150,13 +158,14 @@ fgetln(FILE *fp, size_t *lenp)
 	}
 	*lenp = len;
 #ifdef notdef
-	fp->_lb._base[len] = 0;
+	fp->_lb._base[len] = '\0';
 #endif
 	FUNLOCKFILE(fp);
 	return ((char *)fp->_lb._base);
 
 error:
 	*lenp = 0;		/* ??? */
+	fp->pub._flags |= __SERR;
 	FUNLOCKFILE(fp);
 	return (NULL);		/* ??? */
 }
diff --git a/lib/libc/stdio/fgetwc.c b/lib/libc/stdio/fgetwc.c
index ee8d064..e9cb012 100644
--- a/lib/libc/stdio/fgetwc.c
+++ b/lib/libc/stdio/fgetwc.c
@@ -28,7 +28,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/lib/libc/stdio/fgetwc.c 234799 2012-04-29 16:28:39Z das $
+ * $FreeBSD: head/lib/libc/stdio/fgetwc.c 304641 2016-08-22 22:28:41Z ache $
  */
 
 
@@ -79,18 +79,9 @@ __fgetwc_mbs(FILE *fp, mbstate_t *mbs, int *nread, locale_t locale)
 	size_t nconv;
 	struct xlocale_ctype *l = XLOCALE_CTYPE(locale);
 
-	if (fp->pub._r <= 0 && __srefill(fp)) {
-		*nread = 0;
-		return (WEOF);
-	}
-	if (MB_CUR_MAX == 1) {
-		/* Fast path for single-byte encodings. */
-		wc = *fp->pub._p++;
-		fp->pub._r--;
-		*nread = 1;
-		return (wc);
-	}
 	*nread = 0;
+	if (fp->pub._r <= 0 && __srefill(fp))
+		return (WEOF);
 	do {
 		nconv = l->__mbrtowc(&wc, fp->pub._p, fp->pub._r, mbs);
 		if (nconv == (size_t)-1)
diff --git a/lib/libc/stdio/fgetwln.c b/lib/libc/stdio/fgetwln.c
index a591966..88776b3 100644
--- a/lib/libc/stdio/fgetwln.c
+++ b/lib/libc/stdio/fgetwln.c
@@ -28,7 +28,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/lib/libc/stdio/fgetwln.c 227753 2011-11-20 14:45:42Z theraven $
+ * $FreeBSD: head/lib/libc/stdio/fgetwln.c 304819 2016-08-25 21:14:26Z ache $
  */
 
 
@@ -40,6 +40,8 @@
 #include "local.h"
 #include "xlocale_private.h"
 
+wchar_t *fgetwln_l(FILE * __restrict, size_t *, locale_t);
+
 wchar_t *
 fgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale)
 {
@@ -54,13 +56,15 @@ fgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale)
 	while ((wc = __fgetwc(fp, locale)) != WEOF) {
 #define	GROW	512
 		if (len * sizeof(wchar_t) >= fp->_lb._size &&
-		    __slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
+		    __slbexpand(fp, (len + GROW) * sizeof(wchar_t))) {
+			fp->pub._flags |= __SERR;
 			goto error;
+		}
 		*((wchar_t *)fp->_lb._base + len++) = wc;
 		if (wc == L'\n')
 			break;
 	}
-	if (len == 0)
+	if (len == 0 || (wc == WEOF && !__sfeof(fp)))
 		goto error;
 
 	FUNLOCKFILE(fp);
@@ -72,6 +76,7 @@ error:
 	*lenp = 0;
 	return (NULL);
 }
+
 wchar_t *
 fgetwln(FILE * __restrict fp, size_t *lenp)
 {
diff --git a/lib/libc/stdio/fputwc.c b/lib/libc/stdio/fputwc.c
index fa47282..b7b3855 100644
--- a/lib/libc/stdio/fputwc.c
+++ b/lib/libc/stdio/fputwc.c
@@ -28,7 +28,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: head/lib/libc/stdio/fputwc.c 227753 2011-11-20 14:45:42Z theraven $
+ * $FreeBSD: head/lib/libc/stdio/fputwc.c 304811 2016-08-25 17:30:00Z ache $
  */
 
 
@@ -65,19 +65,9 @@ __fputwc(wchar_t wc, FILE *fp, locale_t locale)
 	wcio->wcio_ungetwc_inbuf = 0;
 	st = &wcio->wcio_mbstate_out;
 
-	if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) {
-		/*
-		 * Assume single-byte locale with no special encoding.
-		 * A more careful test would be to check
-		 * _CurrentRuneLocale->encoding.
-		 */
-		*buf = (unsigned char)wc;
-		len = 1;
-	} else {
-		if ((len = l->__wcrtomb(buf, wc, st)) == (size_t)-1) {
-			fp->pub._flags |= __SERR;
-			return (WEOF);
-		}
+	if ((len = l->__wcrtomb(buf, wc, st)) == (size_t)-1) {
+		fp->pub._flags |= __SERR;
+		return (WEOF);
 	}
 
 	for (i = 0; i < len; i++)
