
/*  File   : substr.c
    Author : Richard A. O'Keefe.
    Updated: 25 May 1984
    Defines: substr()

    char *substr(destination, source, offset, length)

    copies length bytes from source+offset to destination, stopping
    early if a NUL is encountered.  The difference between this and

    strncpy(destination, source+offset, length)

    is that if the offset is negative, it has the same effect as 0,
    and if it exceeds strlen(source), it has the same effect as
    strlen(source).

    After the substring of source is moved to destination, a NUL byte
    is moved to terminate the string, and the result is a pointer to
    this NUL byte, ready to have new stuff stuck on the end.
*/

#define NUL '\0'

char *substr(dst, src, off, len)
    register char *dst, *src;
    register int off, len;
    {
        while (--off >= 0)
            if (!*src++) {              /* We've hit the end */
                *dst = NUL;             /* return empty string */
                return dst;
            }
        while (--len >= 0)
            if (!(*dst++ = *src++)) {   /* We've hit the end */
                return dst-1;           /* dst is already terminated */
            }
        *dst = NUL;                     /* terminate dst with NUL */
        return dst;
    }

