about summary refs log tree commit diff
path: root/src/stdio/ungetc.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/stdio/ungetc.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.xz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.zip
initial check-in, version 0.5.0 v0.5.0
Diffstat (limited to 'src/stdio/ungetc.c')
-rw-r--r--src/stdio/ungetc.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/stdio/ungetc.c b/src/stdio/ungetc.c
new file mode 100644
index 00000000..07181684
--- /dev/null
+++ b/src/stdio/ungetc.c
@@ -0,0 +1,33 @@
+#include "stdio_impl.h"
+
+int ungetc(int c, FILE *f)
+{
+	if (c == EOF) return c;
+
+	FLOCK(f);
+
+	/* Fail if unreadable or writing and unable to flush */
+	if ((f->flags & (F_ERR|F_NORD)) || (f->wpos && __oflow(f))) {
+		FUNLOCK(f);
+		return EOF;
+	}
+
+	/* Clear write mode */
+	f->wbase = f->wpos = f->wstop = f->wend = 0;
+
+	/* Put the file in read mode */
+	if (!f->rpos) f->rpos = f->rend = f->buf;
+
+	/* If unget buffer is already full, fail. */
+	if (f->rpos <= f->buf - UNGET) {
+		FUNLOCK(f);
+		return EOF;
+	}
+
+	/* Put a byte back into the buffer */
+	*--f->rpos = c;
+	f->flags &= ~F_EOF;
+
+	FUNLOCK(f);
+	return c;
+}