News for hackers. Stuff that matters.
Re:Unix v6 Ported to ANSI C by antihero on Tuesday November 08, 2011 @06:18PM (#3213380)
I'm not massively great at C but would this be a fix?

    void*
    memmove(void *vdst, void *vsrc, int n)
    {
        char *dst, *src;

        dst = vdst;
        src = vsrc;
        if(*vdst > *vsrc) {
            *vdst += n;
            *vsrc += n;
            while(n-- > 0)
                *dst-- = *src--;
        }
        else if(*vdst < *vsrc)
            while(n-- > 0)
                *dst++ == *src++;
        return vdst;
    }
Re:Unix v6 Ported to ANSI C by antihero on Wednesday November 09, 2011 @03:40AM (#3214807)
Ok, updated version:

    void*
    memmove(void *vdst, void *vsrc, int n)
    {
        char *dst, *src;

        dst = vdst;
        src = vsrc;
        if(vdst > vsrc) {
            dst = vdst + n;
            src = vsrc + n;
            while(n-- > 0)
                *--dst = *--src;
        }
        else if(vdst < vsrc)
            while(n-- > 0)
                *dst++ == *src++;
        return vdst;
    }

Does this solve the bugs?
Re:Unix v6 Ported to ANSI C by abecedarius on Wednesday November 09, 2011 @11:09AM (#3216022)
There's an == for an =. Also vdst+n is an error, I believe, because sizeof *vdst has no meaning.
Re:Unix v6 Ported to ANSI C by abecedarius on Tuesday November 08, 2011 @08:00PM (#3213722)
At least two bugs: the test should compare vdst to vsrc, not * vdst to * vsrc, and in the first branch you're copying vsrc[n] which is out of range (and skipping vsrc[0]).
Re:Unix v6 Ported to ANSI C by antihero on Wednesday November 09, 2011 @03:38AM (#3214802)
The first bug I'm confused? Surely we want to use the * because we're comparing the position in memory, not what's in the memory? Or does the * mean "compare what is in that memory slot"?

Second bug, cheers.

http://news.ycombinator.com/item?id=3214807

Re:Unix v6 Ported to ANSI C by simcop2387 on Tuesday November 08, 2011 @08:52PM (#3213876)
I see an additional bug in there compared to the original, you're copying the memory below vsrc into the memory below vdst in the first branch. you need to add to dst and src not vdst/vsrc.
Re:Unix v6 Ported to ANSI C by antihero on Wednesday November 09, 2011 @03:42AM (#3214814)