char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
struct passwd passwd, *ignored;
char *name;
+ int e;
- if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0) {
+ if (pw_buf_size == -1) pw_buf_size = 64;
+
+ while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
+ pw_buf_size, &ignored)) == ERANGE) {
+ pw_buf_size = pw_buf_size * 2;
+ pw_buf = talloc_zero_size(ctx, pw_buf_size);
+ }
+
+ if (e == 0) {
char *comma = strchr (passwd.pw_gecos, ',');
if (comma)
name = talloc_strndup (ctx, passwd.pw_gecos,
char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
struct passwd passwd, *ignored;
char *name;
+ int e;
+
+ if (pw_buf_size == -1) pw_buf_size = 64;
+ while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
+ pw_buf_size, &ignored)) == ERANGE) {
+ pw_buf_size = pw_buf_size * 2;
+ pw_buf = talloc_zero_size(ctx, pw_buf_size);
+ }
- if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0)
+ if (e == 0)
name = talloc_strdup (ctx, passwd.pw_name);
else
name = talloc_strdup (ctx, "");
return name;
}
-/* Open the named notmuch configuration file. A filename of NULL will
- * be interpreted as the default configuration file
- * ($HOME/.notmuch-config).
+/* Open the named notmuch configuration file. If the filename is NULL,
+ * the value of the environment variable $NOTMUCH_CONFIG will be used.
+ * If $NOTMUCH_CONFIG is unset, the default configuration file
+ * ($HOME/.notmuch-config) will be used.
*
* If any error occurs, (out of memory, or a permission-denied error,
* etc.), this function will print a message to stderr and return
* in editing the file directly.
*/
notmuch_config_t *
-notmuch_config_open (void *ctx, const char *filename)
+notmuch_config_open (void *ctx,
+ const char *filename,
+ notmuch_bool_t *is_new_ret)
{
GError *error = NULL;
- int config_file_is_new = 0;
+ int is_new = 0;
+ char *notmuch_config_env = NULL;
+
+ if (is_new_ret)
+ *is_new_ret = 0;
notmuch_config_t *config = talloc (ctx, notmuch_config_t);
if (config == NULL) {
talloc_set_destructor (config, notmuch_config_destructor);
- if (filename)
+ if (filename) {
config->filename = talloc_strdup (config, filename);
- else
+ } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
+ config->filename = talloc_strdup (config, notmuch_config_env);
+ } else {
config->filename = talloc_asprintf (config, "%s/.notmuch-config",
getenv ("HOME"));
+ }
config->key_file = g_key_file_new ();
&error))
{
/* We are capable of dealing with a non-existent configuration
- * file, so be silent about that. */
- if (!(error->domain == G_FILE_ERROR &&
+ * file, so be silent about that (unless the user had set a
+ * non-default configuration file with the NOTMUCH_CONFIG
+ * variable)
+ */
+ if (notmuch_config_env ||
+ !(error->domain == G_FILE_ERROR &&
error->code == G_FILE_ERROR_NOENT))
{
fprintf (stderr, "Error reading configuration file %s: %s\n",
config->filename, error->message);
talloc_free (config);
+ g_error_free (error);
return NULL;
}
- config_file_is_new = 1;
+ g_error_free (error);
+ is_new = 1;
}
if (notmuch_config_get_database_path (config) == NULL) {
/* When we create a new configuration file here, we add some
* comments to help the user understand what can be done. */
- if (config_file_is_new) {
+ if (is_new) {
g_key_file_set_comment (config->key_file, NULL, NULL,
toplevel_config_comment, NULL);
g_key_file_set_comment (config->key_file, "database", NULL,
user_config_comment, NULL);
}
+ if (is_new_ret)
+ *is_new_ret = is_new;
+
return config;
}
if (! g_file_set_contents (config->filename, data, length, &error)) {
fprintf (stderr, "Error saving configuration to %s: %s\n",
config->filename, error->message);
+ g_error_free (error);
+ g_free (data);
return 1;
}
+ g_free (data);
return 0;
}