my previous version was wrong; this one is right
[unix-history] / usr / src / lib / libc / string / strncpy.c
CommitLineData
73e0d42e
KB
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
f9303d64
KB
3 * All rights reserved.
4 *
73e0d42e
KB
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * %sccs.include.redist.c%
702612b1
BJ
9 */
10
f9303d64 11#if defined(LIBC_SCCS) && !defined(lint)
a64329b4 12static char sccsid[] = "@(#)strncpy.c 5.6 (Berkeley) %G%";
f9303d64
KB
13#endif /* LIBC_SCCS and not lint */
14
a64329b4 15#include <sys/cdefs.h>
73e0d42e 16#include <string.h>
73e0d42e
KB
17
18/*
19 * Copy src to dst, truncating or null-padding to always copy n bytes.
20 * Return dst.
21 */
702612b1 22char *
73e0d42e
KB
23strncpy(dst, src, n)
24 char *dst;
25 const char *src;
26 register size_t n;
702612b1 27{
73e0d42e
KB
28 if (n != 0) {
29 register char *d = dst;
30 register const char *s = src;
5bc9ee3d 31
73e0d42e
KB
32 do {
33 if ((*d++ = *s++) == 0) {
34 /* NUL pad the remaining n-1 bytes */
35 while (--n != 0)
36 *d++ = 0;
37 break;
38 }
39 } while (--n != 0);
40 }
41 return (dst);
702612b1 42}