[comp.sys.mac.programmer] LSC string copy bug

mac@ics.uci.edu (MAC ICS199 ACCOUNT) (11/16/88)

I'm comparing two string using the standard STRCMP function in
LSC 3.0.

It looks something like this:

strcmp(s1, s2)
char *s1, *s2;
	for (; *s1 == *s2 ; s1++, s2++) if (!*s1) break;
	return (*s1 - *s2);
}

Seem to work fine except when I pass it a string consisting of
leading 0xFF (or '\377') characters in s1. It always return
something less than 0 (the debugger says *s1 == -1). Anyone
know why this doesn't work.

Greg Finnegan
(its too late for a snappy footer)

 

earleh@eleazar.dartmouth.edu (Earle R. Horton) (11/17/88)

In article <930@paris.ics.uci.edu> mac@ics.uci.edu (MAC ICS199 ACCOUNT) writes:
 >
I>I'm comparing two string using the standard STRCMP function in
 >LSC 3.0.
H>
A>It looks something like this:
T>
E>strcmp(s1, s2)
 >char *s1, *s2;
I>	for (; *s1 == *s2 ; s1++, s2++) if (!*s1) break;
N>	return (*s1 - *s2);
E>}
W>
S>Seem to work fine except when I pass it a string consisting of
!>leading 0xFF (or '\377') characters in s1. It always return
!>something less than 0 (the debugger says *s1 == -1). Anyone
!>know why this doesn't work.
 >
This happens because the char data type is signed in most Macintosh C
compilers.  I don't know about LSC 3.0, but this is true for LSC 2.15,
MPW C 2.0.2, and Aztec v 3.6c.  Under these conditions, 0xFF is,
indeed, equal to -1.  If you need a correct return value from strcmp,
recompile the library function to use "unsigned char *s1,*s2;."

If you do this, then your code becomes somewhat less than portable,
since other LSC programmers will have no idea that you are using an
unsigned strcmp().  If you desire portability in addition to correct
results, then rename the modified routine, and include its code in
your programs.

Earle R. Horton. 23 Fletcher Circle, Hanover, NH 03755
(603) 643-4109
Sorry, no fancy stuff, since this program limits my .signature to three

peterson@peterson.applicon.UUCP (11/24/88)

I have an idea...  This routine might be thinking of these characters
as SIGNED.  Try declaring the parameters as unsigned char.

Joe Peterson
Schlumberger Technologies
Billerica, MA

email: peterson@applicon.com

Disclaimer: My company has nothing to do with what I say here, nor does
            it use any other bizarre forms of mind control.