summaryrefslogtreecommitdiff
path: root/testing/toxic/configdir.c
diff options
context:
space:
mode:
authorSimon Levermann <simon@levermaenner.de>2013-08-08 10:15:07 +0200
committerSimon Levermann <simon@levermaenner.de>2013-08-08 10:15:07 +0200
commitd7e2cca854cbc3d0f2418fd95f7033d6b352b587 (patch)
treec2c86720c357bf9af0d067c2e3fc5072dba2daec /testing/toxic/configdir.c
parent225f74e844299c405b744a9d03f4fcc0430b430a (diff)
Attempt to get and create a proper directory for config storage.
Diffstat (limited to 'testing/toxic/configdir.c')
-rw-r--r--testing/toxic/configdir.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c
new file mode 100644
index 00000000..c9f7de85
--- /dev/null
+++ b/testing/toxic/configdir.c
@@ -0,0 +1,91 @@
1/*
2 * Copyright (C) 2013 Tox project All Rights Reserved.
3 *
4 * This file is part of Tox.
5 *
6 * Tox is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Tox is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <string.h>
22#include <stdlib.h>
23#include <stdio.h>
24
25#ifdef _win32
26#include <shlobj.h>
27#endif
28
29#ifdef __APPLE__
30#include <sys/types.h>
31#include <unistd.h>
32#include <pwd.h>
33#endif
34
35char *get_user_config_dir(void)
36{
37 char *user_config_dir;
38
39 #ifdef _win32
40
41 char appdata[MAX_PATH];
42 HRESULT result = SHGetFolderPath(
43 NULL,
44 CSIDL_APPDATA, // TODO: Maybe use CSIDL_LOCAL_APPDATA instead? Not sure.
45 NULL,
46 SHGFP_TYPE_CURRENT,
47 appdata
48 )
49 if (!result) return NULL;
50
51 user_config_dir = malloc(strlen(appdata) + strlen(CONFIGDIR) + 1);
52 if (user_config_dir) {
53 strcpy(user_config_dir, appdata);
54 strcat(user_config_dir, CONFIGDIR);
55 }
56 return user_config_dir;
57
58 #elif defined __APPLE__
59
60 struct passwd *pass = getpwuid(getuid());
61 if (!pass) return NULL;
62 char *home = pass->pw_dir;
63 user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + strlen(CONFIGDIR) + 1);
64
65 if(user_config_dir) {
66 strcpy(user_config_dir, home);
67 strcat(user_config_dir, "/Library/Application Support");
68 strcat(user_config_dir, CONFIGDIR);
69 }
70 return user_config_dir;
71
72 #else
73
74 if (getenv("XDG_CONFIG_HOME")) {
75 user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + strlen(CONFIGDIR) + 1);
76 if (user_config_dir) {
77 strcpy(user_config_dir, getenv("XDG_CONFIG_HOME"));
78 strcat(user_config_dir, CONFIGDIR);
79 }
80 } else {
81 user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + strlen(CONFIGDIR) + 1);
82 if (user_config_dir) {
83 strcpy(user_config_dir, getenv("HOME"));
84 strcat(user_config_dir, "/.config");
85 strcat(user_config_dir, CONFIGDIR);
86 }
87 }
88 return user_config_dir;
89
90 #endif
91} \ No newline at end of file