Hi, I recently got caught out by scandir's quirky memory allocation. Should we add a note so that others don't have to go through this pain too?
Index: scandir.3 =================================================================== RCS file: /cvs/src/lib/libc/gen/scandir.3,v retrieving revision 1.10 diff -u -p -u -r1.10 scandir.3 --- scandir.3 26 Mar 2010 19:30:40 -0000 1.10 +++ scandir.3 19 Jan 2012 12:08:15 -0000 @@ -27,7 +27,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: March 26 2010 $ +.Dd $Mdocdate: January 19 2012 $ .Dt SCANDIR 3 .Os .Sh NAME @@ -53,6 +53,24 @@ It returns the number of entries in the A pointer to the array of directory entries is stored in the location referenced by .Fa namelist . +.Pp +Note that after a call to +.Fn scandir +the allocation size of each +.Fa dirent +structure within the +.Fa namelist +array is not necessarily +.Fa sizeof(struct direct) . +For each directory entry, +.Fn scandir +will allocate space for the integer fields plus as many bytes as is +required to store the NULL-terminated string +.Fa d_name +rounded up to a 4-byte boundary. See +.Xr dir 5 +for the exact layout of +.Fa struct dirent . .Pp The .Fa select -- Best Regards Edd Barrett http://www.theunixzoo.co.uk
