To facilitate implementation of a globbing capability, within an embedded script interpreter for mingw-get, I initially set out to add support for the BSD d_type field in our existing dirent implementation.
The infrastructure needed to support this additional field is already present within the implementation -- the _findfirst() and _findnext() calls used to read directory content also return the required attribute data -- making addition of the feature fairly trivial. However, on inspection of the actual code, I observe the following deficiencies:--
1) The DIR and _WDIR data types, declared in dirent.h *should* be opaque data types, yet this implementation exposes all of the gory internal detail of the implementation, (which is also rather ugly, due to grossly inefficient use of memory).
2) Although ostensibly a POSIX compliant API, this implementation fails POSIX compliance in various aspects, most notably in incorrect use of errno.
3) The efficiency of the implementation is significantly degraded, as a consequence of performing certain of the underlying system calls within the wrong dirent API functions.
To remedy this, I've prepared the attached four stage cumulative patch set, delivering the following features:--
Stage 1: makes DIR and _WDIR data types opaque, as they should be.
Stage 2: rationalises memory usage within DIR and _WDIR, adding the d_type field to their associated struct dirent and struct _wdirent aggregates, with identification for BSD's DT_REG, DT_DIR and DT_UNKNOWN attributes.
Stage 3: corrects the misuse of errno.
Stage 4: refactors the code, to perform underlying system calls within more appropriate API function scope.
I propose to apply these patches in sequence, at intervals over the coming weeks. Okay to commit stage 1 after (say) seven days, to allow for preliminary review?