|
|||||||||||
|
Re: strncpy() and NULL termination
From: Dan Cross <tenser(at)spitfire.ecsel.psu.edu>
Date: Sun Feb 16 1997 - 21:51:04 EST
Eh? Why would you need to do that? strncpy(3) does nul padding... ;-) > So, anyone up for doing a standard routine for logging (syslog?) when a
That's not a bad idea. btw, would folks find something like this useful?
char *
char *dst;
const char *src;
size_t len;
{
register size_t last;
if (len != 0)
{
(void)strncpy(dst, src, len);
last = len - 1;
if (dst[last] != '\0')
{
dst[last] = '\0';
return(NULL);
}
}
return(dst);
} The idea here is to check for truncation by looking at the last character in dst to see if it is non-nul. If that's true, then the string was truncated, so nul-terminate it and use the return as NULL to indicate this. If not, then the string was NOT truncated. The point is that the return value can now be used as an indication of truncation, and action can be taken accordingly. The register variable for ``last'' is an attempt at an optimization, but wether or not this really GETS you anything is debatable. Of course, all the usual caveats regarding standards compliance (or rather, lack thereof) apply.
This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 12:41:02 EDT |
||||||||||
|
|||||||||||