tWriteConfigFile() now writes to a given file handle; functions added to return the paths of the local and global configuration files. - vaccinewars - be a doctor and try to vaccinate the world
 (HTM) git clone git://src.adamsgaard.dk/vaccinewars
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit ce6614bddab8d6289b9250c15335d75c8c49efad
 (DIR) parent e88781ee1d688779256d6c478bd434e095f0486a
 (HTM) Author: Ben Webb <ben@salilab.org>
       Date:   Tue, 26 Feb 2002 13:03:07 +0000
       
       WriteConfigFile() now writes to a given file handle; functions added to
       return the paths of the local and global configuration files.
       
       
       Diffstat:
         M src/dopewars.c                      |      93 +++++++++++++++++++++----------
         M src/dopewars.h                      |       4 +++-
         M src/gui_client/optdialog.c          |       2 +-
       
       3 files changed, 68 insertions(+), 31 deletions(-)
       ---
 (DIR) diff --git a/src/dopewars.c b/src/dopewars.c
       t@@ -2016,9 +2016,9 @@ static void WriteConfigValue(FILE *fp, int GlobalIndex, int StructIndex)
        
        /*
         * Writes all of the configuration file variables that have changed
       - * (together with their values) to standard output.
       + * (together with their values) to the given file.
         */
       -void WriteConfigFile(void)
       +void WriteConfigFile(FILE *fp)
        {
          int i, j;
        
       t@@ -2026,10 +2026,10 @@ void WriteConfigFile(void)
            if (Globals[i].Modified) {
              if (Globals[i].NameStruct[0]) {
                for (j = 1; j <= *Globals[i].MaxIndex; j++) {
       -          WriteConfigValue(stdout, i, j);
       +          WriteConfigValue(fp, i, j);
                }
              } else {
       -        WriteConfigValue(stdout, i, 0);
       +        WriteConfigValue(fp, i, 0);
              }
            }
          }
       t@@ -2275,6 +2275,50 @@ gchar *GetDocIndex(void)
          return path;
        }
        
       +/*
       + * Returns the pathname of the global (all users) configuration file,
       + * as a dynamically-allocated string that must be later freed. On
       + * error, NULL is returned.
       + */
       +gchar *GetGlobalConfigFile(void)
       +{
       +#ifdef CYGWIN
       +  gchar *bindir, *conf = NULL;
       +
       +  /* Global configuration is in the same directory as the dopewars binary */
       +  bindir = GetBinaryDir();
       +  if (bindir) {
       +    conf = g_strdup_printf("%s/dopewars-config.txt", conf);
       +    g_free(bindir);
       +  }
       +  return conf;
       +#else
       +  return g_strdup("/etc/dopewars");
       +#endif
       +}
       +
       +/*
       + * Returns the pathname of the local (per-user) configuration file,
       + * as a dynamically-allocated string that must be later freed. On
       + * error, NULL is returned.
       + */
       +gchar *GetLocalConfigFile(void)
       +{
       +#ifdef CYGWIN
       +  return g_strdup("dopewars-config.txt");
       +#else
       +  gchar *home, *conf = NULL;
       +
       +  /* Local config is in the user's home directory */
       +  home = getenv("HOME");
       +  if (home) {
       +    conf = g_strdup_printf("%s/.dopewars", home);
       +    g_free(home);
       +  }
       +  return conf;
       +#endif
       +}
       +
        /* 
         * Sets up data - such as the location of the high score file - to
         * hard-coded internal values, and then processes the global and
       t@@ -2282,7 +2326,7 @@ gchar *GetDocIndex(void)
         */
        void SetupParameters(void)
        {
       -  char *ConfigFile, *pt;
       +  gchar *conf;
          int i;
        
          /* Initialise variables */
       t@@ -2359,34 +2403,25 @@ void SetupParameters(void)
            AssignName(&StoppedTo[i], _(DefaultStoppedTo[i]));
          }
        
       -#ifdef CYGWIN
       +  /* FIXME: this is a bit risky; we haven't dropped privileges yet,
       +   * so 1. we may be able to read files here that the user shouldn't
       +   * have access to and 2. a bug in the configuration parsing code
       +   * could result in a compromise. BUT we don't know where the high
       +   * score file is until the config files have been parsed. */
        
       -  /* Read the global configuration from the directory the binary is
       -   * installed in */
       -  pt = GetBinaryDir();
       -  if (pt) {
       -    ConfigFile = g_strdup_printf("%s/dopewars-config.txt", pt);
       -    ReadConfigFile(ConfigFile);
       -    g_free(ConfigFile);
       -    g_free(pt);
       +  /* Now read in the global configuration file */
       +  conf = GetGlobalConfigFile();
       +  if (conf) {
       +    ReadConfigFile(conf);
       +    g_free(conf);
          }
        
       -  /* Now read the local configuration from the current directory */
       -  ReadConfigFile("dopewars-config.txt");
       -
       -#else /* CYGWIN */
       -
       -  /* Now read in the global configuration file */
       -  ReadConfigFile("/etc/dopewars");
       -
       -  /* Next, try to read in the .dopewars file in the user's home directory */
       -  pt = getenv("HOME");
       -  if (pt) {
       -    ConfigFile = g_strdup_printf("%s/.dopewars", pt);
       -    ReadConfigFile(ConfigFile);
       -    g_free(ConfigFile);
       +  /* Finally, try the local configuration file */
       +  conf = GetLocalConfigFile();
       +  if (conf) {
       +    ReadConfigFile(conf);
       +    g_free(conf);
          }
       -#endif /* CYGWIN */
        }
        
        void HandleHelpTexts()
 (DIR) diff --git a/src/dopewars.h b/src/dopewars.h
       t@@ -420,8 +420,10 @@ void OpenLog(void);
        void CloseLog(void);
        gboolean IsConnectedPlayer(Player *play);
        void BackupConfig(void);
       -void WriteConfigFile(void);
       +void WriteConfigFile(FILE *fp);
        gchar *GetDocIndex(void);
       +gchar *GetGlobalConfigFile(void);
       +gchar *GetLocalConfigFile(void);
        
        #ifndef CURSES_CLIENT
        void CursesLoop(void);
 (DIR) diff --git a/src/gui_client/optdialog.c b/src/gui_client/optdialog.c
       t@@ -464,7 +464,7 @@ static void list_row_unselect(GtkCList *clist, gint row, gint column,
        static void OKCallback(GtkWidget *widget, GtkWidget *dialog)
        {
          SaveConfigWidgets();
       -  WriteConfigFile();
       +  WriteConfigFile(stdout);
          gtk_widget_destroy(dialog);
        }