From b52c22a758f1cc53c756bdf6354bdc583e95754e Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Mon, 4 Jan 2010 08:05:48 +0000 Subject: [PATCH] Allow config file in XDG-compliant location --- ChangeLog | 4 ++++ configure.ac | 2 +- src/config_file.c | 49 +++++++++++++++++++++++++++++++++++++++++++---- src/display_cow.c | 2 +- xcowsay.6 | 10 +++++++--- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7c6e5d6..10c9da9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,10 @@ * src/display_cow.c (display_cow): Use bubble_x and bubble_y config variables to alter relative location of bubble. + * src/config_file.c (parse_config_file): Allow the config file + to be placed in $XDG_CONFIG_HOME/xcowsayrc as well as + $HOME/.xcowsayrc. + 2010-01-03 Nick Gasson * src/xcowsay.c (main): Add --at option to specify cow location diff --git a/configure.ac b/configure.ac index 72b5702..3554e67 100644 --- a/configure.ac +++ b/configure.ac @@ -26,7 +26,7 @@ AC_TYPE_SIZE_T # Checks for library functions. AC_FUNC_MALLOC -AC_CHECK_FUNCS([strtol setlocale strcasecmp strdup]) +AC_CHECK_FUNCS([strtol setlocale strcasecmp strdup strchr]) # Check for pkg-config packages modules="gtk+-2.0 gdk-pixbuf-2.0" diff --git a/src/config_file.c b/src/config_file.c index ee1c7fc..e8777a9 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -1,5 +1,5 @@ /* config_file.c -- Config file parser. - * Copyright (C) 2008 Nick Gasson + * Copyright (C) 2008-2010 Nick Gasson * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,10 @@ #include #include +#include +#include +#include + #include "config_file.h" #include "settings.h" @@ -141,16 +145,53 @@ static bool is_bool_option(const char *s, bool *bval) return false; } -void parse_config_file(void) +static char *config_file_name(void) { - char fname[FILENAME_MAX]; + // There are two possible locations for the config file: + // - $XDG_CONFIG_HOME/xcowsayrc + // - $HOME/.xcowsayrc + // We prefer the XDG compliant one + // Need to free the result of this function + + char *fname = NULL; + const char *home = getenv("HOME"); if (NULL == home) return; + + const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); + if (xdg_config_home == NULL || *xdg_config_home == '\0') { + // Defaults to $HOME/.config + asprintf(&fname, "%s/.config/xcowsayrc", home); + } + else + asprintf(&fname, "%s/xcowsayrc", xdg_config_home); + + struct stat dummy; + if (stat(fname, &dummy) == 0) + return fname; - snprintf(fname, FILENAME_MAX, "%s/.xcowsayrc", home); + free(fname); + + // Try the home directory + asprintf(&fname, "%s/.xcowsayrc", home); + if (stat(fname, &dummy) == 0) + return fname; + + free(fname); + return NULL; +} + +void parse_config_file(void) +{ + char *fname = config_file_name(); + printf("config file name = %s\n", fname); + if (fname == NULL) + return; FILE *frc = fopen(fname, "r"); + free(fname); + if (NULL == frc) return; diff --git a/src/display_cow.c b/src/display_cow.c index 7caf11a..f5ebcb3 100644 --- a/src/display_cow.c +++ b/src/display_cow.c @@ -1,5 +1,5 @@ /* display_cow.c -- Display a cow in a popup window. - * Copyright (C) 2008 Nick Gasson + * Copyright (C) 2008-2010 Nick Gasson * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/xcowsay.6 b/xcowsay.6 index 493f904..cb395ae 100644 --- a/xcowsay.6 +++ b/xcowsay.6 @@ -46,9 +46,13 @@ request and returns immediately. Otherwise will block until the cow has disappeared. .SH CONFIGURATION FILE -xcowsay reads a configuration file on startup. Currently the file must -be named .xcowsayrc in your home directory, a system-wide config file -might be added in a later release. +xcowsay reads a configuration file on startup. The configuration file +can be stored in the XDG compliant location +.I $XDG_CONFIG_HOME/xcowsayrc +(which will default to +.IR $HOME/.config/xcowsayrc ) +or in your home directory at +.IR $HOME/.xcowsayrc . The configuration file consists of 'option = value' pairs, one per line. The valid keys are given in the next section. For example, the following line sets -- 2.39.2