From: deraadt Date: Tue, 23 Apr 1996 11:47:15 +0000 (+0000) Subject: do not assume "." exists -- it might have gotten unlink()'ed -- we X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=7a0e2aeedbea2e103eddc2b308b1daa4361c5249;p=openbsd 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. --- 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