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.
This commit is contained in:
Ed Schouten 2016-07-28 16:02:30 +00:00
parent 88ad2d7b47
commit f0d2919243

View File

@ -2286,26 +2286,29 @@ mtime_old_timelog(const char *file)
time_t t; time_t t;
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
char *s, *logfname, *dir; char *logfname, *logfnamebuf, *dir, *dirbuf;
t = -1; t = -1;
if ((dir = dirname(file)) == NULL) { if ((dirbuf = strdup(file)) == NULL) {
warn("dirname() of '%s'", file); warn("strdup() of '%s'", file);
return (t); return (t);
} }
if ((s = basename(file)) == NULL) { dir = dirname(dirbuf);
warn("basename() of '%s'", file); if ((logfnamebuf = strdup(file)) == NULL) {
warn("strdup() of '%s'", file);
free(dirbuf);
return (t); return (t);
} else if (s[0] == '/') { }
warnx("Invalid log filename '%s'", s); logfname = basename(logfnamebuf);
return (t); if (logfname[0] == '/') {
} else if ((logfname = strdup(s)) == NULL) warnx("Invalid log filename '%s'", logfname);
err(1, "strdup()"); goto out;
}
if ((dirp = opendir(dir)) == NULL) { if ((dirp = opendir(dir)) == NULL) {
warn("Cannot open log directory '%s'", dir); warn("Cannot open log directory '%s'", dir);
return (t); goto out;
} }
dir_fd = dirfd(dirp); dir_fd = dirfd(dirp);
/* Open the archive dir and find the most recent archive of logfname. */ /* Open the archive dir and find the most recent archive of logfname. */
@ -2322,6 +2325,9 @@ mtime_old_timelog(const char *file)
} }
closedir(dirp); closedir(dirp);
out:
free(dirbuf);
free(logfnamebuf);
return (t); return (t);
} }