diff --git a/applications/test/foamEnv/Make/files b/applications/test/foamEnv/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..b67805d076bf57325816360942716e7b906121b6 --- /dev/null +++ b/applications/test/foamEnv/Make/files @@ -0,0 +1,3 @@ +Test-foamEnv.C + +EXE = $(FOAM_USER_APPBIN)/Test-foamEnv diff --git a/applications/test/foamEnv/Make/options b/applications/test/foamEnv/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..18e6fe47afacb902cddccf82632772447704fd88 --- /dev/null +++ b/applications/test/foamEnv/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/foamEnv/Test-foamEnv.C b/applications/test/foamEnv/Test-foamEnv.C new file mode 100644 index 0000000000000000000000000000000000000000..0555c8be11f4da48f5e4706672997d523b498493 --- /dev/null +++ b/applications/test/foamEnv/Test-foamEnv.C @@ -0,0 +1,79 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Application + Test-etcFiles + +Description + Test etcFiles functionality. + Similar to foamEtcFile script, but automatically prunes nonexistent + directories from the list. + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "OSspecific.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::noFunctionObjects(); + argList::removeOption("case"); + + argList::addArgument("env..."); + + argList::addNote + ( + "Simple test/report OpenFOAM environment" + ); + + argList args(argc, argv, false, true); + + for (int argi = 1; argi < args.size(); ++argi) + { + const std::string envName(args[argi]); + + if (hasEnv(envName)) + { + Info<<"Have env " << envName.c_str() << "=" << getEnv(envName) + << nl; + } + else + { + Info<<"No env " << envName.c_str()<< nl; + } + } + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/MSwindows/MSwindows.C b/src/OSspecific/MSwindows/MSwindows.C index 6afa4e03f2b4e94e0b0e580af6466766902e215c..46f2ae99bbb1f9f30b78213bd12fe78d77d8165f 100644 --- a/src/OSspecific/MSwindows/MSwindows.C +++ b/src/OSspecific/MSwindows/MSwindows.C @@ -7,7 +7,7 @@ ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011 Symscape - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -360,7 +360,7 @@ pid_t Foam::pgid() } -bool Foam::env(const std::string& envName) +bool Foam::hasEnv(const std::string& envName) { // An empty envName => always false return !envName.empty() && diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index e32a27c1fa1aadb8652a6e5a1193cd8c12d29a2c..2ce70e3718242f809275d28528a5e6325f889ff8 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -263,7 +263,7 @@ pid_t Foam::pgid() } -bool Foam::env(const std::string& envName) +bool Foam::hasEnv(const std::string& envName) { // An empty envName => always false return !envName.empty() && ::getenv(envName.c_str()) != nullptr; diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index c0cacb3185eead7e811f73cc91c6db236d7e9fc1..9ec6377dff15e52e6ce037a205a5dc03471ea36f 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -187,7 +187,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) } } - if (env("FOAM_ABORT")) + if (hasEnv("FOAM_ABORT")) { Perr<< nl << *this << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; @@ -246,7 +246,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) void Foam::IOerror::exit(const int) { - exitOrAbort(1, env("FOAM_ABORT")); + exitOrAbort(1, hasEnv("FOAM_ABORT")); } diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index dff61cfac9ad503417f7db4bd770b93bc74ff6d4..897b80a4cad18de99c238a3642de4fdd163f1f7b 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -238,7 +238,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) } } - if (env("FOAM_ABORT")) + if (hasEnv("FOAM_ABORT")) { Perr<< nl << *this << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; @@ -297,7 +297,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) void Foam::error::exit(const int errNo) { - exitOrAbort(errNo, env("FOAM_ABORT")); + exitOrAbort(errNo, hasEnv("FOAM_ABORT")); } diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 0c4fc94c5509cdb2f339e0862d88fea6c303d54f..6c2a96f39938229a74a13e28940640857d6bd3ed 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -66,16 +66,21 @@ pid_t pgid(); //- True if environment variable of given name is defined. // Using an empty name is a no-op and always returns false. -bool env(const std::string& envName); +bool hasEnv(const std::string& envName); //- Get environment value for given envName. -// Return string() if the environment is undefined or envName is empty. +// \return empty string if environment is undefined or envName is empty. string getEnv(const std::string& envName); //- Set an environment variable, return true on success. // Using an empty name is a no-op and always returns false. bool setEnv(const word& name, const std::string& value, const bool overwrite); +//- Deprecated(2020-05) check for existence of environment variable +// \deprecated(2020-05) - use hasEnv() function +FOAM_DEPRECATED_FOR(2020-05, "hasEnv() function") +inline bool env(const std::string& envName) { return Foam::hasEnv(envName); } + //- Return the system's host name, as per hostname(1) // Optionally with the full name (as per the '-f' option) string hostName(const bool full=false);