From b31d5b56ecc4f4ef990ea85714a3b21789a1006d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 11 May 2007 11:10:05 +0000 Subject: [PATCH] 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 --- lib/libutil/pidfile.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c index e002d6534a6d..983d103b9ae1 100644 --- a/lib/libutil/pidfile.c +++ b/lib/libutil/pidfile.c @@ -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);