strlcpy() may be faster than snprintf(), but it is less portable, and this
is not performance critical code anyway. Also, avoid using strlen() to obtain information which we already have. MFC after: 3 weeks
This commit is contained in:
parent
b34aab2337
commit
b31d5b56ec
@ -88,19 +88,19 @@ pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
|
||||
{
|
||||
struct pidfh *pfh;
|
||||
struct stat sb;
|
||||
int error, fd;
|
||||
int error, fd, len;
|
||||
|
||||
pfh = malloc(sizeof(*pfh));
|
||||
if (pfh == NULL)
|
||||
return (NULL);
|
||||
|
||||
if (path == NULL) {
|
||||
snprintf(pfh->pf_path, sizeof(pfh->pf_path), "/var/run/%s.pid",
|
||||
getprogname());
|
||||
} else {
|
||||
strlcpy(pfh->pf_path, path, sizeof(pfh->pf_path));
|
||||
}
|
||||
if (strlen(pfh->pf_path) == sizeof(pfh->pf_path) - 1) {
|
||||
if (path == NULL)
|
||||
len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
|
||||
"/var/run/%s.pid", getprogname());
|
||||
else
|
||||
len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
|
||||
"%s", path);
|
||||
if (len >= (int)sizeof(pfh->pf_path)) {
|
||||
free(pfh);
|
||||
errno = ENAMETOOLONG;
|
||||
return (NULL);
|
||||
|
Loading…
Reference in New Issue
Block a user