summaryrefslogtreecommitdiff
path: root/testing/toxic/configdir.c
diff options
context:
space:
mode:
authorSimon Levermann <simon@levermaenner.de>2013-08-08 16:00:12 +0200
committerSimon Levermann <simon@levermaenner.de>2013-08-08 16:00:12 +0200
commit97e178db3aa9485ab49c646071305164cb5a7681 (patch)
treebe7b8aeca43937550db9264762f8941632a82085 /testing/toxic/configdir.c
parentd7e2cca854cbc3d0f2418fd95f7033d6b352b587 (diff)
Implement proper config directories.
Diffstat (limited to 'testing/toxic/configdir.c')
-rw-r--r--testing/toxic/configdir.c167
1 files changed, 111 insertions, 56 deletions
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c
index c9f7de85..7d11d020 100644
--- a/testing/toxic/configdir.c
+++ b/testing/toxic/configdir.c
@@ -21,71 +21,126 @@
21#include <string.h> 21#include <string.h>
22#include <stdlib.h> 22#include <stdlib.h>
23#include <stdio.h> 23#include <stdio.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <errno.h>
24 27
25#ifdef _win32 28#ifdef WIN32
26#include <shlobj.h> 29#include <shlobj.h>
30#include <direct.h>
27#endif 31#endif
28 32
29#ifdef __APPLE__ 33#ifdef __APPLE__
30#include <sys/types.h>
31#include <unistd.h> 34#include <unistd.h>
32#include <pwd.h> 35#include <pwd.h>
33#endif 36#endif
34 37
38#include "configdir.h"
39
40/*
41 * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash
42 */
35char *get_user_config_dir(void) 43char *get_user_config_dir(void)
36{ 44{
37 char *user_config_dir; 45 char *user_config_dir;
38 46
39 #ifdef _win32 47 #ifdef WIN32
40 48
41 char appdata[MAX_PATH]; 49 char appdata[MAX_PATH];
42 HRESULT result = SHGetFolderPath( 50 HRESULT result = SHGetFolderPath(
43 NULL, 51 NULL,
44 CSIDL_APPDATA, // TODO: Maybe use CSIDL_LOCAL_APPDATA instead? Not sure. 52 CSIDL_APPDATA,
45 NULL, 53 NULL,
46 SHGFP_TYPE_CURRENT, 54 SHGFP_TYPE_CURRENT,
47 appdata 55 appdata
48 ) 56 )
49 if (!result) return NULL; 57 if (!result) return NULL;
50 58
51 user_config_dir = malloc(strlen(appdata) + strlen(CONFIGDIR) + 1); 59 user_config_dir = malloc(strlen(appdata) + 1);
52 if (user_config_dir) { 60 if (user_config_dir) {
53 strcpy(user_config_dir, appdata); 61 strcpy(user_config_dir, appdata);
54 strcat(user_config_dir, CONFIGDIR); 62 }
55 } 63 return user_config_dir;
56 return user_config_dir; 64
57 65 #elif defined __APPLE__
58 #elif defined __APPLE__ 66
59 67 struct passwd *pass = getpwuid(getuid());
60 struct passwd *pass = getpwuid(getuid()); 68 if (!pass) return NULL;
61 if (!pass) return NULL; 69 char *home = pass->pw_dir;
62 char *home = pass->pw_dir; 70 user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1);
63 user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + strlen(CONFIGDIR) + 1); 71
64 72 if(user_config_dir) {
65 if(user_config_dir) { 73 strcpy(user_config_dir, home);
66 strcpy(user_config_dir, home); 74 strcat(user_config_dir, "/Library/Application Support");
67 strcat(user_config_dir, "/Library/Application Support"); 75 }
68 strcat(user_config_dir, CONFIGDIR); 76 return user_config_dir;
69 } 77
70 return user_config_dir; 78 #else
71 79
72 #else 80 if (getenv("XDG_CONFIG_HOME")) {
73 81 user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + 1);
74 if (getenv("XDG_CONFIG_HOME")) { 82 if (user_config_dir) {
75 user_config_dir = malloc(strlen(getenv("XDG_CONFIG_HOME")) + strlen(CONFIGDIR) + 1); 83 strcpy(user_config_dir, getenv("XDG_CONFIG_HOME"));
76 if (user_config_dir) { 84 }
77 strcpy(user_config_dir, getenv("XDG_CONFIG_HOME")); 85 } else {
78 strcat(user_config_dir, CONFIGDIR); 86 user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1);
79 } 87 if (user_config_dir) {
80 } else { 88 strcpy(user_config_dir, getenv("HOME"));
81 user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + strlen(CONFIGDIR) + 1); 89 strcat(user_config_dir, "/.config");
82 if (user_config_dir) { 90 }
83 strcpy(user_config_dir, getenv("HOME")); 91 }
84 strcat(user_config_dir, "/.config"); 92 return user_config_dir;
85 strcat(user_config_dir, CONFIGDIR); 93
86 } 94 #endif
87 } 95}
88 return user_config_dir; 96
89 97/*
90 #endif 98 * Creates the config directory.
99 */
100int create_user_config_dir(char *path)
101{
102
103 int mkdir_err;
104
105 #ifdef WIN32
106
107 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
108 strcpy(fullpath, path);
109 strcat(fullpath, CONFIGDIR);
110
111 mkdir_err = _mkdir(fullpath);
112
113 if (mkdir_err) {
114 if(errno != EEXIST) return -1;
115 struct _stat buf;
116 if(_wstat64(fullpath, &buf)) return -1;
117 if(!S_ISDIR(buf.st_mode)) return -1;
118 }
119
120 #else
121
122 mkdir_err = mkdir(path, 0700);
123 struct stat buf;
124
125 if(mkdir_err) {
126 if(errno != EEXIST) return -1;
127 if(stat(path, &buf)) return -1;
128 if(!S_ISDIR(buf.st_mode)) return -1;
129 }
130
131 char *fullpath = malloc(strlen(path) + strlen(CONFIGDIR) + 1);
132 strcpy(fullpath, path);
133 strcat(fullpath, CONFIGDIR);
134
135 mkdir_err = mkdir(fullpath, 0700);
136
137 if(mkdir_err) {
138 if(errno != EEXIST) return -1;
139 if(stat(fullpath, &buf)) return -1;
140 if(!S_ISDIR(buf.st_mode)) return -1;
141 }
142
143 #endif
144
145 return 0;
91} \ No newline at end of file 146} \ No newline at end of file