+/* Try to open a configuration file. If "env" is specified, open the
+ * value of the specified environment variable. Otherwise look for a
+ * file "local" in the home directory or a file "global" in global
+ * file system. If "result" is non-NULL, a pointer to a newly
+ * allocated buffer containing the used configuration file is
+ * stored there.*/
+FILE *pa_open_config_file(const char *global, const char *local, const char *env, char **result) {
+ const char *e;
+ char h[PATH_MAX];
+
+ if (env && (e = getenv(env))) {
+ if (result)
+ *result = pa_xstrdup(e);
+ return fopen(e, "r");
+ }
+
+ if (local && pa_get_home_dir(h, sizeof(h))) {
+ FILE *f;
+ char *l;
+
+ l = pa_sprintf_malloc("%s/%s", h, local);
+ f = fopen(l, "r");
+
+ if (f || errno != ENOENT) {
+ if (result)
+ *result = l;
+ else
+ pa_xfree(l);
+ return f;
+ }
+
+ pa_xfree(l);
+ }
+
+ if (!global) {
+ if (result)
+ *result = NULL;
+ errno = ENOENT;
+ return NULL;
+ }
+
+ if (result)
+ *result = pa_xstrdup(global);
+
+ return fopen(global, "r");
+}
+