ncftp for win32 3.0.3
[reactos.git] / reactos / apps / utils / net / ncftp / Strn / Strnpcat.c
1 #include <sys/types.h>
2 #include <string.h>
3 #include "Strn.h"
4
5 /*
6 * Concatenate src on the end of dst. The resulting string will have at most
7 * n-1 characters, not counting the NUL terminator which is always appended
8 * unlike strncat. The other big difference is that strncpy uses n as the
9 * max number of characters _appended_, while this routine uses n to limit
10 * the overall length of dst.
11 *
12 * This routine also differs in that it returns a pointer to the end
13 * of the buffer, instead of strncat which returns a pointer to the start.
14 */
15 char *
16 Strnpcat(char *const dst, const char *const src, size_t n)
17 {
18 register size_t i;
19 register char *d;
20 register const char *s;
21 register char c;
22 char *ret;
23
24 if (n != 0 && ((i = strlen(dst)) < (n - 1))) {
25 d = dst + i;
26 s = src;
27 /* If they specified a maximum of n characters, use n - 1 chars to
28 * hold the copy, and the last character in the array as a NUL.
29 * This is the difference between the regular strncpy routine.
30 * strncpy doesn't guarantee that your new string will have a
31 * NUL terminator, but this routine does.
32 */
33 for (++i; i<n; i++) {
34 c = *s++;
35 if (c == '\0') {
36 ret = d; /* Return ptr to end byte. */
37 *d++ = c;
38 #if (STRNP_ZERO_PAD == 1)
39 /* Pad with zeros. */
40 for (; i<n; i++)
41 *d++ = 0;
42 #endif /* STRNP_ZERO_PAD */
43 return ret;
44 }
45 *d++ = c;
46 }
47 /* If we get here, then we have a full string, with n - 1 characters,
48 * so now we NUL terminate it and go home.
49 */
50 *d = 0;
51 return (d); /* Return ptr to end byte. */
52 }
53 return (dst);
54 } /* Strnpcat */