2014-09-18 10:51 GMT+02:00 Dan Carpenter <dan.carpenter@xxxxxxxxxx>: > On Thu, Sep 18, 2014 at 12:12:42AM +0200, Rickard Strandqvist wrote: >> Hi Dan >> >> Ok, I have made two suggestions for strncpy function that also >> guarantees a terminating null character. >> 1) retunerar number of characters to be copied, it can be good to >> have, but was not really satisfied. > > strlcpy() is more popular the strncpy() in the kernel. > > No one uses the return value of strncpy() because what is the point? > There are around 15-20 places which use the return value of strlcpy(). > Some of the place which use the return value assume that the copy fits. > > I think we should return the number of bytes before the NUL or else > count. > >> >> int strncpyz(char *dest, const char *src, size_t count) >> { >> size_t len=0; >> >> if(0 == count) >> return 0; >> >> --count; >> while(len < count && src[len]) >> *dest++ = src[len++]; >> >> do { >> *dest++ = '\0'; >> } >> while(len < count--); >> >> return len; >> } >> >> >> 2) The next version is almost the same code as the regular strncpy, >> but with two extra lines. >> >> char *strncpyz(char *dest, const char *src, size_t count) >> { >> char *tmp = dest; >> >> while (count) { >> if ((*tmp = *src) != 0) >> src++; >> tmp++; >> count--; >> } >> >> if(tmp != dest) >> *--tmp = '\0'; >> >> return dest; >> } >> >> >> Since I did not got any better solution to variant 1, I prefer variant 2. > > I also prefer variant 2. > >> >> Then the next question is of course what it should be called :-) > > I think a lot of people call this function strzcpy(). > > This sort of patch would go through Andrew Morton. > > regards, > dan carpenter Hi Dan Ok, strzcpy is it :) Should I add this as a patch in lib/string.c or email him first.. What is customary in these situations? Kind regards Rickard Strandqvist _______________________________________________ devel mailing list devel@xxxxxxxxxxxxxxxxxxxxxx http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel