diff options
author | Ed Schouten <ed@FreeBSD.org> | 2016-07-28 16:02:30 +0000 |
---|---|---|
committer | Ed Schouten <ed@FreeBSD.org> | 2016-07-28 16:02:30 +0000 |
commit | f0d29192435a0a2fd2dca8e566a2cb3daefeb616 (patch) | |
tree | 55ed73dfe5222733e54e66f63b9c3d3d234a7dbf /usr.sbin | |
parent | 88ad2d7b47fcea0374fb769472addc7e8065b929 (diff) | |
download | src-f0d29192435a0a2fd2dca8e566a2cb3daefeb616.tar.gz src-f0d29192435a0a2fd2dca8e566a2cb3daefeb616.zip |
Clean up use of basename() and dirname().
Pull copies of the input pathname string before calling basename() and
dirname() to make this comply to POSIX. Free these copies at the end of
this function. While there, remove the duplication of the 's' ->
'logfname' string. There is no need for this.
Notes
Notes:
svn path=/head/; revision=303449
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/newsyslog/newsyslog.c | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c index 4108ac988c44..766c756e2e7b 100644 --- a/usr.sbin/newsyslog/newsyslog.c +++ b/usr.sbin/newsyslog/newsyslog.c @@ -2286,26 +2286,29 @@ mtime_old_timelog(const char *file) time_t t; struct dirent *dp; DIR *dirp; - char *s, *logfname, *dir; + char *logfname, *logfnamebuf, *dir, *dirbuf; t = -1; - if ((dir = dirname(file)) == NULL) { - warn("dirname() of '%s'", file); + if ((dirbuf = strdup(file)) == NULL) { + warn("strdup() of '%s'", file); return (t); } - if ((s = basename(file)) == NULL) { - warn("basename() of '%s'", file); + dir = dirname(dirbuf); + if ((logfnamebuf = strdup(file)) == NULL) { + warn("strdup() of '%s'", file); + free(dirbuf); return (t); - } else if (s[0] == '/') { - warnx("Invalid log filename '%s'", s); - return (t); - } else if ((logfname = strdup(s)) == NULL) - err(1, "strdup()"); + } + logfname = basename(logfnamebuf); + if (logfname[0] == '/') { + warnx("Invalid log filename '%s'", logfname); + goto out; + } if ((dirp = opendir(dir)) == NULL) { warn("Cannot open log directory '%s'", dir); - return (t); + goto out; } dir_fd = dirfd(dirp); /* Open the archive dir and find the most recent archive of logfname. */ @@ -2322,6 +2325,9 @@ mtime_old_timelog(const char *file) } closedir(dirp); +out: + free(dirbuf); + free(logfnamebuf); return (t); } |