diff --git a/sc2/src/port.c b/sc2/src/port.c index 622b96b69..857ec1c61 100644 --- a/sc2/src/port.c +++ b/sc2/src/port.c @@ -21,8 +21,12 @@ */ #include +#include "errno.h" #include #include +#ifndef HAVE_READDIR_R +# include +#endif #include "port.h" #ifndef HAVE_STRUPR @@ -78,4 +82,31 @@ setenv (const char *name, const char *value, int overwrite) } #endif +#ifndef HAVE_READDIR_R +// NB. This function calls readdir() directly, and as such has the same +// reentrance issues as that function. For the purposes of UQM it will +// do though. +// Note the POSIX requires that "The pointer returned by readdir() +// points to data which may be overwritten by another call to +// readdir( ) on the same directory stream. This data is not +// overwritten by another call to readdir() on a different directory +// stream." +// NB. This function makes an extra copy of the dirent and will hence be +// slower than a direct call to readdir() or readdir_r(). +int +readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) { + struct dirent *readdir_entry; + + readdir_entry = readdir(dirp); + if (readdir_entry == NULL) { + *result = NULL; + return errno; + } + + *entry = *readdir_entry; + *result = entry; + return 0; +} +#endif + diff --git a/sc2/src/port.h b/sc2/src/port.h index aea93f0fd..01cc995b5 100644 --- a/sc2/src/port.h +++ b/sc2/src/port.h @@ -19,6 +19,11 @@ char *strupr (char *str); #endif +#ifndef HAVE_READDIR_R +# include +int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); +#endif + // Compilation related #ifdef _MSC_VER