| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2020 Mark Wooding |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/home.c |
| 19 | * @brief Find things in the user's home directory |
| 20 | */ |
| 21 | |
| 22 | #include "common.h" |
| 23 | |
| 24 | #include <errno.h> |
| 25 | |
| 26 | #if HAVE_UNISTD_H |
| 27 | # include <unistd.h> |
| 28 | #endif |
| 29 | #if HAVE_PWD_H |
| 30 | # include <pwd.h> |
| 31 | #endif |
| 32 | #if HAVE_SHLOBJ_H |
| 33 | # include <Shlobj.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "mem.h" |
| 37 | #include "home.h" |
| 38 | #include "log.h" |
| 39 | #include "printf.h" |
| 40 | |
| 41 | #if _WIN32 |
| 42 | # define DIRSEP "\\" |
| 43 | #else |
| 44 | # define DIRSEP "/" |
| 45 | #endif |
| 46 | |
| 47 | static char *profiledir; |
| 48 | |
| 49 | |
| 50 | /** @brief Return the user's profile directory |
| 51 | * @return profile directory |
| 52 | * On Unix, this defaults to `$HOME/.disorder/'; on Windows, it's |
| 53 | * `%APPDATA%\DisOrder\'. The trailing delimiter is included. |
| 54 | */ |
| 55 | const char *profile_directory(void) { |
| 56 | char *t; |
| 57 | |
| 58 | if(profiledir) return profiledir; |
| 59 | if((t = getenv("DISORDER_HOME"))) |
| 60 | profiledir = t; |
| 61 | else { |
| 62 | #if _WIN32 |
| 63 | wchar_t *wpath = 0; |
| 64 | char *appdata; |
| 65 | if(SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &wpath) != S_OK) { |
| 66 | disorder_error(0, "error calling SHGetKnownFolderPath"); |
| 67 | return 0; |
| 68 | } |
| 69 | t = win_wtomb(wpath); |
| 70 | CoTaskMemFree(wpath); |
| 71 | byte_xasprintf(&profiledir, "%s\\DisOrder", appdata); |
| 72 | #else |
| 73 | struct passwd *pw; |
| 74 | if(!(t = getenv("HOME"))) { |
| 75 | if(!(pw = getpwuid(getuid()))) |
| 76 | disorder_error(0, "user not found in password database"); |
| 77 | t = pw->pw_dir; |
| 78 | } |
| 79 | byte_xasprintf(&profiledir, "%s/.disorder", t); |
| 80 | #endif |
| 81 | } |
| 82 | return profiledir; |
| 83 | } |
| 84 | |
| 85 | /** @brief Return the name of a file within the user's profile directory |
| 86 | * @param file Basename of the file desired |
| 87 | * @return Full path name of selected file. |
| 88 | * This currently doesn't do anything very useful with directory separators |
| 89 | * within @a file. |
| 90 | */ |
| 91 | char *profile_filename(const char *file) { |
| 92 | const char *d; |
| 93 | char *t; |
| 94 | if(!(d = profile_directory())) return 0; |
| 95 | byte_xasprintf(&t, "%s" DIRSEP "%s", d, file); |
| 96 | return t; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | Local Variables: |
| 101 | c-basic-offset:2 |
| 102 | comment-column:40 |
| 103 | fill-column:79 |
| 104 | indent-tabs-mode:nil |
| 105 | End: |
| 106 | */ |