Merge release-4-5-patches into release-4-6
[alexxy/gromacs.git] / src / gmxlib / futil.c
index a88fff02b622e647e0f3c8c9c170ab8f9165c9f1..ee4d0eccfe5fe4d7100962be0dd96814be8e2c80 100644 (file)
@@ -36,6 +36,7 @@
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
+#include "gmx_header_config.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -50,7 +51,7 @@
 #endif
 
 
-#if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
+#ifdef GMX_NATIVE_WINDOWS
 #include <direct.h>
 #include <io.h>
 #endif
@@ -64,7 +65,7 @@
 #include "statutil.h"
 
 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
 #include "thread_mpi.h"
 #endif
 
@@ -85,7 +86,7 @@ typedef struct t_pstack {
 static t_pstack *pstack=NULL;
 static gmx_bool     bUnbuffered=FALSE;
 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
 /* this linked list is an intrinsically globally shared object, so we have
    to protect it with mutexes */
 static tMPI_Thread_mutex_t pstack_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
@@ -100,7 +101,7 @@ void push_ps(FILE *fp)
 {
     t_pstack *ps;
 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&pstack_mutex);
 #endif
 
@@ -108,7 +109,7 @@ void push_ps(FILE *fp)
     ps->fp   = fp;
     ps->prev = pstack;
     pstack   = ps;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&pstack_mutex);
 #endif
 }
@@ -149,7 +150,7 @@ int ffclose(FILE *fp)
 #else
     t_pstack *ps,*tmp;
     int ret=0;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&pstack_mutex);
 #endif
 
@@ -167,7 +168,7 @@ int ffclose(FILE *fp)
     else {
         while ((ps->prev != NULL) && (ps->prev->fp != fp))
             ps=ps->prev;
-        if (ps->prev->fp == fp) {
+        if ((ps->prev != NULL) && ps->prev->fp == fp) {
             if (ps->prev->fp != NULL)
                 ret = pclose(ps->prev->fp);
             tmp=ps->prev;
@@ -179,7 +180,7 @@ int ffclose(FILE *fp)
                 ret = fclose(fp);
         }
     }
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&pstack_mutex);
 #endif
     return ret;
@@ -194,7 +195,7 @@ int ffclose(FILE *fp)
 void frewind(FILE *fp)
 {
     t_pstack *ps;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&pstack_mutex);
 #endif
 
@@ -202,7 +203,7 @@ void frewind(FILE *fp)
     while (ps != NULL) {
         if (ps->fp == fp) {
             fprintf(stderr,"Cannot rewind compressed file!\n");
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
             tMPI_Thread_mutex_unlock(&pstack_mutex);
 #endif
             return;
@@ -210,7 +211,7 @@ void frewind(FILE *fp)
         ps=ps->prev;
     }
     rewind(fp);
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&pstack_mutex);
 #endif
 }
@@ -245,21 +246,21 @@ gmx_off_t gmx_ftell(FILE *stream)
 gmx_bool is_pipe(FILE *fp)
 {
     t_pstack *ps;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&pstack_mutex);
 #endif
 
     ps=pstack;
     while (ps != NULL) {
         if (ps->fp == fp) {
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
             tMPI_Thread_mutex_unlock(&pstack_mutex);
 #endif
             return TRUE;
         }
         ps=ps->prev;
     }
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&pstack_mutex);
 #endif
     return FALSE;
@@ -303,7 +304,7 @@ gmx_bool gmx_fexist(const char *fname)
     test=fopen(fname,"r");
     if (test == NULL) {
         /*Windows doesn't allow fopen of directory - so we need to check this seperately */
-        #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__) 
+        #ifdef GMX_NATIVE_WINDOWS
             DWORD attr = GetFileAttributes(fname);
             return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
         #else 
@@ -477,6 +478,11 @@ FILE *ffopen(const char *file,const char *mode)
     gmx_bool bRead;
     int  bs;
 
+    if (file == NULL) 
+    {
+        return NULL;
+    }
+
     if (mode[0]=='w') {
         make_backup(file);
     }
@@ -530,7 +536,7 @@ struct gmx_directory
 {
 #ifdef HAVE_DIRENT_H
     DIR  *               dirent_handle;
-#elif (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64)
+#elif (defined GMX_NATIVE_WINDOWS)
     intptr_t             windows_handle;
     struct _finddata_t   finddata;
     int                  first;
@@ -561,7 +567,7 @@ gmx_directory_open(gmx_directory_t *p_gmxdir,const char *dirname)
         *p_gmxdir = NULL;
         rc        = EINVAL;
     }
-#elif (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64)
+#elif (defined GMX_NATIVE_WINDOWS)
     
     if(dirname!=NULL && strlen(dirname)>0)
     {
@@ -656,7 +662,7 @@ gmx_directory_nextfile(gmx_directory_t gmxdir,char *name,int maxlength_name)
         rc      = EINVAL;
     }
     
-#elif (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64)
+#elif (defined GMX_NATIVE_WINDOWS)
     
     if(gmxdir!=NULL)
     {
@@ -702,7 +708,7 @@ gmx_directory_close(gmx_directory_t gmxdir)
     int                     rc;
 #ifdef HAVE_DIRENT_H
     rc = (gmxdir != NULL) ? closedir(gmxdir->dirent_handle) : EINVAL;
-#elif (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64)
+#elif (defined GMX_NATIVE_WINDOWS)
     rc = (gmxdir != NULL) ? _findclose(gmxdir->windows_handle) : EINVAL;
 #else
     gmx_fatal(FARGS,
@@ -760,7 +766,7 @@ gmx_bool search_subdirs(const char *parent, char *libdir)
  */
 static gmx_bool filename_is_absolute(char *name)
 {
-#if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
+#ifdef GMX_NATIVE_WINDOWS
     return ((name[0] == DIR_SEPARATOR) || ((strlen(name)>3) && strncmp(name+1,":\\",2)) == 0);
 #else
     return (name[0] == DIR_SEPARATOR);
@@ -791,7 +797,7 @@ gmx_bool get_libdir(char *libdir)
     /* On windows & cygwin we need to add the .exe extension
      * too, or we wont be able to detect that the file exists
      */
-#if (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || defined __CYGWIN__ || defined __CYGWIN32__)
+#if (defined GMX_NATIVE_WINDOWS || defined GMX_CYGWIN)
     if(strlen(bin_name)<3 || gmx_strncasecmp(bin_name+strlen(bin_name)-4,".exe",4))
         strcat(bin_name,".exe");
 #endif
@@ -802,7 +808,7 @@ gmx_bool get_libdir(char *libdir)
         if (!strchr(bin_name,DIR_SEPARATOR)) {
             /* No slash or backslash in name means it must be in the path - search it! */
             /* Add the local dir since it is not in the path on windows */
-#if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
+#ifdef GMX_NATIVE_WINDOWS
             pdum=_getcwd(system_path,sizeof(system_path)-1);
 #else
             pdum=getcwd(system_path,sizeof(system_path)-1);
@@ -831,7 +837,7 @@ gmx_bool get_libdir(char *libdir)
              * it does not start at the root, i.e.
              * name is relative to the current dir 
              */
-#if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
+#ifdef GMX_NATIVE_WINDOWS
             pdum=_getcwd(buf,sizeof(buf)-1);
 #else
             pdum=getcwd(buf,sizeof(buf)-1);
@@ -844,7 +850,7 @@ gmx_bool get_libdir(char *libdir)
         /* Now we should have a full path and name in full_path,
          * but on unix it might be a link, or a link to a link to a link..
          */
-#if (!defined WIN32 && !defined _WIN32 && !defined WIN64 && !defined _WIN64)
+#ifndef GMX_NATIVE_WINDOWS
         while( (i=readlink(full_path,buf,sizeof(buf)-1)) > 0 ) {
             buf[i]='\0';
             /* If it doesn't start with "/" it is relative */
@@ -872,7 +878,7 @@ gmx_bool get_libdir(char *libdir)
      * locations before giving up, in case we are running from e.g. 
      * a users home directory. This only works on unix or cygwin...
      */
-#if ((!defined WIN32 && !defined _WIN32 && !defined WIN64 && !defined _WIN64) || defined __CYGWIN__ || defined __CYGWIN32__)
+#ifndef GMX_NATIVE_WINDOWS
     if(!found) 
         found=search_subdirs("/usr/local",libdir);
     if(!found) 
@@ -989,7 +995,7 @@ void gmx_tmpnam(char *buf)
      * since windows doesnt support it we have to separate the cases.
      * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
      */
-#if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
+#ifdef GMX_NATIVE_WINDOWS
     _mktemp(buf);
 #else
     fd = mkstemp(buf);
@@ -1036,7 +1042,7 @@ int gmx_truncatefile(char *path, gmx_off_t length)
 
 int gmx_file_rename(const char *oldname, const char *newname)
 {
-#if (!(defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)))
+#ifndef GMX_NATIVE_WINDOWS
     /* under unix, rename() is atomic (at least, it should be). */
     return rename(oldname, newname);
 #else