]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-config.c
Fix linking with gcc to use g++ to link in C++ libs.
[notmuch] / notmuch-config.c
index f7313bfaa055d300114845e0375308ddf1ff9cfa..7252a191373c5cc89f3f47df80ebd67766e8946f 100644 (file)
@@ -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 (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)
        name = talloc_strdup (ctx, passwd.pw_name);
     else
        name = talloc_strdup (ctx, "");
@@ -145,10 +162,15 @@ get_username_from_passwd_file (void *ctx)
  * 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;
+
+    if (is_new_ret)
+       *is_new_ret = 0;
 
     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
     if (config == NULL) {
@@ -188,7 +210,7 @@ notmuch_config_open (void *ctx, const char *filename)
            return NULL;
        }
 
-       config_file_is_new = 1;
+       is_new = 1;
     }
 
     if (notmuch_config_get_database_path (config) == NULL) {
@@ -236,7 +258,7 @@ notmuch_config_open (void *ctx, const char *filename)
 
     /* 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,
@@ -245,6 +267,9 @@ notmuch_config_open (void *ctx, const char *filename)
                                user_config_comment, NULL);
     }
 
+    if (is_new_ret)
+       *is_new_ret = is_new;
+
     return config;
 }