Browse Source

faux.base: faux_isfile() function

Serj Kalichev 3 years ago
parent
commit
13740aaf82
2 changed files with 26 additions and 0 deletions
  1. 25 0
      faux/base/fs.c
  2. 1 0
      faux/faux.h

+ 25 - 0
faux/base/fs.c

@@ -90,6 +90,30 @@ bool_t faux_isdir(const char *path)
 	return BOOL_FALSE;
 }
 
+
+/** @brief If given path is regular file.
+ *
+ * @param [in] path Filesystem path.
+ * @return 0 - success, < 0 on error.
+ */
+bool_t faux_isfile(const char *path)
+{
+	struct stat statbuf = {};
+
+	assert(path);
+	if (!path)
+		return BOOL_FALSE;
+
+	if (stat(path, &statbuf) < 0)
+		return BOOL_FALSE;
+
+	if (S_ISREG(statbuf.st_mode))
+		return BOOL_TRUE;
+
+	return BOOL_FALSE;
+}
+
+
 /** @brief Removes filesystem objects recursively.
  *
  * Function can remove file or directory (recursively).
@@ -130,6 +154,7 @@ bool_t faux_rm(const char *path)
 	return BOOL_TRUE;
 }
 
+
 /** @brief Expand tilde within path due to HOME env var.
  *
  * If first character of path is tilde then expand it to value of

+ 1 - 0
faux/faux.h

@@ -88,6 +88,7 @@ ssize_t faux_read_whole_file(const char *path, void **data);
 // Filesystem
 ssize_t faux_filesize(const char *path);
 bool_t faux_isdir(const char *path);
+bool_t faux_isfile(const char *path);
 bool_t faux_rm(const char *path);
 char *faux_expand_tilde(const char *path);