From ea913a741eb8afedc55158d6f1eddaa5df56f3a3 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sun, 6 Oct 2013 14:17:21 +0200 Subject: [PATCH] setting environment variables differently for easy porting to mingw --- doc/shell.txt | 15 ++++++++------- src/input.cpp | 13 ++++++++++--- src/lmpwindows.h | 14 ++++---------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/doc/shell.txt b/doc/shell.txt index 793a9fc8de..49d02a0040 100644 --- a/doc/shell.txt +++ b/doc/shell.txt @@ -12,7 +12,7 @@ shell command :h3 shell cmd args :pre -cmd = {cd} or {mkdir} or {mv} or {rm} or {rmdir} or {setenv} or arbitrary command :ulb,l +cmd = {cd} or {mkdir} or {mv} or {rm} or {rmdir} or {putenv} or arbitrary command :ulb,l {cd} arg = dir dir = directory to change to {mkdir} args = dir1 dir2 ... @@ -24,9 +24,8 @@ cmd = {cd} or {mkdir} or {mv} or {rm} or {rmdir} or {setenv} or arbitrary comman file1,file2 = one or more filenames to delete {rmdir} args = dir1 dir2 ... dir1,dir2 = one or more directories to delete - {setenv} args = name value - name = name of the environment variable - value = value of the environment variable + {putenv} args = var1=value1 var2=value2 + var=value = one of more definitions of environment variables anything else is passed as a command to the shell for direct execution :pre :ule @@ -38,7 +37,7 @@ shell mkdir tmp1 tmp2 tmp3 shell rmdir tmp1 shell mv log.lammps hold/log.1 shell rm TMP/file1 TMP/file2 -shell setenv LAMMPS_POTENTIALS ../../potentials +shell putenv LAMMPS_POTENTIALS=../../potentials shell my_setup file1 10 file2 shell my_post_process 100 dump.out :pre @@ -76,9 +75,11 @@ The {rmdir} cmd executes the Unix "rmdir" command to remove one or more directories. A directory must be empty to be successfully removed. -The {setenv} cmd defines or updates an environment variable directly. +The {putenv} cmd defines or updates an environment variable directly. Since this command does not pass through the shell, no shell variable -expansion or globbing is performed, but the value string used literally. +expansion or globbing is performed, only '$' expansion of LAMMPS variables +defined with the variable_variable.html command is done, the resulting +string is then used literally. Any other cmd is passed as-is to the shell along with its arguments as one string, invoked by the C-library system() call. For example, diff --git a/src/input.cpp b/src/input.cpp index 1e40d1e4f4..6516006954 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1005,9 +1005,16 @@ void Input::shell() if (me == 0) for (int i = 1; i < narg; i++) rmdir(arg[i]); - } else if (strcmp(arg[0],"setenv") == 0) { - if (narg != 3) error->all(FLERR,"Illegal shell setenv command"); - setenv(arg[1],arg[2],1); + } else if (strcmp(arg[0],"putenv") == 0) { + if (narg < 2) error->all(FLERR,"Illegal shell putenv command"); + for (int i = 1; i < narg; i++) { + char *ptr = strdup(arg[i]); +#ifdef _WIN32 + if (ptr != NULL) _putenv(ptr); +#else + if (ptr != NULL) putenv(ptr); +#endif + } // use work string to concat args back into one string separated by spaces // invoke string in shell via system() diff --git a/src/lmpwindows.h b/src/lmpwindows.h index aa85e1ead8..50f64eafb2 100644 --- a/src/lmpwindows.h +++ b/src/lmpwindows.h @@ -39,20 +39,14 @@ inline double trunc(double x) { // Windows version of mkdir function does not have permission flags #ifndef S_IRWXU # define S_IRWXU 0 +#endif +#ifndef S_IRGRP # define S_IRGRP 0 +#endif +#ifndef S_IXGRP # define S_IXGRP 0 #endif inline int mkdir(const char *path, int){ return _mkdir(path); } -// Windows does not have setenv(). emulate with _putenv_s() -#include -inline int setenv(const char *name, const char *value, int) { - int len=strlen(name)+strlen(value)+2; - char *str = (char *)malloc(len); - strcpy(str,name); - strcat(str,"="); - strcat(str,value); - return _putenv(str); -}