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.
.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