ref: 5382addb796f95bf3f7a1ce2168e179e817ca4d2
parent: a108097fd580a76ad99115e40f78cd68f424a57b
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jul 11 09:28:58 EDT 2020
stdio, ape/stdio: fix order of operations in putc When calling putc, we need to return either EOF or the character returned. To distinguish the two, we need to avoid sign extending 0xff. The code attempted to do this, but the order of operations was wrong, so we ended up masking, setting a character, and then sign extending the character. This fixes things so we mask after assignment.
--- a/sys/include/ape/stdio.h
+++ b/sys/include/ape/stdio.h
@@ -106,7 +106,7 @@
#define getchar() getc(stdin)
extern char *gets(char *);
extern int putc(int, FILE *);
-#define putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):(*(f)->wp++=(c)&_IO_CHMASK))
+#define putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):((*(f)->wp++=(c))&_IO_CHMASK))
extern int _IO_putc(int, FILE *);
extern int putchar(int);
#define putchar(c) putc(c, stdout)
--- a/sys/include/stdio.h
+++ b/sys/include/stdio.h
@@ -92,7 +92,7 @@
#define getchar() getc(stdin)
char *gets(char *);
int putc(int, FILE *);
-#define putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):(*(f)->wp++=(c)&_IO_CHMASK))
+#define putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):((*(f)->wp++=(c))&_IO_CHMASK))
int _IO_putc(int, FILE *);
int putchar(int);
#define putchar(c) putc(c, stdout)
--
⑨