#include "config.h"
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
#ifdef GMX_USE_TNG
#include "tng/tng_io.h"
#endif
#include "gromacs/math/utilities.h"
#include "gromacs/topology/topology.h"
#include "gromacs/utility/basedefinitions.h"
-#include "gromacs/utility/basenetwork.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/programcontext.h"
+#include "gromacs/utility/sysinfo.h"
static const char *modeToVerb(char mode)
{
*/
if (mode == 'w')
{
-#ifndef GMX_FAHCORE
- /* only make backups for normal gromacs */
make_backup(filename);
-#endif
}
/* tng must not be pointing at already allocated memory.
// tng_last_program_name_set(*tng, programInfo);
// }
-#if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
char username[256];
- if (!getlogin_r(username, 256))
+ if (!gmx_getusername(username, 256))
{
if (mode == 'w')
{
// {
// tng_last_user_name_set(*tng, username);
// }
-#endif
}
#else
gmx_file("GROMACS was compiled without TNG support, cannot handle this file type");
#include "xvgr.h"
-#include "config.h"
-
#include <ctype.h>
#include <string.h>
-#include <time.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
#include "gromacs/fileio/gmxfio.h"
#include "gromacs/legacyheaders/copyrite.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/futil.h"
#include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
{
const output_env_t oenv)
{
char pukestr[100], buf[STRLEN];
- time_t t;
if (output_env_get_print_xvgr_codes(oenv))
{
- time(&t);
- gmx_ctime_r(&t, buf, STRLEN);
+ gmx_format_current_time(buf, STRLEN);
fprintf(fp, "# This file was created %s", buf);
try
{
*/
#include "gmxpre.h"
-#include "config.h"
-
#include <math.h>
#include <string.h>
-#include <time.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
#include "gromacs/commandline/pargs.h"
#include "gromacs/fileio/confio.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/futil.h"
#include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
int gmx_covar(int argc, char *argv[])
{
int d, dj, nfit;
atom_id *index, *ifit;
gmx_bool bDiffMass1, bDiffMass2;
- time_t now;
char timebuf[STRLEN];
t_rgb rlo, rmi, rhi;
real *eigenvectors;
out = gmx_ffopen(logfile, "w");
- time(&now);
- gmx_ctime_r(&now, timebuf, STRLEN);
+ gmx_format_current_time(timebuf, STRLEN);
fprintf(out, "Covariance analysis log, written %s\n", timebuf);
fprintf(out, "Program: %s\n", argv[0]);
#include <errno.h>
#include <stdlib.h>
#include <string.h>
-#include <time.h>
#include <fcntl.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "gromacs/legacyheaders/typedefs.h"
#include "gromacs/legacyheaders/types/commrec.h"
#include "gromacs/math/vec.h"
-#include "gromacs/utility/basenetwork.h"
#include "gromacs/utility/baseversion.h"
#include "gromacs/utility/cstringutil.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/futil.h"
#include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
#ifdef GMX_FAHCORE
#include "corewrap.h"
int double_prec;
char *fprog;
char *fntemp; /* the temporary checkpoint file name */
- time_t now;
char timebuf[STRLEN];
int nppnodes, npmenodes;
char buf[1024], suffix[5+STEPSTRSIZE], sbuf[STEPSTRSIZE];
snew(fntemp, strlen(fn));
strcpy(fntemp, fn);
#endif
- time(&now);
- gmx_ctime_r(&now, timebuf, STRLEN);
+ gmx_format_current_time(timebuf, STRLEN);
if (fplog)
{
#include "gromacs/utility/gmxomp.h"
#include "gromacs/utility/smalloc.h"
#include "gromacs/utility/stringutil.h"
+#include "gromacs/utility/sysinfo.h"
#ifdef GMX_GPU
const gmx_bool bGPUBinary = TRUE;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <time.h>
#include "gromacs/fileio/filenm.h"
#include "gromacs/fileio/gmxfio.h"
#include "gromacs/legacyheaders/macros.h"
#include "gromacs/legacyheaders/network.h"
#include "gromacs/legacyheaders/types/commrec.h"
-#include "gromacs/utility/basenetwork.h"
#include "gromacs/utility/cstringutil.h"
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/fatalerror.h"
{
int pid;
char host[256];
- time_t t;
char timebuf[STRLEN];
FILE *fp = *fplog;
/* Get some machine parameters */
gmx_gethostname(host, 256);
-
- time(&t);
-
pid = gmx_getpid();
+ gmx_format_current_time(timebuf, STRLEN);
if (bAppendFiles)
{
);
}
- gmx_ctime_r(&t, timebuf, STRLEN);
-
fprintf(fp,
"Log file opened on %s"
"Host: %s pid: %d rank ID: %d number of ranks: %d\n",
#include <stdio.h>
#include <string.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-
#include "gromacs/essentialdynamics/edsam.h"
#include "gromacs/ewald/pme.h"
#include "gromacs/gmxlib/nonbonded/nb_free_energy.h"
#include "gromacs/utility/cstringutil.h"
#include "gromacs/utility/gmxmpi.h"
#include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
#include "adress.h"
#include <exception>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
#include "gromacs/utility/cstringutil.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/gmxmpi.h"
#include "gromacs/utility/programcontext.h"
-int gmx_gethostname(char *name, size_t len)
-{
- if (len < 8)
- {
- gmx_incons("gmx_gethostname called with len<8");
- }
-#if defined(HAVE_UNISTD_H) && !defined(__native_client__) && !defined(__MINGW32__)
- if (gethostname(name, len-1) != 0)
- {
- std::strncpy(name, "unknown", 8);
- return -1;
- }
- return 0;
-#else
- std::strncpy(name, "unknown", 8);
- return -1;
-#endif
-}
-
gmx_bool gmx_mpi_initialized(void)
{
#ifndef GMX_MPI
extern "C" {
#endif
-/*! \brief
- * Sets the hostname to the value given by gethostname(), if available.
- *
- * \param[out] name Buffer to receive the hostname.
- * \param[in] len Length of buffer \p name (must be >= 8).
- * \returns 0 on success, -1 on error.
- *
- * If the value is not available, "unknown" is returned.
- * \p name should have at least size \p len.
- */
-int gmx_gethostname(char *name, size_t len);
-
/*! \brief
* Returns whether MPI has been initialized.
*
#include "cstringutil.h"
-#include "config.h"
-
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <time.h>
-
-#include <sys/types.h>
-
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_PWD_H
-#include <pwd.h>
-#endif
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
#include "gromacs/utility/basedefinitions.h"
-#include "gromacs/utility/basenetwork.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/futil.h"
#include "gromacs/utility/smalloc.h"
+#include "gromacs/utility/sysinfo.h"
int continuing(char *s)
{
rtrim (str);
}
-char *
-gmx_ctime_r(const time_t *clock, char *buf, int n)
+void nice_header(FILE *out, const char *fn)
{
-#ifdef _MSC_VER
- /* Windows */
- ctime_s( buf, n, clock );
-#elif defined(GMX_NATIVE_WINDOWS)
- char *tmpbuf = ctime( clock );
- strncpy(buf, tmpbuf, n-1);
- buf[n-1] = '\0';
-#elif (defined(__sun))
- /*Solaris*/
- ctime_r(clock, buf, n);
-#else
- char tmpbuf[STRLEN];
- ctime_r(clock, tmpbuf);
- strncpy(buf, tmpbuf, n-1);
- buf[n-1] = '\0';
-#endif
- return buf;
-}
-
-void nice_header (FILE *out, const char *fn)
-{
- const char *unk = "onbekend";
- time_t clock;
- const char *user;
- int gh;
-#ifdef HAVE_PWD_H
- uid_t uid;
-#else
int uid;
-#endif
- char buf[256] = "";
+ char userbuf[256];
+ char hostbuf[256];
char timebuf[STRLEN];
-#ifdef HAVE_PWD_H
- struct passwd *pw;
-#endif
/* Print a nice header above the file */
- time(&clock);
- fprintf (out, "%c\n", COMMENTSIGN);
- fprintf (out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : unk);
-
-#ifdef HAVE_PWD_H
- uid = getuid();
- pw = getpwuid(uid);
- gh = gmx_gethostname(buf, 255);
- /* pw returns null on error (e.g. compute nodes lack /etc/passwd) */
- user = pw ? pw->pw_name : unk;
-#else
- uid = 0;
- gh = -1;
- user = unk;
-#endif
-
- gmx_ctime_r(&clock, timebuf, STRLEN);
- fprintf (out, "%c\tBy user: %s (%d)\n", COMMENTSIGN,
- user ? user : unk, (int) uid);
- fprintf(out, "%c\tOn host: %s\n", COMMENTSIGN, (gh == 0) ? buf : unk);
-
- fprintf (out, "%c\tAt date: %s", COMMENTSIGN, timebuf);
- fprintf (out, "%c\n", COMMENTSIGN);
+ fprintf(out, "%c\n", COMMENTSIGN);
+ fprintf(out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : "unknown");
+
+ uid = gmx_getuid();
+ gmx_getusername(userbuf, 256);
+ gmx_gethostname(hostbuf, 256);
+ gmx_format_current_time(timebuf, STRLEN);
+
+ fprintf(out, "%c\tBy user: %s (%d)\n", COMMENTSIGN, userbuf, uid);
+ fprintf(out, "%c\tOn host: %s\n", COMMENTSIGN, hostbuf);
+ fprintf(out, "%c\tAt date: %s\n", COMMENTSIGN, timebuf);
+ fprintf(out, "%c\n", COMMENTSIGN);
}
-
int gmx_strcasecmp_min(const char *str1, const char *str2)
{
char ch1, ch2;
#include "gromacs/utility/gmx_header_config.h"
#include <stdio.h>
-#include <time.h>
#include "gromacs/utility/basedefinitions.h"
/** Remove leading and trailing whitespace from a string. */
void trim(char *str);
-/** Portable version of ctime_r. */
-char *gmx_ctime_r(const time_t *clock, char *buf, int n);
-
/** Prints creation time stamp and user information into a file as comments. */
void nice_header(FILE *out, const char *fn);
#include "config.h"
+#include <string.h>
+#include <time.h>
+
+#include <sys/types.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
#ifdef GMX_NATIVE_WINDOWS
#include <process.h>
#endif
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+#include "gromacs/utility/gmxassert.h"
+
+namespace
+{
+//! Static return value for cases when a string value is not available.
+const char c_unknown[] = "unknown";
+} // namespace
+
+int gmx_gethostname(char *buf, size_t len)
+{
+ GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
+#if defined(HAVE_UNISTD_H) && !defined(__native_client__) && !defined(__MINGW32__)
+ if (gethostname(buf, len-1) == 0)
+ {
+ buf[len-1] = '\0';
+ return 0;
+ }
+#endif
+ strcpy(buf, c_unknown);
+ return -1;
+}
+
int gmx_getpid()
{
#ifdef GMX_NATIVE_WINDOWS
return getpid();
#endif
}
+
+int gmx_getuid()
+{
+#if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
+ return getuid();
+#else
+ return -1;
+#endif
+}
+
+int gmx_getusername(char *buf, size_t len)
+{
+ GMX_RELEASE_ASSERT(len >= 8, "Input buffer is too short");
+ // TODO: nice_header() used getpwuid() instead; consider using getpwuid_r()
+ // here. If not, get rid of HAVE_PWD_H completely.
+#if defined(HAVE_UNISTD_H) && !defined(__MINGW32__)
+ if (!getlogin_r(buf, len))
+ {
+ buf[len-1] = '\0';
+ return 0;
+ }
+#endif
+ strcpy(buf, c_unknown);
+ return -1;
+}
+
+char *
+gmx_ctime_r(const time_t *clock, char *buf, size_t len)
+{
+#ifdef _MSC_VER
+ /* Windows */
+ ctime_s(buf, len, clock);
+#elif defined(GMX_NATIVE_WINDOWS)
+ char *tmpbuf = ctime(clock);
+ strncpy(buf, tmpbuf, len-1);
+ buf[len-1] = '\0';
+#elif (defined(__sun))
+ /*Solaris*/
+ ctime_r(clock, buf, len);
+#else
+ char tmpbuf[30];
+ ctime_r(clock, tmpbuf);
+ strncpy(buf, tmpbuf, len-1);
+ buf[len-1] = '\0';
+#endif
+ return buf;
+}
+
+void gmx_format_current_time(char *buf, size_t len)
+{
+ time_t clock = time(NULL);
+ gmx_ctime_r(&clock, buf, len);
+}
#ifndef GMX_UTILITY_SYSINFO_H
#define GMX_UTILITY_SYSINFO_H
+#include <stddef.h>
+#include <time.h>
+
#ifdef __cplusplus
extern "C" {
#endif
+/*! \addtogroup module_utility
+ * \{
+ */
+
+/*! \brief
+ * Gets the hostname as given by gethostname(), if available.
+ *
+ * \param[out] buf Buffer to receive the hostname.
+ * \param[in] len Length of buffer \p buf (must be >= 8).
+ * \returns 0 on success, -1 on error.
+ *
+ * If the value is not available, "unknown" is returned.
+ * \p name should have at least size \p len.
+ *
+ * Does not throw.
+ */
+int gmx_gethostname(char *buf, size_t len);
+
/*! \brief
* Returns the process ID of the current process.
*
* Does not throw.
+ */
+int gmx_getpid(void);
+/*! \brief
+ * Returns the current user ID, or -1 if not available.
*
- * \ingroup module_utility
+ * Does not throw.
+ */
+int gmx_getuid(void);
+/*! \brief
+ * Gets the current user name, if available.
+ *
+ * \param[out] buf Buffer to receive the username.
+ * \param[in] len Length of buffer \p buf (must be >= 8).
+ * \returns 0 on success, -1 on error.
+ *
+ * Does not throw.
*/
-int gmx_getpid();
+int gmx_getusername(char *buf, size_t len);
+
+/*! \brief
+ * Portable version of ctime_r.
+ *
+ * Does not throw.
+ */
+char *gmx_ctime_r(const time_t *clock, char *buf, size_t len);
+/*! \brief
+ * Gets the current time as a string.
+ *
+ * \param[out] buf Buffer to receive the string.
+ * \param[in] len Length of buffer \p buf (26 characters should be sufficient).
+ *
+ * Does not throw.
+ */
+void gmx_format_current_time(char *buf, size_t len);
+
+/*! \} */
#ifdef __cplusplus
}