4747
4848#define OPT_MAX 256
4949
50+ static char xdg_config_home_loc [PATH_MAX ];
5051static char home_loc [PATH_MAX ];
5152
5253static struct {
@@ -58,6 +59,7 @@ static struct {
5859 { .file = TUP_OPTIONS_FILE },
5960 { .file = home_loc },
6061#ifndef _WIN32
62+ { .file = xdg_config_home_loc },
6163 { .file = "/etc/tup/options" },
6264#endif
6365};
@@ -67,6 +69,9 @@ static int parse_option_file(int x);
6769static const char * cpu_number (void );
6870static const char * stdout_isatty (void );
6971static const char * get_console_width (void );
72+ #ifndef _WIN32
73+ static int init_xdg_config_home_loc (void );
74+ #endif
7075static int init_home_loc (void );
7176
7277static struct option {
@@ -283,6 +288,11 @@ int tup_option_init(int argc, char **argv)
283288 options [x ].default_value = options [x ].generator ();
284289 }
285290
291+ #ifndef _WIN32
292+ if (init_xdg_config_home_loc () < 0 )
293+ return -1 ;
294+ #endif
295+
286296 if (init_home_loc () < 0 )
287297 return -1 ;
288298
@@ -535,6 +545,28 @@ static const char *stdout_isatty(void)
535545 return buf ;
536546}
537547
548+ #ifndef _WIN32
549+ static int init_xdg_config_home_loc (void ) {
550+ const char * base_dir ;
551+ const char * fmt_string ;
552+
553+ if ((base_dir = getenv ("XDG_CONFIG_HOME" )) && * base_dir != '\0' )
554+ fmt_string = "%s/tup/options" ;
555+ else if ((base_dir = getenv ("HOME" )))
556+ fmt_string = "%s/.config/tup/options" ;
557+ else
558+ return 0 ;
559+
560+ if (snprintf (xdg_config_home_loc , sizeof (xdg_config_home_loc ), fmt_string , base_dir )
561+ >= (signed )sizeof (xdg_config_home_loc )) {
562+ fprintf (stderr , "tup internal error: user-level options file string is too small.\n" );
563+ return -1 ;
564+ }
565+
566+ return 0 ;
567+ }
568+ #endif
569+
538570static int init_home_loc (void )
539571{
540572#if defined(_WIN32 )
0 commit comments