]> git.notmuchmail.org Git - notmuch/commitdiff
Deal with situation where sysconf(_SC_GETPW_R_SIZE_MAX) returns -1
authorAlexander Botero-Lowry <alex.boterolowry@gmail.com>
Tue, 17 Nov 2009 19:30:39 +0000 (11:30 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 17 Nov 2009 20:27:49 +0000 (12:27 -0800)
notmuch-config.c

index 248149c85de0ee0401816537ce84b1adad5a88f0..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 (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, "");