-/* $OpenBSD: ttyname.c,v 1.17 2016/06/27 06:10:04 espie Exp $ */
+/* $OpenBSD: ttyname.c,v 1.18 2016/06/27 16:52:30 espie Exp $ */
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
while ((dirp = readdir(dp))) {
if (dirp->d_type != DT_CHR && dirp->d_type != DT_UNKNOWN)
continue;
- if (dirp->d_namlen > len - sizeof(_PATH_DEV)) {
- (void)closedir(dp);
+ if (fstatat(dirfd(dp), dirp->d_name, &dsb, AT_SYMLINK_NOFOLLOW)
+ || !S_ISCHR(dsb.st_mode) || sb->st_rdev != dsb.st_rdev)
+ continue;
+ (void)closedir(dp);
+ if (dirp->d_namlen > len - sizeof(_PATH_DEV))
return (ERANGE);
- }
memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name,
dirp->d_namlen + 1);
- if (lstat(buf, &dsb) || !S_ISCHR(dsb.st_mode) ||
- sb->st_rdev != dsb.st_rdev)
- continue;
- (void)closedir(dp);
return (0);
}
(void)closedir(dp);