X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-config.c;h=95430db104fce8583ac089f9a3e83f52c08901b0;hp=248149c85de0ee0401816537ce84b1adad5a88f0;hb=c25bc03dc60846090d230cdad21e39faccce35ff;hpb=6bd01e1b340f6a209dde64471bc9d7137511dada diff --git a/notmuch-config.c b/notmuch-config.c index 248149c8..95430db1 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -80,8 +80,17 @@ get_name_from_passwd_file (void *ctx) 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, @@ -104,8 +113,16 @@ get_username_from_passwd_file (void *ctx) 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, ""); @@ -115,9 +132,10 @@ get_username_from_passwd_file (void *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 @@ -151,6 +169,7 @@ notmuch_config_open (void *ctx, { GError *error = NULL; int is_new = 0; + char *notmuch_config_env = NULL; if (is_new_ret) *is_new_ret = 0; @@ -163,11 +182,15 @@ notmuch_config_open (void *ctx, 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); + notmuch_config_env = NULL; + } else { config->filename = talloc_asprintf (config, "%s/.notmuch-config", getenv ("HOME")); + } config->key_file = g_key_file_new (); @@ -190,9 +213,11 @@ notmuch_config_open (void *ctx, fprintf (stderr, "Error reading configuration file %s: %s\n", config->filename, error->message); talloc_free (config); + g_error_free (error); return NULL; } + g_error_free (error); is_new = 1; } @@ -291,9 +316,12 @@ notmuch_config_save (notmuch_config_t *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; }