--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 6F1116DE035A\r
+ for <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:55 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.557\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.557 tagged_above=-999 required=5 tests=[AWL=0.163,\r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7,\r
+ RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id jsX6pIGSbfgP for <notmuch@notmuchmail.org>;\r
+ Fri, 15 Apr 2016 12:30:46 -0700 (PDT)\r
+Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com\r
+ [74.125.82.65]) by arlo.cworth.org (Postfix) with ESMTPS id E38866DE0159 for\r
+ <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:40 -0700 (PDT)\r
+Received: by mail-wm0-f65.google.com with SMTP id l6so8591801wml.3\r
+ for <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:40 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=nikula-org.20150623.gappssmtp.com; s=20150623;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references\r
+ :in-reply-to:references;\r
+ bh=OwZp1jhzj6ccv8nCLEkiKlcpEGmQOwjr0f0aUY8xTt0=;\r
+ b=0ELWerzEVCyKvP7xDo6V4tlOTwtcXzSrFRtJ5ce5//qwddoPu7EIIYY2FLRrpfZokS\r
+ rzldAk6aDnWCKDbjcUeRuvigkwssyYgo7EQT6P1CToSLBfZ7m8uINe6pIExXN6OQvUaB\r
+ 6Z+DE+Jl6M9eDveg2KH+Jzc/zj34zr8As0CZNu0jSs7jBlJxzp69tNWx6903MnB4dnrJ\r
+ eftOSa7Y519rv0BZ3JZUPiMH7mo+7sFkcCHet89IQi7UousVKVIM0qdcUXVe1K/Em/PO\r
+ RO7Wmtw11CQBJXcGAAoUo4nGyoMTfXhBAWMB9CH5N1OkFQ6AC1N0Ct97X8r6tXCqUF5S\r
+ uCDA==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=OwZp1jhzj6ccv8nCLEkiKlcpEGmQOwjr0f0aUY8xTt0=;\r
+ b=Al2dUGqdfMbQ7RuoE/IUBLO0/qwxmshRR6kt4sXX0GNTQ4DXNURBVNpygudmKUCl+R\r
+ WKs1LcSciN/xjh+mUF4AOhr0JkGWM/1bvnWAxXNXFMr6sYH7UATzvwG+wnGMR4EJL2a/\r
+ giksRNB6uSC3e0LOSpH7I3wEQnTWlh4ZmtFQteLv6mD8PiIShnkTZjudsrTfjxXmhiIG\r
+ 7Wp6VVBc1Yv6c4zYbBwfH0GtIaaUUwaV47aoRZMyWvySyH1S804PnRDXE/YLU9xmsQJ+\r
+ PcYSb/044NXDFQTJTaxFqNhRNz0XH+8ajBF4qxSHAqJxOl249vW8/AvoIz4Unpkew+ss\r
+ /Anw==\r
+X-Gm-Message-State:\r
+ AOPr4FWoZBoRLpmYxJB1DW10DTodzwlKv+4WU0QWwMo3KZexgTUIXBUOPUaDF9wUvyLJDA==\r
+X-Received: by 10.28.20.198 with SMTP id 189mr6202754wmu.103.1460748639613;\r
+ Fri, 15 Apr 2016 12:30:39 -0700 (PDT)\r
+Received: from localhost (mobile-access-bcee7f-102.dhcp.inet.fi.\r
+ [188.238.127.102])\r
+ by smtp.gmail.com with ESMTPSA id x2sm50403985wjr.33.2016.04.15.12.30.38\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Fri, 15 Apr 2016 12:30:39 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 3/5] util: add a homebrew scandir implementation\r
+Date: Fri, 15 Apr 2016 22:29:17 +0300\r
+Message-Id:\r
+ <8db6698a2e3ed8378b6a8bcc2fb9935ed27d917d.1460748142.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <cover.1460748142.git.jani@nikula.org>\r
+References: <cover.1460748142.git.jani@nikula.org>\r
+In-Reply-To: <cover.1460748142.git.jani@nikula.org>\r
+References: <cover.1460748142.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 15 Apr 2016 19:30:55 -0000\r
+\r
+Add support for a filter callback with a context parameter, propagate\r
+errors from the filter callback, generate a list of filenames instead\r
+of dirents.\r
+---\r
+ util/Makefile.local | 2 +-\r
+ util/scandir.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ util/scandir.h | 11 +++++++\r
+ 3 files changed, 99 insertions(+), 1 deletion(-)\r
+ create mode 100644 util/scandir.c\r
+ create mode 100644 util/scandir.h\r
+\r
+diff --git a/util/Makefile.local b/util/Makefile.local\r
+index 905f23763468..8893209f320e 100644\r
+--- a/util/Makefile.local\r
++++ b/util/Makefile.local\r
+@@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir)\r
+ \r
+ libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
+ $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \\r
+- $(dir)/util.c\r
++ $(dir)/util.c $(dir)/scandir.c\r
+ \r
+ libutil_modules := $(libutil_c_srcs:.c=.o)\r
+ \r
+diff --git a/util/scandir.c b/util/scandir.c\r
+new file mode 100644\r
+index 000000000000..c69717724235\r
+--- /dev/null\r
++++ b/util/scandir.c\r
+@@ -0,0 +1,87 @@\r
++/* scandir.c - Dedicated scandir implementation.\r
++ *\r
++ * Copyright (c) 2016 Jani Nikula\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program. If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: Jani Nikula <jani@nikula.org>\r
++ */\r
++\r
++#include "scandir.h"\r
++\r
++#include <dirent.h>\r
++#include <errno.h>\r
++#include <stdlib.h>\r
++#include <string.h>\r
++\r
++int scandirx (const char *path, char ***namelist,\r
++ int (*filter)(const struct dirent *dirent, void *context),\r
++ int (*compar)(const void *a, const void *b),\r
++ void *context)\r
++{\r
++ DIR *dir;\r
++ struct dirent *d;\r
++ char **array = NULL;\r
++ int i, count = 0, array_size = 0;\r
++ char *d_name;\r
++\r
++ dir = opendir (path);\r
++ if (!dir)\r
++ return -1;\r
++\r
++ while ((d = readdir (dir)) != NULL) {\r
++ if (filter) {\r
++ int selected = filter (d, context);\r
++ if (selected < 0)\r
++ goto err;\r
++ else if (! selected)\r
++ continue;\r
++ }\r
++\r
++ if (count == array_size) {\r
++ char **new_array;\r
++\r
++ array_size = array_size ? 2 * array_size : 16;\r
++\r
++ new_array = realloc (array, array_size * sizeof (*array));\r
++ if (! new_array)\r
++ goto err;\r
++\r
++ array = new_array;\r
++ }\r
++\r
++ d_name = strdup (d->d_name);\r
++ if (! d_name)\r
++ goto err;\r
++\r
++ array[count++] = d_name;\r
++ }\r
++\r
++ closedir (dir);\r
++\r
++ if (compar)\r
++ qsort (array, count, sizeof (*array), compar);\r
++\r
++ *namelist = array;\r
++\r
++ return count;\r
++\r
++err:\r
++ for (i = 0; i < count; i++) {\r
++ free (array[i]);\r
++ }\r
++ free (array);\r
++\r
++ return -1;\r
++}\r
+diff --git a/util/scandir.h b/util/scandir.h\r
+new file mode 100644\r
+index 000000000000..cc5ed95a8b1b\r
+--- /dev/null\r
++++ b/util/scandir.h\r
+@@ -0,0 +1,11 @@\r
++#ifndef _SCANDIR_H\r
++#define _SCANDIR_H\r
++\r
++#include <dirent.h>\r
++\r
++int scandirx (const char *path, char ***namelist,\r
++ int (*filter)(const struct dirent *dirent, void *context),\r
++ int (*compar)(const void *a, const void *b),\r
++ void *context);\r
++\r
++#endif /* _SCANDIR_H */\r
+-- \r
+2.1.4\r
+\r