On Thu, Aug 5, 2010 at 7:37 PM, Dan Carpenter <error27@xxxxxxxxx> wrote: > > Both strncpy() and strlcpy() take a limitter. The difference is that > strlcpy() always takes on a terminator and strncpy() only adds a > terminator if there is space. > > strlcpy() is a BSD function that never caught on in Linux. The glibc > maintainers think that if you accidentally chop off the last part of a > word that makes you an idiot. They think you should known the length of > your data at all times and use memcpy() or a proper string library. > > I prefer strlcpy() to strncpy(). Some people do stuff like: > strncpy(bar, foo, n); > bar[n] = '\0'; > You have to read through the code to find if n is "sizeof(bar)" or > "sizeof(bar) - 1". Which is a pain in the arse. strlcpy() is explicit > and it's just one line of code instead of two. > > The other tricky thing you should remember about strncpy() is that the > posix version writes NUL chars from the end of the string to the > limitter but the kernel version only copies one NUL character. > You should spend some time on reading the source code of strlcpy() and strncpy(). the example use of them is: char dst[24]; char *src = "test"; strncpy(dst, src, sizeof(dst) - 1); strlcpy(dst, src, sizeof(dst)); both of them don't need to zero dst, and they don't need to pad zero at then end of the dst. -- Regards, Changli Gao(xiaosuo@xxxxxxxxx) -- To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html