diff options
author | EuAndreh <eu@euandre.org> | 2021-06-27 09:49:57 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-06-27 09:49:57 -0300 |
commit | 401459b5f4ef5b7b5f69a26393cbc4a210ba7262 (patch) | |
tree | 35fc61c14a8562cda321c6d1ff1b777b4a4381b9 /src/remembering-c.c | |
parent | src/remembering-c.c: Mark all functions as static (diff) | |
download | remembering-401459b5f4ef5b7b5f69a26393cbc4a210ba7262.tar.gz remembering-401459b5f4ef5b7b5f69a26393cbc4a210ba7262.tar.xz |
src/remembering-c.c: Add get_profile_file()
Diffstat (limited to '')
-rw-r--r-- | src/remembering-c.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/remembering-c.c b/src/remembering-c.c index 39429d6..2e7395f 100644 --- a/src/remembering-c.c +++ b/src/remembering-c.c @@ -52,6 +52,51 @@ static int missing(FILE *stream, const char *const argument) { return 0; } +static char *get_profile_file(const char *const profile) { + char *home = NULL; + if (!(home = getenv("XDG_DATA_HOME"))) { + if (!(home = getenv("HOME"))) { + fprintf(stderr, "Unable to get $XDG_DATA_HOME or $HOME environment variables\n"); + return NULL; + } + const char *const suffix = "/.local/share/remembering"; + const size_t home_suffix_size = + strlen(home) + + strlen(suffix) + + sizeof(char); + char *const home_with_suffix = malloc(home_suffix_size); + if (!home_with_suffix) { + perror("get_profile_file() - home_with_suffix"); + return NULL; + } + strcat(home_with_suffix, home); + strcat(home_with_suffix, suffix); + home = home_with_suffix; + } else { + if (!(home = strdup(home))) { + perror("get_profile_file() - strdup()"); + return NULL; + } + } + + size_t size = + strlen(home) + + strlen("/") + + strlen(profile) + + sizeof(char); + char *const profile_file = malloc(size); + if (!profile_file) { + perror("get_profile_file() - profile_file"); + free(home); + return NULL; + } + strcat(profile_file, home); + strcat(profile_file, "/"); + strcat(profile_file, profile); + + return profile_file; +} + int main(int argc, char *argv[]) { #ifdef TEST return EXIT_SUCCESS; @@ -106,5 +151,14 @@ int main(int argc, char *argv[]) { return EXIT_USAGE; } + char *const profile_file = get_profile_file(profile); + if(!profile_file) { + return EXIT_ERROR; + } + + printf("profile: %s\ncommand: %s\n", profile, command); + printf("profile_file: %s\n", profile_file); + + free(profile_file); return 0; } |