diff --git a/usr.bin/stat/stat.c b/usr.bin/stat/stat.c index 1ab10ea4b08b..63c242b9b9b0 100644 --- a/usr.bin/stat/stat.c +++ b/usr.bin/stat/stat.c @@ -30,7 +30,7 @@ #include #if 0 #ifndef lint -__RCSID("$NetBSD: stat.c,v 1.13 2003/07/25 03:21:17 atatat Exp $"); +__RCSID("$NetBSD: stat.c,v 1.18 2004/05/28 04:48:31 atatat Exp $"); #endif #endif @@ -52,6 +52,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -313,8 +314,17 @@ main(int argc, char *argv[]) rc = fstat(STDIN_FILENO, &st); } else { file = argv[0]; - if (usestat) - rc = stat(file, &st); + if (usestat) { + /* + * Try stat() and if it fails, fall back to + * lstat() just in case we're examining a + * broken symlink. + */ + if ((rc = stat(file, &st)) == -1 && + errno == ENOENT && + (rc = lstat(file, &st)) == -1) + errno = ENOENT; + } else rc = lstat(file, &st); }