diff --git a/builtin-functions/_functions.txt b/builtin-functions/_functions.txt index 9e6e139f39..307451ee4c 100644 --- a/builtin-functions/_functions.txt +++ b/builtin-functions/_functions.txt @@ -1620,3 +1620,6 @@ class DateTimeImmutable implements DateTimeInterface { } function getenv(string $varname = '', bool $local_only = false): mixed; + +//2022-05-01 comm644 +function touch($filename ::: string, int $mtime = 0, int $atime = 0): bool; diff --git a/runtime/files.cpp b/runtime/files.cpp index 06a346f965..45b9c57fd5 100644 --- a/runtime/files.cpp +++ b/runtime/files.cpp @@ -939,3 +939,4 @@ void free_files_lib() { dl::leave_critical_section(); } +#include "files_touch.cpp" \ No newline at end of file diff --git a/runtime/files.h b/runtime/files.h index 0da8704f05..e70c3c2460 100644 --- a/runtime/files.h +++ b/runtime/files.h @@ -72,3 +72,5 @@ Optional file_file_get_contents(const string &name); void global_init_files_lib(); void free_files_lib(); + +#include "files_touch.h" \ No newline at end of file diff --git a/runtime/files_touch.cpp b/runtime/files_touch.cpp new file mode 100644 index 0000000000..a74624c1ab --- /dev/null +++ b/runtime/files_touch.cpp @@ -0,0 +1,22 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2023 LLC Sigmalab +// Distributed under the GPL v3 License, see LICENSE.notice.txt +#include +#include +#include + +bool f$touch(const string filename, const int64_t mtime, const int64_t atime) +{ + if (FILE *file = fopen(filename.c_str(), "a+")) { + fclose(file); + } + else { + return false; + } + + time_t now = std::time(NULL); + time_t val_mtime = !mtime ? now : mtime; + time_t val_atime = !atime ? now : atime; + const utimbuf timebuf = {val_atime, val_mtime}; + return utime(filename.c_str(), &timebuf ) == 0; +} diff --git a/runtime/files_touch.h b/runtime/files_touch.h new file mode 100644 index 0000000000..13ef8cc22f --- /dev/null +++ b/runtime/files_touch.h @@ -0,0 +1,9 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2023 LLC Sigmalab +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once +#include "runtime/kphp_core.h" +#include + +bool f$touch(const string filename, const int64_t mtime=0, const int64_t atime=0);