summaryrefslogtreecommitdiff
path: root/fgetwc_fix.c
diff options
context:
space:
mode:
authorjaseg <code@jaseg.net>2014-10-25 16:31:26 +0200
committerjaseg <code@jaseg.net>2014-10-25 16:31:26 +0200
commit14b47df4525a53d21c1d88d605f66ffe486e7e75 (patch)
tree61e2bbe05f29e0b6cb7ec1fb6bdc8e8417e6a30a /fgetwc_fix.c
parente9a8e1c85ad32e740cff9ab9ed1511871c39c510 (diff)
downloadlolcat-14b47df4525a53d21c1d88d605f66ffe486e7e75.tar.gz
lolcat-14b47df4525a53d21c1d88d605f66ffe486e7e75.tar.bz2
lolcat-14b47df4525a53d21c1d88d605f66ffe486e7e75.zip
Fixed --help, also added some randomness.
Diffstat (limited to 'fgetwc_fix.c')
-rw-r--r--fgetwc_fix.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/fgetwc_fix.c b/fgetwc_fix.c
new file mode 100644
index 0000000..ed770ce
--- /dev/null
+++ b/fgetwc_fix.c
@@ -0,0 +1,33 @@
+
+#include <stdlib.h>
+#include <wchar.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "fgetwc_fix.h"
+
+/* code blatantly ripped from newlib. If you are from newlib: newlib rocks, keep going! */
+wint_t _fgetwc_fixed(FILE *fp) {
+ wchar_t wc;
+ size_t res;
+ size_t nconv = 0;
+ char buf[MB_CUR_MAX];
+ mbstate_t mbstate;
+ memset(&mbstate, 0, sizeof(mbstate));
+
+ while((buf[nconv++] = fgetc(fp)) != EOF){
+ res = mbrtowc(&wc, buf, nconv, &mbstate);
+ if (res == (size_t)-1) /* invalid sequence */
+ break;
+ else if (res == (size_t)-2) /* incomplete sequence */
+ continue;
+ else if (res == 0)
+ return L'\0';
+ else
+ return wc;
+ }
+
+ errno = EILSEQ;
+ return WEOF;
+}