Unlike memcpy
the function memmove
allows overlapping memory areas, i.e. a part of the destination area is allowed to be a part of the source area. Therefore the implementation must make sure that the source area isn't overwritten by the destination area data before the source area data has been read.
The code
if(d < s) {
checks that the destination area is placed in memory before the source area. If so it's safe to copy from source to destination char-by-char starting from the lowest address and going towards higher addresses.
In other words, when the comparision d < s
is true, it's safe to "move the memory" using a simple copy-loop like:
while(len--)
*d++ = *s++;
So to answer your specific question, i.e.
Does it just compare the first character of d and s, or does it look at the whole string?
Neither. It's not a string compare in any way. It's not a character compare in any way.
The code is comparing pointers to the two areas (i.e. destination and source area) to figure out how the memory move can be done.
Edit:
As mentioned in a number of comments (Andrew Henle, M.M.) the C standard doesn't allow comparing pointers that point to different objects (which the shown memmove
code may do). However, when looking at a specific implementation of memove
, the implementation is for a specific system. So despite the code not being strictly standard compliant, the designers know that it will actually work on the system, it was designed for.