Throw on failure in gmx_file_rename
[alexxy/gromacs.git] / src / gromacs / utility / futil.cpp
index ccffa764d188e0836ba9118075e03516ea27ce66..34467e1f1d120f3adfefee22f4a11fb9c06cc4ea 100644 (file)
@@ -3,7 +3,8 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017, The GROMACS development team.
+ * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -45,6 +46,7 @@
 #include <cstdlib>
 #include <cstring>
 
+#include <mutex>
 #include <tuple>
 
 #include <fcntl.h>
 #include <sys/types.h>
 
 #ifdef HAVE_UNISTD_H
-#include <unistd.h>
+#    include <unistd.h>
 #endif
 #if GMX_NATIVE_WINDOWS
-#include <direct.h>   // For _chdir() and _getcwd()
-#include <io.h>
-#include <windows.h>
+#    include <direct.h> // For _chdir() and _getcwd()
+#    include <io.h>
+#    include <windows.h>
 #endif
 
 #include "gromacs/utility/cstringutil.h"
@@ -65,7 +67,6 @@
 #include "gromacs/utility/dir_separator.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/fatalerror.h"
-#include "gromacs/utility/mutex.h"
 #include "gromacs/utility/path.h"
 #include "gromacs/utility/programcontext.h"
 #include "gromacs/utility/smalloc.h"
    compressed or .gzipped files. This way we can distinguish between them
    without having to change the semantics of reading from/writing to files)
  */
-typedef struct t_pstack {
-    FILE            *fp;
-    struct t_pstack *prev;
+typedef struct t_pstack
+{
+    FILE*            fp;
+    struct t_pstack* prev;
 } t_pstack;
 
-static t_pstack    *pstack           = nullptr;
-static bool         bUnbuffered      = false;
-static int          s_maxBackupCount = 0;
+// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
+static t_pstack* pstack = nullptr;
+// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
+static bool bUnbuffered = false;
+// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
+static int s_maxBackupCount = 0;
 
 /* this linked list is an intrinsically globally shared object, so we have
    to protect it with mutexes */
-static gmx::Mutex pstack_mutex;
+// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
+static std::mutex pstack_mutex;
 
-using Lock = gmx::lock_guard<gmx::Mutex>;
+using Lock = std::lock_guard<std::mutex>;
 
 namespace gmx
 {
 namespace
 {
 //! Global library file finder; stores the object set with setLibraryFileFinder().
-const DataFileFinder *g_libFileFinder;
+const DataFileFinder* g_libFileFinder; //NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 //! Default library file finder if nothing is set.
-const DataFileFinder  g_defaultLibFileFinder;
-}   // namespace
+const DataFileFinder g_defaultLibFileFinder;
+} // namespace
 
-const DataFileFinder &getLibraryFileFinder()
+const DataFileFindergetLibraryFileFinder()
 {
     if (g_libFileFinder != nullptr)
     {
@@ -109,7 +115,7 @@ const DataFileFinder &getLibraryFileFinder()
     return g_defaultLibFileFinder;
 }
 
-void setLibraryFileFinder(const DataFileFinder *finder)
+void setLibraryFileFinder(const DataFileFinderfinder)
 {
     g_libFileFinder = finder;
 }
@@ -125,7 +131,7 @@ void gmx_set_max_backup_count(int count)
 {
     if (count < 0)
     {
-        const char *env = getenv("GMX_MAXBACKUP");
+        const charenv = getenv("GMX_MAXBACKUP");
         if (env != nullptr)
         {
             // TODO: Check that the value is converted properly.
@@ -146,11 +152,11 @@ void gmx_set_max_backup_count(int count)
     s_maxBackupCount = count;
 }
 
-static void push_ps(FILE *fp)
+static void push_ps(FILEfp)
 {
-    t_pstack *ps;
+    t_pstack* ps = nullptr;
 
-    Lock      pstackLock(pstack_mutex);
+    Lock pstackLock(pstack_mutex);
 
     snew(ps, 1);
     ps->fp   = fp;
@@ -159,42 +165,33 @@ static void push_ps(FILE *fp)
 }
 
 #if GMX_FAHCORE
-/* don't use pipes!*/
-#define popen fah_fopen
-#define pclose fah_fclose
-#define SKIP_FFOPS 1
-#else
-#ifdef gmx_ffclose
-#undef gmx_ffclose
+#    ifdef gmx_ffclose
+#        undef gmx_ffclose
+#    endif
 #endif
 #if (!HAVE_PIPES && !defined(__native_client__))
-static FILE *popen(const char *nm, const char *mode)
+static FILE* popen(const char* /* nm */, const char* /* mode */)
 {
     gmx_impl("Sorry no pipes...");
 
     return NULL;
 }
 
-static int pclose(FILE *fp)
+static int pclose(FILE* /* fp */)
 {
     gmx_impl("Sorry no pipes...");
 
     return 0;
 }
 #endif /* !HAVE_PIPES && !defined(__native_client__) */
-#endif /* GMX_FAHCORE */
 
-int gmx_ffclose(FILE *fp)
+int gmx_ffclose(FILEfp)
 {
-#ifdef SKIP_FFOPS
-    return fclose(fp);
-#else
-    t_pstack *ps, *tmp;
-    int       ret = 0;
+    int ret = 0;
 
-    Lock      pstackLock(pstack_mutex);
+    Lock pstackLock(pstack_mutex);
 
-    ps = pstack;
+    t_pstack* ps = pstack;
     if (ps == nullptr)
     {
         if (fp != nullptr)
@@ -223,8 +220,8 @@ int gmx_ffclose(FILE *fp)
             {
                 ret = pclose(ps->prev->fp);
             }
-            tmp      = ps->prev;
-            ps->prev = ps->prev->prev;
+            t_pstack* tmp = ps->prev;
+            ps->prev      = ps->prev->prev;
             sfree(tmp);
         }
         else
@@ -237,15 +234,14 @@ int gmx_ffclose(FILE *fp)
     }
 
     return ret;
-#endif
 }
 
 
-void frewind(FILE *fp)
+void frewind(FILEfp)
 {
-    Lock      pstackLock(pstack_mutex);
+    Lock pstackLock(pstack_mutex);
 
-    t_pstack *ps = pstack;
+    t_pstackps = pstack;
     while (ps != nullptr)
     {
         if (ps->fp == fp)
@@ -258,49 +254,49 @@ void frewind(FILE *fp)
     rewind(fp);
 }
 
-int gmx_fseek(FILE *stream, gmx_off_t offset, int whence)
+int gmx_fseek(FILEstream, gmx_off_t offset, int whence)
 {
 #if HAVE_FSEEKO
     return fseeko(stream, offset, whence);
 #else
-#if HAVE__FSEEKI64
+#    if HAVE__FSEEKI64
     return _fseeki64(stream, offset, whence);
-#else
+#    else
     return fseek(stream, offset, whence);
-#endif
+#    endif
 #endif
 }
 
-gmx_off_t gmx_ftell(FILE *stream)
+gmx_off_t gmx_ftell(FILEstream)
 {
 #if HAVE_FSEEKO
     return ftello(stream);
 #else
-#if HAVE__FSEEKI64
-#ifndef __MINGW32__
+#    if HAVE__FSEEKI64
+#        ifndef __MINGW32__
     return _ftelli64(stream);
-#else
+#        else
     return ftello64(stream);
-#endif
-#else
+#        endif
+#    else
     return ftell(stream);
-#endif
+#    endif
 #endif
 }
 
-int gmx_truncate(const std::string &filename, gmx_off_t length)
+int gmx_truncate(const std::stringfilename, gmx_off_t length)
 {
-#if GMX_NATIVE_WINDOWS
-    FILE *fp = fopen(filename.c_str(), "rb+");
+#if GMX_NATIVE_WINDOWS && !GMX_FAHCORE
+    FILEfp = fopen(filename.c_str(), "rb+");
     if (fp == NULL)
     {
         return -1;
     }
-#ifdef _MSC_VER
+#    ifdef _MSC_VER
     int rc = _chsize_s(fileno(fp), length);
-#else
+#    else
     int rc = _chsize(fileno(fp), length);
-#endif
+#    endif
     fclose(fp);
     return rc;
 #else
@@ -308,9 +304,9 @@ int gmx_truncate(const std::string &filename, gmx_off_t length)
 #endif
 }
 
-static FILE *uncompress(const std::string &fn, const char *mode)
+static FILE* uncompress(const std::string& fn, const char* mode)
 {
-    FILE       *fp;
+    FILE*       fp  = nullptr;
     std::string buf = "uncompress -c < " + fn;
     fprintf(stderr, "Going to execute '%s'\n", buf.c_str());
     if ((fp = popen(buf.c_str(), mode)) == nullptr)
@@ -322,9 +318,9 @@ static FILE *uncompress(const std::string &fn, const char *mode)
     return fp;
 }
 
-static FILE *gunzip(const std::string &fn, const char *mode)
+static FILE* gunzip(const std::string& fn, const char* mode)
 {
-    FILE       *fp;
+    FILE*       fp  = nullptr;
     std::string buf = "gunzip -c < ";
     buf += fn;
     fprintf(stderr, "Going to execute '%s'\n", buf.c_str());
@@ -337,24 +333,22 @@ static FILE *gunzip(const std::string &fn, const char *mode)
     return fp;
 }
 
-gmx_bool gmx_fexist(const std::string &fname)
+gmx_bool gmx_fexist(const std::stringfname)
 {
-    FILE *test;
-
     if (fname.empty())
     {
         return FALSE;
     }
-    test = fopen(fname.c_str(), "r");
+    FILE* test = fopen(fname.c_str(), "r");
     if (test == nullptr)
     {
-        /*Windows doesn't allow fopen of directory - so we need to check this seperately */
-        #if GMX_NATIVE_WINDOWS
+/*Windows doesn't allow fopen of directory - so we need to check this separately */
+#if GMX_NATIVE_WINDOWS
         DWORD attr = GetFileAttributes(fname.c_str());
         return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
-        #else
+#else
         return FALSE;
-        #endif
+#endif
     }
     else
     {
@@ -363,12 +357,13 @@ gmx_bool gmx_fexist(const std::string &fname)
     }
 }
 
-static std::string backup_fn(const std::string &file)
+static std::string backup_fn(const std::stringfile)
 {
-    int          count = 1;
+    int count = 1;
 
-    std::string  directory, fn, buf;
-    std::tie(directory, fn) = gmx::Path::getParentPathAndBasename(file);
+    std::string directory = gmx::Path::getParentPath(file);
+    std::string fn        = gmx::Path::getFilename(file);
+    std::string buf;
     if (directory.empty())
     {
         directory = ".";
@@ -377,23 +372,24 @@ static std::string backup_fn(const std::string &file)
     {
         buf = gmx::formatString("%s/#%s.%d#", directory.c_str(), fn.c_str(), count);
         count++;
-    }
-    while ((count <= s_maxBackupCount) && gmx_fexist(buf));
+    } while ((count <= s_maxBackupCount) && gmx_fexist(buf));
 
     /* Arbitrarily bail out */
     if (count > s_maxBackupCount)
     {
         /* TODO: The error message is only accurate for code that starts with
          * Gromacs command-line interface. */
-        gmx_fatal(FARGS, "Won't make more than %d backups of %s for you.\n"
+        gmx_fatal(FARGS,
+                  "Won't make more than %d backups of %s for you.\n"
                   "The env.var. GMX_MAXBACKUP controls this maximum, -1 disables backups.",
-                  s_maxBackupCount, fn.c_str());
+                  s_maxBackupCount,
+                  fn.c_str());
     }
 
     return buf;
 }
 
-void make_backup(const std::string &name)
+void make_backup(const std::stringname)
 {
     if (s_maxBackupCount <= 0)
     {
@@ -404,8 +400,7 @@ void make_backup(const std::string &name)
         auto backup = backup_fn(name);
         if (rename(name.c_str(), backup.c_str()) == 0)
         {
-            fprintf(stderr, "\nBack Off! I just backed up %s to %s\n",
-                    name.c_str(), backup.c_str());
+            fprintf(stderr, "\nBack Off! I just backed up %s to %s\n", name.c_str(), backup.c_str());
         }
         else
         {
@@ -414,14 +409,9 @@ void make_backup(const std::string &name)
     }
 }
 
-FILE *gmx_ffopen(const std::string &file, const char *mode)
+FILE* gmx_ffopen(const std::string& file, const char* mode)
 {
-#ifdef SKIP_FFOPS
-    return fopen(file, mode);
-#else
-    FILE    *ff = nullptr;
-    gmx_bool bRead;
-    int      bs;
+    FILE* ff = nullptr;
 
     if (file.empty())
     {
@@ -433,7 +423,7 @@ FILE *gmx_ffopen(const std::string &file, const char *mode)
         make_backup(file);
     }
 
-    bRead = (mode[0] == 'r' && mode[1] != '+');
+    bool bRead = (mode[0] == 'r' && mode[1] != '+');
     if (!bRead || gmx_fexist(file))
     {
         if ((ff = fopen(file.c_str(), mode)) == nullptr)
@@ -443,26 +433,19 @@ FILE *gmx_ffopen(const std::string &file, const char *mode)
         /* Check whether we should be using buffering (default) or not
          * (for debugging)
          */
-        const char *bufsize = nullptr;
+        const charbufsize = nullptr;
         if (bUnbuffered || ((bufsize = getenv("GMX_LOG_BUFFER")) != nullptr))
         {
             /* Check whether to use completely unbuffered */
-            if (bUnbuffered)
-            {
-                bs = 0;
-            }
-            else
-            {
-                bs = strtol(bufsize, nullptr, 10);
-            }
+            const int bs = bUnbuffered ? 0 : strtol(bufsize, nullptr, 10);
             if (bs <= 0)
             {
                 setbuf(ff, nullptr);
             }
             else
             {
-                char *ptr;
-                snew(ptr, bs+8);
+                char* ptr = nullptr;
+                snew(ptr, bs + 8);
                 if (setvbuf(ff, ptr, _IOFBF, bs) != 0)
                 {
                     gmx_file("Buffering File");
@@ -480,7 +463,7 @@ FILE *gmx_ffopen(const std::string &file, const char *mode)
         }
         else
         {
-            compressedFileName  = file;
+            compressedFileName = file;
             compressedFileName += ".gz";
             if (gmx_fexist(compressedFileName))
             {
@@ -493,46 +476,42 @@ FILE *gmx_ffopen(const std::string &file, const char *mode)
         }
     }
     return ff;
-#endif
 }
 
 namespace gmx
 {
 
-std::string findLibraryFile(const std::string &filename, bool bAddCWD, bool bFatal)
+std::string findLibraryFile(const std::stringfilename, bool bAddCWD, bool bFatal)
 {
     std::string result;
     try
     {
-        const DataFileFinder &finder = getLibraryFileFinder();
-        result = finder.findFile(DataFileOptions(filename)
-                                     .includeCurrentDir(bAddCWD)
-                                     .throwIfNotFound(bFatal));
+        const DataFileFinder& finder = getLibraryFileFinder();
+        result                       = finder.findFile(
+                DataFileOptions(filename).includeCurrentDir(bAddCWD).throwIfNotFound(bFatal));
     }
-    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
     return result;
 }
 
-std::string findLibraryFile(const char *filename, bool bAddCWD, bool bFatal)
+std::string findLibraryFile(const charfilename, bool bAddCWD, bool bFatal)
 {
     return findLibraryFile(std::string(filename), bAddCWD, bFatal);
 }
 
-FilePtr openLibraryFile(const std::string &filename, bool bAddCWD, bool bFatal)
+FilePtr openLibraryFile(const std::stringfilename, bool bAddCWD, bool bFatal)
 {
     FilePtr fp;
     try
     {
-        const DataFileFinder &finder = getLibraryFileFinder();
-        fp = finder.openFile(DataFileOptions(filename)
-                                 .includeCurrentDir(bAddCWD)
-                                 .throwIfNotFound(bFatal));
+        const DataFileFinder& finder = getLibraryFileFinder();
+        fp = finder.openFile(DataFileOptions(filename).includeCurrentDir(bAddCWD).throwIfNotFound(bFatal));
     }
-    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
     return fp;
 }
 
-FilePtr openLibraryFile(const char *filename, bool bAddCWD, bool bFatal)
+FilePtr openLibraryFile(const charfilename, bool bAddCWD, bool bFatal)
 {
     return openLibraryFile(std::string(filename), bAddCWD, bFatal);
 }
@@ -542,16 +521,18 @@ FilePtr openLibraryFile(const char *filename, bool bAddCWD, bool bFatal)
 /*! \brief Use mkstemp (or similar function to make a new temporary
  * file and (on non-Windows systems) return a file descriptor to it.
  *
+ * Note: not thread-safe on non-Windows systems
+ *
  * \todo Use std::string and std::vector<char>. */
-static int makeTemporaryFilename(char *buf)
+static int makeTemporaryFilename(charbuf)
 {
-    int len;
+    int len = 0;
 
     if ((len = strlen(buf)) < 7)
     {
         gmx_fatal(FARGS, "Buf passed to gmx_tmpnam must be at least 7 bytes long");
     }
-    for (int i = len-6; (i < len); i++)
+    for (int i = len - 6; (i < len); i++)
     {
         buf[i] = 'X';
     }
@@ -559,28 +540,30 @@ static int makeTemporaryFilename(char *buf)
      * since windows doesnt support it we have to separate the cases.
      * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
      */
-    int fd;
 #if GMX_NATIVE_WINDOWS
     _mktemp(buf);
     if (buf == NULL)
     {
-        gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
-                  strerror(errno));
+        gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf, strerror(errno));
     }
-    fd = 0;
+    int fd = 0;
 #else
-    fd = mkstemp(buf);
+    int fd = mkstemp(buf);
+
+    /* mkstemp creates 0600 files - respect umask instead */
+    mode_t currUmask = umask(0);
+    umask(currUmask);
+    fchmod(fd, 0666 & ~currUmask);
 
     if (fd < 0)
     {
-        gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
-                  strerror(errno));
+        gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf, strerror(errno));
     }
 #endif
     return fd;
 }
 // TODO use std::string
-void gmx_tmpnam(char *buf)
+void gmx_tmpnam(charbuf)
 {
     int fd = makeTemporaryFilename(buf);
 #if !GMX_NATIVE_WINDOWS
@@ -589,9 +572,9 @@ void gmx_tmpnam(char *buf)
 }
 
 // TODO use std::string
-FILE *gmx_fopen_temporary(char *buf)
+FILE* gmx_fopen_temporary(char* buf)
 {
-    FILE *fpout = nullptr;
+    FILEfpout = nullptr;
     int   fd    = makeTemporaryFilename(buf);
 
 #if GMX_NATIVE_WINDOWS
@@ -609,25 +592,34 @@ FILE *gmx_fopen_temporary(char *buf)
     return fpout;
 }
 
-int gmx_file_rename(const char *oldname, const char *newname)
+void gmx_file_rename(const char* oldname, const char* newname)
 {
+    int code;
 #if !GMX_NATIVE_WINDOWS
     /* under unix, rename() is atomic (at least, it should be). */
-    return rename(oldname, newname);
+    code = rename(oldname, newname);
 #else
-    if (MoveFileEx(oldname, newname,
-                   MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH))
+    if (MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH))
     {
-        return 0;
+#    if GMX_FAHCORE
+        /* This just lets the F@H checksumming system know about the rename */
+        fcRename(oldname, newname);
+#    endif
+        code = 0;
     }
     else
     {
-        return 1;
+        code = 1;
     }
 #endif
+    if (code != 0)
+    {
+        auto errorMsg = gmx::formatString("Failed to rename %s to %s.", oldname, newname);
+        GMX_THROW(gmx::FileIOError(errorMsg));
+    }
 }
 
-int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_empty)
+int gmx_file_copy(const char* oldname, const char* newname, gmx_bool copy_if_empty)
 {
     gmx::FilePtr in(fopen(oldname, "rb"));
     if (!in)
@@ -648,17 +640,15 @@ int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_emp
     }
 
     /* the full copy buffer size: */
-    constexpr int     FILECOPY_BUFSIZE = 1<<16;
+    constexpr int     FILECOPY_BUFSIZE = 1 << 16;
     std::vector<char> buf(FILECOPY_BUFSIZE);
 
     while (!feof(in.get()))
     {
-        size_t nread;
-
-        nread = fread(buf.data(), sizeof(char), FILECOPY_BUFSIZE, in.get());
+        size_t nread = fread(buf.data(), sizeof(char), FILECOPY_BUFSIZE, in.get());
         if (nread > 0)
         {
-            size_t ret;
+            size_t ret = 0;
             if (!out)
             {
                 /* so this is where we open when copy_if_empty is false:
@@ -684,24 +674,19 @@ int gmx_file_copy(const char *oldname, const char *newname, gmx_bool copy_if_emp
 }
 
 
-int gmx_fsync(FILE *fp)
+int gmx_fsync(FILEfp)
 {
     int rc = 0;
 
-#if GMX_FAHCORE
-    /* the fahcore defines its own os-independent fsync */
-    rc = fah_fsync(fp);
-#else /* GMX_FAHCORE */
     {
-        int fn;
-
         /* get the file number */
 #if HAVE_FILENO
-        fn = fileno(fp);
+        int fn = fileno(fp);
 #elif HAVE__FILENO
-        fn = _fileno(fp);
+        int fn = _fileno(fp);
 #else
-        fn = -1;
+        GMX_UNUSED_VALUE(fp);
+        int fn = -1;
 #endif
 
         /* do the actual fsync */
@@ -714,7 +699,6 @@ int gmx_fsync(FILE *fp)
 #endif
         }
     }
-#endif /* GMX_FAHCORE */
 
     /* We check for these error codes this way because POSIX requires them
        to be defined, and using anything other than macros is unlikely: */
@@ -737,30 +721,30 @@ int gmx_fsync(FILE *fp)
     return rc;
 }
 
-void gmx_chdir(const char *directory)
+void gmx_chdir(const chardirectory)
 {
 #if GMX_NATIVE_WINDOWS
     int rc = _chdir(directory);
 #else
-    int rc = chdir(directory);
+    int   rc   = chdir(directory);
 #endif
     if (rc != 0)
     {
-        gmx_fatal(FARGS, "Cannot change directory to '%s'. Reason: %s",
-                  directory, strerror(errno));
+        auto message = gmx::formatString(
+                "Cannot change directory to '%s'. Reason: %s", directory, strerror(errno));
+        GMX_THROW(gmx::FileIOError(message));
     }
 }
 
-void gmx_getcwd(char *buffer, size_t size)
+void gmx_getcwd(charbuffer, size_t size)
 {
 #if GMX_NATIVE_WINDOWS
-    char *pdum = _getcwd(buffer, size);
+    charpdum = _getcwd(buffer, size);
 #else
-    char *pdum = getcwd(buffer, size);
+    charpdum = getcwd(buffer, size);
 #endif
     if (pdum == nullptr)
     {
-        gmx_fatal(FARGS, "Cannot get working directory. Reason: %s",
-                  strerror(errno));
+        gmx_fatal(FARGS, "Cannot get working directory. Reason: %s", strerror(errno));
     }
 }