Neil Kurzman wrote:
<snip>
Not list[i][strlen[line])=line; use strncpy()
<snip>
I would suggest using strlcpy instead. The latest source code is
available from the OpenBSD ftp. strlcpy guarantees null termination
of the string and is more efficient since it does not pad
the string buffer with redundant NULs. strncpy can be a problem if
used improperly (you have to remember to pass the correct
count of characters and then manually terminate the string).
Also, strlcpy is more secure and more intuitive to use.
You simply pass the size of the buffer in bytes as the last
parameter.
/* xstrlcpy.c */
#include <stddef.h>
/*
@synopsis
#include <xstring.h>
size_t xstrlcpy (char *dst, const char *src, size_t siz);
@description
Copies not more than siz-1 characters into the array
pointed to by dst from the string pointed to by src
(characters that follow a null character are not copied).
If copying takes place between two objects that overlap,
the behavior is undefined.
This function does not add redundant NUL characters
to fill in the size requirements. The array pointed
to by dst is guaranteed to be terminated by a NUL
character (unless siz is zero).
@param dst pointer to the destination buffer array
@param src pointer to the source string
@param siz the size of the destination buffer array
object including the space for a terminating
NUL character.
@return
The length of the string pointed to by dst after
copying (the number of characters before the terminating
null character).
If the returned value is greater than or equal to
siz, truncation has occurred.
@remarks
This function should be used in place of the functions
xstrcpy and xstrncpy whenever possible due to the following
reasons:
* reduces the risk of a buffer overflow
* guarantees NUL-termination of the destination buffer
* is more logical since you only have to remember to
pass the size of the entire destination array object
* efficiency is much better than xstrncpy while negligibly
lesser than xstrcpy.
The xstrncpy function is useful when you do not
need to terminate the destination array with a NUL-character
and want to copy exactly the number of characters specified.
@references
* ISO/IEC 9899:1999 (C Standard) -- 7.21.2.4 -- The strncpy function
* "strlcpy and strlcat -- consistent, safe, string copy
and concatenation" - Todd C. Miller and Theo de Raadt
@source
The source code for this function was obtained from the
OpenBSD libc string routines library. It is available under a
BSD-style license.
@see
xstrncpy
xstrcpy
*/
/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*/
size_t
xstrlcpy(char *dst, const char *src, size_t siz)
{
char *d = dst;
const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
Regards,
Jonathan.
--
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP
"We must do something. This is something. Therefore, we must do this."
- Keith Thompson