2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions from sysinfo.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_utility
53 #include <sys/types.h>
54 #ifdef HAVE_SYS_TIME_H
55 # include <sys/time.h>
57 #if GMX_NATIVE_WINDOWS
68 #include "gromacs/utility/basedefinitions.h"
69 #include "gromacs/utility/gmxassert.h"
73 //! Static return value for cases when a string value is not available.
74 const char c_unknown[] = "unknown";
77 int gmx_gethostname(char* buf, size_t len)
79 GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
80 #if GMX_NATIVE_WINDOWS
82 if (GetComputerName(buf, &dlen))
86 #elif defined(HAVE_UNISTD_H) && !defined(__native_client__)
87 if (gethostname(buf, len - 1) == 0)
93 strcpy(buf, c_unknown);
99 #if GMX_NATIVE_WINDOWS
108 #if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
115 int gmx_getusername(char* buf, size_t len)
117 GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
118 // TODO: nice_header() used getpwuid() instead; consider using getpwuid_r()
119 // here. If not, get rid of HAVE_PWD_H completely.
120 #if GMX_NATIVE_WINDOWS
122 if (GetUserName(buf, &dlen))
126 #elif defined(HAVE_UNISTD_H) && !__has_feature(memory_sanitizer) // MSAN Issue 83
127 if (!getlogin_r(buf, len))
133 strcpy(buf, c_unknown);
137 std::string gmx_ctime_r(const time_t* clock)
140 std::array<char, 1024> buf;
141 ctime_s(buf.data(), buf.size(), clock);
142 return std::string(buf.begin(), buf.end());
143 #elif GMX_NATIVE_WINDOWS
144 char* tmpbuf = ctime(clock);
146 #elif (defined(__sun))
148 std::array<char, 1024> buf;
149 ctime_r(clock, buf.data());
150 return std::string(buf.begin(), buf.end());
152 std::array<char, 1024> buf;
153 ctime_r(clock, buf.data());
154 return std::string(buf.begin(), buf.end());
158 std::string gmx_format_current_time()
160 time_t clock = time(nullptr);
161 return gmx_ctime_r(&clock);
164 int gmx_set_nice(int level)
167 // TODO: This may not be reliable, but currently the return value is not
169 if (nice(level) != -1)
174 GMX_UNUSED_VALUE(level);