strncpy () function
Prototype: extern char *strncpy (char *dest, char *src, int n);
How to use: #include <string.h>
Function: Copies the first n bytes of the string that is referred to as the null end of SRC to the array referred to by Dest.
Description: Assume that the first n bytes of Src do not contain a null character. The result does not end with a null character.
Assume that the SRC length is less than n bytes. The dest is padded with null until n bytes are copied.
The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string.
Returns a pointer to Dest (the last element that points to dest)
Correlation function: memccpy,memcpy,stpcpy,strcpy
strcpy, strncpy, strlcpy method of Use
Many people already know to use strncpy instead of strcpy to prevent buffer out of bounds.
But the assumption is also to consider the efficiency of execution. Perhaps strlcpy is a better way.
1. strcpy
We know that strcpy is based on the conclusion of the "/", assuming that the space of the to is not enough, it will cause buffer overflow.
strcpy general implementation code such as the following (from OpenBSD 3.9):
char *
strcpy (char *to, const char *from)
{
char *save = to;
for (; (*to = *from)! = ' "; ++from, ++to);
return (save);
}
But usually, our from is derived from the user's input, which is very likely a very large string, so the strcpy is not secure enough.
2. strncpy
In ANSI C, the security version number of strcpy is strncpy.
Char *strncpy (char *s1, const char *S2, size_t N);
But strncpy's behavior is very bizarre (not in accordance with our usual habits). The standard specifies that n is not sizeof (S1), but the number of char to be copied. One of the most common problems is that strncpy doesn't help you make sure
End.
Char Buf[8];
strncpy (buf, "ABCDEFGH", 8);
Looking at this program, BUF will be filled with "abcdefgh", but there is no end-of-break.
Other than that. Assuming S2 content is less, and n is larger, strncpy will fill the space between.
There is an efficiency problem, such as the following:
Char buf[80];
strncpy (buf, "ABCDEFGH", 79);
The above strncpy will fill in 79 char. And not just "ABCDEFGH" itself.
The standard use of strncpy is: (hand-written)
strncpy (path, SRC, sizeof (PATH)-1);
Path[sizeof (Path)-1] = ' + ';
Len = strlen (path);
3. strlcpy
Copy src to string dst of size siz. At the most siz-1 characters
would be copied. Always NUL terminates (unless siz = = 0).
Returns strlen (SRC); If retval >= siz, truncation occurred.
size_t
strlcpy (char *dst, const char *SRC, size_t siz);
With the use of strlcpy, we do not need to manually responsible for the, only need to strlcpy sizeof (DST) can:
strlcpy (path, SRC, sizeof (path));
Len = strlen (path);
if (Len >= sizeof (PATH))
printf ("src is truncated.");
And strlcpy returned is strlen (str). Therefore, it is very convenient for us to infer whether the data is truncated.
[* A Little history *]
strlcpy does not belong to ANSI C, so far it is not standard.
strlcpy from OpenBSD 2.4, and then very much unix-like system libc in the strlcpy function, I personally in the FreeBSD, Linux found strlcpy. (Linux is using the glibc,
GLIBC there is strlcpy, then all the Linux version number should also have strlcpy)
But there is no strlcpy under Windows. The corresponding is the strcpy_s function
///////////////////////////////////////////////////////////////////////////
strncpy
Prototype: extern char *strncpy (char *dest, char *src, int n);
How to use: #include <string.h>
Function: Copies the first n bytes of the string that is referred to as the null end of SRC to the array referred to by Dest.
Description
Assuming that the first n bytes of Src do not contain a null character, the result does not end with a null character.
Assuming that the SRC length is less than n bytes, the dest is padded with null until n bytes are copied.
The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string.
Returns a pointer to the dest.
Example:
Strncpy.c
#include <syslib.h>
#include <string.h>
Main ()
{
Char *s= "Golden Global View";
Char *d= "Hello, GGV programmers";
Char *p=strdup (s);
CLRSCR ();
TextMode (0x00); Enable 6 lines Mode
strncpy (D,s,strlen (s));
printf ("%s\n", D);
strncpy (P,s,strlen (d));
printf ("%s", p);
GetChar ();
return 0;
}
View article strncpy () features better articles