17
17
* along with this program. If not, see <http://www.gnu.org/licenses>.
18
18
*/
19
19
20
+ #include <errno.h>
20
21
#include <stdlib.h>
21
22
#include <pwd.h>
22
23
#include <sys/types.h>
23
24
#include <string.h>
25
+ #include <time.h>
24
26
#include <unistd.h>
25
27
#include <stdio.h>
26
28
#include <ctype.h>
29
+ #include <sys/stat.h>
27
30
28
31
#include "option-table.h"
29
32
#include "query-assign.h"
@@ -199,6 +202,69 @@ void print_help(void)
199
202
nvgetopt_print_help (__options , 0 , print_help_helper );
200
203
}
201
204
205
+ /*
206
+ * locate_default_rc_file() - find a suitable location for the default
207
+ * configuration file. This will be one of:
208
+ * - $XDG_CONFIG_HOME/nvidia/settings-rc,
209
+ * - $HOME/.config/nvidia/settings-rc or
210
+ * - $HOME/.nvidia-settings-rc
211
+ * The last of which is chosen only if the file already exists, for
212
+ * backwards compatibility.
213
+ *
214
+ * The parent directory, `nvidia` for the first two options will be
215
+ * created if it doesn't already exist.
216
+ *
217
+ * The string returned is malloc'ed, but must not be freed as it is
218
+ * re-used if this is called multiple times.
219
+ */
220
+ const char * locate_default_rc_file (void )
221
+ {
222
+ static char * default_rc_file = NULL ;
223
+ const char * home ;
224
+ const char * xdg_config_home ;
225
+
226
+ if (default_rc_file ) {
227
+ return default_rc_file ;
228
+ }
229
+
230
+ home = get_user_home ();
231
+ xdg_config_home = getenv ("XDG_CONFIG_HOME" );
232
+
233
+ /* Prefer the legacy dot-file in $HOME if it exists. */
234
+
235
+ if (home ) {
236
+ nv_append_sprintf (& default_rc_file , "%s/.nvidia-settings-rc" , home );
237
+
238
+ if (access (default_rc_file , F_OK ) == 0 ) {
239
+ return default_rc_file ;
240
+ }
241
+
242
+ nvfree (default_rc_file );
243
+ default_rc_file = NULL ;
244
+ }
245
+
246
+ if (xdg_config_home ) {
247
+ nv_append_sprintf (& default_rc_file , "%s/nvidia" , xdg_config_home );
248
+ } else if (home ) {
249
+ nv_append_sprintf (& default_rc_file , "%s/.config/nvidia" , home );
250
+ } else {
251
+ /* Store in the current directory as the last resort if we cannot find a home. */
252
+
253
+ return ".nvidia-settings-rc" ;
254
+ }
255
+
256
+ if (access (default_rc_file , F_OK ) != 0 ) {
257
+ if (mkdir (default_rc_file , 0755 ) < 0 ) {
258
+ fprintf (stderr ,
259
+ "Failed to create the default configuration directory '%s': %s.\n" ,
260
+ default_rc_file , strerror (errno ));
261
+ }
262
+ }
263
+
264
+ nv_append_sprintf (& default_rc_file , "/settings-rc" );
265
+
266
+ return default_rc_file ;
267
+ }
202
268
203
269
/*
204
270
* parse_command_line() - malloc an Options structure, initialize it
@@ -218,7 +284,7 @@ Options *parse_command_line(int argc, char *argv[],
218
284
int boolval ;
219
285
220
286
op = nvalloc (sizeof (Options ));
221
- op -> config = DEFAULT_RC_FILE ;
287
+ op -> config = locate_default_rc_file () ;
222
288
op -> write_config = NV_TRUE ;
223
289
224
290
/*
0 commit comments