
/*  File   : strnrpt.c
    Author : Thomas P Devitt
    Updated: 28 November 1986
    Defines: strnrpt()

    strnrpt(dst, n, src, k) "RePeaTs" the string src into dst  k  times,
    but  will  truncate  the  result at n characters if necessary.  E.g.
    strnrpt(dst, 7, "hack ", 2) will move "hack ha" to dst.

    The  result  is  the number of characters moved, not counting the
    closing NUL, which is always appended.

    Equivalent to strrpt-ing into an infinite size buffer and then
    strnmov-ing the result.
*/

#include "stdio.h"

int strnrpt(dst, n, src, k)
register char *dst;
         int  n;
         char *src;
         int  k;
{
    char *save;
    register char *p;

    for (save = dst; --k >= 0; dst--)
        for (p = src; (*dst++ = *p++) && (n > 0) ;--n )
           ;

    *dst = '\0';
    return dst-save;
}

