1 Return-Path: <jani@nikula.org>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by arlo.cworth.org (Postfix) with ESMTP id 6F1116DE035A
\r
6 for <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:55 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org
\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"
\r
10 X-Spam-Score: -0.557
\r
12 X-Spam-Status: No, score=-0.557 tagged_above=-999 required=5 tests=[AWL=0.163,
\r
13 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7,
\r
14 RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled
\r
15 Received: from arlo.cworth.org ([127.0.0.1])
\r
16 by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)
\r
17 with ESMTP id jsX6pIGSbfgP for <notmuch@notmuchmail.org>;
\r
18 Fri, 15 Apr 2016 12:30:46 -0700 (PDT)
\r
19 Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com
\r
20 [74.125.82.65]) by arlo.cworth.org (Postfix) with ESMTPS id E38866DE0159 for
\r
21 <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:40 -0700 (PDT)
\r
22 Received: by mail-wm0-f65.google.com with SMTP id l6so8591801wml.3
\r
23 for <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:40 -0700 (PDT)
\r
24 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
25 d=nikula-org.20150623.gappssmtp.com; s=20150623;
\r
26 h=from:to:cc:subject:date:message-id:in-reply-to:references
\r
27 :in-reply-to:references;
\r
28 bh=OwZp1jhzj6ccv8nCLEkiKlcpEGmQOwjr0f0aUY8xTt0=;
\r
29 b=0ELWerzEVCyKvP7xDo6V4tlOTwtcXzSrFRtJ5ce5//qwddoPu7EIIYY2FLRrpfZokS
\r
30 rzldAk6aDnWCKDbjcUeRuvigkwssyYgo7EQT6P1CToSLBfZ7m8uINe6pIExXN6OQvUaB
\r
31 6Z+DE+Jl6M9eDveg2KH+Jzc/zj34zr8As0CZNu0jSs7jBlJxzp69tNWx6903MnB4dnrJ
\r
32 eftOSa7Y519rv0BZ3JZUPiMH7mo+7sFkcCHet89IQi7UousVKVIM0qdcUXVe1K/Em/PO
\r
33 RO7Wmtw11CQBJXcGAAoUo4nGyoMTfXhBAWMB9CH5N1OkFQ6AC1N0Ct97X8r6tXCqUF5S
\r
35 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
36 d=1e100.net; s=20130820;
\r
37 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
\r
38 :references:in-reply-to:references;
\r
39 bh=OwZp1jhzj6ccv8nCLEkiKlcpEGmQOwjr0f0aUY8xTt0=;
\r
40 b=Al2dUGqdfMbQ7RuoE/IUBLO0/qwxmshRR6kt4sXX0GNTQ4DXNURBVNpygudmKUCl+R
\r
41 WKs1LcSciN/xjh+mUF4AOhr0JkGWM/1bvnWAxXNXFMr6sYH7UATzvwG+wnGMR4EJL2a/
\r
42 giksRNB6uSC3e0LOSpH7I3wEQnTWlh4ZmtFQteLv6mD8PiIShnkTZjudsrTfjxXmhiIG
\r
43 7Wp6VVBc1Yv6c4zYbBwfH0GtIaaUUwaV47aoRZMyWvySyH1S804PnRDXE/YLU9xmsQJ+
\r
44 PcYSb/044NXDFQTJTaxFqNhRNz0XH+8ajBF4qxSHAqJxOl249vW8/AvoIz4Unpkew+ss
\r
47 AOPr4FWoZBoRLpmYxJB1DW10DTodzwlKv+4WU0QWwMo3KZexgTUIXBUOPUaDF9wUvyLJDA==
\r
48 X-Received: by 10.28.20.198 with SMTP id 189mr6202754wmu.103.1460748639613;
\r
49 Fri, 15 Apr 2016 12:30:39 -0700 (PDT)
\r
50 Received: from localhost (mobile-access-bcee7f-102.dhcp.inet.fi.
\r
52 by smtp.gmail.com with ESMTPSA id x2sm50403985wjr.33.2016.04.15.12.30.38
\r
53 (version=TLSv1/SSLv3 cipher=OTHER);
\r
54 Fri, 15 Apr 2016 12:30:39 -0700 (PDT)
\r
55 From: Jani Nikula <jani@nikula.org>
\r
56 To: notmuch@notmuchmail.org
\r
57 Subject: [RFC PATCH 3/5] util: add a homebrew scandir implementation
\r
58 Date: Fri, 15 Apr 2016 22:29:17 +0300
\r
60 <8db6698a2e3ed8378b6a8bcc2fb9935ed27d917d.1460748142.git.jani@nikula.org>
\r
61 X-Mailer: git-send-email 2.1.4
\r
62 In-Reply-To: <cover.1460748142.git.jani@nikula.org>
\r
63 References: <cover.1460748142.git.jani@nikula.org>
\r
64 In-Reply-To: <cover.1460748142.git.jani@nikula.org>
\r
65 References: <cover.1460748142.git.jani@nikula.org>
\r
66 X-BeenThere: notmuch@notmuchmail.org
\r
67 X-Mailman-Version: 2.1.20
\r
69 List-Id: "Use and development of the notmuch mail system."
\r
70 <notmuch.notmuchmail.org>
\r
71 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,
\r
72 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
73 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>
\r
74 List-Post: <mailto:notmuch@notmuchmail.org>
\r
75 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
76 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,
\r
77 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
78 X-List-Received-Date: Fri, 15 Apr 2016 19:30:55 -0000
\r
80 Add support for a filter callback with a context parameter, propagate
\r
81 errors from the filter callback, generate a list of filenames instead
\r
84 util/Makefile.local | 2 +-
\r
85 util/scandir.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
86 util/scandir.h | 11 +++++++
\r
87 3 files changed, 99 insertions(+), 1 deletion(-)
\r
88 create mode 100644 util/scandir.c
\r
89 create mode 100644 util/scandir.h
\r
91 diff --git a/util/Makefile.local b/util/Makefile.local
\r
92 index 905f23763468..8893209f320e 100644
\r
93 --- a/util/Makefile.local
\r
94 +++ b/util/Makefile.local
\r
95 @@ -5,7 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir)
\r
97 libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \
\r
98 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \
\r
100 + $(dir)/util.c $(dir)/scandir.c
\r
102 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
104 diff --git a/util/scandir.c b/util/scandir.c
\r
105 new file mode 100644
\r
106 index 000000000000..c69717724235
\r
108 +++ b/util/scandir.c
\r
110 +/* scandir.c - Dedicated scandir implementation.
\r
112 + * Copyright (c) 2016 Jani Nikula
\r
114 + * This program is free software: you can redistribute it and/or modify
\r
115 + * it under the terms of the GNU General Public License as published by
\r
116 + * the Free Software Foundation, either version 3 of the License, or
\r
117 + * (at your option) any later version.
\r
119 + * This program is distributed in the hope that it will be useful,
\r
120 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
121 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
122 + * GNU General Public License for more details.
\r
124 + * You should have received a copy of the GNU General Public License
\r
125 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
127 + * Author: Jani Nikula <jani@nikula.org>
\r
130 +#include "scandir.h"
\r
132 +#include <dirent.h>
\r
133 +#include <errno.h>
\r
134 +#include <stdlib.h>
\r
135 +#include <string.h>
\r
137 +int scandirx (const char *path, char ***namelist,
\r
138 + int (*filter)(const struct dirent *dirent, void *context),
\r
139 + int (*compar)(const void *a, const void *b),
\r
143 + struct dirent *d;
\r
144 + char **array = NULL;
\r
145 + int i, count = 0, array_size = 0;
\r
148 + dir = opendir (path);
\r
152 + while ((d = readdir (dir)) != NULL) {
\r
154 + int selected = filter (d, context);
\r
155 + if (selected < 0)
\r
157 + else if (! selected)
\r
161 + if (count == array_size) {
\r
162 + char **new_array;
\r
164 + array_size = array_size ? 2 * array_size : 16;
\r
166 + new_array = realloc (array, array_size * sizeof (*array));
\r
170 + array = new_array;
\r
173 + d_name = strdup (d->d_name);
\r
177 + array[count++] = d_name;
\r
183 + qsort (array, count, sizeof (*array), compar);
\r
185 + *namelist = array;
\r
190 + for (i = 0; i < count; i++) {
\r
197 diff --git a/util/scandir.h b/util/scandir.h
\r
198 new file mode 100644
\r
199 index 000000000000..cc5ed95a8b1b
\r
201 +++ b/util/scandir.h
\r
203 +#ifndef _SCANDIR_H
\r
204 +#define _SCANDIR_H
\r
206 +#include <dirent.h>
\r
208 +int scandirx (const char *path, char ***namelist,
\r
209 + int (*filter)(const struct dirent *dirent, void *context),
\r
210 + int (*compar)(const void *a, const void *b),
\r
213 +#endif /* _SCANDIR_H */
\r