summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Source/cmake.cxx30
-rw-r--r--Source/kwsys/SystemTools.cxx36
-rw-r--r--Source/kwsys/SystemTools.hxx.in5
3 files changed, 71 insertions, 0 deletions
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index 82db89e..4eb92ad 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -941,6 +941,8 @@ void CMakeCommandUsage(const char* program)
<< " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar "
"archive\n"
<< " time command [args] ... - run command and return elapsed time\n"
+ << " touch file - touch a file.\n"
+ << " touch_nocreate file - touch a file but do not create it.\n"
#if defined(_WIN32) && !defined(__CYGWIN__)
<< " write_regv key value - write registry value\n"
<< " delete_regv key - delete registry value\n"
@@ -1096,6 +1098,34 @@ int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
}
return 0;
}
+ // Touch file
+ else if (args[1] == "touch" && args.size() > 2)
+ {
+ for (std::string::size_type cc = 2; cc < args.size(); cc ++)
+ {
+ // Complain if the file could not be removed, still exists,
+ // and the -f option was not given.
+ if(!cmSystemTools::Touch(args[cc].c_str(), true))
+ {
+ return 1;
+ }
+ }
+ return 0;
+ }
+ // Touch file
+ else if (args[1] == "touch_nocreate" && args.size() > 2)
+ {
+ for (std::string::size_type cc = 2; cc < args.size(); cc ++)
+ {
+ // Complain if the file could not be removed, still exists,
+ // and the -f option was not given.
+ if(!cmSystemTools::Touch(args[cc].c_str(), false))
+ {
+ return 1;
+ }
+ }
+ return 0;
+ }
// Clock command
else if (args[1] == "time" && args.size() > 2)
diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx
index 28efd40..e9d72ed 100644
--- a/Source/kwsys/SystemTools.cxx
+++ b/Source/kwsys/SystemTools.cxx
@@ -50,6 +50,7 @@
// support for realpath call
#ifndef _WIN32
+#include <utime.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/wait.h>
@@ -75,6 +76,11 @@
# define HAVE_TTY_INFO 1
#endif
+#ifdef _MSC_VER
+#include <sys/utime.h>
+#else
+#include <utime.h>
+#endif
// This is a hack to prevent warnings about these functions being
// declared but not referenced.
@@ -836,6 +842,36 @@ bool SystemTools::FileExists(const char* filename)
}
}
+bool SystemTools::Touch(const char* filename, bool create)
+{
+ if(create && !SystemTools::FileExists(filename))
+ {
+ FILE* file = fopen(filename, "a+b");
+ if(file)
+ {
+ fclose(file);
+ return true;
+ }
+ return false;
+ }
+#ifdef _MSC_VER
+#define utime _utime
+#define utimbuf _utimbuf
+#endif
+ struct stat fromStat;
+ if(stat(filename, &fromStat) < 0)
+ {
+ return false;
+ }
+ struct utimbuf buf;
+ buf.actime = fromStat.st_atime;
+ buf.modtime = SystemTools::GetTime();
+ if(utime(filename, &buf) < 0)
+ {
+ return false;
+ }
+ return true;
+}
bool SystemTools::FileTimeCompare(const char* f1, const char* f2,
int* result)
diff --git a/Source/kwsys/SystemTools.hxx.in b/Source/kwsys/SystemTools.hxx.in
index 7679846..7cad203 100644
--- a/Source/kwsys/SystemTools.hxx.in
+++ b/Source/kwsys/SystemTools.hxx.in
@@ -280,6 +280,11 @@ public:
static unsigned long FileLength(const char *filename);
/**
+ Change the modification time or create a file
+ */
+ static bool Touch(const char* filename, bool create);
+
+ /**
* Compare file modification times.
* Return true for successful comparison and false for error.
* When true is returned, result has -1, 0, +1 for