laman@sdcsvax.UUCP (Mike Laman) (08/11/84)
There are two bugs in the "portable" routine "getsh()" in the file screen/setupterm.c. The first bug is that the comparison "if (*p == 0377)" is incorrect if characters are signed by default. If *p contained the BYTE value '\377', sign extension would make it so *p != 0377 since 0377 is an integer and will NOT be sign extended. The second bug is that "rv = *p++" will do sign extension (for systems with characters being signed). What we want is to sign extend on ONLY the "high part"; more precisely, we want to sign extend ONLY on the assignment that follows which puts the high byte in and NOT on the assignment that puts the low byte in. [ I suspect that if this routine was used, it was used on a machine where characters are unsigned. ] The original getsh() follows: getsh(p) register char *p; { register int rv; if(*p == 0377) return -1; rv = *p++; rv += *p * 256; return rv; } the following should do the trick: getsh(p) register char *p; { register int rv; rv = *((unsigned char *) p); if(rv == 0377) return -1; /* This is a pretty common case */ return rv + (*++p * 256); } Instead of this though, I use the following macro (since this is for a machine with signed characters): #define getsh(ip) (*((unsigned char *) ip) | (*(ip+1) << 8)) Folks on machines with unsigned character implementations could use: #define getsh(ip) ((*ip == 0377) ? -1 : (*ip | (*(ip+1) << 8)))) [ This should serve the perpose required as stated in the comment above the macro definitions. ] Mike Laman, NCR @ Torrey Pines UUCP: {ucbvax,philabs,sdcsla}!sdcsvax!laman