From 7a0e2aeedbea2e103eddc2b308b1daa4361c5249 Mon Sep 17 00:00:00 2001 From: deraadt Date: Tue, 23 Apr 1996 11:47:15 +0000 Subject: [PATCH] do not assume "." exists -- it might have gotten unlink()'ed -- we don't want closedir() to SIGSEGV. as well, sample code in man pages should be very robust and not hint that closedir() might survive being passed opendir()'s failure code. --- lib/libc/gen/directory.3 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/libc/gen/directory.3 b/lib/libc/gen/directory.3 index 4cd377abe7d..15bf5c1e952 100644 --- a/lib/libc/gen/directory.3 +++ b/lib/libc/gen/directory.3 @@ -157,12 +157,14 @@ Sample code which searchs a directory for entry ``name'' is: .Bd -literal -offset indent len = strlen(name); dirp = opendir("."); -while ((dp = readdir(dirp)) != NULL) - if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { - (void)closedir(dirp); - return FOUND; - } -(void)closedir(dirp); +if (dirp) { + while ((dp = readdir(dirp)) != NULL) + if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { + (void)closedir(dirp); + return FOUND; + } + (void)closedir(dirp); +} return NOT_FOUND; .Ed .Sh SEE ALSO -- 2.20.1