Follow style(9) in example code and handle opendir(3) error.

This commit is contained in:
Pawel Jakub Dawidek 2011-03-26 07:15:57 +00:00
parent 8c98d9bae1
commit 32981eb31b

View File

@ -209,13 +209,16 @@ Sample code which searches 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)) {
if (dirp == NULL)
return (ERROR);
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) {
(void)closedir(dirp);
return FOUND;
return (FOUND);
}
}
(void)closedir(dirp);
return NOT_FOUND;
return (NOT_FOUND);
.Ed
.Sh SEE ALSO
.Xr close 2 ,