#include <sys/stat.h>
#include <sys/types.h>
-#ifdef HAVE_DIRENT_H
-/* POSIX */
-#include <dirent.h>
-#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef GMX_NATIVE_WINDOWS
-#include <direct.h>
+#include <direct.h> // For _chdir() and _getcwd()
#include <io.h>
#include <windows.h>
#endif
#endif
}
-/* Our own implementation of dirent-like functionality to scan directories. */
-struct gmx_directory
-{
-#if defined(GMX_NATIVE_WINDOWS)
- intptr_t windows_handle;
- struct _finddata_t finddata;
- int first;
-#elif defined(HAVE_DIRENT_H)
- DIR * dirent_handle;
-#else
- int dummy;
-#endif
-};
-
-
-int
-gmx_directory_open(gmx_directory_t *p_gmxdir, const char *dirname)
-{
- struct gmx_directory * gmxdir;
- int rc;
-
- snew(gmxdir, 1);
-
- *p_gmxdir = gmxdir;
-
-#if defined(GMX_NATIVE_WINDOWS)
- if (dirname != NULL && strlen(dirname) > 0)
- {
- char * tmpname;
- int len;
-
- len = strlen(dirname);
- snew(tmpname, len+3);
-
- strncpy(tmpname, dirname, len+1);
-
- /* Remove possible trailing directory separator */
- if (tmpname[len] == '/' || tmpname[len] == '\\')
- {
- tmpname[len] = '\0';
- }
-
- /* Add wildcard */
- strcat(tmpname, "/*");
-
- gmxdir->first = 1;
- if ( (gmxdir->windows_handle = _findfirst(tmpname, &gmxdir->finddata)) > 0L)
- {
- rc = 0;
- }
- else
- {
- if (errno == EINVAL)
- {
- sfree(gmxdir);
- *p_gmxdir = NULL;
- rc = EINVAL;
- }
- else
- {
- rc = 0;
- }
- }
- }
- else
- {
- rc = EINVAL;
- }
-#elif defined(HAVE_DIRENT_H)
- if ( (gmxdir->dirent_handle = opendir(dirname)) != NULL)
- {
- rc = 0;
- }
- else
- {
- sfree(gmxdir);
- *p_gmxdir = NULL;
- rc = EINVAL;
- }
-#else
- gmx_fatal(FARGS,
- "Source compiled without POSIX dirent or windows support - cannot scan directories.\n"
- "In the very unlikely event this is not a compile-time mistake you could consider\n"
- "implementing support for your platform in futil.c, but contact the developers\n"
- "to make sure it's really necessary!\n");
- rc = -1;
-#endif
- return rc;
-}
-
-
-int
-gmx_directory_nextfile(gmx_directory_t gmxdir, char *name, int maxlength_name)
-{
- int rc;
-
-#if defined(GMX_NATIVE_WINDOWS)
- if (gmxdir != NULL)
- {
- if (gmxdir->windows_handle <= 0)
- {
-
- name[0] = '\0';
- rc = ENOENT;
- }
- else if (gmxdir->first == 1)
- {
- strncpy(name, gmxdir->finddata.name, maxlength_name);
- rc = 0;
- gmxdir->first = 0;
- }
- else
- {
- if (_findnext(gmxdir->windows_handle, &gmxdir->finddata) == 0)
- {
- strncpy(name, gmxdir->finddata.name, maxlength_name);
- rc = 0;
- }
- else
- {
- name[0] = '\0';
- rc = ENOENT;
- }
- }
- }
- else
- {
- name[0] = '\0';
- rc = EINVAL;
- }
-#elif defined(HAVE_DIRENT_H)
- struct dirent * direntp_large;
- struct dirent * p;
-
-
- if (gmxdir != NULL && gmxdir->dirent_handle != NULL)
- {
- /* On some platforms no space is present for d_name in dirent.
- * Since d_name is guaranteed to be the last entry, allocating
- * extra space for dirent will allow more size for d_name.
- * GMX_MAX_PATH should always be >= the max possible d_name.
- */
- smalloc(direntp_large, sizeof(*direntp_large) + GMX_PATH_MAX);
- rc = readdir_r(gmxdir->dirent_handle, direntp_large, &p);
-
- if (p != NULL && rc == 0)
- {
- strncpy(name, direntp_large->d_name, maxlength_name);
- }
- else
- {
- name[0] = '\0';
- rc = ENOENT;
- }
- sfree(direntp_large);
- }
- else
- {
- name[0] = '\0';
- rc = EINVAL;
- }
-#else
- gmx_fatal(FARGS,
- "Source compiled without POSIX dirent or windows support - cannot scan directories.\n");
- rc = -1;
-#endif
- return rc;
-}
-
-
-int
-gmx_directory_close(gmx_directory_t gmxdir)
-{
- int rc;
-#if defined(GMX_NATIVE_WINDOWS)
- rc = (gmxdir != NULL) ? _findclose(gmxdir->windows_handle) : EINVAL;
-#elif defined(HAVE_DIRENT_H)
- rc = (gmxdir != NULL) ? closedir(gmxdir->dirent_handle) : EINVAL;
-#else
- gmx_fatal(FARGS,
- "Source compiled without POSIX dirent or windows support - cannot scan directories.\n");
- rc = -1;
-#endif
-
- sfree(gmxdir);
- return rc;
-}
-
char *low_gmxlibfn(const char *file, gmx_bool bAddCWD, gmx_bool bFatal)
{