diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-01 21:49:34 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-01 21:49:34 +0200 |
commit | 6d77eb748a6b15b9f473ab86371bfa474898cef1 (patch) | |
tree | a0a7186f0b046f3380fef374dfba4736dea3e309 | |
parent | 8391390610a31209c230ec3c253db30c52b5dcb7 (diff) | |
download | ck-6d77eb748a6b15b9f473ab86371bfa474898cef1.tar.gz ck-6d77eb748a6b15b9f473ab86371bfa474898cef1.tar.bz2 ck-6d77eb748a6b15b9f473ab86371bfa474898cef1.zip |
Add swap tilde and home function
-rw-r--r-- | src/ckutil.c | 32 | ||||
-rw-r--r-- | src/ckutil.h | 2 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/ckutil.c b/src/ckutil.c index 79d7fd3..5f8da98 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -143,6 +143,38 @@ int str_is_empty(const char *s) { return 1; } +int swap_tilde_and_home(char *ret, const char *s) { + if (!ret || !s) { + return -1; + } + char *home = getenv("HOME"); + if (!home) { + return -1; + } + ulong i; + ulong homelen = strlen(home); + + if (s[0] == '~') { + strcpy(ret, home); + for (i = 1; i < strlen(s); i++) { + ret[homelen - 1 + i] = s[i]; + } + return 0; + } + + for (i = 0; i < strlen(home); i++) { + if (s[i] != home[i]) { + return -1; + } + } + /* s starts with $HOME */ + strcpy(ret, "~"); + for (i = homelen; i < strlen(s); i++) { + ret[i + 1 - homelen] = s[i]; + } + return 0; +} + int util_own_grp_copy(const char *dest, const char *original) { if (!dest || !original) { return -1; diff --git a/src/ckutil.h b/src/ckutil.h index 8ca9ca1..72216ef 100644 --- a/src/ckutil.h +++ b/src/ckutil.h @@ -59,6 +59,8 @@ extern void str_join_dirname_with_basename(char *ret, const char *path, * else returns 0. */ extern int str_is_empty(const char *s); +extern int swap_tilde_and_home(char *ret, const char *s); + /*********************/ /* utility functions */ /*********************/ |