Merge release-4-5-patches into release-4-6
[alexxy/gromacs.git] / src / gmxlib / statutil.c
index 75541029824cd99875ebf71ecca7cb86d7cd5a85..fd7ff31cae32ee9c4af1162dcdcef699d0a499db 100644 (file)
@@ -39,6 +39,7 @@
 
 
 #include <ctype.h>
+#include <assert.h>
 #include "copyrite.h"
 #include "sysstuff.h"
 #include "macros.h"
@@ -57,7 +58,7 @@
 #include "mtop_util.h"
 #include "gmxfio.h"
 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
 #include "thread_mpi.h"
 #endif
 
@@ -80,7 +81,7 @@
 static const char *program_name=NULL;
 static char *cmd_line=NULL;
 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
 /* For now, some things here are simply not re-entrant, so
    we have to actively lock them. */
 static tMPI_Thread_mutex_t init_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
@@ -99,29 +100,26 @@ static tMPI_Thread_mutex_t init_mutex=TMPI_THREAD_MUTEX_INITIALIZER;
 const char *ShortProgram(void)
 {
     const char *pr,*ret;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&init_mutex);
 #endif
     pr=ret=program_name; 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&init_mutex);
 #endif
     if ((pr=strrchr(ret,DIR_SEPARATOR)) != NULL)
         ret=pr+1;
-    /* Strip away the libtool prefix if it's still there. */
-    if(strlen(ret) > 3 && !strncmp(ret, "lt-", 3))
-        ret = ret + 3;
     return ret;
 }
 
 const char *Program(void)
 {
     const char *ret;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&init_mutex);
 #endif
     ret=program_name; 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&init_mutex);
 #endif
     return ret;
@@ -130,11 +128,11 @@ const char *Program(void)
 const char *command_line(void)
 {
     const char *ret;
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&init_mutex);
 #endif
     ret=cmd_line; 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&init_mutex);
 #endif
     return ret;
@@ -142,23 +140,25 @@ const char *command_line(void)
 
 void set_program_name(const char *argvzero)
 {
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&init_mutex);
 #endif
-    /* When you run a dynamically linked program before installing
-     * it, libtool uses wrapper scripts and prefixes the name with "lt-".
-     * Until libtool is fixed to set argv[0] right, rip away the prefix:
-     */
     if (program_name == NULL)
     {
-        if(strlen(argvzero)>3 && !strncmp(argvzero,"lt-",3))
-            program_name=strdup(argvzero+3);
-        else
-            program_name=strdup(argvzero);
+        /* if filename has file ending (e.g. .exe) then strip away */
+         char* extpos=strrchr(argvzero,'.');
+         if(extpos > strrchr(argvzero,DIR_SEPARATOR))
+         {
+             program_name=gmx_strndup(argvzero,extpos-argvzero);
+         }
+         else
+         {
+             program_name=gmx_strdup(argvzero);
+         }
     }
     if (program_name == NULL)
         program_name="GROMACS";
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&init_mutex);
 #endif
 }
@@ -169,7 +169,7 @@ void set_command_line(int argc, char *argv[])
     int i;
     size_t cmdlength;
 
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_lock(&init_mutex);
 #endif
     if (cmd_line==NULL)
@@ -188,7 +188,7 @@ void set_command_line(int argc, char *argv[])
             strcat(cmd_line," ");
         }
     }
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
     tMPI_Thread_mutex_unlock(&init_mutex);
 #endif
 
@@ -252,7 +252,7 @@ int check_times(real t)
 
 static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
 {
-    int i,j;
+    int i=0,j;
     const char *select = NULL;
 
     if (bCanTime)
@@ -267,7 +267,8 @@ static void set_default_time_unit(const char *time_list[], gmx_bool bCanTime)
             }
         }
     }
-    if (!bCanTime || select == NULL || strcmp(time_list[i], select) != 0)
+    if (!bCanTime || select == NULL || 
+        time_list[i]==NULL || strcmp(time_list[i], select) != 0)
     {
         /* Set it to the default: ps */
         i = 1;
@@ -334,8 +335,8 @@ t_topology *read_top(const char *fn,int *ePBC)
 
 static void usage(const char *type,const char *arg)
 {
-    if (arg != NULL)
-        gmx_fatal(FARGS,"Expected %s argument for option %s\n",type,arg);
+    assert(arg);
+    gmx_fatal(FARGS,"Expected %s argument for option %s\n",type,arg);
 }
 
 int iscan(int argc,char *argv[],int *i)
@@ -737,14 +738,14 @@ void parse_common_args(int *argc,char *argv[],unsigned long Flags,
     /* The some system, e.g. the catamount kernel on cray xt3 do not have nice(2). */
     if (nicelevel != 0 && !bExit)
     {
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
         static gmx_bool nice_set=FALSE; /* only set it once */
         tMPI_Thread_mutex_lock(&init_mutex);
         if (!nice_set)
         {
 #endif
             i=nice(nicelevel); /* assign ret value to avoid warnings */
-#ifdef GMX_THREADS
+#ifdef GMX_THREAD_MPI
             nice_set=TRUE;
         }
         tMPI_Thread_mutex_unlock(&init_mutex);
@@ -819,10 +820,10 @@ void parse_common_args(int *argc,char *argv[],unsigned long Flags,
             gmx_cmd(argv[1]);
         }
     } 
-    if (bExit) {
-        if (gmx_parallel_env_initialized())
-            /*gmx_abort(gmx_node_rank(),gmx_node_num(),0);*/
-            gmx_finalize();
+    if (bExit)
+    {
+        gmx_finalize_par();
+
         exit(0);
     }
 #undef FF