On Fri, May 04, 2001 at 11:52:09AM +0000, Conor Daly wrote:
> only strncpy() needs to be followed by a null terminator and only if it
> reads less than the length of src. so, assuming
>> char *src;
>> strncpy(dest, src, strlen(src) * sizeof(char));
>> dest will be null terminated. Or does strlrn() leave out the null
> terminator when returning a length?
either you don't understand or you're leaving out information. so...
#define STRSIZE 80
char *
strdup2(char *s1, char *s2)
{
char *s;
if ((s=(char *)malloc(STRSIZE + 1)) == NULL) {
return NULL;
}
/* the following initialises the string. s[0]=0;s[STRSIZE]=0; *
* would work too. */
memset(s, 0, STRSIZE + 1);
/* from here on in we don't let str* routines near s[STRSIZE] *
* so we know it will stay 0. */
strncpy(s, s1, STRSIZE);
strncpy(s, s2, STRSIZE - strlen(s));
return s;
}
strlen works by counting chars until it reaches '\0'. therefore don't use
it if you're not sure the string is null terminated.
sizeof(char) is not required.
kevin
--
kevin at suberic.net "nobody likes a monkey on their back. i had
fork()'ed on 37058400 three. they were cramping my style. it was
meatspace place: work time to get rid of them."
http://suberic.net/~kevin --porter, "payback"
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!