From ba58914cb1476b94d1b6b2e42bc12471c37ea6b3 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Tue, 28 Mar 2023 02:34:23 +0200 Subject: Allow user to specify OS-specific file mode flags --- mkdir_r.c | 4 ++-- mkdir_r.h | 4 +++- posix.c | 4 ++-- private_include/mkdir_r_private.h | 4 +++- win32.c | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mkdir_r.c b/mkdir_r.c index b91daae..d83cf63 100644 --- a/mkdir_r.c +++ b/mkdir_r.c @@ -20,7 +20,7 @@ #include #include -int mkdir_r(const char *const path) +int mkdir_r(const char *const path, const int flags) { int ret = -1; char *dup = NULL; @@ -62,7 +62,7 @@ int mkdir_r(const char *const path) if (!*dir) /* Path starting with delimiter character. */ ; - else if (mkdir_r_port(dir)) + else if (mkdir_r_port(dir, flags)) goto exit; dir = dup; diff --git a/mkdir_r.h b/mkdir_r.h index fb40373..0d7ea7d 100644 --- a/mkdir_r.h +++ b/mkdir_r.h @@ -19,6 +19,8 @@ /** * Recursive directory creation. * @param path Directory path + * @param flags File mode. Might be ignored if the underlying implementation + * does not support it. * @return 0 if successful, -1 otherwise. * @note Sets @c errno to @c ENOMEM if internal dynamic allocation fails. * @note Sets @c errno to @c EINVAL if an invalid or empty directory path is @@ -26,6 +28,6 @@ * @note Sets @c errno to @c ENOTDIR if one of the elements from @c path is not * a directory. */ -int mkdir_r(const char *const path); +int mkdir_r(const char *const path, int flags); #endif /* MKDIR_R_H */ diff --git a/posix.c b/posix.c index d2d3651..4b8fde1 100644 --- a/posix.c +++ b/posix.c @@ -19,7 +19,7 @@ #include #include -int mkdir_r_port(const char *const dir) +int mkdir_r_port(const char *const dir, const int flags) { if (!access(dir, 0)) { @@ -33,7 +33,7 @@ int mkdir_r_port(const char *const dir) return -1; } } - else if (mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) + else if (mkdir(dir, flags)) return -1; return 0; diff --git a/private_include/mkdir_r_private.h b/private_include/mkdir_r_private.h index 12efdbb..b2e4482 100644 --- a/private_include/mkdir_r_private.h +++ b/private_include/mkdir_r_private.h @@ -19,10 +19,12 @@ /** * Creates a directory using OS-specific API. * @param dir Directory path. + * @param flags File mode. Might be ignored if the underlying implementation + * does not support it. * @return 0 if successful, -1 otherwise. * @note Sets @c errno to @c ENOTDIR if one of the elements from @c path is not * a directory. */ -int mkdir_r_port(const char *const dir); +int mkdir_r_port(const char *const dir, int flags); #endif /* MKDIR_R_PRIVATE_H */ diff --git a/win32.c b/win32.c index eff119b..e091d8a 100644 --- a/win32.c +++ b/win32.c @@ -17,7 +17,7 @@ #include #include -int mkdir_r_port(const char *const dir) +int mkdir_r_port(const char *const dir, const int flags) { if (!CreateDirectoryA(dir, NULL)) { -- cgit v1.2.3