| 70 #define UI_ENV_HOME "HOME" |
72 #define UI_ENV_HOME "HOME" |
| 71 #else |
73 #else |
| 72 #define UI_PATH_SEPARATOR '\\' |
74 #define UI_PATH_SEPARATOR '\\' |
| 73 #define UI_ENV_HOME "USERPROFILE" |
75 #define UI_ENV_HOME "USERPROFILE" |
| 74 #endif |
76 #endif |
| |
77 |
| |
78 #define UI_XDG_CONFIG_HOME_VAR "XDG_CONFIG_HOME" |
| 75 |
79 |
| 76 char* ui_configfile(const char *name) { |
80 char* ui_configfile(const char *name) { |
| 77 const char *appname = ui_appname(); |
81 const char *appname = ui_appname(); |
| 78 if(!appname) { |
82 if(!appname) { |
| 79 return NULL; |
83 return NULL; |
| 100 cxBufferPutString(&buf, "Library/Application Support/"); |
104 cxBufferPutString(&buf, "Library/Application Support/"); |
| 101 #elif defined(_WIN32) |
105 #elif defined(_WIN32) |
| 102 // on Windows the app dir is $USERPROFILE/AppData/Local/$APPNAME/ |
106 // on Windows the app dir is $USERPROFILE/AppData/Local/$APPNAME/ |
| 103 cxBufferPutString(&buf, "AppData\\Local\\"); |
107 cxBufferPutString(&buf, "AppData\\Local\\"); |
| 104 #else |
108 #else |
| 105 // app dir is $HOME/.$APPNAME/ |
109 if(use_xdg_config_home) { |
| 106 cxBufferPut(&buf, '.'); |
110 // app dir is $HOME/.config/$APPNAME/ |
| |
111 char *xdghome = getenv(UI_XDG_CONFIG_HOME_VAR); |
| |
112 size_t xdghomelen = xdghome ? strlen(xdghome) : 0; |
| |
113 if(xdghome && xdghomelen > 0) { |
| |
114 cxBufferSeek(&buf, 0, SEEK_SET); |
| |
115 cxBufferPutString(&buf, xdghome); |
| |
116 if(xdghome[xdghomelen-1] != '/') { |
| |
117 cxBufferPut(&buf, '/'); |
| |
118 } |
| |
119 } else { |
| |
120 cxBufferPutString(&buf, ".config/"); |
| |
121 } |
| |
122 } else { |
| |
123 cxBufferPut(&buf, '.'); |
| |
124 } |
| |
125 |
| 107 #endif |
126 #endif |
| 108 cxBufferPutString(&buf, appname); |
127 cxBufferPutString(&buf, appname); |
| 109 cxBufferPut(&buf, UI_PATH_SEPARATOR); |
128 cxBufferPut(&buf, UI_PATH_SEPARATOR); |
| 110 |
129 |
| 111 // add file name |
130 // add file name |
| 137 |
156 |
| 138 char *dir = ui_configfile(NULL); |
157 char *dir = ui_configfile(NULL); |
| 139 if(!dir) { |
158 if(!dir) { |
| 140 return; |
159 return; |
| 141 } |
160 } |
| |
161 size_t len = strlen(dir); |
| |
162 if(len < 2) { |
| |
163 return; |
| |
164 } |
| 142 if(ui_mkdir(dir)) { |
165 if(ui_mkdir(dir)) { |
| |
166 if(errno == ENOENT) { |
| |
167 char *parent = strdup(dir); |
| |
168 for(int i=len-2;i>=0;i--) { |
| |
169 if(parent[i] == '/') { |
| |
170 parent[i] = 0; |
| |
171 break; |
| |
172 } |
| |
173 } |
| |
174 // try creating the parent |
| |
175 int err = ui_mkdir(parent); |
| |
176 if(err) { |
| |
177 fprintf(stderr, "Error: Cannot create directory %s: %s\n", strerror(errno)); |
| |
178 free(parent); |
| |
179 free(dir); |
| |
180 return; |
| |
181 } |
| |
182 free(parent); |
| |
183 |
| |
184 // try dir again |
| |
185 if(!ui_mkdir(dir)) { |
| |
186 errno = EEXIST; // success |
| |
187 } |
| |
188 } |
| |
189 |
| 143 if(errno != EEXIST) { |
190 if(errno != EEXIST) { |
| 144 fprintf(stderr, "Ui Error: Cannot create directory %s\n", dir); |
191 fprintf(stderr, "Error: Cannot create directory %s: %s\n", dir, strerror(errno)); |
| 145 free(dir); |
192 free(dir); |
| 146 return; |
193 return; |
| 147 } |
194 } |
| 148 } |
195 } |
| 149 free(dir); |
196 free(dir); |