readdir.3: Handle -1 error from pathconf() in example code snippet

Improve the example demonstrating allocation of a buffer
for readdir_r() to handle -1 error return from pathconf().
Otherwise, naive readers may think that pathconf() return
value can be used without checking.

Reported-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2012-07-07 07:40:07 +02:00
parent 02ff975dc7
commit eb059156a7
1 changed files with 5 additions and 3 deletions

View File

@ -31,7 +31,7 @@
.\" Rework discussion of nonstandard structure fields.
.\" 2008-09-11, mtk, Document readdir_r().
.\"
.TH READDIR 3 2010-09-10 "" "Linux Programmer's Manual"
.TH READDIR 3 2012-07-07 "" "Linux Programmer's Manual"
.SH NAME
readdir, readdir_r \- read a directory
.SH SYNOPSIS
@ -226,8 +226,10 @@ as follows:
.in +4n
.nf
len = offsetof(struct dirent, d_name) +
pathconf(dirpath, _PC_NAME_MAX) + 1
name_max = pathconf(dirpath, _PC_NAME_MAX);
if (name_max == \-1) /* Limit not defined, or error */
name_max = 255; /* Take a guess */
len = offsetof(struct dirent, d_name) + name_max + 1;
entryp = malloc(len);
.fi