aboutsummaryrefslogtreecommitdiff
path: root/stdlib/paths.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-29 20:06:09 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-29 20:06:09 -0400
commit05515d8645326efa3db6311f6be1be776452c68a (patch)
tree15e58e72fc1aeec61017f02d8bf555e38a8dfda3 /stdlib/paths.c
parent8f7f66d7c894544c7c7e8ffae5d74e3cc602256f (diff)
Add DateTime
Diffstat (limited to 'stdlib/paths.c')
-rw-r--r--stdlib/paths.c51
1 files changed, 36 insertions, 15 deletions
diff --git a/stdlib/paths.c b/stdlib/paths.c
index 0119dbf0..0519201d 100644
--- a/stdlib/paths.c
+++ b/stdlib/paths.c
@@ -157,56 +157,77 @@ public bool Path$exists(Path_t path)
return (stat(Text$as_c_string(path), &sb) == 0);
}
-public bool Path$is_file(Path_t path, bool follow_symlinks)
+static inline int path_stat(Path_t path, bool follow_symlinks, struct stat *sb)
{
path = Path$_expand_home(path);
- struct stat sb;
const char *path_str = Text$as_c_string(path);
- int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
+ return follow_symlinks ? stat(path_str, sb) : lstat(path_str, sb);
+}
+
+public bool Path$is_file(Path_t path, bool follow_symlinks)
+{
+ struct stat sb;
+ int status = path_stat(path, follow_symlinks, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFREG;
}
public bool Path$is_directory(Path_t path, bool follow_symlinks)
{
- path = Path$_expand_home(path);
struct stat sb;
- const char *path_str = Text$as_c_string(path);
- int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
+ int status = path_stat(path, follow_symlinks, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFDIR;
}
public bool Path$is_pipe(Path_t path, bool follow_symlinks)
{
- path = Path$_expand_home(path);
struct stat sb;
- const char *path_str = Text$as_c_string(path);
- int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
+ int status = path_stat(path, follow_symlinks, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFIFO;
}
public bool Path$is_socket(Path_t path, bool follow_symlinks)
{
- path = Path$_expand_home(path);
struct stat sb;
- const char *path_str = Text$as_c_string(path);
- int status = follow_symlinks ? stat(path_str, &sb) : lstat(path_str, &sb);
+ int status = path_stat(path, follow_symlinks, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFSOCK;
}
public bool Path$is_symlink(Path_t path)
{
- path = Path$_expand_home(path);
struct stat sb;
- const char *path_str = Text$as_c_string(path);
- int status = stat(path_str, &sb);
+ int status = path_stat(path, false, &sb);
if (status != 0) return false;
return (sb.st_mode & S_IFMT) == S_IFLNK;
}
+public OptionalDateTime_t Path$modified(Path_t path, bool follow_symlinks)
+{
+ struct stat sb;
+ int status = path_stat(path, follow_symlinks, &sb);
+ if (status != 0) return NULL_DATETIME;
+ return (DateTime_t){.tv_sec=sb.st_mtime};
+}
+
+public OptionalDateTime_t Path$accessed(Path_t path, bool follow_symlinks)
+{
+ struct stat sb;
+ int status = path_stat(path, follow_symlinks, &sb);
+ if (status != 0) return NULL_DATETIME;
+ return (DateTime_t){.tv_sec=sb.st_atime};
+}
+
+public OptionalDateTime_t Path$changed(Path_t path, bool follow_symlinks)
+{
+ struct stat sb;
+ int status = path_stat(path, follow_symlinks, &sb);
+ if (status != 0) return NULL_DATETIME;
+ return (DateTime_t){.tv_sec=sb.st_ctime};
+}
+
static void _write(Path_t path, Array_t bytes, int mode, int permissions)
{
path = Path$_expand_home(path);