1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
28 Functions used by OpenFOAM that are specific to POSIX compliant
29 operating systems and need to be replaced or emulated on other systems.
34 \*---------------------------------------------------------------------------*/
39 #include "fileNameList.H"
42 #include <sys/types.h>
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 //- Return the PID of this process
54 //- Return the parent PID of this process
57 //- Return the group PID of this process
60 //- Return true if environment variable of given name is defined
61 bool env(const word&);
63 //- Return environment variable of given name
64 // Return string() if the environment is undefined
65 string getEnv(const word&);
67 //- Set an environment variable
68 bool setEnv(const word& name, const std::string& value, const bool overwrite);
70 //- Return the system's host name, as per hostname(1)
71 // Optionally with the full name (as per the '-f' option)
72 word hostName(const bool full=false);
74 //- Return the system's domain name, as per hostname(1) with the '-d' option
77 //- Return the user's login name
80 //- Is user administrator
81 bool isAdministrator();
83 //- Return home directory path name for the current user
86 //- Return home directory path name for a particular user
87 fileName home(const word& userName);
89 //- Return current working directory path name
92 //- Change the current directory to the one given and return true,
94 bool chDir(const fileName& dir);
96 //- Search for files from user/group/shipped directories.
97 // The search scheme allows for version-specific and
98 // version-independent files using the following hierarchy:
99 // - \b user settings:
100 // - ~/.OpenFOAM/\<VERSION\>
102 // - \b group (site) settings (when $WM_PROJECT_SITE is set):
103 // - $WM_PROJECT_SITE/\<VERSION\>
104 // - $WM_PROJECT_SITE
105 // - \b group (site) settings (when $WM_PROJECT_SITE is not set):
106 // - $WM_PROJECT_INST_DIR/site/\<VERSION\>
107 // - $WM_PROJECT_INST_DIR/site/
108 // - \b other (shipped) settings:
109 // - $WM_PROJECT_DIR/etc/
111 // \return The list of full paths of all the matching files or
112 // an empty list if the name cannot be found.
113 // Optionally abort if the file cannot be found
114 fileNameList findEtcFiles(const fileName&, bool mandatory=false);
116 //- Search for a file using findEtcFiles.
117 // \return The full path name of the first file found which in the
118 // search hierarchy or an empty fileName if the name cannot be found.
119 // Optionally abort if the file cannot be found
120 fileName findEtcFile(const fileName&, bool mandatory=false);
122 //- Make a directory and return an error if it could not be created
123 // and does not already exist
124 bool mkDir(const fileName&, mode_t=0777);
126 //- Set the file mode
127 bool chMod(const fileName&, const mode_t);
129 //- Return the file mode
130 mode_t mode(const fileName&);
132 //- Return the file type: DIRECTORY or FILE
133 fileName::Type type(const fileName&);
135 //- Does the name exist (as DIRECTORY or FILE) in the file system?
136 // Optionally enable/disable check for gzip file.
137 bool exists(const fileName&, const bool checkGzip=true);
139 //- Does the name exist as a DIRECTORY in the file system?
140 bool isDir(const fileName&);
142 //- Does the name exist as a FILE in the file system?
143 // Optionally enable/disable check for gzip file.
144 bool isFile(const fileName&, const bool checkGzip=true);
146 //- Return size of file
147 off_t fileSize(const fileName&);
149 //- Return time of last file modification
150 time_t lastModified(const fileName&);
152 //- Read a directory and return the entries as a string list
156 const fileName::Type=fileName::FILE,
157 const bool filtergz=true
160 //- Copy, recursively if necessary, the source to the destination
161 bool cp(const fileName& src, const fileName& dst);
163 //- Create a softlink. dst should not exist. Returns true if successful.
164 bool ln(const fileName& src, const fileName& dst);
166 //- Rename src to dst
167 bool mv(const fileName& src, const fileName& dst);
169 //- Rename to a corresponding backup file
170 // If the backup file already exists, attempt with "01" .. "99" suffix
171 bool mvBak(const fileName&, const std::string& ext = "bak");
173 //- Remove a file, returning true if successful otherwise false
174 bool rm(const fileName&);
176 //- Remove a dirctory and its contents
177 bool rmDir(const fileName&);
179 //- Sleep for the specified number of seconds
180 unsigned int sleep(const unsigned int);
182 //- Close file descriptor
183 void fdClose(const int);
185 //- Check if machine is up by pinging given port
186 bool ping(const word&, const label port, const label timeOut);
188 //- Check if machine is up by pinging port 22 (ssh) and 222 (rsh)
189 bool ping(const word&, const label timeOut=10);
191 //- Execute the specified command
192 int system(const std::string& command);
194 //- open a shared library. Return handle to library. Print error message
195 // if library cannot be loaded (check = true)
196 void* dlOpen(const fileName& lib, const bool check = true);
198 //- Close a dlopened library using handle. Return true if successful
201 //- Lookup a symbol in a dlopened library using handle to library
202 void* dlSym(void* handle, const std::string& symbol);
204 //- Report if symbol in a dlopened library could be found
205 bool dlSymFound(void* handle, const std::string& symbol);
207 //- Return all loaded libraries
208 fileNameList dlLoaded();
211 // Low level random numbers. Use Random class instead.
213 //- Seed random number generator.
214 void osRandomSeed(const label seed);
216 //- Return random integer (uniform distribution between 0 and 2^31)
217 label osRandomInteger();
219 //- Return random double precision (uniform distribution between 0 and 1)
220 scalar osRandomDouble();
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 } // End namespace Foam
227 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 // ************************************************************************* //