/*
原型:char * strncpy(char *dest, char *src,size_tnum);
功能:(c/c++)复制src中的内容(字符,数字、汉字....)到dest,复制多少由num的值决定,返回指向dest的指针。
如果遇到null字符('\0'),且还没有到num个字符时,就用(num - n)(n是遇到null字符前已经有的非null字符个数)个null字符附加到destination。
注意:并不是添加到destination的最后,而是紧跟着由source中复制而来的字符后面。
*/
#include <stdio.h>
char* strncpy(char *dest, const char *src, size_t count)
{
char *address = dest;
while (count && (*dest = *src) != '\0')
{
src++;
dest++;
count--;
}
while(count--)
{
*dest++ = '\0';
}
return address;
}
int main ()
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
/* copy to sized buffer (overflow safe): */
strncpy( str2, str1, sizeof(str2) );
/* partial copy (only 5 chars): */
strncpy ( str3, str2, 5 );
str3[5] = '\0'; /* null character manually added */
puts (str1);
puts (str2);
puts (str3);
return 0;
}
//百度百科上的实现
char *strncpy(char *dest, const char *src, int n)
{
char c;
char *s = dest;
if (n >= 4)
{
size_t n4 = n >> 2;
while(1)
{
c = *src++;
*dest++ = c;
if (c == '\0')
break;
c = *src++;
*dest++ = c;
if (c == '\0')
break;
c = *src++;
*dest++ = c;
if (c == '\0')
break;
c = *src++;
*dest++ = c;
if (c == '\0')
break;
if (--n4 == 0)
goto last_chars;
}
n -= dest - s;
goto zero_fill;
}
last_chars:
n &= 3;
if (n == 0)
return s;
for (;;)
{
c = *src++;
--n;
*dest++ = c;
if (c == '\0')
break;
if (n == 0)
return s;
}
zero_fill:
while (n-- > 0)
dest[n] = '\0';
return s;
}
//linux源码
#ifndef __HAVE_ARCH_STRNCPY
/**
* strncpy - Copy a length-limited, %NUL-terminated string
* @dest: Where to copy the string to
* @src: Where to copy the string from
* @count: The maximum number of bytes to copy
*
* The result is not %NUL-terminated if the source exceeds
* @count bytes.
*
* In the case where the length of @src is less than that of
* count, the remainder of @dest will be padded with %NUL.
*
*/
char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
while (count) {
if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
}
return dest;
}
EXPORT_SYMBOL(strncpy);
#endif
/***
*strncpy.c - copy at most n characters of string
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strncpy() - copy at most n characters of string
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
/***
*char *strncpy(dest, source, count) - copy at most n characters
*
*Purpose:
* Copies count characters from the source string to the
* destination. If count is less than the length of source,
* NO NULL CHARACTER is put onto the end of the copied string.
* If count is greater than the length of sources, dest is padded
* with null characters to length count.
*
*
*Entry:
* char *dest - pointer to destination
* char *source - source string for copy
* unsigned count - max number of characters to copy
*
*Exit:
* returns dest
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strncpy (
char * dest,
const char * source,
size_t count
)
{
char *start = dest;
while (count && (*dest++ = *source++)) /* copy string */
count--;
if (count) /* pad out with zeroes */
while (--count)
*dest++ = '\0';
return (start);
}