Browse Source

faux.base: faux_expand_tilde()

Serj Kalichev 4 years ago
parent
commit
36c24b4874
2 changed files with 32 additions and 0 deletions
  1. 31 0
      faux/base/fs.c
  2. 1 0
      faux/faux.h

+ 31 - 0
faux/base/fs.c

@@ -13,6 +13,8 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#include "faux/str.h"
+
 /** @brief Removes filesystem objects recursively.
  *
  * Function can remove file or directory (recursively).
@@ -50,3 +52,32 @@ int faux_rm(const char *path) {
 
 	return rmdir(path);
 }
+
+/** @brief Expand tilde within path due to HOME env var.
+ *
+ * If first character of path is tilde then expand it to value of
+ * environment variable HOME. If tilde is not the first character or
+ * HOME is not defined then return copy of original path.
+ *
+ * @warning The resulting string must be freed by faux_str_free() later.
+ *
+ * @param [in] path Path to expand.
+ * @return Expanded string or NULL on error.
+ */
+char *faux_expand_tilde(const char *path) {
+
+	char *home_dir = getenv("HOME");
+	char *result = NULL;
+
+	assert(path);
+	if (!path)
+		return NULL;
+
+	// Tilde can be the first character only to be expanded
+	if (home_dir && (path[0] == '~'))
+		result = faux_str_sprintf("%s%s", home_dir, &path[1]);
+	else
+		result = faux_str_dup(path);
+
+	return result;
+}

+ 1 - 0
faux/faux.h

@@ -72,6 +72,7 @@ size_t faux_read_block(int fd, void *buf, size_t n);
 
 // Filesystem
 int faux_rm(const char *path);
+char *faux_expand_tilde(const char *path);
 
 C_DECL_END