diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-23 06:50:42 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-23 06:50:42 -0400 |
commit | bcb87139a2d8c5b308cfb82635e9483e867c9dbc (patch) | |
tree | 822a967fe0a795b605f1b97d5cd063d5b4b7179d /testing/toxic/configdir.c | |
parent | 733066a57e8d5403c91475fb6b095f44a81bf270 (diff) | |
parent | 4d89d63c3f1489e248261983f6c73a8b083b3336 (diff) |
Merge branch 'master' into new-api
Diffstat (limited to 'testing/toxic/configdir.c')
-rw-r--r-- | testing/toxic/configdir.c | 165 |
1 files changed, 0 insertions, 165 deletions
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c deleted file mode 100644 index a43dd1de..00000000 --- a/testing/toxic/configdir.c +++ /dev/null | |||
@@ -1,165 +0,0 @@ | |||
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 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | #include <errno.h> | ||
27 | |||
28 | #ifdef WIN32 | ||
29 | #include <shlobj.h> | ||
30 | #include <direct.h> | ||
31 | #else /* WIN32 */ | ||
32 | #include <unistd.h> | ||
33 | #include <pwd.h> | ||
34 | #endif /* WIN32 */ | ||
35 | |||
36 | #include "configdir.h" | ||
37 | |||
38 | /** | ||
39 | * @brief Get the users config directory. | ||
40 | * | ||
41 | * This is without a trailing slash. | ||
42 | * | ||
43 | * @return The users config dir or NULL on error. | ||
44 | */ | ||
45 | char *get_user_config_dir(void) | ||
46 | { | ||
47 | char *user_config_dir; | ||
48 | #ifdef WIN32 | ||
49 | char appdata[MAX_PATH]; | ||
50 | BOOL ok; | ||
51 | |||
52 | ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE); | ||
53 | |||
54 | if (!ok) { | ||
55 | return NULL; | ||
56 | } | ||
57 | |||
58 | user_config_dir = strdup(appdata); | ||
59 | |||
60 | return user_config_dir; | ||
61 | |||
62 | #else /* WIN32 */ | ||
63 | |||
64 | #ifndef NSS_BUFLEN_PASSWD | ||
65 | #define NSS_BUFLEN_PASSWD 4096 | ||
66 | #endif /* NSS_BUFLEN_PASSWD */ | ||
67 | |||
68 | struct passwd pwd; | ||
69 | struct passwd *pwdbuf; | ||
70 | const char *home; | ||
71 | char buf[NSS_BUFLEN_PASSWD]; | ||
72 | size_t len; | ||
73 | int rc; | ||
74 | |||
75 | rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); | ||
76 | |||
77 | if (rc == 0) { | ||
78 | home = pwd.pw_dir; | ||
79 | } else { | ||
80 | home = getenv("HOME"); | ||
81 | |||
82 | if (home == NULL) { | ||
83 | return NULL; | ||
84 | } | ||
85 | |||
86 | /* env variables can be tainted */ | ||
87 | snprintf(buf, sizeof(buf), "%s", home); | ||
88 | home = buf; | ||
89 | } | ||
90 | |||
91 | # if defined(__APPLE__) | ||
92 | len = strlen(home) + strlen("/Library/Application Support") + 1; | ||
93 | user_config_dir = malloc(len); | ||
94 | |||
95 | if (user_config_dir == NULL) { | ||
96 | return NULL; | ||
97 | } | ||
98 | |||
99 | snprintf(user_config_dir, len, "%s/Library/Application Support", home); | ||
100 | # else /* __APPLE__ */ | ||
101 | |||
102 | if (!(user_config_dir = getenv("XDG_CONFIG_HOME"))) { | ||
103 | len = strlen(home) + strlen("/.config") + 1; | ||
104 | user_config_dir = malloc(len); | ||
105 | |||
106 | if (user_config_dir == NULL) { | ||
107 | return NULL; | ||
108 | } | ||
109 | |||
110 | snprintf(user_config_dir, len, "%s/.config", home); | ||
111 | } | ||
112 | |||
113 | # endif /* __APPLE__ */ | ||
114 | |||
115 | return user_config_dir; | ||
116 | #undef NSS_BUFLEN_PASSWD | ||
117 | #endif /* WIN32 */ | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * Creates the config directory. | ||
122 | */ | ||
123 | int create_user_config_dir(char *path) | ||
124 | { | ||
125 | |||
126 | int mkdir_err; | ||
127 | |||
128 | #ifdef WIN32 | ||
129 | |||
130 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | ||
131 | strcpy(fullpath, path); | ||
132 | strcat(fullpath, CONFIGDIR); | ||
133 | |||
134 | mkdir_err = _mkdir(fullpath); | ||
135 | struct __stat64 buf; | ||
136 | |||
137 | if (mkdir_err && (errno != EEXIST || _wstat64(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | ||
138 | free(fullpath); | ||
139 | return -1; | ||
140 | } | ||
141 | |||
142 | #else | ||
143 | |||
144 | mkdir_err = mkdir(path, 0700); | ||
145 | struct stat buf; | ||
146 | |||
147 | if (mkdir_err && (errno != EEXIST || stat(path, &buf) || !S_ISDIR(buf.st_mode))) { | ||
148 | return -1; | ||
149 | } | ||
150 | |||
151 | char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1); | ||
152 | strcpy(fullpath, path); | ||
153 | strcat(fullpath, CONFIGDIR); | ||
154 | |||
155 | mkdir_err = mkdir(fullpath, 0700); | ||
156 | |||
157 | if (mkdir_err && (errno != EEXIST || stat(fullpath, &buf) || !S_ISDIR(buf.st_mode))) { | ||
158 | free(fullpath); | ||
159 | return -1; | ||
160 | } | ||
161 | |||
162 | #endif | ||
163 | free(fullpath); | ||
164 | return 0; | ||
165 | } | ||