
/*  File   : strnrev.c
    Author : Richard A. O'Keefe.
    Updated: 1 June 1984
    Defines: strnrev()

    char *strnrev(dst, src, len)

    copies len characters of src to dst, in REVERSE order.  dst will
    be terminated by a NUL character.

    Returns a pointer to dst.

    Note: this function is perfectly happy to reverse a string into the
    same place, strnrev(x, x, L) will work.

    It will not work for partially overlapping source and destination.
*/

#define  NUL   '\0'

char *
strnrev(dsta, srca, len)
register char *dsta, *srca;
         int  len;
    {
        char *dstz, *srcz, *result;
        char t;

        result = dsta;

        for (srcz = srca; --len >= 0 && *srcz; srcz++) ;
        dstz = dsta+(srcz-srca);
        /*
        If srcz was stopped by len running out, it points just after
        the last character of the source string, and it and dstz are
        just right.  Otherwise, it was stopped by hitting NUL, and is
        in the right place. dstz should get a NUL.
        */
        *dstz = NUL;
        while (srcz > srca) {
            t = *--srcz;
            *--dstz = *srca++;
            *dsta++ = t;
        }
        return result;
    }

