As documented in the glibc manual, “Some systems don’t define the d_name
element sufficiently long”, and it provides an example of using a union to
properly allocate the storage under the dirent.
{
DIR *d = xopendir (".");
- struct dirent buf = { 0, };
- TEST_VERIFY (xreaddir_r (d, &buf));
- TEST_COMPARE_STRING (buf.d_name, ".");
- while (xreaddir_r (d, &buf))
+ union
+ {
+ struct dirent d;
+ char b[offsetof (struct dirent, d_name) + NAME_MAX + 1];
+ } buf;
+ TEST_VERIFY (xreaddir_r (d, &buf.d));
+ TEST_COMPARE_STRING (buf.d.d_name, ".");
+ while (xreaddir_r (d, &buf.d))
;
xclosedir (d);
}