do not assume "." exists -- it might have gotten unlink()'ed -- we
authorderaadt <deraadt@openbsd.org>
Tue, 23 Apr 1996 11:47:15 +0000 (11:47 +0000)
committerderaadt <deraadt@openbsd.org>
Tue, 23 Apr 1996 11:47:15 +0000 (11:47 +0000)
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

index 4cd377a..15bf5c1 100644 (file)
@@ -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